Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**********************************************************************************
* $URL$
* $Id$
***********************************************************************************
*
* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009 The Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************************/
package org.sakaiproject.tool.assessment.facade;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.query.Query;
import org.sakaiproject.component.cover.ServerConfigurationService;
import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.event.cover.EventTrackingService;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.exception.TypeException;
import org.sakaiproject.rubrics.api.RubricsConstants;
import org.sakaiproject.rubrics.api.RubricsService;
import org.sakaiproject.rubrics.api.beans.RubricTransferBean;
import org.sakaiproject.rubrics.api.model.ToolItemRubricAssociation;
import org.sakaiproject.samigo.util.SamigoConstants;
import org.sakaiproject.site.api.Group;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.spring.SpringBeanLocator;
import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAccessControl;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentFeedback;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentMetaData;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentTemplateData;
import org.sakaiproject.tool.assessment.data.dao.assessment.AttachmentData;
import org.sakaiproject.tool.assessment.data.dao.assessment.EvaluationModel;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemTag;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemTextAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentData;
import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemTextAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.SectionAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
import org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData;
import org.sakaiproject.tool.assessment.data.dao.assessment.SecuredIPAddress;
import org.sakaiproject.tool.assessment.data.dao.authz.AuthorizationData;
import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAccessControlIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAttachmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentBaseIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentMetaDataIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AttachmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemMetaDataIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemTextAttachmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemTextIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionAttachmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
import org.sakaiproject.tool.assessment.entity.api.CoreAssessmentEntityProvider;
import org.sakaiproject.tool.assessment.entity.api.ItemEntityProvider;
import org.sakaiproject.tool.assessment.facade.util.PagingUtilQueriesAPI;
import org.sakaiproject.tool.assessment.integration.context.IntegrationContextFactory;
import org.sakaiproject.tool.assessment.integration.helper.ifc.GradebookServiceHelper;
import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
import org.sakaiproject.tool.assessment.services.PersistenceService;
import org.sakaiproject.tool.assessment.services.QuestionPoolService;
import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.HibernateQueryException;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.web.client.HttpClientErrorException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AssessmentFacadeQueries extends HibernateDaoSupport implements AssessmentFacadeQueriesAPI {
// private ResourceBundle rb =
// ResourceBundle.getBundle("org.sakaiproject.tool.assessment.bundle.Messages");
public static final String TITLE = "title";
public AssessmentFacadeQueries() {
}
public IdImpl getId(String id) {
return new IdImpl(id);
}
public IdImpl getId(Long id) {
return new IdImpl(id);
}
public IdImpl getId(long id) {
return new IdImpl(id);
}
public IdImpl getAssessmentId(String id) {
return new IdImpl(id);
}
public IdImpl getAssessmentId(Long id) {
return new IdImpl(id);
}
public IdImpl getAssessmentId(long id) {
return new IdImpl(id);
}
public IdImpl getAssessmentTemplateId(String id) {
return new IdImpl(id);
}
public IdImpl getAssessmentTemplateId(Long id) {
return new IdImpl(id);
}
public IdImpl getAssessmentTemplateId(long id) {
return new IdImpl(id);
}
public Long addTemplate() {
AssessmentTemplateData assessmentTemplate = new AssessmentTemplateData(
Long.valueOf(0), "title", "description", "comments",
TypeD.HOMEWORK, Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1),
Integer.valueOf(1), "1", new Date(), "1", new Date());
AssessmentAccessControl s = new AssessmentAccessControl(Integer.valueOf(0),
Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), SamigoConstants.NOTI_PREF_INSTRUCTOR_EMAIL_DEFAULT,
new Date(), new Date(), new Date(), new Date(), new Date(), Integer.valueOf(1),
Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), "Thanks for submitting",
"anonymous");
s.setAssessmentBase(assessmentTemplate);
assessmentTemplate
.setAssessmentAccessControl((AssessmentAccessControlIfc) s);
assessmentTemplate.addAssessmentMetaData(
"ASSESSMENTTEMPLATE_OBJECTIVES",
" assesmentT: the objective is to ...");
// take default submission model
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount()
.intValue();
while (retryCount > 0) {
try {
getHibernateTemplate().save(assessmentTemplate);
retryCount = 0;
} catch (Exception e) {
log.warn("problem saving template: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e,
retryCount);
}
}
return assessmentTemplate.getAssessmentTemplateId();
}
public void removeTemplate(Long assessmentId) {
AssessmentTemplateData assessment = (AssessmentTemplateData) getHibernateTemplate()
.load(AssessmentTemplateData.class, assessmentId);
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount()
.intValue();
while (retryCount > 0) {
try {
getHibernateTemplate().delete(assessment);
retryCount = 0;
} catch (Exception e) {
log.warn("problem delete template: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e,
retryCount);
}
}
}
public Long addAssessment(Long assessmentTemplateId) {
AssessmentData assessment = new AssessmentData( Long.valueOf(0),
"assessment title", "assessment description",
"assessment acomments", assessmentTemplateId, TypeD.HOMEWORK,
Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1),
"1", new Date(), "1", new Date());
AssessmentAccessControl s = new AssessmentAccessControl(Integer.valueOf(1),
Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1),
Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), SamigoConstants.NOTI_PREF_INSTRUCTOR_EMAIL_DEFAULT,
new Date(), new Date(), new Date(), new Date(), new Date(), Integer.valueOf(1),
Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1),"Thanks for submitting",
"anonymous");
s.setAssessmentBase(assessment);
assessment.setAssessmentAccessControl((AssessmentAccessControlIfc) s);
assessment.addAssessmentMetaData("ASSESSMENT_OBJECTIVES",
" assesment: the objective is to ...");
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount()
.intValue();
while (retryCount > 0) {
try {
getHibernateTemplate().save(assessment);
retryCount = 0;
} catch (Exception e) {
log.warn("problem saving assessment: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e,
retryCount);
}
}
return assessment.getAssessmentId();
}
public AssessmentBaseData load(Long id) {
AssessmentBaseData a = (AssessmentBaseData) getHibernateTemplate()
.load(AssessmentBaseData.class, id);
if (a.getIsTemplate().equals(Boolean.TRUE)) {
return (AssessmentTemplateData) a;
} else {
return (AssessmentData) a;
}
}
public AssessmentTemplateData loadTemplate(Long assessmentTemplateId) {
return (AssessmentTemplateData) getHibernateTemplate().load(
AssessmentTemplateData.class, assessmentTemplateId);
}
public AssessmentData loadAssessment(Long assessmentId) {
return (AssessmentData) getHibernateTemplate().load(
AssessmentData.class, assessmentId);
}
/*
* The following methods are real
*
*/
public AssessmentTemplateFacade getAssessmentTemplate(
Long assessmentTemplateId) {
AssessmentTemplateData template = (AssessmentTemplateData) getHibernateTemplate()
.load(AssessmentTemplateData.class, assessmentTemplateId);
return new AssessmentTemplateFacade(template);
}
// sakai2.0 we want to scope it by creator, users can only see their
// templates plus the "Default Template"
public List getAllAssessmentTemplates() {
final String agent = AgentFacade.getAgentString();
final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
HibernateCallback> hcb = session -> {
Query q = session.createQuery(
"select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.typeId)"
+ " from AssessmentTemplateData a where a.assessmentBaseId = :id or"
+ " a.createdBy = :agent or a.typeId = :type order by a.title"
);
q.setParameter("id", 1L);
q.setParameter("agent", agent);
q.setParameter("type", typeId);
return q.list();
};
List list = getHibernateTemplate().execute(hcb);
List templateList = new ArrayList<>();
for (AssessmentTemplateData a : list) {
AssessmentTemplateFacade f = new AssessmentTemplateFacade(a);
templateList.add(f);
}
return templateList;
}
// sakai2.0 we want to scope it by creator, users can only see their
// templates plus the "Default Template"
public List getAllActiveAssessmentTemplates() {
final String agent = AgentFacade.getAgentString();
final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
HibernateCallback> hcb = session -> {
Query q = session.createQuery(
"select new AssessmentTemplateData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.typeId)"
+ " from AssessmentTemplateData a where a.status = :status and (a.assessmentBaseId = :id or"
+ " a.createdBy = :agent or a.typeId = :type) order by a.title");
q.setParameter("status", 1);
q.setParameter("id", 1L);
q.setParameter("agent", agent);
q.setParameter("type", typeId);
return q.list();
};
List list = getHibernateTemplate().execute(hcb);
List templateList = new ArrayList<>();
for (AssessmentTemplateData a : list) {
AssessmentTemplateFacade f = new AssessmentTemplateFacade(a);
templateList.add(f);
}
return templateList;
}
/**
*
* @return a list of AssessmentTemplateFacade. However, it is IMPORTANT to
* note that it is not a full object, it contains merely
* assessmentBaseId (which is the templateId) & title. This methods
* is used when a list of template titles is required for displaying
* purposes. In Sakai2.0, template are scoped by creator, i.e. users
* can only see their own template plus the "Default Template"
*/
public List getTitleOfAllActiveAssessmentTemplates() {
final String agent = AgentFacade.getAgentString();
final Long typeId = TypeD.TEMPLATE_SYSTEM_DEFINED;
HibernateCallback> hcb = session -> {
Query q = session.createQuery(
"select new AssessmentTemplateData(a.assessmentBaseId, a.title) "
+ " from AssessmentTemplateData a where a.status = :status and "
+ " (a.assessmentBaseId = :id or a.createdBy = :agent or typeId = :type) order by a.title"
);
q.setParameter("status", 1);
q.setParameter("id", Long.valueOf(1));
q.setParameter("agent", agent);
q.setParameter("type", typeId.longValue());
return q.list();
};
List list = getHibernateTemplate().execute(hcb);
List templateList = new ArrayList<>();
for (AssessmentTemplateData a : list) {
a.setAssessmentTemplateId(a.getAssessmentBaseId());
AssessmentTemplateFacade f = new AssessmentTemplateFacade(a.getAssessmentBaseId(), a.getTitle());
templateList.add(f);
}
return templateList;
}
public AssessmentFacade getAssessment(Long assessmentId) {
try {
AssessmentData assessment = getHibernateTemplate().get(AssessmentData.class, assessmentId);
if (assessment != null) {
assessment.setSectionSet(getSectionSetForAssessment(assessment));
return new AssessmentFacade(assessment);
}
} catch (DataAccessException dae) {
log.warn("Could not retrieve assessment: {}", assessmentId, dae);
}
return null;
}
private Set getSectionSetForAssessment(AssessmentData assessment) {
List sectionList = (List) getHibernateTemplate().findByNamedParam("from SectionData s where s.assessment.assessmentBaseId = :id", "id", assessment.getAssessmentBaseId());
Hibernate.initialize(sectionList);
return new HashSet<>(sectionList);
}
public void removeAssessment(final Long assessmentId) {
// if pubAssessment exist, simply set assessment to inactive else delete assessment
List count = (List) getHibernateTemplate()
.findByNamedParam("select count(p) from PublishedAssessmentData p where p.assessmentId = :id", "id", assessmentId);
log.debug("removeAssesment: no. of pub Assessment = {}", count.size());
Iterator iter = count.iterator();
int i = ((Long) iter.next()).intValue();
AssessmentData assessment = (AssessmentData) getHibernateTemplate().load(AssessmentData.class, assessmentId);
// SAK-42943 Commented because attachments aren't removed on soft deletion, but this will be handy whenever hard deletion is added
/*if (i < 1) {
AssessmentService s = new AssessmentService();
List resourceIdList = s.getAssessmentResourceIdList(assessment);
if (log.isDebugEnabled()) log.debug("*** we have no. of resource in assessment=" + resourceIdList.size());
s.deleteResources(resourceIdList);
}*/
RubricsService rubricsService = (RubricsService) SpringBeanLocator.getInstance().getBean("org.sakaiproject.rubrics.api.RubricsService");
rubricsService.softDeleteRubricAssociationsByItemIdPrefix(assessmentId + ".", RubricsConstants.RBCS_TOOL_SAMIGO);
assessment.setLastModifiedBy(AgentFacade.getAgentString());
assessment.setLastModifiedDate(new Date());
assessment.setStatus(AssessmentIfc.DEAD_STATUS);
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount();
while (retryCount > 0) {
try {
getHibernateTemplate().update(assessment);
retryCount = 0;
} catch (Exception e) {
log.warn("problem updating asssessment: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper()
.retryDeadlock(e, retryCount);
}
}
}
/* this assessment comes with a default section */
public AssessmentData cloneAssessmentFromTemplate(AssessmentTemplateData t) {
AssessmentData assessment = new AssessmentData(t.getParentId(),
"Assessment created with" + t.getTitle(), t.getDescription(), t
.getComments(), t.getAssessmentTemplateId(),
TypeD.HOMEWORK, // by default for now
t.getInstructorNotification(), t.getTesteeNotification(), t
.getMultipartAllowed(), t.getStatus(), AgentFacade
.getAgentString(), new Date(), AgentFacade
.getAgentString(), new Date());
try {
// deal with Access Control
AssessmentAccessControl controlOrig = (AssessmentAccessControl) t
.getAssessmentAccessControl();
if (controlOrig != null) {
AssessmentAccessControl control = (AssessmentAccessControl) controlOrig
.clone();
control.setAssessmentBase(assessment);
assessment.setAssessmentAccessControl(control);
}
// deal with feedback
AssessmentFeedback feedbackOrig = (AssessmentFeedback) t
.getAssessmentFeedback();
if (feedbackOrig != null) {
AssessmentFeedback feedback = (AssessmentFeedback) feedbackOrig
.clone();
feedback.setAssessmentBase(assessment);
assessment.setAssessmentFeedback(feedback);
}
// deal with evaluation
EvaluationModel evalOrig = (EvaluationModel) t.getEvaluationModel();
if (evalOrig != null) {
EvaluationModel eval = (EvaluationModel) evalOrig.clone();
eval.setAssessmentBase(assessment);
assessment.setEvaluationModel(eval);
}
// deal with MetaData
HashSet h = new HashSet();
Set s = t.getAssessmentMetaDataSet();
Iterator iter = s.iterator();
while (iter.hasNext()) {
AssessmentMetaData mOrig = (AssessmentMetaData) iter.next();
if (mOrig.getLabel() != null) {
AssessmentMetaData m = new AssessmentMetaData(assessment,
mOrig.getLabel(), mOrig.getEntry());
h.add(m);
}
}
assessment.setAssessmentMetaDataSet(h);
// we need to add the FIRST section to an assessment
// it is a requirement that each assesment must have at least one
// section
HashSet sh = new HashSet();
SectionData section = new SectionData(
null,
new Integer("1"), // FIRST section
"Default", "", TypeD.DEFAULT_SECTION,
SectionData.ACTIVE_STATUS, AgentFacade.getAgentString(),
new Date(), AgentFacade.getAgentString(), new Date());
section.setAssessment(assessment);
// add default part type, and question Ordering
section.addSectionMetaData(SectionDataIfc.AUTHOR_TYPE,
SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE.toString());
section.addSectionMetaData(SectionDataIfc.QUESTIONS_ORDERING,
SectionDataIfc.AS_LISTED_ON_ASSESSMENT_PAGE.toString());
sh.add(section);
assessment.setSectionSet(sh);
} catch (CloneNotSupportedException ex) {
log.error(ex.getMessage(), ex);
}
return assessment;
}
/**
* This method is the same as createAssessment() except that no default
* section will be created with the assessment.
*/
public AssessmentFacade createAssessmentWithoutDefaultSection(String title,
String description, Long typeId, Long templateId) throws Exception {
return createAssessmentWithoutDefaultSection(title, description, typeId, templateId, null);
}
public AssessmentFacade createAssessmentWithoutDefaultSection(String title,
String description, Long typeId, Long templateId, String siteId) throws Exception {
// this assessment came with one default section
AssessmentData assessment = null;
try {
assessment = prepareAssessment(title, description, typeId,
templateId, siteId);
} catch (Exception e) {
throw new Exception(e);
}
assessment.setSectionSet(new HashSet());
getHibernateTemplate().save(assessment);
// register assessment with current site
registerWithSite(assessment.getAssessmentId().toString(), siteId);
return new AssessmentFacade(assessment);
}
private AssessmentData prepareAssessment(String title, String description,
Long typeId, Long templateId, String siteId) throws Exception {
// #1 - get the template (a facade) and create Assessment based on it
AssessmentTemplateFacade template = getAssessmentTemplate(templateId);
AssessmentData assessment = cloneAssessmentFromTemplate((AssessmentTemplateData) template
.getData());
assessment.setTitle(title);
assessment.setDescription(description);
assessment.setTypeId(typeId);
AssessmentAccessControl control = (AssessmentAccessControl) assessment
.getAssessmentAccessControl();
if (control == null) {
control = new AssessmentAccessControl();
}
// Set default value for timed assessment
control.setTimedAssessment(Integer.valueOf(0));
control.setTimeLimit(Integer.valueOf(0));
// set accessControl.releaseTo based on default setting in metaData
String defaultReleaseTo = template
.getAssessmentMetaDataByLabel("releaseTo");
if (("ANONYMOUS_USERS").equals(defaultReleaseTo)) {
control.setReleaseTo("Anonymous Users");
} else {
if (siteId == null || siteId.length() == 0) {
control.setReleaseTo(AgentFacade.getCurrentSiteName());
} else {
control.setReleaseTo(AgentFacade.getSiteName(siteId));
}
}
EvaluationModel evaluation = (EvaluationModel) assessment
.getEvaluationModel();
if (evaluation == null) {
evaluation = new EvaluationModel();
}
org.sakaiproject.grading.api.GradingService g = null;
boolean integrated = IntegrationContextFactory.getInstance()
.isIntegrated();
try {
if (integrated) {
g = (org.sakaiproject.grading.api.GradingService) SpringBeanLocator.getInstance().getBean(
"org.sakaiproject.grading.api.GradingService");
}
GradebookServiceHelper gbsHelper = IntegrationContextFactory
.getInstance().getGradebookServiceHelper();
} catch (HibernateQueryException e) {
log.warn("Gradebook Error: " + e.getMessage());
evaluation
.setToGradeBook(EvaluationModelIfc.GRADEBOOK_NOT_AVAILABLE.toString());
throw new Exception(e);
}
return assessment;
}
public AssessmentFacade createAssessment(String title, String description,
Long typeId, Long templateId) throws Exception {
return createAssessment(title, description, typeId, templateId, null);
}
public AssessmentFacade createAssessment(String title, String description,
Long typeId, Long templateId, String siteId) throws Exception {
// this assessment comes with a default section
AssessmentData assessment = null;
try {
assessment = prepareAssessment(title, description, typeId,
templateId, siteId);
} catch (Exception e) {
throw new Exception(e);
}
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount()
.intValue();
while (retryCount > 0) {
try {
getHibernateTemplate().save(assessment);
retryCount = 0;
} catch (Exception e) {
log.warn("problem saving assessment: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e,
retryCount);
}
}
// register assessmnet with current site
registerWithSite(assessment.getAssessmentId().toString(), siteId);
return new AssessmentFacade(assessment);
}
private void registerWithSite(String qualifierIdString, String siteId) {
if (siteId == null || siteId.length() == 0) {
PersistenceService.getInstance().getAuthzQueriesFacade()
.createAuthorization(AgentFacade.getCurrentSiteId(),
"EDIT_ASSESSMENT", qualifierIdString);
} else {
PersistenceService.getInstance().getAuthzQueriesFacade()
.createAuthorization(siteId,
"EDIT_ASSESSMENT", qualifierIdString);
}
}
private void registerWithCurrentSite(String qualifierIdString) {
registerWithSite(qualifierIdString, null);
}
public List getAllAssessments(String orderBy) {
List list = (List) getHibernateTemplate().find("from AssessmentData a order by a." + orderBy);
List assessmentList = new ArrayList<>();
for (AssessmentData a : list) {;
assessmentList.add(new AssessmentFacade(a));
}
return assessmentList;
}
public List getAllActiveAssessments(String orderBy) {
List list = (List) getHibernateTemplate().findByNamedParam(
"from AssessmentData a where a.status = :status order by a." + orderBy, "status", 1);
List assessmentList = new ArrayList<>();
for (AssessmentData a : list) {
a.setSectionSet(getSectionSetForAssessment(a));
AssessmentFacade f = new AssessmentFacade(a);
assessmentList.add(f);
}
return assessmentList;
}
public List getBasicInfoOfAllActiveAssessments(String orderBy, boolean ascending) {
String query = "select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate)from AssessmentData a where a.status = :status order by a." + orderBy;
if (ascending) {
query += " asc";
} else {
query += " desc";
}
List list = (List) getHibernateTemplate().findByNamedParam(query, "status", 1);
List assessmentList = new ArrayList<>();
for (AssessmentData a : list) {
assessmentList.add(new AssessmentFacade(a.getAssessmentBaseId(), a.getTitle(), a.getLastModifiedDate()));
}
return assessmentList;
}
public List getBasicInfoOfAllActiveAssessmentsByAgent(String orderBy, final String siteAgentId, boolean ascending) {
// Get the list of assessment
StringBuilder sb = new StringBuilder("select new AssessmentData(a.assessmentBaseId, a.title, a.lastModifiedDate, a.lastModifiedBy, ac.startDate, ac.dueDate, ac.releaseTo) ");
sb.append("from AssessmentData a, AuthorizationData z, AssessmentAccessControl ac where a.status = :status and ");
sb.append("a.assessmentBaseId=z.qualifierId and z.functionId = :fid ");
sb.append("and z.agentIdString = :site ");
sb.append("and ac.assessmentBase.assessmentBaseId = a.assessmentBaseId order by a.");
sb.append(orderBy);
String query = sb.toString();
if (ascending)
query += " asc";
else
query += " desc";
final String hql = query;
HibernateCallback> hcb = session -> {
Query q = session.createQuery(hql);
q.setParameter("status", 1);
q.setParameter("fid", "EDIT_ASSESSMENT");
q.setParameter("site", siteAgentId);
return q.list();
};
List list = getHibernateTemplate().execute(hcb);
// Get the number of question in each assessment
HibernateCallback> hcb2 = session -> {
Query q2 = session.createQuery(
"select a.assessmentBaseId, count(*) from ItemData i, SectionData s, AssessmentData a, AuthorizationData z " +
"where a = s.assessment and s = i.section and a.assessmentBaseId = z.qualifierId and z.functionId = :fid and z.agentIdString = :site " +
"group by a.assessmentBaseId ");
q2.setParameter("fid", "EDIT_ASSESSMENT");
q2.setParameter("site", siteAgentId);
return q2.list();
};
List