001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/SectionFacade.java $
003: * $Id: SectionFacade.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
021:
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.List;
029: import java.util.Iterator;
030: import java.util.Set;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.osid.assessment.AssessmentException;
035: import org.osid.assessment.Section;
036: import org.osid.shared.Type;
037: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
038: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
039: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData;
040: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
041: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
042: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionAttachmentIfc;
043: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
044: import org.sakaiproject.tool.assessment.data.ifc.shared.TypeIfc;
045: import org.sakaiproject.tool.assessment.osid.assessment.impl.SectionImpl;
046: import org.sakaiproject.tool.assessment.services.PersistenceService;
047:
048: public class SectionFacade implements Serializable, SectionDataIfc,
049: Comparable {
050: private static Log log = LogFactory.getLog(SectionFacade.class);
051:
052: private static final long serialVersionUID = 7526471155622776147L;
053:
054: private org.osid.assessment.Section section;
055: // We have 2 sets of properties:
056: // #1) properties according to org.osid.assessment.Section. However, we will
057: // not have property "displayName" because I am not sure what it is.
058: // Properties "description" will be persisted through data - daisyf 07/28/04
059: private org.osid.shared.Id id;
060: private String description;
061: private SectionDataIfc data;
062: private org.osid.shared.Type sectionType;
063: // #2) properties according to SectionDataIfc
064: private Long sectionId;
065: private Long assessmentId;
066: private AssessmentFacade assessment;
067: private Integer duration;
068: private Integer sequence;
069: private String title;
070: private Long typeId;
071: private Integer status;
072: private String createdBy;
073: private Date createdDate;
074: private String lastModifiedBy;
075: private Date lastModifiedDate;
076: private Set itemSet;
077: private Set metaDataSet = new HashSet();
078: private HashMap metaDataMap = new HashMap();
079: private Set itemFacadeSet;
080: private Set sectionAttachmentSet;
081:
082: /** SectionFacade is the class that is exposed to developer
083: * It contains some of the useful methods specified in
084: * org.osid.assessment.Section and it implements
085: * org.sakaiproject.tool.assessment.ifc.
086: * When new methods is added to osid api, this code is still workable.
087: * If signature in any of the osid methods that we mirrored changes,
088: * we only need to modify those particular methods.
089: * - daisyf
090: */
091:
092: public SectionFacade() {
093: // need to hook SectionFacade.data to SectionData, our POJO for Hibernate
094: // persistence
095: this .data = new SectionData();
096: SectionImpl sectionImpl = new SectionImpl(); //<-- place holder
097: section = (Section) sectionImpl;
098: try {
099: section.updateData(this .data);
100: } catch (AssessmentException ex) {
101: throw new DataFacadeException(ex.getMessage());
102: }
103: }
104:
105: /**
106: * This is a very important constructor. Please make sure that you have
107: * set all the properties (declared above as private) of SectionFacade using
108: * the "data" supplied. "data" is a org.osid.assessment.Section properties
109: * and I use it to store info about an section.
110: * @param data
111: */
112: public SectionFacade(SectionDataIfc data) {
113: this .data = data;
114: SectionImpl sectionImpl = new SectionImpl(); // place holder
115: section = (Section) sectionImpl;
116: try {
117: section.updateData(this .data);
118: } catch (AssessmentException ex) {
119: throw new DataFacadeException(ex.getMessage());
120: }
121: this .id = getId();
122: this .description = getDescription();
123: this .assessmentId = getAssessmentId();
124: this .sectionType = getSectionType();
125: this .sequence = getSequence();
126: this .duration = getDuration();
127: this .typeId = getTypeId();
128: this .status = getStatus();
129: this .createdBy = getCreatedBy();
130: this .createdDate = getCreatedDate();
131: this .lastModifiedBy = getLastModifiedBy();
132: this .lastModifiedDate = getLastModifiedDate();
133: this .itemSet = getItemSet();
134: this .metaDataSet = getSectionMetaDataSet();
135: this .metaDataMap = getSectionMetaDataMap(this .metaDataSet);
136: this .sectionAttachmentSet = getSectionAttachmentSet();
137: // *TODO* will work on returning itemFacade later, sorry! daisyf 11/22/04
138: }
139:
140: // the following method's signature has a one to one relationship to
141: // org.sakaiproject.tool.assessment.osid.section.SectionImpl
142: // which implements org.osid.assessment.Section
143:
144: /**
145: * Get the Id for this SectionFacade.
146: * @return org.osid.shared.Id
147: */
148: org.osid.shared.Id getId() {
149: try {
150: this .data = (SectionDataIfc) section.getData();
151: } catch (AssessmentException ex) {
152: throw new DataFacadeException(ex.getMessage());
153: }
154:
155: SectionFacadeQueriesAPI sectionFacadeQueries = PersistenceService
156: .getInstance().getSectionFacadeQueries();
157: return sectionFacadeQueries.getId(this .data.getSectionId());
158: /**
159: SectionFacadeQueries sectionFacadeQueries = new SectionFacadeQueries();
160: return sectionFacadeQueries.getSectionId(this.data.getSectionId());
161: ItemFacadeQueriesAPI itemFacadeQueries = new ItemFacadeQueries();
162: return itemFacadeQueries.getItemId(this.data.getSectionId());
163: */
164: }
165:
166: /**
167: * Get the Type for this SectionFacade.
168: * @return org.osid.shared.Type
169: */
170: Type getSectionType() {
171: try {
172: this .data = (SectionDataIfc) section.getData();
173: } catch (AssessmentException ex) {
174: throw new DataFacadeException(ex.getMessage());
175: }
176: TypeFacadeQueriesAPI typeFacadeQueries = PersistenceService
177: .getInstance().getTypeFacadeQueries();
178: return typeFacadeQueries.getTypeById(this .data.getTypeId());
179: }
180:
181: /**
182: * Get the data for this SectionFacade.
183: * @return SectionDataIfc
184: */
185: public SectionDataIfc getData() {
186: return this .data;
187: }
188:
189: /**
190: * Call setDate() to update data in SectionFacade
191: * @param data
192: */
193: public void updateData(SectionDataIfc data) {
194: setData(data);
195: }
196:
197: /**
198: * Set data for SectionFacade
199: * @param data
200: */
201: public void setData(SectionDataIfc data) {
202: this .data = data;
203: }
204:
205: // the following methods implements
206: // org.sakaiproject.tool.assessment.ifc.SectionDataIfc
207: public Long getSectionId() throws DataFacadeException {
208: try {
209: this .data = (SectionDataIfc) section.getData();
210: } catch (AssessmentException ex) {
211: throw new DataFacadeException(ex.getMessage());
212: }
213: return this .data.getSectionId();
214: }
215:
216: /**
217: * Set sectionId for SectionFacade
218: * @param sectionId
219: */
220: public void setSectionId(Long sectionId) {
221: this .sectionId = sectionId;
222: this .data.setSectionId(sectionId);
223: }
224:
225: public Long getAssessmentId() throws DataFacadeException {
226: try {
227: this .data = (SectionDataIfc) section.getData();
228: } catch (AssessmentException ex) {
229: throw new DataFacadeException(ex.getMessage());
230: }
231: return this .data.getAssessmentId();
232: }
233:
234: /**
235: * Set sectionId for SectionFacade
236: * @param sectionId
237: */
238: public void setAssessmentId(Long assessmentId) {
239: this .assessmentId = assessmentId;
240: this .data.setAssessmentId(assessmentId);
241: }
242:
243: // expect a return of AssessmentFacade from this method
244: public AssessmentIfc getAssessment() throws DataFacadeException {
245: try {
246: this .data = (SectionDataIfc) section.getData();
247: } catch (AssessmentException ex) {
248: throw new DataFacadeException(ex.getMessage());
249: }
250: return new AssessmentFacade(this .data.getAssessment());
251: }
252:
253: // section is AssessmentFacade not AssessmentData
254: public void setAssessment(AssessmentIfc assessment) {
255: this .assessment = (AssessmentFacade) assessment;
256: AssessmentData d = (AssessmentData) this .assessment.getData();
257: this .data.setAssessment(d);
258: }
259:
260: public Integer getDuration() throws DataFacadeException {
261: try {
262: this .data = (SectionDataIfc) section.getData();
263: } catch (AssessmentException ex) {
264: throw new DataFacadeException(ex.getMessage());
265: }
266: return this .data.getDuration();
267: }
268:
269: /**
270: * Set duration for SectionFacade
271: * @param duration
272: */
273: public void setDuration(Integer duration) {
274: this .duration = duration;
275: this .data.setDuration(duration);
276: }
277:
278: public Integer getSequence() throws DataFacadeException {
279: try {
280: this .data = (SectionDataIfc) section.getData();
281: } catch (AssessmentException ex) {
282: throw new DataFacadeException(ex.getMessage());
283: }
284: return this .data.getSequence();
285: }
286:
287: public void setSequence(Integer sequence) {
288: this .sequence = sequence;
289: this .data.setSequence(sequence);
290: }
291:
292: public String getTitle() throws DataFacadeException {
293: try {
294: this .data = (SectionDataIfc) section.getData();
295: } catch (AssessmentException ex) {
296: throw new DataFacadeException(ex.getMessage());
297: }
298: return this .data.getTitle();
299: }
300:
301: /**
302: * Set instruction for SectionFacade
303: * e.g. "Match the following sentences", "In the score between 1-5, specify
304: * your preference"
305: * @param instruction
306: */
307: public void setTitle(String title) {
308: this .title = title;
309: this .data.setTitle(title);
310: }
311:
312: public String getDescription() throws DataFacadeException {
313: try {
314: this .data = (SectionDataIfc) section.getData();
315: } catch (AssessmentException ex) {
316: throw new DataFacadeException(ex.getMessage());
317: }
318: return this .data.getDescription();
319: }
320:
321: /**
322: * Set description for SectionFacade
323: * @param description
324: */
325: public void setDescription(String description) {
326: this .description = description;
327: this .data.setDescription(description);
328: }
329:
330: public Long getTypeId() throws DataFacadeException {
331: try {
332: this .data = (SectionDataIfc) section.getData();
333: } catch (AssessmentException ex) {
334: throw new DataFacadeException(ex.getMessage());
335: }
336: return this .data.getTypeId();
337: }
338:
339: /**
340: * Set TypeId for SectionType. This property is used to indicate question type.
341: * e.g. 1 = Multiple Choice, 2 = Multiple Correct. Please check out
342: * ddl/02_TypeData.sql and table "type".
343: * @param typeId
344: */
345: public void setTypeId(Long typeId) {
346: this .typeId = typeId;
347: this .data.setTypeId(typeId);
348: }
349:
350: /**
351: * Get status of SectionFacade. 1 = active, 0 = inactive
352: * @return
353: * @throws DataFacadeException
354: */
355: public Integer getStatus() throws DataFacadeException {
356: try {
357: this .data = (SectionDataIfc) section.getData();
358: } catch (AssessmentException ex) {
359: throw new DataFacadeException(ex.getMessage());
360: }
361: return this .data.getStatus();
362: }
363:
364: /**
365: * Set status for SectionFacade. 1 = active, 0 = inactive
366: * @param status
367: */
368: public void setStatus(Integer status) {
369: this .status = status;
370: this .data.setStatus(status);
371: }
372:
373: /**
374: * Get createdBy for SectionFacade. This represents the agentId of the person
375: * who created the record
376: * @return
377: * @throws DataFacadeException
378: */
379: public String getCreatedBy() throws DataFacadeException {
380: try {
381: this .data = (SectionDataIfc) section.getData();
382: } catch (AssessmentException ex) {
383: throw new DataFacadeException(ex.getMessage());
384: }
385: return this .data.getCreatedBy();
386: }
387:
388: /**
389: * Set createdBy for SectionFacade. This represents the agentId of the person
390: * who created the record
391: * @param createdBy
392: */
393: public void setCreatedBy(String createdBy) {
394: this .createdBy = createdBy;
395: this .data.setCreatedBy(createdBy);
396: }
397:
398: /**
399: * Get the creation date of SectionFacade.
400: * @return
401: * @throws DataFacadeException
402: */
403: public Date getCreatedDate() throws DataFacadeException {
404: try {
405: this .data = (SectionDataIfc) section.getData();
406: } catch (AssessmentException ex) {
407: throw new DataFacadeException(ex.getMessage());
408: }
409: return this .data.getCreatedDate();
410: }
411:
412: /**
413: * Set the creation date of SectionFacade
414: * @param createdDate
415: */
416: public void setCreatedDate(Date createdDate) {
417: this .createdDate = createdDate;
418: this .data.setCreatedDate(createdDate);
419: }
420:
421: /**
422: * Get the agentId of the person who last modified SectionFacade
423: * @return
424: * @throws DataFacadeException
425: */
426: public String getLastModifiedBy() throws DataFacadeException {
427: try {
428: this .data = (SectionDataIfc) section.getData();
429: } catch (AssessmentException ex) {
430: throw new DataFacadeException(ex.getMessage());
431: }
432: return this .data.getLastModifiedBy();
433: }
434:
435: /**
436: * set the agentId of the person who last modified sectionFacade
437: * @param lastModifiedBy
438: */
439: public void setLastModifiedBy(String lastModifiedBy) {
440: this .lastModifiedBy = lastModifiedBy;
441: this .data.setLastModifiedBy(lastModifiedBy);
442: }
443:
444: /**
445: * Get the date when SectionFacade where last modified By
446: * @return
447: * @throws DataFacadeException
448: */
449: public Date getLastModifiedDate() throws DataFacadeException {
450: try {
451: this .data = (SectionDataIfc) section.getData();
452: } catch (AssessmentException ex) {
453: throw new DataFacadeException(ex.getMessage());
454: }
455: return this .data.getLastModifiedDate();
456: }
457:
458: /**
459: * Set the last modified date
460: * @param lastModifiedBy
461: */
462: public void setLastModifiedDate(Date lastModifiedDate) {
463: this .lastModifiedDate = lastModifiedDate;
464: this .data.setLastModifiedDate(lastModifiedDate);
465: }
466:
467: /**
468: * Get section text set (question text set) from SectionFacade.data
469: * @return
470: * @throws DataFacadeException
471: */
472: public Set getItemFacadeSet() throws DataFacadeException {
473: this .itemFacadeSet = new HashSet();
474: try {
475: this .data = (SectionDataIfc) section.getData();
476: Set set = this .data.getItemSet();
477: Iterator iter = set.iterator();
478: while (iter.hasNext()) {
479: ItemFacade itemFacade = new ItemFacade(
480: (ItemDataIfc) iter.next());
481: this .itemFacadeSet.add(itemFacade);
482: }
483: //this.sectionSet = data.getSectionSet();
484: } catch (AssessmentException ex) {
485: throw new DataFacadeException(ex.getMessage());
486: }
487: return this .itemFacadeSet;
488: }
489:
490: public Set getItemSet() throws DataFacadeException {
491: try {
492: this .data = (SectionDataIfc) section.getData();
493: } catch (AssessmentException ex) {
494: throw new DataFacadeException(ex.getMessage());
495: }
496: return this .data.getItemSet();
497: }
498:
499: /**
500: * Set section text (question text) in SectionFacade.data
501: * @param sectionTextSet
502: */
503: public void setItemSet(Set itemSet) {
504: this .itemSet = itemSet;
505: this .data.setItemSet(itemSet);
506: }
507:
508: public Set getSectionMetaDataSet() throws DataFacadeException {
509: try {
510: this .data = (SectionDataIfc) section.getData();
511: } catch (AssessmentException ex) {
512: throw new DataFacadeException(ex.getMessage());
513: }
514: return this .data.getSectionMetaDataSet();
515: }
516:
517: /**
518: * Set section metadata in SectionFacade.data
519: * @param metaDataSet
520: */
521: public void setSectionMetaDataSet(Set metaDataSet) {
522: this .metaDataSet = metaDataSet;
523: this .data.setSectionMetaDataSet(metaDataSet);
524: }
525:
526: public HashMap getSectionMetaDataMap(Set metaDataSet) {
527: HashMap metaDataMap = new HashMap();
528: if (metaDataSet != null) {
529: for (Iterator i = metaDataSet.iterator(); i.hasNext();) {
530: SectionMetaData sectionMetaData = (SectionMetaData) i
531: .next();
532: metaDataMap.put(sectionMetaData.getLabel(),
533: sectionMetaData.getEntry());
534: }
535: }
536: return metaDataMap;
537: }
538:
539: public void addItem(ItemFacade itemFacade) {
540: addItem(itemFacade.getData());
541: }
542:
543: public void addItem(ItemDataIfc itemDataIfc) {
544: if (this .itemSet == null) {
545: setItemSet(new HashSet());
546: }
547: this .data.getItemSet().add(itemDataIfc);
548: this .itemSet = this .data.getItemSet();
549: }
550:
551: public String getSectionMetaDataByLabel(String label) {
552: return (String) this .metaDataMap.get(label);
553: }
554:
555: /**
556: * Add a Meta Data to SectionFacade
557: * @param label
558: * @param entry
559: */
560: public void addSectionMetaData(String label, String entry) {
561: if (this .metaDataSet == null) {
562: setSectionMetaDataSet(new HashSet());
563: this .metaDataMap = new HashMap();
564: }
565:
566: if (this .metaDataMap.get(label) != null) {
567: // just update
568: Iterator iter = this .metaDataSet.iterator();
569: while (iter.hasNext()) {
570: SectionMetaData metadata = (SectionMetaData) iter
571: .next();
572: if (metadata.getLabel().equals(label))
573: metadata.setEntry(entry);
574: }
575: } else {
576:
577: this .metaDataMap.put(label, entry);
578: this .data.getSectionMetaDataSet().add(
579: new SectionMetaData((SectionData) this .data, label,
580: entry));
581: this .metaDataSet = this .data.getSectionMetaDataSet();
582:
583: }
584:
585: }
586:
587: public TypeIfc getType() {
588: return getSectionTypeFacade();
589: }
590:
591: public TypeFacade getSectionTypeFacade() {
592: try {
593: this .data = (SectionDataIfc) section.getData();
594: } catch (AssessmentException ex) {
595: throw new DataFacadeException(ex.getMessage());
596: }
597: TypeFacadeQueriesAPI typeFacadeQueries = PersistenceService
598: .getInstance().getTypeFacadeQueries();
599: return typeFacadeQueries.getTypeFacadeById(this .data
600: .getTypeId());
601: }
602:
603: public ArrayList getItemArray() {
604: ArrayList list = new ArrayList();
605: Iterator iter = itemSet.iterator();
606: while (iter.hasNext()) {
607: list.add(iter.next());
608: }
609: return list;
610: }
611:
612: public ArrayList getItemArraySortedForGrading() {
613: // placeholder for now, need to have it 'cuz they are in ifc.
614: ArrayList list = getItemArray();
615: Collections.sort(list);
616: return list;
617: }
618:
619: public ArrayList getItemArraySorted() {
620: ArrayList list = getItemArray();
621: Collections.sort(list);
622: return list;
623: }
624:
625: public ArrayList getItemArraySortedWithRandom(long seed) {
626: // placeholder for now, need to have it 'cuz they are in ifc.
627: ArrayList list = getItemArray();
628: Collections.sort(list);
629: return list;
630: }
631:
632: public int compareTo(Object o) {
633: SectionFacade a = (SectionFacade) o;
634: return sequence.compareTo(a.sequence);
635: }
636:
637: public Set getSectionAttachmentSet() throws DataFacadeException {
638: try {
639: this .data = (SectionDataIfc) section.getData();
640: } catch (AssessmentException ex) {
641: throw new DataFacadeException(ex.getMessage());
642: }
643: return this .data.getSectionAttachmentSet();
644: }
645:
646: public void setSectionAttachmentSet(Set sectionAttachmentSet) {
647: this .sectionAttachmentSet = sectionAttachmentSet;
648: this .data.setSectionAttachmentSet(sectionAttachmentSet);
649: }
650:
651: public List getSectionAttachmentList() {
652: ArrayList list = new ArrayList();
653: if (sectionAttachmentSet != null) {
654: Iterator iter = sectionAttachmentSet.iterator();
655: while (iter.hasNext()) {
656: SectionAttachmentIfc a = (SectionAttachmentIfc) iter
657: .next();
658: list.add(a);
659: }
660: }
661: return list;
662: }
663:
664: }
|