0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/AssessmentFacadeQueries.java $
0003: * $Id: AssessmentFacadeQueries.java 9912 2006-05-24 23:45:33Z daisyf@stanford.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
0007: *
0008: * Licensed under the Educational Community License, Version 1.0 (the"License");
0009: * you may not use this file except in compliance with the License.
0010: * You may obtain a copy of the License at
0011: *
0012: * http://www.opensource.org/licenses/ecl1.php
0013: *
0014: * Unless required by applicable law or agreed to in writing, software
0015: * distributed under the License is distributed on an "AS IS" BASIS,
0016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: * See the License for the specific language governing permissions and
0018: * limitations under the License.
0019: *
0020: **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
0021:
0022: import java.sql.SQLException;
0023: import java.util.ArrayList;
0024: import java.util.Date;
0025: import java.util.HashMap;
0026: import java.util.HashSet;
0027: import java.util.Iterator;
0028: import java.util.List;
0029: import java.util.Set;
0030:
0031: import org.apache.commons.logging.Log;
0032: import org.apache.commons.logging.LogFactory;
0033: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0034: import org.springframework.orm.hibernate3.HibernateCallback;
0035: import org.springframework.orm.hibernate3.HibernateQueryException;
0036: import org.hibernate.HibernateException;
0037: import org.hibernate.Query;
0038: import org.hibernate.Session;
0039:
0040: import org.sakaiproject.service.gradebook.shared.GradebookService;
0041: import org.sakaiproject.spring.SpringBeanLocator;
0042: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAccessControl;
0043:
0044: import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
0045: import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
0046: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAttachment;
0047: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData;
0048: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
0049: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentFeedback;
0050: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentMetaData;
0051: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentTemplateData;
0052: import org.sakaiproject.tool.assessment.data.dao.assessment.AttachmentData;
0053: import org.sakaiproject.tool.assessment.data.dao.assessment.EvaluationModel;
0054: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
0055: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
0056: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback;
0057: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
0058: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
0059: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionAttachment;
0060: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
0061: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData;
0062: import org.sakaiproject.tool.assessment.data.dao.assessment.SecuredIPAddress;
0063: import org.sakaiproject.tool.assessment.data.dao.authz.AuthorizationData;
0064: import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
0065: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAccessControlIfc;
0066: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
0067: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentBaseIfc;
0068: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentMetaDataIfc;
0069: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAttachmentIfc;
0070: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
0071: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
0072: import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
0073: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionAttachmentIfc;
0074: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
0075: import org.sakaiproject.tool.assessment.facade.util.PagingUtilQueriesAPI;
0076: import org.sakaiproject.tool.assessment.integration.context.IntegrationContextFactory;
0077: import org.sakaiproject.tool.assessment.integration.helper.ifc.GradebookServiceHelper;
0078: import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
0079: import org.sakaiproject.tool.assessment.qti.constants.AuthoringConstantStrings;
0080: import org.sakaiproject.tool.assessment.services.PersistenceService;
0081: import org.sakaiproject.tool.assessment.services.QuestionPoolService;
0082: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
0083: import org.sakaiproject.component.cover.ServerConfigurationService;
0084: import org.sakaiproject.content.api.ContentResource;
0085: import org.sakaiproject.content.cover.ContentHostingService;
0086: import org.sakaiproject.entity.api.ResourceProperties;
0087: import org.sakaiproject.exception.IdUnusedException;
0088: import org.sakaiproject.exception.PermissionException;
0089: import org.sakaiproject.exception.TypeException;
0090:
0091: public class AssessmentFacadeQueries extends HibernateDaoSupport
0092: implements AssessmentFacadeQueriesAPI {
0093: private static Log log = LogFactory
0094: .getLog(AssessmentFacadeQueries.class);
0095:
0096: // private ResourceBundle rb =
0097: // ResourceBundle.getBundle("org.sakaiproject.tool.assessment.bundle.Messages");
0098:
0099: public static final String LASTMODIFIEDDATE = "lastModifiedDate";
0100:
0101: public static final String TITLE = "title";
0102:
0103: public AssessmentFacadeQueries() {
0104: }
0105:
0106: public IdImpl getId(String id) {
0107: return new IdImpl(id);
0108: }
0109:
0110: public IdImpl getId(Long id) {
0111: return new IdImpl(id);
0112: }
0113:
0114: public IdImpl getId(long id) {
0115: return new IdImpl(id);
0116: }
0117:
0118: public IdImpl getAssessmentId(String id) {
0119: return new IdImpl(id);
0120: }
0121:
0122: public IdImpl getAssessmentId(Long id) {
0123: return new IdImpl(id);
0124: }
0125:
0126: public IdImpl getAssessmentId(long id) {
0127: return new IdImpl(id);
0128: }
0129:
0130: public IdImpl getAssessmentTemplateId(String id) {
0131: return new IdImpl(id);
0132: }
0133:
0134: public IdImpl getAssessmentTemplateId(Long id) {
0135: return new IdImpl(id);
0136: }
0137:
0138: public IdImpl getAssessmentTemplateId(long id) {
0139: return new IdImpl(id);
0140: }
0141:
0142: public static void main(String[] args) throws DataFacadeException {
0143: AssessmentFacadeQueriesAPI instance = new AssessmentFacadeQueries();
0144: // add an assessmentTemplate
0145: if (args[0].equals("addTemplate")) {
0146: Long assessmentTemplateId = instance.addTemplate();
0147: AssessmentTemplateData a = instance
0148: .loadTemplate(assessmentTemplateId);
0149: print(a);
0150: AssessmentTemplateFacade af = new AssessmentTemplateFacade(
0151: a);
0152: printFacade(af);
0153: }
0154: if (args[0].equals("removeT")) {
0155: instance.removeTemplate(new Long(args[1]));
0156: }
0157: if (args[0].equals("addA")) {
0158: Long assessmentId = instance
0159: .addAssessment(new Long(args[1]));
0160: AssessmentData a = instance.loadAssessment(assessmentId);
0161: print(a);
0162: }
0163: if (args[0].equals("loadT")) {
0164: AssessmentTemplateData a = (AssessmentTemplateData) instance
0165: .load(new Long(args[1]));
0166: print(a);
0167: }
0168: if (args[0].equals("loadA")) {
0169: AssessmentData a = (AssessmentData) instance.load(new Long(
0170: args[1]));
0171: print(a);
0172: }
0173: System.exit(0);
0174: }
0175:
0176: public static void print(AssessmentBaseData a) {
0177: if (a.getIsTemplate().equals(Boolean.FALSE)) {
0178: }
0179: /*
0180: * log.debug("**assessment due date: " +
0181: * a.getAssessmentAccessControl().getDueDate()); log.debug("**assessment
0182: * control #" + a.getAssessmentAccessControl()); log.debug("**assessment
0183: * metadata" + a.getAssessmentMetaDataSet()); log.debug("**Objective not
0184: * lazy = " + a.getAssessmentMetaDataByLabel("ASSESSMENT_OBJECTIVES"));
0185: */
0186: }
0187:
0188: public static void printFacade(AssessmentTemplateFacade a) {
0189: /*
0190: * log.debug("**assessmentId #" + a.getAssessmentTemplateId());
0191: * log.debug("**assessment due date: " +
0192: * a.getAssessmentAccessControl().getDueDate()); log.debug("**assessment
0193: * control #" + a.getAssessmentAccessControl()); log.debug("**assessment
0194: * metadata" + a.getAssessmentMetaDataSet()); log.debug("**Objective not
0195: * lazy = " + a.getAssessmentMetaDataByLabel("ASSESSMENT_OBJECTIVE"));
0196: */
0197: }
0198:
0199: public Long addTemplate() {
0200: AssessmentTemplateData assessmentTemplate = new AssessmentTemplateData(
0201: new Long(0), "title", "description", "comments",
0202: TypeD.HOMEWORK, new Integer(1), new Integer(1),
0203: new Integer(1), new Integer(1), "1", new Date(), "1",
0204: new Date());
0205: AssessmentAccessControl s = new AssessmentAccessControl(
0206: new Integer(0), new Integer(0), new Integer(0),
0207: new Integer(0), new Integer(0), new Integer(0),
0208: new Integer(0), new Integer(0), new Date(), new Date(),
0209: new Date(), new Date(), new Date(), new Integer(1),
0210: new Integer(1), new Integer(1),
0211: "Thanks for submitting", "anonymous");
0212: s.setAssessmentBase(assessmentTemplate);
0213: assessmentTemplate
0214: .setAssessmentAccessControl((AssessmentAccessControlIfc) s);
0215: assessmentTemplate.addAssessmentMetaData(
0216: "ASSESSMENTTEMPLATE_OBJECTIVES",
0217: " assesmentT: the objective is to ...");
0218: // take default submission model
0219: int retryCount = PersistenceService.getInstance()
0220: .getRetryCount().intValue();
0221: while (retryCount > 0) {
0222: try {
0223: getHibernateTemplate().save(assessmentTemplate);
0224: retryCount = 0;
0225: } catch (Exception e) {
0226: log.warn("problem saving template: " + e.getMessage());
0227: retryCount = PersistenceService.getInstance()
0228: .retryDeadlock(e, retryCount);
0229: }
0230: }
0231: return assessmentTemplate.getAssessmentTemplateId();
0232: }
0233:
0234: public void removeTemplate(Long assessmentId) {
0235: AssessmentTemplateData assessment = (AssessmentTemplateData) getHibernateTemplate()
0236: .load(AssessmentTemplateData.class, assessmentId);
0237: int retryCount = PersistenceService.getInstance()
0238: .getRetryCount().intValue();
0239: while (retryCount > 0) {
0240: try {
0241: getHibernateTemplate().delete(assessment);
0242: retryCount = 0;
0243: } catch (Exception e) {
0244: log.warn("problem delete template: " + e.getMessage());
0245: retryCount = PersistenceService.getInstance()
0246: .retryDeadlock(e, retryCount);
0247: }
0248: }
0249: }
0250:
0251: public Long addAssessment(Long assessmentTemplateId) {
0252:
0253: AssessmentData assessment = new AssessmentData(new Long(0),
0254: "assessment title", "assessment description",
0255: "assessment acomments", assessmentTemplateId,
0256: TypeD.HOMEWORK, new Integer(1), new Integer(1),
0257: new Integer(1), new Integer(1), "1", new Date(), "1",
0258: new Date());
0259: AssessmentAccessControl s = new AssessmentAccessControl(
0260: new Integer(1), new Integer(1), new Integer(1),
0261: new Integer(1), new Integer(1), new Integer(1),
0262: new Integer(1), new Integer(1), new Date(), new Date(),
0263: new Date(), new Date(), new Date(), new Integer(1),
0264: new Integer(1), new Integer(1),
0265: "Thanks for submitting", "anonymous");
0266:
0267: s.setAssessmentBase(assessment);
0268: assessment
0269: .setAssessmentAccessControl((AssessmentAccessControlIfc) s);
0270: assessment.addAssessmentMetaData("ASSESSMENT_OBJECTIVES",
0271: " assesment: the objective is to ...");
0272: int retryCount = PersistenceService.getInstance()
0273: .getRetryCount().intValue();
0274: while (retryCount > 0) {
0275: try {
0276: getHibernateTemplate().save(assessment);
0277: retryCount = 0;
0278: } catch (Exception e) {
0279: log
0280: .warn("problem saving assessment: "
0281: + e.getMessage());
0282: retryCount = PersistenceService.getInstance()
0283: .retryDeadlock(e, retryCount);
0284: }
0285: }
0286: return assessment.getAssessmentId();
0287: }
0288:
0289: public AssessmentBaseData load(Long id) {
0290: AssessmentBaseData a = (AssessmentBaseData) getHibernateTemplate()
0291: .load(AssessmentBaseData.class, id);
0292: if (a.getIsTemplate().equals(Boolean.TRUE)) {
0293: return (AssessmentTemplateData) a;
0294: } else {
0295: return (AssessmentData) a;
0296: }
0297: }
0298:
0299: public AssessmentTemplateData loadTemplate(Long assessmentTemplateId) {
0300: return (AssessmentTemplateData) getHibernateTemplate().load(
0301: AssessmentTemplateData.class, assessmentTemplateId);
0302: }
0303:
0304: public AssessmentData loadAssessment(Long assessmentId) {
0305: return (AssessmentData) getHibernateTemplate().load(
0306: AssessmentData.class, assessmentId);
0307: }
0308:
0309: /*
0310: * The following methods are real
0311: *
0312: */
0313: public AssessmentTemplateFacade getAssessmentTemplate(
0314: Long assessmentTemplateId) {
0315: AssessmentTemplateData template = (AssessmentTemplateData) getHibernateTemplate()
0316: .load(AssessmentTemplateData.class,
0317: assessmentTemplateId);
0318: return new AssessmentTemplateFacade(template);
0319: }
0320:
0321: // sakai2.0 we want to scope it by creator, users can only see their
0322: // templates plus the "Default Template"
0323: public ArrayList getAllAssessmentTemplates() {
0324: final String agent = AgentFacade.getAgentString();
0325: final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
0326: final String query = "select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.typeId)"
0327: + " from AssessmentTemplateData a where a.assessmentBaseId=1 or"
0328: + " a.createdBy=? or a.typeId=? order by a.title";
0329: HibernateCallback hcb = new HibernateCallback() {
0330: public Object doInHibernate(Session session)
0331: throws HibernateException, SQLException {
0332: Query q = session.createQuery(query);
0333: q.setString(0, agent);
0334: q.setLong(1, typeId.longValue());
0335: return q.list();
0336: };
0337: };
0338: List list = getHibernateTemplate().executeFind(hcb);
0339: // List list = getHibernateTemplate().find(query,
0340: // new Object[]{agent},
0341: // new org.hibernate.type.Type[] {Hibernate.STRING});
0342: ArrayList templateList = new ArrayList();
0343: for (int i = 0; i < list.size(); i++) {
0344: AssessmentTemplateData a = (AssessmentTemplateData) list
0345: .get(i);
0346: AssessmentTemplateFacade f = new AssessmentTemplateFacade(a);
0347: templateList.add(f);
0348: }
0349: return templateList;
0350: }
0351:
0352: // sakai2.0 we want to scope it by creator, users can only see their
0353: // templates plus the "Default Template"
0354: public ArrayList getAllActiveAssessmentTemplates() {
0355: final String agent = AgentFacade.getAgentString();
0356: final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
0357: final String query = "select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.typeId)"
0358: + " from AssessmentTemplateData a where a.status=1 and (a.assessmentBaseId=1 or"
0359: + " a.createdBy=? or a.typeId=?) order by a.title";
0360: HibernateCallback hcb = new HibernateCallback() {
0361: public Object doInHibernate(Session session)
0362: throws HibernateException, SQLException {
0363: Query q = session.createQuery(query);
0364: q.setString(0, agent);
0365: q.setLong(1, typeId.longValue());
0366: return q.list();
0367: };
0368: };
0369: List list = getHibernateTemplate().executeFind(hcb);
0370:
0371: // List list = getHibernateTemplate().find(query,
0372: // new Object[]{agent},
0373: // new org.hibernate.type.Type[] {Hibernate.STRING});
0374: ArrayList templateList = new ArrayList();
0375: for (int i = 0; i < list.size(); i++) {
0376: AssessmentTemplateData a = (AssessmentTemplateData) list
0377: .get(i);
0378: AssessmentTemplateFacade f = new AssessmentTemplateFacade(a);
0379: templateList.add(f);
0380: }
0381: return templateList;
0382: }
0383:
0384: /**
0385: *
0386: * @return a list of AssessmentTemplateFacade. However, it is IMPORTANT to
0387: * note that it is not a full object, it contains merely
0388: * assessmentBaseId (which is the templateId) & title. This methods
0389: * is used when a list of template titles is required for displaying
0390: * purposes. In Sakai2.0, template are scoped by creator, i.e. users
0391: * can only see their own template plus the "Default Template"
0392: */
0393: public ArrayList getTitleOfAllActiveAssessmentTemplates() {
0394: final String agent = AgentFacade.getAgentString();
0395: final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
0396: final String query = "select new AssessmentTemplateData(a.assessmentBaseId, a.title) "
0397: + " from AssessmentTemplateData a where a.status=1 and "
0398: + " (a.assessmentBaseId=1 or a.createdBy=? or typeId=?) order by a.title";
0399: HibernateCallback hcb = new HibernateCallback() {
0400: public Object doInHibernate(Session session)
0401: throws HibernateException, SQLException {
0402: Query q = session.createQuery(query);
0403: q.setString(0, agent);
0404: q.setLong(1, typeId.longValue());
0405: return q.list();
0406: };
0407: };
0408: List list = getHibernateTemplate().executeFind(hcb);
0409:
0410: // List list = getHibernateTemplate().find(query,
0411: // new Object[]{agent},
0412: // new org.hibernate.type.Type[] {Hibernate.STRING});
0413: ArrayList templateList = new ArrayList();
0414: for (int i = 0; i < list.size(); i++) {
0415: AssessmentTemplateData a = (AssessmentTemplateData) list
0416: .get(i);
0417: a.setAssessmentTemplateId(a.getAssessmentBaseId());
0418: AssessmentTemplateFacade f = new AssessmentTemplateFacade(a
0419: .getAssessmentBaseId(), a.getTitle());
0420: templateList.add(f);
0421: }
0422: return templateList;
0423: }
0424:
0425: public AssessmentFacade getAssessment(Long assessmentId) {
0426: AssessmentData assessment = (AssessmentData) getHibernateTemplate()
0427: .load(AssessmentData.class, assessmentId);
0428: assessment
0429: .setSectionSet(getSectionSetForAssessment(assessment));
0430: return new AssessmentFacade(assessment);
0431: }
0432:
0433: /**
0434: * IMPORTANT: 1. we have declared SectionData as lazy loading, so we need to
0435: * initialize it using getHibernateTemplate().initialize(java.lang.Object).
0436: * Unfortunately, we are using Spring 1.0.2 which does not support this
0437: * Hibernate feature. I tried upgrading Spring to 1.1.3. Then it failed to
0438: * load all the OR maps correctly. So for now, I am just going to initialize
0439: * it myself. I will take a look at it again next year. - daisyf (12/13/04)
0440: */
0441: private HashSet getSectionSetForAssessment(AssessmentData assessment) {
0442: List sectionList = getHibernateTemplate().find(
0443: "from SectionData s where s.assessment.assessmentBaseId="
0444: + assessment.getAssessmentBaseId());
0445: HashSet set = new HashSet();
0446: for (int j = 0; j < sectionList.size(); j++) {
0447: set.add((SectionData) sectionList.get(j));
0448: }
0449: return set;
0450: }
0451:
0452: public void removeAssessment(Long assessmentId) {
0453: AssessmentData assessment = (AssessmentData) getHibernateTemplate()
0454: .load(AssessmentData.class, assessmentId);
0455: // if pubAssessment exist, simply set assessment to inactive
0456: // else delete assessment
0457: List count = getHibernateTemplate()
0458: .find(
0459: "select count(p) from PublishedAssessmentData p where p.assessmentId=?",
0460: assessmentId);
0461: // log.debug("no. of pub Assessment =" + count.size());
0462: Iterator iter = count.iterator();
0463: int i = ((Integer) iter.next()).intValue();
0464: if (i > 0) {
0465: assessment.setStatus(AssessmentIfc.DEAD_STATUS);
0466: int retryCount = PersistenceService.getInstance()
0467: .getRetryCount().intValue();
0468: while (retryCount > 0) {
0469: try {
0470: getHibernateTemplate().update(assessment);
0471: retryCount = 0;
0472: } catch (Exception e) {
0473: log.warn("problem updating assessment: "
0474: + e.getMessage());
0475: retryCount = PersistenceService.getInstance()
0476: .retryDeadlock(e, retryCount);
0477: }
0478: }
0479: } else {
0480: // need to check if item in sections belongs to any QuestionPool
0481: QuestionPoolService qpService = new QuestionPoolService();
0482: HashMap h = qpService.getQuestionPoolItemMap();
0483: checkForQuestionPoolItem(assessment, h);
0484: Set sectionSet = getSectionSetForAssessment(assessment);
0485: assessment.setSectionSet(sectionSet);
0486:
0487: // removal of resource should be done here but for some reason it
0488: // doesn't work.
0489: // Debugging log in Content doesn't show anything.
0490: // So I am doing it in RemoveAssessmentListener
0491: // #2 - remove any resources attachment
0492: AssessmentService s = new AssessmentService();
0493: List resourceIdList = s
0494: .getAssessmentResourceIdList(assessment);
0495: log.debug("*** we have no. of resource in assessment="
0496: + resourceIdList.size());
0497: s.deleteResources(resourceIdList);
0498:
0499: int retryCount = PersistenceService.getInstance()
0500: .getRetryCount().intValue();
0501: while (retryCount > 0) {
0502: try {
0503: getHibernateTemplate().delete(assessment);
0504: retryCount = 0;
0505: } catch (Exception e) {
0506: log.warn("problem deleting assessment: "
0507: + e.getMessage());
0508: retryCount = PersistenceService.getInstance()
0509: .retryDeadlock(e, retryCount);
0510: }
0511: }
0512: // true below => regular assessment (not published assessment)
0513: PersistenceService.getInstance().getAuthzQueriesFacade()
0514: .removeAuthorizationByQualifier(
0515: assessment.getAssessmentId().toString(),
0516: false);
0517: }
0518: }
0519:
0520: /* this assessment comes with a default section */
0521: public AssessmentData cloneAssessmentFromTemplate(
0522: AssessmentTemplateData t) {
0523: // log.debug("**** DEFAULT templateId inside clone" +
0524: // t.getAssessmentTemplateId());
0525: AssessmentData assessment = new AssessmentData(t.getParentId(),
0526: "Assessment created with" + t.getTitle(), t
0527: .getDescription(), t.getComments(), t
0528: .getAssessmentTemplateId(),
0529: TypeD.HOMEWORK, // by default for now
0530: t.getInstructorNotification(), t
0531: .getTesteeNotification(), t
0532: .getMultipartAllowed(), t.getStatus(),
0533: AgentFacade.getAgentString(), new Date(), AgentFacade
0534: .getAgentString(), new Date());
0535: try {
0536: // deal with Access Control
0537: AssessmentAccessControl controlOrig = (AssessmentAccessControl) t
0538: .getAssessmentAccessControl();
0539: if (controlOrig != null) {
0540: AssessmentAccessControl control = (AssessmentAccessControl) controlOrig
0541: .clone();
0542: control.setAssessmentBase(assessment);
0543: assessment.setAssessmentAccessControl(control);
0544: }
0545: // deal with feedback
0546: AssessmentFeedback feedbackOrig = (AssessmentFeedback) t
0547: .getAssessmentFeedback();
0548: if (feedbackOrig != null) {
0549: AssessmentFeedback feedback = (AssessmentFeedback) feedbackOrig
0550: .clone();
0551: feedback.setAssessmentBase(assessment);
0552: assessment.setAssessmentFeedback(feedback);
0553: }
0554: // deal with evaluation
0555: EvaluationModel evalOrig = (EvaluationModel) t
0556: .getEvaluationModel();
0557: if (evalOrig != null) {
0558: EvaluationModel eval = (EvaluationModel) evalOrig
0559: .clone();
0560: eval.setAssessmentBase(assessment);
0561: assessment.setEvaluationModel(eval);
0562: }
0563: // deal with MetaData
0564: HashSet h = new HashSet();
0565: Set s = t.getAssessmentMetaDataSet();
0566: Iterator iter = s.iterator();
0567: while (iter.hasNext()) {
0568: AssessmentMetaData mOrig = (AssessmentMetaData) iter
0569: .next();
0570: if (mOrig.getLabel() != null) {
0571: AssessmentMetaData m = new AssessmentMetaData(
0572: assessment, mOrig.getLabel(), mOrig
0573: .getEntry());
0574: h.add(m);
0575: }
0576: }
0577: assessment.setAssessmentMetaDataSet(h);
0578: // we need to add the FIRST section to an assessment
0579: // it is a requirement that each assesment must have at least one
0580: // section
0581: HashSet sh = new HashSet();
0582: SectionData section = new SectionData(
0583: null,
0584: new Integer("1"), // FIRST section
0585: "Default", "", TypeD.DEFAULT_SECTION,
0586: SectionData.ACTIVE_STATUS, AgentFacade
0587: .getAgentString(), new Date(), AgentFacade
0588: .getAgentString(), new Date());
0589: section.setAssessment(assessment);
0590:
0591: // add default part type, and question Ordering
0592: section.addSectionMetaData(SectionDataIfc.AUTHOR_TYPE,
0593: SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE
0594: .toString());
0595: section.addSectionMetaData(
0596: SectionDataIfc.QUESTIONS_ORDERING,
0597: SectionDataIfc.AS_LISTED_ON_ASSESSMENT_PAGE
0598: .toString());
0599:
0600: sh.add(section);
0601: assessment.setSectionSet(sh);
0602: } catch (CloneNotSupportedException ex) {
0603: ex.printStackTrace();
0604: }
0605: return assessment;
0606: }
0607:
0608: /**
0609: * This method is the same as createAssessment() except that no default
0610: * section will be created with the assessment.
0611: */
0612: public AssessmentFacade createAssessmentWithoutDefaultSection(
0613: String title, String description, Long typeId,
0614: Long templateId) throws Exception {
0615: // this assessment came with one default section
0616: AssessmentData assessment = null;
0617: try {
0618: assessment = prepareAssessment(title, description, typeId,
0619: templateId);
0620: } catch (Exception e) {
0621: throw new Exception(e);
0622: }
0623: assessment.setSectionSet(new HashSet());
0624: getHibernateTemplate().save(assessment);
0625:
0626: // register assessmnet with current site
0627: registerWithCurrentSite(assessment.getAssessmentId().toString());
0628: return new AssessmentFacade(assessment);
0629: }
0630:
0631: private AssessmentData prepareAssessment(String title,
0632: String description, Long typeId, Long templateId)
0633: throws Exception {
0634: // #1 - get the template (a facade) and create Assessment based on it
0635: AssessmentTemplateFacade template = getAssessmentTemplate(templateId);
0636: AssessmentData assessment = cloneAssessmentFromTemplate((AssessmentTemplateData) template
0637: .getData());
0638: assessment.setTitle(title);
0639: assessment.setDescription(description);
0640: assessment.setTypeId(typeId);
0641: AssessmentAccessControl control = (AssessmentAccessControl) assessment
0642: .getAssessmentAccessControl();
0643: if (control == null) {
0644: control = new AssessmentAccessControl();
0645: }
0646:
0647: // set accessControl.releaseTo based on default setting in metaData
0648: String defaultReleaseTo = template
0649: .getAssessmentMetaDataByLabel("releaseTo");
0650: if (("ANONYMOUS_USERS").equals(defaultReleaseTo)) {
0651: control.setReleaseTo("Anonymous Users");
0652: } else {
0653: control.setReleaseTo(AgentFacade.getCurrentSiteName());
0654: }
0655:
0656: /*
0657: * if (AgentFacade.isStandaloneEnvironment())
0658: * control.setReleaseTo("Authenticated Users"); else
0659: * control.setReleaseTo(AgentFacade.getCurrentSiteName());
0660: */
0661: EvaluationModel evaluation = (EvaluationModel) assessment
0662: .getEvaluationModel();
0663: if (evaluation == null) {
0664: evaluation = new EvaluationModel();
0665: }
0666: GradebookService g = null;
0667: boolean integrated = IntegrationContextFactory.getInstance()
0668: .isIntegrated();
0669: try {
0670: if (integrated) {
0671: g = (GradebookService) SpringBeanLocator
0672: .getInstance()
0673: .getBean(
0674: "org.sakaiproject.service.gradebook.GradebookService");
0675: }
0676:
0677: GradebookServiceHelper gbsHelper = IntegrationContextFactory
0678: .getInstance().getGradebookServiceHelper();
0679: if (!gbsHelper.gradebookExists(GradebookFacade
0680: .getGradebookUId(), g))
0681: evaluation
0682: .setToGradeBook(EvaluationModelIfc.GRADEBOOK_NOT_AVAILABLE
0683: .toString());
0684: } catch (HibernateQueryException e) {
0685: log.warn("Gradebook Error: " + e.getMessage());
0686: evaluation
0687: .setToGradeBook(EvaluationModelIfc.GRADEBOOK_NOT_AVAILABLE
0688: .toString());
0689: throw new Exception(e);
0690: }
0691:
0692: return assessment;
0693: }
0694:
0695: public AssessmentFacade createAssessment(String title,
0696: String description, Long typeId, Long templateId)
0697: throws Exception {
0698:
0699: // this assessment comes with a default section
0700: AssessmentData assessment = null;
0701: try {
0702: assessment = prepareAssessment(title, description, typeId,
0703: templateId);
0704: } catch (Exception e) {
0705: throw new Exception(e);
0706: }
0707:
0708: int retryCount = PersistenceService.getInstance()
0709: .getRetryCount().intValue();
0710: while (retryCount > 0) {
0711: try {
0712: getHibernateTemplate().save(assessment);
0713: retryCount = 0;
0714: } catch (Exception e) {
0715: log
0716: .warn("problem saving assessment: "
0717: + e.getMessage());
0718: retryCount = PersistenceService.getInstance()
0719: .retryDeadlock(e, retryCount);
0720: }
0721: }
0722: // register assessmnet with current site
0723: registerWithCurrentSite(assessment.getAssessmentId().toString());
0724: return new AssessmentFacade(assessment);
0725: }
0726:
0727: private void registerWithCurrentSite(String qualifierIdString) {
0728: PersistenceService.getInstance().getAuthzQueriesFacade()
0729: .createAuthorization(AgentFacade.getCurrentSiteId(),
0730: "EDIT_ASSESSMENT", qualifierIdString);
0731: }
0732:
0733: public ArrayList getAllAssessments(String orderBy) {
0734: List list = getHibernateTemplate().find(
0735: "from AssessmentData a order by a." + orderBy);
0736: ArrayList assessmentList = new ArrayList();
0737: for (int i = 0; i < list.size(); i++) {
0738: AssessmentData a = (AssessmentData) list.get(i);
0739: AssessmentFacade f = new AssessmentFacade(a);
0740: assessmentList.add(f);
0741: }
0742: return assessmentList;
0743: }
0744:
0745: public ArrayList getAllActiveAssessments(String orderBy) {
0746: List list = getHibernateTemplate().find(
0747: "from AssessmentData a where a.status=1 order by a."
0748: + orderBy);
0749: ArrayList assessmentList = new ArrayList();
0750: for (int i = 0; i < list.size(); i++) {
0751: AssessmentData a = (AssessmentData) list.get(i);
0752: a.setSectionSet(getSectionSetForAssessment(a));
0753: AssessmentFacade f = new AssessmentFacade(a);
0754: assessmentList.add(f);
0755: }
0756: return assessmentList;
0757: }
0758:
0759: public ArrayList getBasicInfoOfAllActiveAssessments(String orderBy,
0760: boolean ascending) {
0761:
0762: String query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate)from AssessmentData a where a.status=1 order by a."
0763: + orderBy;
0764:
0765: if (ascending) {
0766: query += " asc";
0767: } else {
0768: query += " desc";
0769: }
0770:
0771: List list = getHibernateTemplate().find(query);
0772:
0773: ArrayList assessmentList = new ArrayList();
0774: for (int i = 0; i < list.size(); i++) {
0775: AssessmentData a = (AssessmentData) list.get(i);
0776: AssessmentFacade f = new AssessmentFacade(a
0777: .getAssessmentBaseId(), a.getTitle(), a
0778: .getLastModifiedDate());
0779: assessmentList.add(f);
0780: }
0781: return assessmentList;
0782: }
0783:
0784: public ArrayList getBasicInfoOfAllActiveAssessmentsByAgent(
0785: String orderBy, final String siteAgentId, boolean ascending) {
0786: String query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate) "
0787: + " from AssessmentData a, AuthorizationData z where a.status=1 and "
0788: + " a.assessmentBaseId=z.qualifierId and z.functionId='EDIT_ASSESSMENT' "
0789: + " and z.agentIdString=? order by a." + orderBy;
0790: if (ascending)
0791: query += " asc";
0792: else
0793: query += " desc";
0794:
0795: final String hql = query;
0796: HibernateCallback hcb = new HibernateCallback() {
0797: public Object doInHibernate(Session session)
0798: throws HibernateException, SQLException {
0799: Query q = session.createQuery(hql);
0800: q.setString(0, siteAgentId);
0801: return q.list();
0802: };
0803: };
0804: List list = getHibernateTemplate().executeFind(hcb);
0805:
0806: // List list = getHibernateTemplate().find(query,
0807: // new Object[] {siteAgentId},
0808: // new org.hibernate.type.Type[] {Hibernate.STRING});
0809: ArrayList assessmentList = new ArrayList();
0810: for (int i = 0; i < list.size(); i++) {
0811: AssessmentData a = (AssessmentData) list.get(i);
0812: AssessmentFacade f = new AssessmentFacade(a
0813: .getAssessmentBaseId(), a.getTitle(), a
0814: .getLastModifiedDate());
0815: assessmentList.add(f);
0816: }
0817: return assessmentList;
0818: }
0819:
0820: public ArrayList getBasicInfoOfAllActiveAssessmentsByAgent(
0821: String orderBy, final String siteAgentId) {
0822: final String query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate) "
0823: + " from AssessmentData a, AuthorizationData z where a.status=1 and "
0824: + " a.assessmentBaseId=z.qualifierId and z.functionId='EDIT_ASSESSMENT' "
0825: + " and z.agentIdString=? order by a." + orderBy;
0826:
0827: HibernateCallback hcb = new HibernateCallback() {
0828: public Object doInHibernate(Session session)
0829: throws HibernateException, SQLException {
0830: Query q = session.createQuery(query);
0831: q.setString(0, siteAgentId);
0832: return q.list();
0833: };
0834: };
0835: List list = getHibernateTemplate().executeFind(hcb);
0836:
0837: // List list = getHibernateTemplate().find(query,
0838: // new Object[] {siteAgentId},
0839: // new org.hibernate.type.Type[] {Hibernate.STRING});
0840: ArrayList assessmentList = new ArrayList();
0841: for (int i = 0; i < list.size(); i++) {
0842: AssessmentData a = (AssessmentData) list.get(i);
0843: AssessmentFacade f = new AssessmentFacade(a
0844: .getAssessmentBaseId(), a.getTitle(), a
0845: .getLastModifiedDate());
0846: assessmentList.add(f);
0847: }
0848: return assessmentList;
0849: }
0850:
0851: public AssessmentFacade getBasicInfoOfAnAssessment(Long assessmentId) {
0852: AssessmentData a = (AssessmentData) getHibernateTemplate()
0853: .load(AssessmentData.class, assessmentId);
0854: AssessmentFacade f = new AssessmentFacade(a
0855: .getAssessmentBaseId(), a.getTitle(), a
0856: .getLastModifiedDate());
0857: f.setCreatedBy(a.getCreatedBy());
0858: return f;
0859: }
0860:
0861: public ArrayList getSettingsOfAllActiveAssessments(String orderBy) {
0862: List list = getHibernateTemplate().find(
0863: "from AssessmentData a where a.status=1 order by a."
0864: + orderBy);
0865: ArrayList assessmentList = new ArrayList();
0866: // IMPORTANT:
0867: // 1. we do not want any Section info, so set loadSection to false
0868: // 2. We have also declared SectionData as lazy loading. If loadSection
0869: // is set
0870: // to true, we will see null pointer
0871: Boolean loadSection = Boolean.FALSE;
0872: for (int i = 0; i < list.size(); i++) {
0873: AssessmentData a = (AssessmentData) list.get(i);
0874: AssessmentFacade f = new AssessmentFacade(a, loadSection);
0875: assessmentList.add(f);
0876: }
0877: return assessmentList;
0878: }
0879:
0880: public ArrayList getAllAssessments(int pageSize, int pageNumber,
0881: String orderBy) {
0882: String queryString = "from AssessmentData a order by a."
0883: + orderBy;
0884: PagingUtilQueriesAPI pagingUtilQueries = PersistenceService
0885: .getInstance().getPagingUtilQueries();
0886: List pageList = pagingUtilQueries.getAll(pageSize, pageNumber,
0887: queryString);
0888: ArrayList assessmentList = new ArrayList();
0889: for (int i = 0; i < pageList.size(); i++) {
0890: AssessmentData a = (AssessmentData) pageList.get(i);
0891: AssessmentFacade f = new AssessmentFacade(a);
0892: // log.debug("**** assessment facade Id=" + f.getAssessmentId());
0893: assessmentList.add(f);
0894: }
0895: return assessmentList;
0896: }
0897:
0898: public int getQuestionSize(final Long assessmentId) {
0899: HibernateCallback hcb = new HibernateCallback() {
0900: public Object doInHibernate(Session session)
0901: throws HibernateException, SQLException {
0902: Query q = session
0903: .createQuery("select count(i) from ItemData i, SectionData s, AssessmentData a where a = s.assessment and s = i.section and a.assessmentBaseId=?");
0904: q.setLong(0, assessmentId.longValue());
0905: return q.list();
0906: };
0907: };
0908: List size = getHibernateTemplate().executeFind(hcb);
0909:
0910: // List size = getHibernateTemplate().find(
0911: // "select count(i) from ItemData i, SectionData s, AssessmentData a
0912: // where a = s.assessment and s = i.section and a.assessmentBaseId=?",
0913: // new Object[] {assessmentId}
0914: // , new org.hibernate.type.Type[] {Hibernate.LONG});
0915: Iterator iter = size.iterator();
0916: if (iter.hasNext()) {
0917: int i = ((Integer) iter.next()).intValue();
0918: return i;
0919: } else {
0920: return 0;
0921: }
0922: }
0923:
0924: public void deleteAllSecuredIP(AssessmentIfc assessment) {
0925: int retryCount = PersistenceService.getInstance()
0926: .getRetryCount().intValue();
0927: while (retryCount > 0) {
0928: try {
0929: Long assessmentId = assessment.getAssessmentId();
0930: List ip = getHibernateTemplate()
0931: .find(
0932: "from SecuredIPAddress s where s.assessment.assessmentBaseId=?",
0933: assessmentId);
0934: if (ip.size() > 0) {
0935: SecuredIPAddress s = (SecuredIPAddress) ip.get(0);
0936: AssessmentData a = (AssessmentData) s
0937: .getAssessment();
0938: a.setSecuredIPAddressSet(new HashSet());
0939: getHibernateTemplate().deleteAll(ip);
0940: retryCount = 0;
0941: } else
0942: retryCount = 0;
0943: } catch (Exception e) {
0944: log.warn("problem deleting ip address: "
0945: + e.getMessage());
0946: retryCount = PersistenceService.getInstance()
0947: .retryDeadlock(e, retryCount);
0948: }
0949: }
0950: }
0951:
0952: public void saveOrUpdate(AssessmentFacade assessment) {
0953: AssessmentData data = (AssessmentData) assessment.getData();
0954: data.setLastModifiedBy(AgentFacade.getAgentString());
0955: data.setLastModifiedDate(new Date());
0956: int retryCount = PersistenceService.getInstance()
0957: .getRetryCount().intValue();
0958: while (retryCount > 0) {
0959: try {
0960: getHibernateTemplate().saveOrUpdate(data);
0961: retryCount = 0;
0962: } catch (Exception e) {
0963: log
0964: .warn("problem save new settings: "
0965: + e.getMessage());
0966: retryCount = PersistenceService.getInstance()
0967: .retryDeadlock(e, retryCount);
0968: }
0969: }
0970: }
0971:
0972: public void deleteAllMetaData(AssessmentBaseIfc t) {
0973:
0974: int retryCount = PersistenceService.getInstance()
0975: .getRetryCount().intValue();
0976: while (retryCount > 0) {
0977: try {
0978: List metadatas = getHibernateTemplate()
0979: .find(
0980: "from AssessmentMetaData a where a.assessment.assessmentBaseId = ?",
0981: t.getAssessmentBaseId());
0982: if (metadatas.size() > 0) {
0983: AssessmentMetaDataIfc m = (AssessmentMetaDataIfc) metadatas
0984: .get(0);
0985: AssessmentBaseIfc a = (AssessmentBaseIfc) m
0986: .getAssessment();
0987: a.setAssessmentMetaDataSet(new HashSet());
0988: getHibernateTemplate().deleteAll(metadatas);
0989: retryCount = 0;
0990: } else
0991: retryCount = 0;
0992: } catch (Exception e) {
0993: log
0994: .warn("problem deleting metadata: "
0995: + e.getMessage());
0996: retryCount = PersistenceService.getInstance()
0997: .retryDeadlock(e, retryCount);
0998: }
0999: }
1000: }
1001:
1002: public void saveOrUpdate(final AssessmentTemplateData template) {
1003: template.setLastModifiedBy(AgentFacade.getAgentString());
1004: template.setLastModifiedDate(new Date());
1005: int retryCount = PersistenceService.getInstance()
1006: .getRetryCount().intValue();
1007: while (retryCount > 0) {
1008: try {
1009: getHibernateTemplate().saveOrUpdate(template);
1010: retryCount = 0;
1011: } catch (Exception e) {
1012: log.warn("problem save or update template: "
1013: + e.getMessage());
1014: retryCount = PersistenceService.getInstance()
1015: .retryDeadlock(e, retryCount);
1016: }
1017: }
1018: }
1019:
1020: public void deleteTemplate(Long templateId) {
1021: int retryCount = PersistenceService.getInstance()
1022: .getRetryCount().intValue();
1023: while (retryCount > 0) {
1024: try {
1025: getHibernateTemplate().delete(
1026: getAssessmentTemplate(templateId).getData());
1027: retryCount = 0;
1028: } catch (Exception e) {
1029: log.warn("problem delete template: " + e.getMessage());
1030: retryCount = PersistenceService.getInstance()
1031: .retryDeadlock(e, retryCount);
1032: }
1033: }
1034: }
1035:
1036: public SectionFacade addSection(Long assessmentId) {
1037: // #1 - get the assessment and attach teh new section to it
1038: // we are working with Data instead of Facade in this method but should
1039: // return
1040: // SectionFacade at the end
1041: AssessmentData assessment = loadAssessment(assessmentId);
1042: // lazy loading on sectionSet, so need to initialize it
1043: Set sectionSet = getSectionSetForAssessment(assessment);
1044: assessment.setSectionSet(sectionSet);
1045:
1046: // #2 - will called the section "Section d" here d is the total no. of
1047: // section in
1048: // this assessment
1049:
1050: // #2 section has no default name - per Marc's new mockup
1051:
1052: SectionData section = new SectionData(
1053: null,
1054: new Integer(sectionSet.size() + 1), // NEXT section
1055: "", "", TypeD.DEFAULT_SECTION,
1056: SectionData.ACTIVE_STATUS,
1057: AgentFacade.getAgentString(), new Date(), AgentFacade
1058: .getAgentString(), new Date());
1059: section.setAssessment(assessment);
1060: section.setAssessmentId(assessment.getAssessmentId());
1061:
1062: // add default part type, and question Ordering
1063: section
1064: .addSectionMetaData(SectionDataIfc.AUTHOR_TYPE,
1065: SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE
1066: .toString());
1067: section.addSectionMetaData(SectionDataIfc.QUESTIONS_ORDERING,
1068: SectionDataIfc.AS_LISTED_ON_ASSESSMENT_PAGE.toString());
1069:
1070: sectionSet.add(section);
1071: int retryCount = PersistenceService.getInstance()
1072: .getRetryCount().intValue();
1073: while (retryCount > 0) {
1074: try {
1075: getHibernateTemplate().saveOrUpdate(section);
1076: retryCount = 0;
1077: } catch (Exception e) {
1078: log.warn("problem save or update assessment: "
1079: + e.getMessage());
1080: retryCount = PersistenceService.getInstance()
1081: .retryDeadlock(e, retryCount);
1082: }
1083: }
1084: return new SectionFacade(section);
1085: }
1086:
1087: public SectionFacade getSection(Long sectionId) {
1088: SectionData section = (SectionData) getHibernateTemplate()
1089: .load(SectionData.class, sectionId);
1090: return new SectionFacade(section);
1091: }
1092:
1093: public void removeSection(Long sectionId) {
1094: SectionData section = loadSection(sectionId);
1095: if (section != null) {
1096: // need to check that items in the selected section is not
1097: // associated
1098: // with any pool
1099: QuestionPoolService qpService = new QuestionPoolService();
1100: HashMap h = qpService.getQuestionPoolItemMap();
1101: checkForQuestionPoolItem(section, h);
1102:
1103: AssessmentData assessment = (AssessmentData) section
1104: .getAssessment();
1105: assessment.setLastModifiedBy(AgentFacade.getAgentString());
1106: assessment.setLastModifiedDate(new Date());
1107:
1108: // lazy loading on sectionSet, so need to initialize it
1109: Set sectionSet = getSectionSetForAssessment(assessment);
1110: assessment.setSectionSet(sectionSet);
1111: ArrayList sections = assessment.getSectionArraySorted();
1112: // need to reorder the remaining section
1113: HashSet set = new HashSet();
1114: int count = 1;
1115: for (int i = 0; i < sections.size(); i++) {
1116: SectionData s = (SectionData) sections.get(i);
1117: if (!(s.getSectionId()).equals(section.getSectionId())) {
1118: s.setSequence(new Integer(count++));
1119: set.add(s);
1120: }
1121: }
1122: assessment.setSectionSet(set);
1123: int retryCount = PersistenceService.getInstance()
1124: .getRetryCount().intValue();
1125: while (retryCount > 0) {
1126: try {
1127: getHibernateTemplate().update(assessment); // sections
1128: // reordered
1129: retryCount = 0;
1130: } catch (Exception e) {
1131: log.warn("problem updating asssessment: "
1132: + e.getMessage());
1133: retryCount = PersistenceService.getInstance()
1134: .retryDeadlock(e, retryCount);
1135: }
1136: }
1137:
1138: // get list of attachment in section
1139: AssessmentService service = new AssessmentService();
1140: List sectionAttachmentList = service
1141: .getSectionResourceIdList(section);
1142: service.deleteResources(sectionAttachmentList);
1143:
1144: // remove assessment
1145: retryCount = PersistenceService.getInstance()
1146: .getRetryCount().intValue();
1147: while (retryCount > 0) {
1148: try {
1149: getHibernateTemplate().delete(section);
1150: retryCount = 0;
1151: } catch (Exception e) {
1152: log.warn("problem deletint section: "
1153: + e.getMessage());
1154: retryCount = PersistenceService.getInstance()
1155: .retryDeadlock(e, retryCount);
1156: }
1157: }
1158: }
1159: }
1160:
1161: public SectionData loadSection(Long sectionId) {
1162: return (SectionData) getHibernateTemplate().load(
1163: SectionData.class, sectionId);
1164: }
1165:
1166: public void saveOrUpdateSection(SectionFacade section) {
1167: int retryCount = PersistenceService.getInstance()
1168: .getRetryCount().intValue();
1169: while (retryCount > 0) {
1170: try {
1171: getHibernateTemplate().saveOrUpdate(section.getData());
1172: retryCount = 0;
1173: } catch (Exception e) {
1174: log.warn("problem save or update section: "
1175: + e.getMessage());
1176: retryCount = PersistenceService.getInstance()
1177: .retryDeadlock(e, retryCount);
1178: }
1179: }
1180: }
1181:
1182: /**
1183: * This method return a list of ItemData belings to the section with the
1184: * given sectionId
1185: *
1186: * @param sectionId
1187: * @return
1188: */
1189: private List loadAllItems(Long sectionId) {
1190: return getHibernateTemplate().find(
1191: "from ItemData i where i.section.sectionId="
1192: + sectionId);
1193: }
1194:
1195: /**
1196: * This method move a set of questions form one section to another
1197: *
1198: * @param sourceSectionId
1199: * @param destSectionId
1200: */
1201: public void moveAllItems(Long sourceSectionId, Long destSectionId) {
1202: SectionData destSection = loadSection(destSectionId);
1203: List list = loadAllItems(sourceSectionId);
1204: Set set = destSection.getItemSet();
1205: if (set == null) {
1206: set = new HashSet();
1207: }
1208: int itemNum = set.size();
1209: for (int i = 0; i < list.size(); i++) {
1210: ItemDataIfc a = (ItemDataIfc) list.get(i);
1211: a.setSection(destSection);
1212: a.setSequence(new Integer(++itemNum));
1213: set.add(a);
1214: }
1215: destSection.setItemSet(set);
1216: int retryCount = PersistenceService.getInstance()
1217: .getRetryCount().intValue();
1218: while (retryCount > 0) {
1219: try {
1220: getHibernateTemplate().update(destSection);
1221: retryCount = 0;
1222: } catch (Exception e) {
1223: log.warn("problem updating section: " + e.getMessage());
1224: retryCount = PersistenceService.getInstance()
1225: .retryDeadlock(e, retryCount);
1226: }
1227: }
1228: }
1229:
1230: /**
1231: * This method remove a set of questions form one section that is random
1232: * draw
1233: *
1234: * @param sourceSectionId
1235: */
1236: public void removeAllItems(Long sourceSectionId) {
1237: SectionData section = loadSection(sourceSectionId);
1238:
1239: AssessmentData assessment = (AssessmentData) section
1240: .getAssessment();
1241: assessment.setLastModifiedBy(AgentFacade.getAgentString());
1242: assessment.setLastModifiedDate(new Date());
1243:
1244: Set itemSet = section.getItemSet();
1245: // HashSet newItemSet = new HashSet();
1246: Iterator iter = itemSet.iterator();
1247: // log.debug("***itemSet before=" + itemSet.size());
1248: while (iter.hasNext()) {
1249: ItemData item = (ItemData) iter.next();
1250: // item belongs to a pool, set section=null so
1251: // item won't get deleted during section deletion
1252: item.setSection(null);
1253: int retryCount = PersistenceService.getInstance()
1254: .getRetryCount().intValue();
1255: while (retryCount > 0) {
1256: try {
1257: getHibernateTemplate().update(item);
1258: retryCount = 0;
1259: } catch (Exception e) {
1260: log
1261: .warn("problem updating item: "
1262: + e.getMessage());
1263: retryCount = PersistenceService.getInstance()
1264: .retryDeadlock(e, retryCount);
1265: }
1266: }
1267: }
1268: // update assessment info (LastModifiedBy and LastModifiedDate)
1269: int retryCount = PersistenceService.getInstance()
1270: .getRetryCount().intValue();
1271: while (retryCount > 0) {
1272: try {
1273: getHibernateTemplate().update(assessment); // sections
1274: // reordered
1275: retryCount = 0;
1276: } catch (Exception e) {
1277: log.warn("problem updating asssessment: "
1278: + e.getMessage());
1279: retryCount = PersistenceService.getInstance()
1280: .retryDeadlock(e, retryCount);
1281: }
1282: }
1283: // need to reload the section again.
1284: // section= loadSection(sourceSectionId);
1285: // section.setItemSet(newItemSet);
1286: // getHibernateTemplate().update(section);
1287: }
1288:
1289: // sakai2.0 we want to scope it by creator, users can only see their
1290: // templates plus the "Default Template"
1291: public ArrayList getBasicInfoOfAllActiveAssessmentTemplates(
1292: String orderBy) {
1293: final String agent = AgentFacade.getAgentString();
1294: final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
1295: final String query = "select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.typeId)"
1296: + " from AssessmentTemplateData a where a.status=1 and (a.assessmentBaseId=1 or"
1297: + " a.createdBy=? or typeId=?) order by a." + orderBy;
1298:
1299: HibernateCallback hcb = new HibernateCallback() {
1300: public Object doInHibernate(Session session)
1301: throws HibernateException, SQLException {
1302: Query q = session.createQuery(query);
1303: q.setString(0, agent);
1304: q.setLong(1, typeId.longValue());
1305: return q.list();
1306: };
1307: };
1308: List list = getHibernateTemplate().executeFind(hcb);
1309:
1310: // List list = getHibernateTemplate().find(query,
1311: // new Object[]{agent},
1312: // new org.hibernate.type.Type[] {Hibernate.STRING});
1313: ArrayList assessmentList = new ArrayList();
1314: for (int i = 0; i < list.size(); i++) {
1315: AssessmentTemplateData a = (AssessmentTemplateData) list
1316: .get(i);
1317: AssessmentTemplateFacade f = new AssessmentTemplateFacade(a
1318: .getAssessmentBaseId(), a.getTitle(), a
1319: .getLastModifiedDate(), a.getTypeId());
1320: assessmentList.add(f);
1321: }
1322: return assessmentList;
1323: }
1324:
1325: public void checkForQuestionPoolItem(AssessmentData assessment,
1326: HashMap qpItemHash) {
1327: Set sectionSet = getSectionSetForAssessment(assessment);
1328: Iterator iter = sectionSet.iterator();
1329: while (iter.hasNext()) {
1330: SectionData s = (SectionData) iter.next();
1331: checkForQuestionPoolItem(s, qpItemHash);
1332: }
1333: }
1334:
1335: public void checkForQuestionPoolItem(SectionData section,
1336: HashMap qpItemHash) {
1337: Set itemSet = section.getItemSet();
1338: HashSet newItemSet = new HashSet();
1339: Iterator iter = itemSet.iterator();
1340: // log.debug("***itemSet before=" + itemSet.size());
1341: while (iter.hasNext()) {
1342: ItemData item = (ItemData) iter.next();
1343: if (qpItemHash.get(item.getItemId().toString()) != null) {
1344: // item belongs to a pool, in this case, set section=null so
1345: // item won't get deleted during section deletion
1346: item.setSection(null);
1347: int retryCount = PersistenceService.getInstance()
1348: .getRetryCount().intValue();
1349: while (retryCount > 0) {
1350: try {
1351: getHibernateTemplate().update(item);
1352: retryCount = 0;
1353: } catch (Exception e) {
1354: log.warn("problem updating item: "
1355: + e.getMessage());
1356: retryCount = PersistenceService.getInstance()
1357: .retryDeadlock(e, retryCount);
1358: }
1359: }
1360: } else {
1361: newItemSet.add(item);
1362: }
1363: }
1364: // log.debug("***itemSet after=" + newItemSet.size());
1365: section.setItemSet(newItemSet);
1366: int retryCount = PersistenceService.getInstance()
1367: .getRetryCount().intValue();
1368: while (retryCount > 0) {
1369: try {
1370: getHibernateTemplate().update(section);
1371: retryCount = 0;
1372: } catch (Exception e) {
1373: log.warn("problem updating section: " + e.getMessage());
1374: retryCount = PersistenceService.getInstance()
1375: .retryDeadlock(e, retryCount);
1376: }
1377: }
1378: }
1379:
1380: public boolean assessmentTitleIsUnique(final Long assessmentBaseId,
1381: String title, Boolean isTemplate) {
1382: title = title.trim();
1383: final String currentSiteId = AgentFacade.getCurrentSiteId();
1384: final String agentString = AgentFacade.getAgentString();
1385: List list;
1386: boolean isUnique = true;
1387: String query = "";
1388: if (isTemplate.booleanValue()) { // templates are person scoped
1389: query = "select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate)"
1390: + " from AssessmentTemplateData a, AuthorizationData z where "
1391: + " a.title=? and a.assessmentBaseId!=? and a.createdBy=? and a.status=1";
1392:
1393: final String hql = query;
1394: final String titlef = title;
1395: HibernateCallback hcb = new HibernateCallback() {
1396: public Object doInHibernate(Session session)
1397: throws HibernateException, SQLException {
1398: Query q = session.createQuery(hql);
1399: q.setString(0, titlef);
1400: q.setLong(1, assessmentBaseId.longValue());
1401: q.setString(2, agentString);
1402: return q.list();
1403: };
1404: };
1405: list = getHibernateTemplate().executeFind(hcb);
1406:
1407: // list = getHibernateTemplate().find(query,
1408: // new Object[]{title,assessmentBaseId,agentString},
1409: // new org.hibernate.type.Type[] {Hibernate.STRING, Hibernate.LONG,
1410: // Hibernate.STRING});
1411: } else { // assessments are site scoped
1412: query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate)"
1413: + " from AssessmentData a, AuthorizationData z where "
1414: + " a.title=? and a.assessmentBaseId!=? and z.functionId='EDIT_ASSESSMENT' and "
1415: + " a.assessmentBaseId=z.qualifierId and z.agentIdString=?";
1416:
1417: final String hql = query;
1418: final String titlef = title;
1419: HibernateCallback hcb = new HibernateCallback() {
1420: public Object doInHibernate(Session session)
1421: throws HibernateException, SQLException {
1422: Query q = session.createQuery(hql);
1423: q.setString(0, titlef);
1424: q.setLong(1, assessmentBaseId.longValue());
1425: q.setString(2, currentSiteId);
1426: return q.list();
1427: };
1428: };
1429: list = getHibernateTemplate().executeFind(hcb);
1430:
1431: // list = getHibernateTemplate().find(query,
1432: // new Object[]{title,assessmentBaseId,currentSiteId},
1433: // new org.hibernate.type.Type[] {Hibernate.STRING, Hibernate.LONG,
1434: // Hibernate.STRING});
1435: }
1436: if (list.size() > 0) {
1437: // query in mysql & hsqldb are not case sensitive, check that title
1438: // found is indeed what we
1439: // are looking
1440: for (int i = 0; i < list.size(); i++) {
1441: AssessmentBaseIfc a = (AssessmentBaseIfc) list.get(i);
1442: if ((title).equals(a.getTitle().trim())) {
1443: isUnique = false;
1444: break;
1445: }
1446: }
1447: }
1448: return isUnique;
1449: }
1450:
1451: public List getAssessmentByTemplate(final Long templateId) {
1452: final String query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate) "
1453: + " from AssessmentData a where a.assessmentTemplateId=?";
1454:
1455: HibernateCallback hcb = new HibernateCallback() {
1456: public Object doInHibernate(Session session)
1457: throws HibernateException, SQLException {
1458: Query q = session.createQuery(query);
1459: q.setLong(0, templateId.longValue());
1460: return q.list();
1461: };
1462: };
1463: return getHibernateTemplate().executeFind(hcb);
1464:
1465: // return getHibernateTemplate().find(query,
1466: // new Object[]{ templateId },
1467: // new org.hibernate.type.Type[] { Hibernate.LONG });
1468: }
1469:
1470: public List getDefaultMetaDataSet() {
1471: final String query = " from AssessmentMetaData m where m.assessment.assessmentBaseId=?";
1472:
1473: HibernateCallback hcb = new HibernateCallback() {
1474: public Object doInHibernate(Session session)
1475: throws HibernateException, SQLException {
1476: Query q = session.createQuery(query);
1477: q.setLong(0, 1L);
1478: return q.list();
1479: };
1480: };
1481: return getHibernateTemplate().executeFind(hcb);
1482:
1483: // return getHibernateTemplate().find(query,
1484: // new Object[]{ new Long(1) },
1485: // new org.hibernate.type.Type[] { Hibernate.LONG });
1486:
1487: }
1488:
1489: private String fileSizeInKB(int fileSize) {
1490: String fileSizeString = "1";
1491: int size = Math.round(fileSize / 1024);
1492: if (size > 0) {
1493: fileSizeString = size + "";
1494: }
1495: return fileSizeString;
1496: }
1497:
1498: public String getRelativePath(String url, String protocol) {
1499: // replace whitespace with %20
1500: url = replaceSpace(url);
1501: int index = url.lastIndexOf(protocol);
1502: if (index == 0) {
1503: url = url.substring(protocol.length());
1504: }
1505: return url;
1506: }
1507:
1508: private String replaceSpace(String tempString) {
1509: String newString = "";
1510: char[] oneChar = new char[1];
1511: for (int i = 0; i < tempString.length(); i++) {
1512: if (tempString.charAt(i) != ' ') {
1513: oneChar[0] = tempString.charAt(i);
1514: String concatString = new String(oneChar);
1515: newString = newString.concat(concatString);
1516: } else {
1517: newString = newString.concat("%20");
1518: }
1519: }
1520: return newString;
1521: }
1522:
1523: public void removeItemAttachment(Long itemAttachmentId) {
1524: ItemAttachment itemAttachment = (ItemAttachment) getHibernateTemplate()
1525: .load(ItemAttachment.class, itemAttachmentId);
1526: ItemDataIfc item = itemAttachment.getItem();
1527: // String resourceId = itemAttachment.getResourceId();
1528: int retryCount = PersistenceService.getInstance()
1529: .getRetryCount().intValue();
1530: while (retryCount > 0) {
1531: try {
1532: if (item != null) { // need to dissociate with item before
1533: // deleting in Hibernate 3
1534: Set set = item.getItemAttachmentSet();
1535: set.remove(itemAttachment);
1536: getHibernateTemplate().delete(itemAttachment);
1537: retryCount = 0;
1538: }
1539: } catch (Exception e) {
1540: log.warn("problem delete itemAttachment: "
1541: + e.getMessage());
1542: retryCount = PersistenceService.getInstance()
1543: .retryDeadlock(e, retryCount);
1544: }
1545: }
1546: }
1547:
1548: public void updateAssessmentLastModifiedInfo(
1549: AssessmentFacade assessmentFacade) {
1550: int retryCount = PersistenceService.getInstance()
1551: .getRetryCount().intValue();
1552: AssessmentBaseIfc data = assessmentFacade.getData();
1553: data.setLastModifiedBy(AgentFacade.getAgentString());
1554: data.setLastModifiedDate(new Date());
1555: retryCount = PersistenceService.getInstance().getRetryCount()
1556: .intValue();
1557: while (retryCount > 0) {
1558: try {
1559: getHibernateTemplate().update(data);
1560: retryCount = 0;
1561: } catch (Exception e) {
1562: log
1563: .warn("problem update assessment: "
1564: + e.getMessage());
1565: retryCount = PersistenceService.getInstance()
1566: .retryDeadlock(e, retryCount);
1567: }
1568: }
1569: }
1570:
1571: public ItemAttachmentIfc createItemAttachment(ItemDataIfc item,
1572: String resourceId, String filename, String protocol) {
1573: ItemAttachment attach = null;
1574: Boolean isLink = Boolean.FALSE;
1575: try {
1576: ContentResource cr = ContentHostingService
1577: .getResource(resourceId);
1578: if (cr != null) {
1579: ResourceProperties p = cr.getProperties();
1580: attach = new ItemAttachment();
1581: attach.setItem(item);
1582: attach.setResourceId(resourceId);
1583: attach.setMimeType(cr.getContentType());
1584: // we want to display kb, so divide by 1000 and round the result
1585: attach.setFileSize(new Long(""
1586: + fileSizeInKB(cr.getContentLength())));
1587: if (cr.getContentType().lastIndexOf("url") > -1) {
1588: isLink = Boolean.TRUE;
1589: if (!filename.toLowerCase().startsWith("http")) {
1590: String adjustedFilename = "http://" + filename;
1591: attach.setFilename(adjustedFilename);
1592: } else {
1593: attach.setFilename(filename);
1594: }
1595: } else {
1596: attach.setFilename(filename);
1597: }
1598: attach.setIsLink(isLink);
1599: attach.setStatus(ItemAttachmentIfc.ACTIVE_STATUS);
1600: attach.setCreatedBy(p.getProperty(p
1601: .getNamePropCreator()));
1602: attach.setCreatedDate(new Date());
1603: attach.setLastModifiedBy(p.getProperty(p
1604: .getNamePropModifiedBy()));
1605: attach.setLastModifiedDate(new Date());
1606: attach.setLocation(getRelativePath(cr.getUrl(),
1607: protocol));
1608: // getHibernateTemplate().save(attach);
1609: }
1610: } catch (PermissionException pe) {
1611: pe.printStackTrace();
1612: } catch (IdUnusedException ie) {
1613: ie.printStackTrace();
1614: } catch (TypeException te) {
1615: te.printStackTrace();
1616: }
1617: return attach;
1618: }
1619:
1620: public SectionAttachmentIfc createSectionAttachment(
1621: SectionDataIfc section, String resourceId, String filename,
1622: String protocol) {
1623: SectionAttachment attach = null;
1624: Boolean isLink = Boolean.FALSE;
1625: try {
1626: ContentResource cr = ContentHostingService
1627: .getResource(resourceId);
1628: if (cr != null) {
1629: ResourceProperties p = cr.getProperties();
1630: attach = new SectionAttachment();
1631: attach.setSection(section);
1632: attach.setResourceId(resourceId);
1633: attach.setMimeType(cr.getContentType());
1634: // we want to display kb, so divide by 1000 and round the result
1635: attach.setFileSize(new Long(fileSizeInKB(cr
1636: .getContentLength())));
1637: if (cr.getContentType().lastIndexOf("url") > -1) {
1638: isLink = Boolean.TRUE;
1639: if (!filename.toLowerCase().startsWith("http")) {
1640: String adjustedFilename = "http://" + filename;
1641: attach.setFilename(adjustedFilename);
1642: } else {
1643: attach.setFilename(filename);
1644: }
1645: } else {
1646: attach.setFilename(filename);
1647: }
1648: attach.setIsLink(isLink);
1649: attach.setStatus(SectionAttachmentIfc.ACTIVE_STATUS);
1650: attach.setCreatedBy(p.getProperty(p
1651: .getNamePropCreator()));
1652: attach.setCreatedDate(new Date());
1653: attach.setLastModifiedBy(p.getProperty(p
1654: .getNamePropModifiedBy()));
1655: attach.setLastModifiedDate(new Date());
1656: attach.setLocation(getRelativePath(cr.getUrl(),
1657: protocol));
1658: // getHibernateTemplate().save(attach);
1659: }
1660: } catch (PermissionException pe) {
1661: pe.printStackTrace();
1662: } catch (IdUnusedException ie) {
1663: ie.printStackTrace();
1664: } catch (TypeException te) {
1665: te.printStackTrace();
1666: }
1667:
1668: return attach;
1669: }
1670:
1671: public void removeSectionAttachment(Long sectionAttachmentId) {
1672: SectionAttachment sectionAttachment = (SectionAttachment) getHibernateTemplate()
1673: .load(SectionAttachment.class, sectionAttachmentId);
1674: SectionDataIfc section = sectionAttachment.getSection();
1675: // String resourceId = sectionAttachment.getResourceId();
1676: int retryCount = PersistenceService.getInstance()
1677: .getRetryCount().intValue();
1678: while (retryCount > 0) {
1679: try {
1680: if (section != null) { // need to dissociate with section
1681: // before deleting in Hibernate 3
1682: Set set = section.getSectionAttachmentSet();
1683: set.remove(sectionAttachment);
1684: getHibernateTemplate().delete(sectionAttachment);
1685: retryCount = 0;
1686: }
1687: } catch (Exception e) {
1688: log.warn("problem delete sectionAttachment: "
1689: + e.getMessage());
1690: retryCount = PersistenceService.getInstance()
1691: .retryDeadlock(e, retryCount);
1692: }
1693: }
1694: }
1695:
1696: public AssessmentAttachmentIfc createAssessmentAttachment(
1697: AssessmentIfc assessment, String resourceId,
1698: String filename, String protocol) {
1699: AssessmentAttachment attach = null;
1700: Boolean isLink = Boolean.FALSE;
1701: try {
1702: ContentResource cr = ContentHostingService
1703: .getResource(resourceId);
1704: if (cr != null) {
1705: ResourceProperties p = cr.getProperties();
1706: attach = new AssessmentAttachment();
1707: attach.setAssessment(assessment);
1708: attach.setResourceId(resourceId);
1709: attach.setFilename(filename);
1710: attach.setMimeType(cr.getContentType());
1711: // we want to display kb, so divide by 1000 and round the result
1712: attach.setFileSize(new Long(fileSizeInKB(cr
1713: .getContentLength())));
1714: if (cr.getContentType().lastIndexOf("url") > -1) {
1715: isLink = Boolean.TRUE;
1716: if (!filename.toLowerCase().startsWith("http")) {
1717: String adjustedFilename = "http://" + filename;
1718: attach.setFilename(adjustedFilename);
1719: } else {
1720: attach.setFilename(filename);
1721: }
1722: } else {
1723: attach.setFilename(filename);
1724: }
1725: attach.setIsLink(isLink);
1726: attach.setStatus(AssessmentAttachmentIfc.ACTIVE_STATUS);
1727: attach.setCreatedBy(p.getProperty(p
1728: .getNamePropCreator()));
1729: attach.setCreatedDate(new Date());
1730: attach.setLastModifiedBy(p.getProperty(p
1731: .getNamePropModifiedBy()));
1732: attach.setLastModifiedDate(new Date());
1733: attach.setLocation(getRelativePath(cr.getUrl(),
1734: protocol));
1735: // getHibernateTemplate().save(attach);
1736: }
1737: } catch (PermissionException pe) {
1738: pe.printStackTrace();
1739: } catch (IdUnusedException ie) {
1740: ie.printStackTrace();
1741: } catch (TypeException te) {
1742: te.printStackTrace();
1743: }
1744: return attach;
1745: }
1746:
1747: public void removeAssessmentAttachment(Long assessmentAttachmentId) {
1748: AssessmentAttachment assessmentAttachment = (AssessmentAttachment) getHibernateTemplate()
1749: .load(AssessmentAttachment.class,
1750: assessmentAttachmentId);
1751: AssessmentIfc assessment = assessmentAttachment.getAssessment();
1752: // String resourceId = assessmentAttachment.getResourceId();
1753: int retryCount = PersistenceService.getInstance()
1754: .getRetryCount().intValue();
1755: while (retryCount > 0) {
1756: try {
1757: if (assessment != null) { // need to dissociate with
1758: // assessment before deleting in
1759: // Hibernate 3
1760: Set set = assessment.getAssessmentAttachmentSet();
1761: set.remove(assessmentAttachment);
1762: getHibernateTemplate().delete(assessmentAttachment);
1763: retryCount = 0;
1764: }
1765: } catch (Exception e) {
1766: log.warn("problem delete assessmentAttachment: "
1767: + e.getMessage());
1768: retryCount = PersistenceService.getInstance()
1769: .retryDeadlock(e, retryCount);
1770: }
1771: }
1772: }
1773:
1774: public AttachmentData createEmailAttachment(String resourceId,
1775: String filename, String protocol) {
1776: AttachmentData attach = null;
1777: Boolean isLink = Boolean.FALSE;
1778: try {
1779: ContentResource cr = ContentHostingService
1780: .getResource(resourceId);
1781: if (cr != null) {
1782: ResourceProperties p = cr.getProperties();
1783: attach = new AttachmentData();
1784: attach.setResourceId(resourceId);
1785: attach.setMimeType(cr.getContentType());
1786: // we want to display kb, so divide by 1000 and round
1787: // the result
1788: attach.setFileSize(new Long(fileSizeInKB(cr
1789: .getContentLength())));
1790: if (cr.getContentType().lastIndexOf("url") > -1) {
1791: isLink = Boolean.TRUE;
1792: if (!filename.toLowerCase().startsWith("http")) {
1793: String adjustedFilename = "http://" + filename;
1794: attach.setFilename(adjustedFilename);
1795: } else {
1796: attach.setFilename(filename);
1797: }
1798: } else {
1799: attach.setFilename(filename);
1800: }
1801: attach.setIsLink(isLink);
1802: attach.setStatus(SectionAttachmentIfc.ACTIVE_STATUS);
1803: attach.setCreatedBy(p.getProperty(p
1804: .getNamePropCreator()));
1805: attach.setCreatedDate(new Date());
1806: attach.setLastModifiedBy(p.getProperty(p
1807: .getNamePropModifiedBy()));
1808: attach.setLastModifiedDate(new Date());
1809: attach.setLocation(getRelativePath(cr.getUrl(),
1810: protocol));
1811: }
1812: } catch (PermissionException pe) {
1813: pe.printStackTrace();
1814: } catch (IdUnusedException ie) {
1815: ie.printStackTrace();
1816: } catch (TypeException te) {
1817: te.printStackTrace();
1818: }
1819: return attach;
1820: }
1821:
1822: public void saveOrUpdateAttachments(List list) {
1823: getHibernateTemplate().saveOrUpdateAll(list);
1824: }
1825:
1826: public List getAllActiveAssessmentsByAgent(final String siteAgentId) {
1827: final String query = "select a "
1828: + " from AssessmentData a,AuthorizationData z where a.status=1 and "
1829: + "a.assessmentBaseId=z.qualifierId and z.functionId='EDIT_ASSESSMENT' "
1830: + " and z.agentIdString=?";
1831:
1832: HibernateCallback hcb = new HibernateCallback() {
1833: public Object doInHibernate(Session session)
1834: throws HibernateException, SQLException {
1835: Query q = session.createQuery(query);
1836: q.setString(0, siteAgentId);
1837: return q.list();
1838: };
1839: };
1840: return getHibernateTemplate().executeFind(hcb);
1841: }
1842:
1843: public void copyAllAssessments(String fromContext, String toContext) {
1844: List list = getAllActiveAssessmentsByAgent(fromContext);
1845: ArrayList newList = new ArrayList();
1846: for (int i = 0; i < list.size(); i++) {
1847: AssessmentData a = (AssessmentData) list.get(i);
1848: log.debug("****protocol:"
1849: + ServerConfigurationService.getServerUrl());
1850: AssessmentData new_a = prepareAssessment(a,
1851: ServerConfigurationService.getServerUrl());
1852: newList.add(new_a);
1853: }
1854: getHibernateTemplate().saveOrUpdateAll(newList); // write
1855: // authorization
1856: for (int i = 0; i < newList.size(); i++) {
1857: AssessmentData a = (AssessmentData) newList.get(i);
1858: PersistenceService.getInstance().getAuthzQueriesFacade()
1859: .createAuthorization(toContext, "EDIT_ASSESSMENT",
1860: a.getAssessmentId().toString());
1861: }
1862: }
1863:
1864: public AssessmentData prepareAssessment(AssessmentData a,
1865: String protocol) {
1866: AssessmentData newAssessment = new AssessmentData(
1867: new Long("0"), a.getTitle(), a.getDescription(), a
1868: .getComments(), null, TypeFacade.HOMEWORK, a
1869: .getInstructorNotification(), a
1870: .getTesteeNotification(), a
1871: .getMultipartAllowed(), a.getStatus(), a
1872: .getCreatedBy(), new Date(), a
1873: .getLastModifiedBy(), new Date());
1874: // section set
1875: Set newSectionSet = prepareSectionSet(newAssessment, a
1876: .getSectionSet(), protocol);
1877: newAssessment.setSectionSet(newSectionSet);
1878: // access control
1879: AssessmentAccessControl newAccessControl = prepareAssessmentAccessControl(
1880: newAssessment, (AssessmentAccessControl) a
1881: .getAssessmentAccessControl());
1882: newAssessment.setAssessmentAccessControl(newAccessControl);
1883: // evaluation model
1884: EvaluationModel newEvaluationModel = prepareEvaluationModel(
1885: newAssessment, (EvaluationModel) a.getEvaluationModel());
1886: newAssessment.setEvaluationModel(newEvaluationModel);
1887: // feedback
1888: AssessmentFeedback newFeedback = prepareAssessmentFeedback(
1889: newAssessment, (AssessmentFeedback) a
1890: .getAssessmentFeedback());
1891: newAssessment.setAssessmentFeedback(newFeedback);
1892: // metadata
1893: Set newMetaDataSet = prepareAssessmentMetaDataSet(
1894: newAssessment, a.getAssessmentMetaDataSet());
1895: log.debug(" metadata set" + a.getAssessmentMetaDataSet());
1896: log.debug(" new metadata set" + newMetaDataSet);
1897: newAssessment.setAssessmentMetaDataSet(newMetaDataSet);
1898: // let's check if we need a newUrl
1899: String releaseTo = newAccessControl.getReleaseTo();
1900: if (releaseTo != null) {
1901: boolean anonymousAllowed = ((releaseTo)
1902: .indexOf(AuthoringConstantStrings.ANONYMOUS) > -1);
1903: if (anonymousAllowed) {
1904: // generate an alias to the pub assessment
1905: String alias = AgentFacade.getAgentString()
1906: + (new Date()).getTime();
1907: AssessmentMetaData meta = new AssessmentMetaData(
1908: newAssessment, "ALIAS", alias);
1909: newMetaDataSet.add(meta);
1910: newAssessment.setAssessmentMetaDataSet(newMetaDataSet);
1911: }
1912: }
1913: // IPAddress
1914: Set newIPSet = prepareSecuredIPSet(newAssessment, a
1915: .getSecuredIPAddressSet());
1916: newAssessment.setSecuredIPAddressSet(newIPSet);
1917: // attachmentSet
1918: Set newAssessmentAttachmentSet = prepareAssessmentAttachmentSet(
1919: newAssessment, a.getAssessmentAttachmentSet(), protocol);
1920: newAssessment
1921: .setAssessmentAttachmentSet(newAssessmentAttachmentSet);
1922:
1923: return newAssessment;
1924: }
1925:
1926: public AssessmentFeedback prepareAssessmentFeedback(
1927: AssessmentData p, AssessmentFeedback a) {
1928: if (a == null) {
1929: return null;
1930: }
1931: AssessmentFeedback newFeedback = new AssessmentFeedback(a
1932: .getFeedbackDelivery(), a.getFeedbackAuthoring(), a
1933: .getEditComponents(), a.getShowQuestionText(), a
1934: .getShowStudentResponse(), a.getShowCorrectResponse(),
1935: a.getShowStudentScore(), a
1936: .getShowStudentQuestionScore(), a
1937: .getShowQuestionLevelFeedback(), a
1938: .getShowSelectionLevelFeedback(), a
1939: .getShowGraderComments(), a.getShowStatistics());
1940: newFeedback.setAssessmentBase(p);
1941: return newFeedback;
1942: }
1943:
1944: public AssessmentAccessControl prepareAssessmentAccessControl(
1945: AssessmentData p, AssessmentAccessControl a) {
1946: if (a == null) {
1947: return new AssessmentAccessControl();
1948: }
1949: AssessmentAccessControl newAccessControl = new AssessmentAccessControl(
1950: a.getSubmissionsAllowed(), a.getSubmissionsSaved(), a
1951: .getAssessmentFormat(), a.getBookMarkingItem(),
1952: a.getTimeLimit(), a.getTimedAssessment(), a
1953: .getRetryAllowed(), a.getLateHandling(), a
1954: .getStartDate(), a.getDueDate(), a
1955: .getScoreDate(), a.getFeedbackDate(), a
1956: .getRetractDate(), a.getAutoSubmit(), a
1957: .getItemNavigation(), a.getItemNumbering(), a
1958: .getSubmissionMessage(), a.getReleaseTo());
1959: newAccessControl.setUsername(a.getUsername());
1960: newAccessControl.setPassword(a.getPassword());
1961: newAccessControl.setFinalPageUrl(a.getFinalPageUrl());
1962: newAccessControl.setUnlimitedSubmissions(a
1963: .getUnlimitedSubmissions());
1964: newAccessControl.setAssessmentBase(p);
1965: return newAccessControl;
1966: }
1967:
1968: public EvaluationModel prepareEvaluationModel(AssessmentData p,
1969: EvaluationModel e) {
1970: if (e == null) {
1971: return null;
1972: }
1973: EvaluationModel newEvaluationModel = new EvaluationModel(e
1974: .getEvaluationComponents(), e.getScoringType(), e
1975: .getNumericModelId(), e.getFixedTotalScore(), e
1976: .getGradeAvailable(), e.getIsStudentIdPublic(), e
1977: .getAnonymousGrading(), e.getAutoScoring(), e
1978: .getToGradeBook());
1979: newEvaluationModel.setAssessmentBase(p);
1980: return newEvaluationModel;
1981: }
1982:
1983: public Set prepareAssessmentMetaDataSet(AssessmentData p,
1984: Set metaDataSet) {
1985: HashSet h = new HashSet();
1986: Iterator i = metaDataSet.iterator();
1987: while (i.hasNext()) {
1988: AssessmentMetaData metaData = (AssessmentMetaData) i.next();
1989: AssessmentMetaData newMetaData = new AssessmentMetaData(p,
1990: metaData.getLabel(), metaData.getEntry());
1991: h.add(newMetaData);
1992: }
1993: return h;
1994: }
1995:
1996: public Set prepareSecuredIPSet(AssessmentData p, Set ipSet) {
1997: HashSet h = new HashSet();
1998: Iterator i = ipSet.iterator();
1999: while (i.hasNext()) {
2000: SecuredIPAddress ip = (SecuredIPAddress) i.next();
2001: SecuredIPAddress newIP = new SecuredIPAddress(p, ip
2002: .getHostname(), ip.getIpAddress());
2003: h.add(newIP);
2004: }
2005: return h;
2006: }
2007:
2008: public Set prepareSectionSet(AssessmentData newAssessment,
2009: Set sectionSet, String protocol) {
2010: log.debug("new section size = " + sectionSet.size());
2011: HashSet h = new HashSet();
2012: Iterator i = sectionSet.iterator();
2013: while (i.hasNext()) {
2014: SectionData section = (SectionData) i.next();
2015:
2016: // TODO note: 4/28 need to check if a part is random draw , if it is
2017: // then need to add questions from pool to this section, at this
2018: // point,
2019:
2020: SectionData newSection = new SectionData(section
2021: .getDuration(), section.getSequence(), section
2022: .getTitle(), section.getDescription(), section
2023: .getTypeId(), section.getStatus(), section
2024: .getCreatedBy(), section.getCreatedDate(), section
2025: .getLastModifiedBy(), section.getLastModifiedDate());
2026: Set newSectionAttachmentSet = prepareSectionAttachmentSet(
2027: newSection, section.getSectionAttachmentSet(),
2028: protocol);
2029: newSection.setSectionAttachmentSet(newSectionAttachmentSet);
2030: Set newItemSet = prepareItemSet(newSection, section
2031: .getItemSet(), protocol);
2032: newSection.setItemSet(newItemSet);
2033: Set newMetaDataSet = prepareSectionMetaDataSet(newSection,
2034: section.getSectionMetaDataSet());
2035: newSection.setSectionMetaDataSet(newMetaDataSet);
2036: newSection.setAssessment(newAssessment);
2037: h.add(newSection);
2038: }
2039: return h;
2040: }
2041:
2042: public Set prepareSectionMetaDataSet(SectionData newSection,
2043: Set metaDataSet) {
2044: HashSet h = new HashSet();
2045: Iterator n = metaDataSet.iterator();
2046: while (n.hasNext()) {
2047: SectionMetaData sectionMetaData = (SectionMetaData) n
2048: .next();
2049: SectionMetaData newSectionMetaData = new SectionMetaData(
2050: newSection, sectionMetaData.getLabel(),
2051: sectionMetaData.getEntry());
2052: h.add(newSectionMetaData);
2053: }
2054: return h;
2055: }
2056:
2057: public Set prepareItemSet(SectionData newSection, Set itemSet,
2058: String protocol) {
2059: log.debug("new item size = " + itemSet.size());
2060: HashSet h = new HashSet();
2061: Iterator j = itemSet.iterator();
2062: while (j.hasNext()) {
2063: ItemData item = (ItemData) j.next();
2064: ItemData newItem = new ItemData(newSection, item
2065: .getSequence(), item.getDuration(), item
2066: .getInstruction(), item.getDescription(), item
2067: .getTypeId(), item.getGrade(), item.getScore(),
2068: item.getHint(), item.getHasRationale(), item
2069: .getStatus(), item.getCreatedBy(), item
2070: .getCreatedDate(),
2071: item.getLastModifiedBy(), item
2072: .getLastModifiedDate(), null, null, null, // set ItemTextSet, itemMetaDataSet and
2073: // itemFeedbackSet later
2074: item.getTriesAllowed());
2075: Set newItemTextSet = prepareItemTextSet(newItem, item
2076: .getItemTextSet());
2077: Set newItemMetaDataSet = prepareItemMetaDataSet(newItem,
2078: item.getItemMetaDataSet());
2079: Set newItemFeedbackSet = prepareItemFeedbackSet(newItem,
2080: item.getItemFeedbackSet());
2081: Set newItemAttachmentSet = prepareItemAttachmentSet(
2082: newItem, item.getItemAttachmentSet(), protocol);
2083: newItem.setItemTextSet(newItemTextSet);
2084: newItem.setItemMetaDataSet(newItemMetaDataSet);
2085: newItem.setItemFeedbackSet(newItemFeedbackSet);
2086: newItem.setItemAttachmentSet(newItemAttachmentSet);
2087: h.add(newItem);
2088: }
2089: return h;
2090: }
2091:
2092: public Set prepareItemTextSet(ItemData newItem, Set itemTextSet) {
2093: log.debug("new item text size = " + itemTextSet.size());
2094: HashSet h = new HashSet();
2095: Iterator k = itemTextSet.iterator();
2096: while (k.hasNext()) {
2097: ItemText itemText = (ItemText) k.next();
2098: log.debug("item text id =" + itemText.getId());
2099: ItemText newItemText = new ItemText(newItem, itemText
2100: .getSequence(), itemText.getText(), null);
2101: Set newAnswerSet = prepareAnswerSet(newItemText, itemText
2102: .getAnswerSet());
2103: newItemText.setAnswerSet(newAnswerSet);
2104: h.add(newItemText);
2105: }
2106: return h;
2107: }
2108:
2109: public Set prepareItemMetaDataSet(ItemData newItem,
2110: Set itemMetaDataSet) {
2111: HashSet h = new HashSet();
2112: Iterator n = itemMetaDataSet.iterator();
2113: while (n.hasNext()) {
2114: ItemMetaData itemMetaData = (ItemMetaData) n.next();
2115: ItemMetaData newItemMetaData = new ItemMetaData(newItem,
2116: itemMetaData.getLabel(), itemMetaData.getEntry());
2117: h.add(newItemMetaData);
2118: }
2119: return h;
2120: }
2121:
2122: public Set prepareItemFeedbackSet(ItemData newItem,
2123: Set itemFeedbackSet) {
2124: HashSet h = new HashSet();
2125: Iterator o = itemFeedbackSet.iterator();
2126: while (o.hasNext()) {
2127: ItemFeedback itemFeedback = (ItemFeedback) o.next();
2128: ItemFeedback newItemFeedback = new ItemFeedback(newItem,
2129: itemFeedback.getTypeId(), itemFeedback.getText());
2130: h.add(newItemFeedback);
2131: }
2132: return h;
2133: }
2134:
2135: public Set prepareItemAttachmentSet(ItemData newItem,
2136: Set itemAttachmentSet, String protocol) {
2137: HashSet h = new HashSet();
2138: Iterator o = itemAttachmentSet.iterator();
2139: while (o.hasNext()) {
2140: ItemAttachment itemAttachment = (ItemAttachment) o.next();
2141: try {
2142: // create a copy of the resource
2143: AssessmentService service = new AssessmentService();
2144: ContentResource cr_copy = service
2145: .createCopyOfContentResource(itemAttachment
2146: .getResourceId(), itemAttachment
2147: .getFilename());
2148: // get relative path
2149: String url = getRelativePath(cr_copy.getUrl(), protocol);
2150:
2151: ItemAttachment newItemAttachment = new ItemAttachment(
2152: null, newItem, cr_copy.getId(), itemAttachment
2153: .getFilename(), itemAttachment
2154: .getMimeType(), itemAttachment
2155: .getFileSize(), itemAttachment
2156: .getDescription(), url, itemAttachment
2157: .getIsLink(), itemAttachment
2158: .getStatus(), itemAttachment
2159: .getCreatedBy(), itemAttachment
2160: .getCreatedDate(), itemAttachment
2161: .getLastModifiedBy(), itemAttachment
2162: .getLastModifiedDate());
2163: h.add(newItemAttachment);
2164: } catch (Exception e) {
2165: log.warn(e.getMessage());
2166: }
2167: }
2168: return h;
2169: }
2170:
2171: public Set prepareSectionAttachmentSet(SectionData newSection,
2172: Set sectionAttachmentSet, String protocol) {
2173: HashSet h = new HashSet();
2174: Iterator o = sectionAttachmentSet.iterator();
2175: while (o.hasNext()) {
2176: SectionAttachment sectionAttachment = (SectionAttachment) o
2177: .next();
2178: // create a copy of the resource
2179: AssessmentService service = new AssessmentService();
2180: ContentResource cr_copy = service
2181: .createCopyOfContentResource(sectionAttachment
2182: .getResourceId(), sectionAttachment
2183: .getFilename());
2184:
2185: // get relative path
2186: String url = getRelativePath(cr_copy.getUrl(), protocol);
2187:
2188: SectionAttachment newSectionAttachment = new SectionAttachment(
2189: null, newSection, cr_copy.getId(),
2190: sectionAttachment.getFilename(), sectionAttachment
2191: .getMimeType(), sectionAttachment
2192: .getFileSize(), sectionAttachment
2193: .getDescription(), url, sectionAttachment
2194: .getIsLink(),
2195: sectionAttachment.getStatus(), sectionAttachment
2196: .getCreatedBy(), sectionAttachment
2197: .getCreatedDate(), sectionAttachment
2198: .getLastModifiedBy(), sectionAttachment
2199: .getLastModifiedDate());
2200: h.add(newSectionAttachment);
2201: }
2202: return h;
2203: }
2204:
2205: public Set prepareAssessmentAttachmentSet(
2206: AssessmentData newAssessment, Set assessmentAttachmentSet,
2207: String protocol) {
2208: HashSet h = new HashSet();
2209: Iterator o = assessmentAttachmentSet.iterator();
2210: while (o.hasNext()) {
2211: AssessmentAttachment assessmentAttachment = (AssessmentAttachment) o
2212: .next();
2213: // create a copy of the resource
2214: AssessmentService service = new AssessmentService();
2215: ContentResource cr_copy = service
2216: .createCopyOfContentResource(assessmentAttachment
2217: .getResourceId(), assessmentAttachment
2218: .getFilename());
2219:
2220: // get relative path
2221: String url = getRelativePath(cr_copy.getUrl(), protocol);
2222:
2223: AssessmentAttachment newAssessmentAttachment = new AssessmentAttachment(
2224: null, newAssessment, cr_copy.getId(),
2225: assessmentAttachment.getFilename(),
2226: assessmentAttachment.getMimeType(),
2227: assessmentAttachment.getFileSize(),
2228: assessmentAttachment.getDescription(), url,
2229: assessmentAttachment.getIsLink(),
2230: assessmentAttachment.getStatus(),
2231: assessmentAttachment.getCreatedBy(),
2232: assessmentAttachment.getCreatedDate(),
2233: assessmentAttachment.getLastModifiedBy(),
2234: assessmentAttachment.getLastModifiedDate());
2235: h.add(newAssessmentAttachment);
2236: }
2237: return h;
2238: }
2239:
2240: public Set prepareAnswerSet(ItemText newItemText, Set answerSet) {
2241: log.debug("new answer size = " + answerSet.size());
2242: HashSet h = new HashSet();
2243: Iterator l = answerSet.iterator();
2244: while (l.hasNext()) {
2245: Answer answer = (Answer) l.next();
2246: Answer newAnswer = new Answer(newItemText,
2247: answer.getText(), answer.getSequence(), answer
2248: .getLabel(), answer.getIsCorrect(), answer
2249: .getGrade(), answer.getScore(), null);
2250: Set newAnswerFeedbackSet = prepareAnswerFeedbackSet(
2251: newAnswer, answer.getAnswerFeedbackSet());
2252: newAnswer.setAnswerFeedbackSet(newAnswerFeedbackSet);
2253: h.add(newAnswer);
2254: }
2255: return h;
2256: }
2257:
2258: public Set prepareAnswerFeedbackSet(Answer newAnswer,
2259: Set answerFeedbackSet) {
2260: HashSet h = new HashSet();
2261: Iterator m = answerFeedbackSet.iterator();
2262: while (m.hasNext()) {
2263: AnswerFeedback answerFeedback = (AnswerFeedback) m.next();
2264: AnswerFeedback newAnswerFeedback = new AnswerFeedback(
2265: newAnswer, answerFeedback.getTypeId(),
2266: answerFeedback.getText());
2267: h.add(newAnswerFeedback);
2268: }
2269: return h;
2270: }
2271:
2272: public String getAssessmentSiteId(String assessmentId) {
2273: String query = "select a from AuthorizationData a where "
2274: + " a.functionId='EDIT_ASSESSMENT' and a.qualifierId="
2275: + assessmentId;
2276: List l = getHibernateTemplate().find(query);
2277: if (l.size() > 0) {
2278: AuthorizationData a = (AuthorizationData) l.get(0);
2279: return a.getAgentIdString();
2280: } else
2281: return null;
2282: }
2283:
2284: public String getAssessmentCreatedBy(String assessmentId) {
2285: String query = "select a from AssessmentData a where a.assessmentBaseId = "
2286: + assessmentId;
2287: List l = getHibernateTemplate().find(query);
2288: if (l.size() > 0) {
2289: AssessmentData a = (AssessmentData) l.get(0);
2290: return a.getCreatedBy();
2291: } else
2292: return null;
2293: }
2294: }
|