001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/services/assessment/AssessmentService.java $
003: * $Id: AssessmentService.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.services.assessment;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.content.api.ContentResource;
030: import org.sakaiproject.content.cover.ContentHostingService;
031: import org.sakaiproject.exception.IdInvalidException;
032: import org.sakaiproject.exception.IdUnusedException;
033: import org.sakaiproject.exception.IdUsedException;
034: import org.sakaiproject.exception.InUseException;
035: import org.sakaiproject.exception.InconsistentException;
036: import org.sakaiproject.exception.OverQuotaException;
037: import org.sakaiproject.exception.PermissionException;
038: import org.sakaiproject.exception.ServerOverloadException;
039: import org.sakaiproject.exception.TypeException;
040: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentTemplateData;
041: import org.sakaiproject.tool.assessment.data.dao.assessment.AttachmentData;
042: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAttachmentIfc;
043: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentBaseIfc;
044: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
045: import org.sakaiproject.tool.assessment.data.ifc.assessment.AttachmentIfc;
046: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
047: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
048: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionAttachmentIfc;
049: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
050: import org.sakaiproject.tool.assessment.facade.AgentFacade;
051: import org.sakaiproject.tool.assessment.facade.AssessmentFacade;
052: import org.sakaiproject.tool.assessment.facade.AssessmentFacadeQueriesAPI;
053: import org.sakaiproject.tool.assessment.facade.AssessmentTemplateFacade;
054: import org.sakaiproject.tool.assessment.facade.SectionFacade;
055: import org.sakaiproject.tool.assessment.facade.TypeFacade;
056: import org.sakaiproject.tool.assessment.services.PersistenceService;
057: import org.sakaiproject.tool.cover.ToolManager;
058:
059: /**
060: * The AssessmentService calls the service locator to reach the manager on the
061: * back end.
062: *
063: * @author Rachel Gollub <rgollub@stanford.edu>
064: */
065: public class AssessmentService {
066: private static Log log = LogFactory.getLog(AssessmentService.class);
067:
068: /**
069: * Creates a new QuestionPoolService object.
070: */
071: public AssessmentService() {
072: }
073:
074: public AssessmentTemplateFacade getAssessmentTemplate(
075: String assessmentTemplateId) {
076: try {
077: return PersistenceService.getInstance()
078: .getAssessmentFacadeQueries()
079: .getAssessmentTemplate(
080: new Long(assessmentTemplateId));
081: } catch (Exception e) {
082: log.error(e);
083: throw new RuntimeException(e);
084: }
085: }
086:
087: public AssessmentFacade getAssessment(String assessmentId) {
088: try {
089: return PersistenceService.getInstance()
090: .getAssessmentFacadeQueries().getAssessment(
091: new Long(assessmentId));
092: } catch (Exception e) {
093: log.error(e);
094: throw new RuntimeException(e);
095: }
096: }
097:
098: public AssessmentFacade getBasicInfoOfAnAssessment(
099: String assessmentId) {
100: try {
101: return PersistenceService.getInstance()
102: .getAssessmentFacadeQueries()
103: .getBasicInfoOfAnAssessment(new Long(assessmentId));
104: } catch (Exception e) {
105: log.error(e);
106: throw new RuntimeException(e);
107: }
108: }
109:
110: public ArrayList getAllAssessmentTemplates() {
111: try {
112: return PersistenceService.getInstance()
113: .getAssessmentFacadeQueries()
114: .getAllAssessmentTemplates();
115: } catch (Exception e) {
116: log.error(e);
117: throw new RuntimeException(e);
118: }
119: }
120:
121: public ArrayList getAllActiveAssessmentTemplates() {
122: try {
123: return PersistenceService.getInstance()
124: .getAssessmentFacadeQueries()
125: .getAllActiveAssessmentTemplates();
126: } catch (Exception e) {
127: log.error(e);
128: throw new RuntimeException(e);
129: }
130: }
131:
132: public ArrayList getTitleOfAllActiveAssessmentTemplates() {
133: try {
134: return PersistenceService.getInstance()
135: .getAssessmentFacadeQueries()
136: .getTitleOfAllActiveAssessmentTemplates();
137: } catch (Exception e) {
138: log.error(e);
139: throw new RuntimeException(e);
140: }
141: }
142:
143: public ArrayList getAllAssessments(String orderBy) {
144: return PersistenceService.getInstance()
145: .getAssessmentFacadeQueries()
146: .getAllAssessments(orderBy); // signalling all & no paging
147: }
148:
149: public ArrayList getAllActiveAssessments(String orderBy) {
150: return PersistenceService.getInstance()
151: .getAssessmentFacadeQueries().getAllActiveAssessments(
152: orderBy); // signalling all & no
153: // paging
154: }
155:
156: /**
157: * @param orderBy
158: * @return an ArrayList of AssessmentFacade. It is IMPORTANT to note that
159: * the object is a partial object which contains no SectionFacade
160: */
161: public ArrayList getSettingsOfAllActiveAssessments(String orderBy) {
162: return PersistenceService.getInstance()
163: .getAssessmentFacadeQueries()
164: .getSettingsOfAllActiveAssessments(orderBy); // signalling
165: // all & no
166: // paging
167: }
168:
169: /**
170: * @param orderBy
171: * @return an ArrayList of AssessmentFacade. It is IMPORTANT to note that
172: * the object is a partial object which contains only Assessment
173: * basic info such as title, lastModifiedDate. This method is used
174: * by Authoring Front Door
175: */
176: public ArrayList getBasicInfoOfAllActiveAssessments(String orderBy,
177: boolean ascending) {
178: String siteAgentId = AgentFacade.getCurrentSiteId();
179: return PersistenceService.getInstance()
180: .getAssessmentFacadeQueries()
181: .getBasicInfoOfAllActiveAssessmentsByAgent(orderBy,
182: siteAgentId, ascending); // signalling all & no
183: // paging
184: }
185:
186: public ArrayList getBasicInfoOfAllActiveAssessments(String orderBy) {
187: String siteAgentId = AgentFacade.getCurrentSiteId();
188: return PersistenceService.getInstance()
189: .getAssessmentFacadeQueries()
190: .getBasicInfoOfAllActiveAssessmentsByAgent(orderBy,
191: siteAgentId); // signalling
192: // all
193: // & no
194: // paging
195: }
196:
197: public ArrayList getAllAssessments(int pageSize, int pageNumber,
198: String orderBy) {
199: try {
200: if (pageSize > 0 && pageNumber > 0) {
201: return PersistenceService.getInstance()
202: .getAssessmentFacadeQueries()
203: .getAllAssessments(pageSize, pageNumber,
204: orderBy);
205: } else {
206: return PersistenceService.getInstance()
207: .getAssessmentFacadeQueries()
208: .getAllAssessments(orderBy);
209: }
210: } catch (Exception e) {
211: log.error(e);
212: throw new RuntimeException(e);
213: }
214: }
215:
216: public AssessmentFacade createAssessment(String title,
217: String description, String typeId, String templateId)
218: throws Exception {
219: AssessmentFacade assessment = null;
220: try {
221: AssessmentTemplateFacade assessmentTemplate = null;
222: // #1 - check templateId and prepared it in Long
223: Long templateIdLong = AssessmentTemplateFacade.DEFAULTTEMPLATE;
224: if (templateId != null && !templateId.equals(""))
225: templateIdLong = new Long(templateId);
226:
227: // #2 - check typeId and prepared it in Long
228: Long typeIdLong = TypeFacade.HOMEWORK;
229: if (typeId != null && !typeId.equals(""))
230: typeIdLong = new Long(typeId);
231:
232: AssessmentFacadeQueriesAPI queries = PersistenceService
233: .getInstance().getAssessmentFacadeQueries();
234: log.debug("**** AssessmentFacadeQueries=" + queries);
235: assessment = queries.createAssessment(title, description,
236: typeIdLong, templateIdLong);
237: } catch (Exception e) {
238: log.error(e);
239: throw new Exception(e);
240: }
241: return assessment;
242: }
243:
244: public int getQuestionSize(String assessmentId) {
245: return PersistenceService.getInstance()
246: .getAssessmentFacadeQueries().getQuestionSize(
247: new Long(assessmentId));
248: }
249:
250: public void update(AssessmentFacade assessment) {
251: PersistenceService.getInstance().getAssessmentFacadeQueries()
252: .saveOrUpdate(assessment);
253: }
254:
255: public void save(AssessmentTemplateData template) {
256: PersistenceService.getInstance().getAssessmentFacadeQueries()
257: .saveOrUpdate(template);
258: }
259:
260: public void deleteAllSecuredIP(AssessmentIfc assessment) {
261: PersistenceService.getInstance().getAssessmentFacadeQueries()
262: .deleteAllSecuredIP(assessment);
263: }
264:
265: public void saveAssessment(AssessmentFacade assessment) {
266: PersistenceService.getInstance().getAssessmentFacadeQueries()
267: .saveOrUpdate(assessment);
268: }
269:
270: public void deleteAssessmentTemplate(Long assessmentId) {
271: PersistenceService.getInstance().getAssessmentFacadeQueries()
272: .deleteTemplate(assessmentId);
273: }
274:
275: public void removeAssessment(String assessmentId) {
276: PersistenceService.getInstance().getAssessmentFacadeQueries()
277: .removeAssessment(new Long(assessmentId));
278: }
279:
280: /**
281: * public int checkDelete(long assessmentId){ return
282: * assessmentService.checkDelete(assessmentId); }
283: *
284: * public void deleteAssessment(Id assessmentId) throws
285: * osid.assessment.AssessmentException {
286: * assessmentService.deleteAssessment(assessmentId); }
287: *
288: * public AssessmentIterator getAssessments() throws
289: * osid.assessment.AssessmentException { return
290: * assessmentService.getAssessments(); }
291: *
292: */
293:
294: public SectionFacade addSection(String assessmentId) {
295: SectionFacade section = null;
296: try {
297: Long assessmentIdLong = new Long(assessmentId);
298: AssessmentFacadeQueriesAPI queries = PersistenceService
299: .getInstance().getAssessmentFacadeQueries();
300: section = queries.addSection(assessmentIdLong);
301: } catch (Exception e) {
302: e.printStackTrace();
303: }
304: return section;
305: }
306:
307: public void removeSection(String sectionId) {
308: try {
309: Long sectionIdLong = new Long(sectionId);
310: AssessmentFacadeQueriesAPI queries = PersistenceService
311: .getInstance().getAssessmentFacadeQueries();
312: queries.removeSection(sectionIdLong);
313: } catch (Exception e) {
314: e.printStackTrace();
315: }
316:
317: }
318:
319: public SectionFacade getSection(String sectionId) {
320: try {
321: return PersistenceService.getInstance()
322: .getAssessmentFacadeQueries().getSection(
323: new Long(sectionId));
324: } catch (Exception e) {
325: log.error(e);
326: throw new RuntimeException(e);
327: }
328: }
329:
330: public void saveOrUpdateSection(SectionFacade section) {
331: try {
332: PersistenceService.getInstance()
333: .getAssessmentFacadeQueries().saveOrUpdateSection(
334: section);
335: } catch (Exception e) {
336: log.error(e);
337: throw new RuntimeException(e);
338: }
339: }
340:
341: public void moveAllItems(String sourceSectionId,
342: String destSectionId) {
343: PersistenceService.getInstance().getAssessmentFacadeQueries()
344: .moveAllItems(new Long(sourceSectionId),
345: new Long(destSectionId)); // signalling all & no
346: // paging
347: }
348:
349: public void removeAllItems(String sourceSectionId) {
350: PersistenceService.getInstance().getAssessmentFacadeQueries()
351: .removeAllItems(new Long(sourceSectionId));
352: }
353:
354: public ArrayList getBasicInfoOfAllActiveAssessmentTemplates(
355: String orderBy) {
356: return PersistenceService.getInstance()
357: .getAssessmentFacadeQueries()
358: .getBasicInfoOfAllActiveAssessmentTemplates(orderBy); // signalling
359: // all &
360: // no
361: // paging
362: }
363:
364: public AssessmentFacade createAssessmentWithoutDefaultSection(
365: String title, String description, String typeId,
366: String templateId) throws Exception {
367: AssessmentFacade assessment = null;
368: try {
369: AssessmentTemplateFacade assessmentTemplate = null;
370: // #1 - check templateId and prepared it in Long
371: Long templateIdLong = AssessmentTemplateFacade.DEFAULTTEMPLATE;
372: if (templateId != null && !templateId.equals(""))
373: templateIdLong = new Long(templateId);
374:
375: // #2 - check typeId and prepared it in Long
376: Long typeIdLong = TypeFacade.HOMEWORK;
377: if (typeId != null && !typeId.equals(""))
378: typeIdLong = new Long(typeId);
379:
380: AssessmentFacadeQueriesAPI queries = PersistenceService
381: .getInstance().getAssessmentFacadeQueries();
382: assessment = queries.createAssessmentWithoutDefaultSection(
383: title, description, typeIdLong, templateIdLong);
384: } catch (Exception e) {
385: log.error(e);
386: throw new Exception(e);
387: }
388: return assessment;
389: }
390:
391: public boolean assessmentTitleIsUnique(String assessmentBaseId,
392: String title, boolean isTemplate) {
393: return PersistenceService.getInstance()
394: .getAssessmentFacadeQueries().assessmentTitleIsUnique(
395: new Long(assessmentBaseId), title,
396: Boolean.valueOf(isTemplate));
397: }
398:
399: public List getAssessmentByTemplate(String templateId) {
400: return PersistenceService.getInstance()
401: .getAssessmentFacadeQueries().getAssessmentByTemplate(
402: new Long(templateId));
403: }
404:
405: public List getDefaultMetaDataSet() {
406: return PersistenceService.getInstance()
407: .getAssessmentFacadeQueries().getDefaultMetaDataSet();
408: }
409:
410: public void deleteAllMetaData(AssessmentBaseIfc assessment) {
411: PersistenceService.getInstance().getAssessmentFacadeQueries()
412: .deleteAllMetaData(assessment);
413: }
414:
415: public ItemAttachmentIfc createItemAttachment(ItemDataIfc item,
416: String resourceId, String filename, String protocol) {
417: ItemAttachmentIfc attachment = null;
418: try {
419: AssessmentFacadeQueriesAPI queries = PersistenceService
420: .getInstance().getAssessmentFacadeQueries();
421: attachment = queries.createItemAttachment(item, resourceId,
422: filename, protocol);
423: } catch (Exception e) {
424: e.printStackTrace();
425: }
426: return attachment;
427: }
428:
429: public void removeItemAttachment(String attachmentId) {
430: PersistenceService.getInstance().getAssessmentFacadeQueries()
431: .removeItemAttachment(new Long(attachmentId));
432: }
433:
434: public void updateAssessmentLastModifiedInfo(
435: AssessmentFacade assessmentFacade) {
436: try {
437: PersistenceService.getInstance()
438: .getAssessmentFacadeQueries()
439: .updateAssessmentLastModifiedInfo(assessmentFacade);
440: } catch (Exception e) {
441: log.error(e);
442: throw new RuntimeException(e);
443: }
444: }
445:
446: public SectionAttachmentIfc createSectionAttachment(
447: SectionDataIfc section, String resourceId, String filename,
448: String protocol) {
449: SectionAttachmentIfc attachment = null;
450: try {
451: AssessmentFacadeQueriesAPI queries = PersistenceService
452: .getInstance().getAssessmentFacadeQueries();
453: attachment = queries.createSectionAttachment(section,
454: resourceId, filename, protocol);
455: } catch (Exception e) {
456: e.printStackTrace();
457: }
458: return attachment;
459: }
460:
461: public void removeSectionAttachment(String attachmentId) {
462: PersistenceService.getInstance().getAssessmentFacadeQueries()
463: .removeSectionAttachment(new Long(attachmentId));
464: }
465:
466: public AssessmentAttachmentIfc createAssessmentAttachment(
467: AssessmentIfc assessment, String resourceId,
468: String filename, String protocol) {
469: AssessmentAttachmentIfc attachment = null;
470: try {
471: AssessmentFacadeQueriesAPI queries = PersistenceService
472: .getInstance().getAssessmentFacadeQueries();
473: attachment = queries.createAssessmentAttachment(assessment,
474: resourceId, filename, protocol);
475: } catch (Exception e) {
476: e.printStackTrace();
477: }
478: return attachment;
479: }
480:
481: public void removeAssessmentAttachment(String attachmentId) {
482: PersistenceService.getInstance().getAssessmentFacadeQueries()
483: .removeAssessmentAttachment(new Long(attachmentId));
484: }
485:
486: public AttachmentData createEmailAttachment(String resourceId,
487: String filename, String protocol) {
488: AttachmentData attachment = null;
489: try {
490: AssessmentFacadeQueriesAPI queries = PersistenceService
491: .getInstance().getAssessmentFacadeQueries();
492: attachment = queries.createEmailAttachment(resourceId,
493: filename, protocol);
494: } catch (Exception e) {
495: e.printStackTrace();
496: }
497: return attachment;
498: }
499:
500: public List getAssessmentResourceIdList(AssessmentIfc pub) {
501: List resourceIdList = new ArrayList();
502: List list = pub.getAssessmentAttachmentList();
503: if (list != null) {
504: resourceIdList = getResourceIdList(list);
505: }
506: Set sectionSet = pub.getSectionSet();
507: Iterator iter = sectionSet.iterator();
508: while (iter.hasNext()) {
509: SectionDataIfc section = (SectionDataIfc) iter.next();
510: List sectionAttachments = getSectionResourceIdList(section);
511: if (sectionAttachments != null) {
512: resourceIdList.addAll(sectionAttachments);
513: }
514: }
515: log.debug("*** resource size=" + resourceIdList.size());
516: return resourceIdList;
517: }
518:
519: public List getSectionResourceIdList(SectionDataIfc section) {
520: List resourceIdList = new ArrayList();
521: List list = section.getSectionAttachmentList();
522: if (list != null) {
523: resourceIdList = getResourceIdList(list);
524: }
525: Set itemSet = section.getItemSet();
526: Iterator iter1 = itemSet.iterator();
527: while (iter1.hasNext()) {
528: ItemDataIfc item = (ItemDataIfc) iter1.next();
529: List itemAttachments = getItemResourceIdList(item);
530: if (itemAttachments != null) {
531: resourceIdList.addAll(itemAttachments);
532: }
533: }
534: return resourceIdList;
535: }
536:
537: public List getItemResourceIdList(ItemDataIfc item) {
538: List resourceIdList = new ArrayList();
539: List list = item.getItemAttachmentList();
540: if (list != null) {
541: resourceIdList = getResourceIdList(list);
542: }
543: return resourceIdList;
544: }
545:
546: private List getResourceIdList(List list) {
547: List resourceIdList = new ArrayList();
548: for (int i = 0; i < list.size(); i++) {
549: AttachmentIfc attach = (AttachmentIfc) list.get(i);
550: resourceIdList.add(attach.getResourceId());
551: }
552: return resourceIdList;
553: }
554:
555: public void deleteResources(List resourceIdList) {
556: if (resourceIdList == null)
557: return;
558: for (int i = 0; i < resourceIdList.size(); i++) {
559: String resourceId = (String) resourceIdList.get(i);
560: resourceId = resourceId.trim();
561: if (resourceId.toLowerCase().startsWith("/attachment")) {
562: try {
563: log.debug("removing=" + resourceId);
564: ContentHostingService.removeResource(resourceId);
565: } catch (PermissionException e) {
566: log.warn("cannot remove resourceId=" + resourceId
567: + ":" + e.getMessage());
568: log
569: .warn("PermissionException from ContentHostingService:"
570: + e.getMessage());
571: } catch (IdUnusedException e) {
572: log.warn("cannot remove resourceId=" + resourceId
573: + ":" + e.getMessage());
574: log
575: .warn("IdUnusedException from ContentHostingService:"
576: + e.getMessage());
577: } catch (TypeException e) {
578: log.warn("cannot remove resourceId=" + resourceId
579: + ":" + e.getMessage());
580: log
581: .warn("TypeException from ContentHostingService:"
582: + e.getMessage());
583: } catch (InUseException e) {
584: log.warn("cannot remove resourceId=" + resourceId
585: + ":" + e.getMessage());
586: log
587: .warn("InUseException from ContentHostingService:"
588: + e.getMessage());
589: }
590: }
591: }
592: }
593:
594: public void saveOrUpdateAttachments(List list) {
595: PersistenceService.getInstance().getAssessmentFacadeQueries()
596: .saveOrUpdateAttachments(list);
597: }
598:
599: public ContentResource createCopyOfContentResource(
600: String resourceId, String filename) {
601: // trouble using Validator, so use string replacement instead
602: // java.lang.NoClassDefFoundError: org/sakaiproject/util/Validator
603: filename = filename.replaceAll("http://", "http:__");
604: ContentResource cr_copy = null;
605: try {
606: // create a copy of the resource
607: ContentResource cr = ContentHostingService
608: .getResource(resourceId);
609: cr_copy = ContentHostingService.addAttachmentResource(
610: filename, ToolManager.getCurrentPlacement()
611: .getContext(), ToolManager.getTool(
612: "sakai.samigo").getTitle(), cr
613: .getContentType(), cr.getContent(), cr
614: .getProperties());
615: } catch (IdInvalidException e) {
616: log.warn(e.getMessage());
617: } catch (PermissionException e) {
618: log.warn(e.getMessage());
619: } catch (IdUnusedException e) {
620: log.warn(e.getMessage());
621: } catch (TypeException e) {
622: log.warn(e.getMessage());
623: } catch (InconsistentException e) {
624: log.warn(e.getMessage());
625: } catch (IdUsedException e) {
626: log.warn(e.getMessage());
627: } catch (OverQuotaException e) {
628: log.warn(e.getMessage());
629: } catch (ServerOverloadException e) {
630: log.warn(e.getMessage());
631: }
632: return cr_copy;
633: }
634:
635: public void copyAllAssessments(String fromContext, String toContext) {
636: try {
637: PersistenceService.getInstance()
638: .getAssessmentFacadeQueries().copyAllAssessments(
639: fromContext, toContext);
640: } catch (Exception e) {
641: log.error(e);
642: throw new RuntimeException(e);
643: }
644:
645: }
646:
647: public List getAllActiveAssessmentsbyAgent(String fromContext) {
648: try {
649: return PersistenceService.getInstance()
650: .getAssessmentFacadeQueries()
651: .getAllActiveAssessmentsByAgent(fromContext);
652: } catch (Exception e) {
653: log.error(e);
654: throw new RuntimeException(e);
655: }
656:
657: }
658:
659: public String getAssessmentSiteId(String assessmentId) {
660: return PersistenceService.getInstance()
661: .getAssessmentFacadeQueries().getAssessmentSiteId(
662: assessmentId);
663: }
664:
665: public String getAssessmentCreatedBy(String assessmentId) {
666: return PersistenceService.getInstance()
667: .getAssessmentFacadeQueries().getAssessmentCreatedBy(
668: assessmentId);
669: }
670:
671: }
|