0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/AssessmentGradingFacadeQueries.java $
0003: * $Id: AssessmentGradingFacadeQueries.java 9348 2006-05-13 06:14:57Z 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.io.InputStream;
0023: import java.io.File;
0024: import java.sql.Connection;
0025: import java.sql.PreparedStatement;
0026: import java.sql.ResultSet;
0027: import java.sql.SQLException;
0028: import java.util.ArrayList;
0029: import java.util.Collection;
0030: import java.util.Date;
0031: import java.util.HashMap;
0032: import java.util.HashSet;
0033: import java.util.Iterator;
0034: import java.util.List;
0035: import java.util.Set;
0036:
0037: import org.hibernate.Criteria;
0038: import org.hibernate.HibernateException;
0039: import org.hibernate.Query;
0040: import org.hibernate.Session;
0041: import org.hibernate.criterion.Criterion;
0042: import org.hibernate.criterion.Disjunction;
0043: import org.hibernate.criterion.Expression;
0044: import org.hibernate.criterion.Order;
0045:
0046: import org.apache.commons.logging.Log;
0047: import org.apache.commons.logging.LogFactory;
0048: import org.sakaiproject.tool.assessment.services.PersistenceService;
0049: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentData;
0050: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemData;
0051: import org.sakaiproject.tool.assessment.data.dao.grading.AssessmentGradingData;
0052: import org.sakaiproject.tool.assessment.data.dao.grading.ItemGradingData;
0053: import org.sakaiproject.tool.assessment.data.dao.grading.MediaData;
0054: import org.sakaiproject.tool.assessment.data.dao.grading.StudentGradingSummaryData;
0055: import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
0056: import org.sakaiproject.tool.assessment.data.ifc.assessment.PublishedAssessmentIfc;
0057: import org.sakaiproject.tool.assessment.data.ifc.grading.AssessmentGradingIfc;
0058: import org.sakaiproject.tool.assessment.data.ifc.grading.ItemGradingIfc;
0059: import org.sakaiproject.tool.assessment.data.ifc.grading.StudentGradingSummaryIfc;
0060: import org.springframework.orm.hibernate3.HibernateCallback;
0061: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0062:
0063: public class AssessmentGradingFacadeQueries extends HibernateDaoSupport
0064: implements AssessmentGradingFacadeQueriesAPI {
0065: private static Log log = LogFactory
0066: .getLog(AssessmentGradingFacadeQueries.class);
0067:
0068: public AssessmentGradingFacadeQueries() {
0069: }
0070:
0071: public List getTotalScores(final String publishedId, String which) {
0072: try {
0073: // sectionSet of publishedAssessment is defined as lazy loading in
0074: // Hibernate OR map, so we need to initialize them. Unfortunately our
0075: // spring-1.0.2.jar does not support HibernateTemplate.intialize(Object)
0076: // so we need to do it ourselves
0077: PublishedAssessmentData assessment = PersistenceService
0078: .getInstance()
0079: .getPublishedAssessmentFacadeQueries()
0080: .loadPublishedAssessment(new Long(publishedId));
0081: HashSet sectionSet = PersistenceService.getInstance()
0082: .getPublishedAssessmentFacadeQueries()
0083: .getSectionSetForAssessment(assessment);
0084: assessment.setSectionSet(sectionSet);
0085: // proceed to get totalScores
0086: // Object[] objects = new Object[2];
0087: // objects[0] = new Long(publishedId);
0088: // objects[1] = new Boolean(true);
0089: // Type[] types = new Type[2];
0090: // types[0] = Hibernate.LONG;
0091: // types[1] = Hibernate.BOOLEAN;
0092:
0093: final HibernateCallback hcb = new HibernateCallback() {
0094: public Object doInHibernate(Session session)
0095: throws HibernateException, SQLException {
0096: Query q = session
0097: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by a.agentId ASC, a.finalScore DESC, a.submittedDate DESC");
0098: q.setLong(0, Long.parseLong(publishedId));
0099: q.setBoolean(1, true);
0100: return q.list();
0101: };
0102: };
0103: List list = getHibernateTemplate().executeFind(hcb);
0104:
0105: // List list = getHibernateTemplate().find(
0106: // "from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by agentId ASC, finalScore DESC, submittedDate DESC",
0107: // objects, types);
0108:
0109: // last submission
0110: if (which.equals(EvaluationModelIfc.LAST_SCORE.toString())) {
0111: final HibernateCallback hcb2 = new HibernateCallback() {
0112: public Object doInHibernate(Session session)
0113: throws HibernateException, SQLException {
0114: Query q = session
0115: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by a.agentId ASC, a.submittedDate DESC");
0116: q.setLong(0, Long.parseLong(publishedId));
0117: q.setBoolean(1, true);
0118: return q.list();
0119: };
0120: };
0121: list = getHibernateTemplate().executeFind(hcb2);
0122:
0123: // list = getHibernateTemplate().find(
0124: // "from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by agentId ASC, submittedDate DESC",
0125: // objects, types);
0126: }
0127:
0128: if (which.equals(EvaluationModelIfc.ALL_SCORE.toString())) {
0129: return list;
0130: } else {
0131: // only take highest or latest
0132: Iterator items = list.iterator();
0133: ArrayList newlist = new ArrayList();
0134: String agentid = null;
0135: AssessmentGradingData data = (AssessmentGradingData) items
0136: .next();
0137: // daisyf add the following line on 12/15/04
0138: data.setPublishedAssessmentId(assessment
0139: .getPublishedAssessmentId());
0140: agentid = data.getAgentId();
0141: newlist.add(data);
0142: while (items.hasNext()) {
0143: while (items.hasNext()) {
0144: data = (AssessmentGradingData) items.next();
0145: if (!data.getAgentId().equals(agentid)) {
0146: agentid = data.getAgentId();
0147: newlist.add(data);
0148: break;
0149: }
0150: }
0151: }
0152: return newlist;
0153: }
0154: } catch (RuntimeException e) {
0155: e.printStackTrace();
0156: return new ArrayList();
0157: }
0158: }
0159:
0160: public List getAllSubmissions(final String publishedId) {
0161: // Object[] objects = new Object[1];
0162: // objects[0] = new Long(publishedId);
0163: // Type[] types = new Type[1];
0164: // types[0] = Hibernate.LONG;
0165:
0166: final HibernateCallback hcb = new HibernateCallback() {
0167: public Object doInHibernate(Session session)
0168: throws HibernateException, SQLException {
0169: Query q = session
0170: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=1");
0171: q.setLong(0, Long.parseLong(publishedId));
0172: return q.list();
0173: };
0174: };
0175: return getHibernateTemplate().executeFind(hcb);
0176:
0177: // List list = getHibernateTemplate().find("from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=1", objects, types);
0178: // return list;
0179: }
0180:
0181: public HashMap getItemScores(Long publishedId, final Long itemId,
0182: String which) {
0183: try {
0184: List scores = getTotalScores(publishedId.toString(), which);
0185: HashMap map = new HashMap();
0186: //List list = new ArrayList();
0187:
0188: // make final for callback to access
0189: final Iterator iter = scores.iterator();
0190:
0191: HibernateCallback hcb = new HibernateCallback() {
0192: public Object doInHibernate(Session session)
0193: throws HibernateException, SQLException {
0194: Criteria criteria = session
0195: .createCriteria(ItemGradingData.class);
0196: Disjunction disjunction = Expression.disjunction();
0197:
0198: /** make list from AssessmentGradingData ids */
0199: List gradingIdList = new ArrayList();
0200: while (iter.hasNext()) {
0201: AssessmentGradingData data = (AssessmentGradingData) iter
0202: .next();
0203: gradingIdList
0204: .add(data.getAssessmentGradingId());
0205: }
0206:
0207: /** create or disjunctive expression for (in clauses) */
0208: List tempList = new ArrayList();
0209: for (int i = 0; i < gradingIdList.size(); i += 50) {
0210: if (i + 50 > gradingIdList.size()) {
0211: tempList = gradingIdList.subList(i,
0212: gradingIdList.size());
0213: disjunction.add(Expression.in(
0214: "assessmentGradingId", tempList));
0215: } else {
0216: tempList = gradingIdList.subList(i, i + 50);
0217: disjunction.add(Expression.in(
0218: "assessmentGradingId", tempList));
0219: }
0220: }
0221:
0222: if (itemId.equals(new Long(0))) {
0223: criteria.add(disjunction);
0224: //criteria.add(Expression.isNotNull("submittedDate"));
0225: } else {
0226:
0227: /** create logical and between the pubCriterion and the disjunction criterion */
0228: //Criterion pubCriterion = Expression.eq("publishedItem.itemId", itemId);
0229: Criterion pubCriterion = Expression.eq(
0230: "publishedItemId", itemId);
0231: criteria.add(Expression.and(pubCriterion,
0232: disjunction));
0233: //criteria.add(Expression.isNotNull("submittedDate"));
0234: }
0235: criteria.addOrder(Order.asc("agentId"));
0236: criteria.addOrder(Order.desc("submittedDate"));
0237: //return criteria.list();
0238: //large list cause out of memory error (java heap space)
0239: return criteria.setMaxResults(10000).list();
0240: }
0241: };
0242: List temp = (List) getHibernateTemplate().execute(hcb);
0243:
0244: Iterator iter2 = temp.iterator();
0245: while (iter2.hasNext()) {
0246: ItemGradingData data = (ItemGradingData) iter2.next();
0247: ArrayList this one = (ArrayList) map.get(data
0248: .getPublishedItemId());
0249: if (this one == null)
0250: this one = new ArrayList();
0251: this one.add(data);
0252: map.put(data.getPublishedItemId(), this one);
0253: }
0254: return map;
0255: } catch (Exception e) {
0256: e.printStackTrace();
0257: return new HashMap();
0258: }
0259: }
0260:
0261: /**
0262: * This returns a hashmap of all the latest item entries, keyed by
0263: * item id for easy retrieval.
0264: * return (Long publishedItemId, ArrayList itemGradingData)
0265: */
0266: public HashMap getLastItemGradingData(final Long publishedId,
0267: final String agentId) {
0268: try {
0269: // Object[] objects = new Object[2];
0270: // objects[0] = publishedId;
0271: // objects[1] = agentId;
0272: // Type[] types = new Type[2];
0273: // types[0] = Hibernate.LONG;
0274: // types[1] = Hibernate.STRING;
0275:
0276: final HibernateCallback hcb = new HibernateCallback() {
0277: public Object doInHibernate(Session session)
0278: throws HibernateException, SQLException {
0279: Query q = session
0280: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? order by a.submittedDate DESC");
0281: q.setLong(0, publishedId.longValue());
0282: q.setString(1, agentId);
0283: return q.list();
0284: };
0285: };
0286: List scores = getHibernateTemplate().executeFind(hcb);
0287:
0288: // ArrayList scores = (ArrayList) getHibernateTemplate().find("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? order by submittedDate DESC", objects, types);
0289: HashMap map = new HashMap();
0290: if (scores.isEmpty())
0291: return new HashMap();
0292: AssessmentGradingData gdata = (AssessmentGradingData) scores
0293: .toArray()[0];
0294: // initialize itemGradingSet
0295: gdata.setItemGradingSet(getItemGradingSet(gdata
0296: .getAssessmentGradingId()));
0297: if (gdata.getForGrade().booleanValue())
0298: return new HashMap();
0299: Iterator iter = gdata.getItemGradingSet().iterator();
0300: while (iter.hasNext()) {
0301: ItemGradingData data = (ItemGradingData) iter.next();
0302: ArrayList this one = (ArrayList) map.get(data
0303: .getPublishedItemId());
0304: if (this one == null)
0305: this one = new ArrayList();
0306: this one.add(data);
0307: map.put(data.getPublishedItemId(), this one);
0308: }
0309: return map;
0310: } catch (Exception e) {
0311: e.printStackTrace();
0312: return new HashMap();
0313: }
0314: }
0315:
0316: /**
0317: * This returns a hashmap of all the submitted items, keyed by
0318: * item id for easy retrieval.
0319: */
0320: public HashMap getStudentGradingData(String assessmentGradingId) {
0321: try {
0322: HashMap map = new HashMap();
0323: AssessmentGradingData gdata = load(new Long(
0324: assessmentGradingId));
0325: log.debug("****#6, gdata=" + gdata);
0326: //log.debug("****#7, item size="+gdata.getItemGradingSet().size());
0327: Iterator iter = gdata.getItemGradingSet().iterator();
0328: while (iter.hasNext()) {
0329: ItemGradingData data = (ItemGradingData) iter.next();
0330: ArrayList this one = (ArrayList) map.get(data
0331: .getPublishedItemId());
0332: if (this one == null)
0333: this one = new ArrayList();
0334: this one.add(data);
0335: map.put(data.getPublishedItemId(), this one);
0336: }
0337: return map;
0338: } catch (Exception e) {
0339: e.printStackTrace();
0340: return new HashMap();
0341: }
0342: }
0343:
0344: public HashMap getSubmitData(final Long publishedId,
0345: final String agentId, final Integer scoringoption) {
0346: try {
0347: // Object[] objects = new Object[3];
0348: // objects[0] = publishedId;
0349: // objects[1] = agentId;
0350: // objects[2] = new Boolean(true);
0351: // Type[] types = new Type[3];
0352: // types[0] = Hibernate.LONG;
0353: // types[1] = Hibernate.STRING;
0354: // types[2] = Hibernate.BOOLEAN;
0355:
0356: final HibernateCallback hcb = new HibernateCallback() {
0357: public Object doInHibernate(Session session)
0358: throws HibernateException, SQLException {
0359: log.debug("scoringoption = " + scoringoption);
0360: if (EvaluationModelIfc.LAST_SCORE
0361: .equals(scoringoption)) {
0362: // last submission
0363: Query q = session
0364: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate DESC");
0365: q.setLong(0, publishedId.longValue());
0366: q.setString(1, agentId);
0367: q.setBoolean(2, true);
0368: return q.list();
0369: } else {
0370: //highest submission
0371: Query q1 = session
0372: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.finalScore DESC, a.submittedDate DESC");
0373: q1.setLong(0, publishedId.longValue());
0374: q1.setString(1, agentId);
0375: q1.setBoolean(2, true);
0376: return q1.list();
0377: }
0378: };
0379: };
0380: List scores = getHibernateTemplate().executeFind(hcb);
0381:
0382: // ArrayList scores = (ArrayList) getHibernateTemplate().find("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by submittedDate DESC", objects, types);
0383: HashMap map = new HashMap();
0384: if (scores.isEmpty())
0385: return new HashMap();
0386: AssessmentGradingData gdata = (AssessmentGradingData) scores
0387: .toArray()[0];
0388: gdata.setItemGradingSet(getItemGradingSet(gdata
0389: .getAssessmentGradingId()));
0390: Iterator iter = gdata.getItemGradingSet().iterator();
0391: while (iter.hasNext()) {
0392: ItemGradingData data = (ItemGradingData) iter.next();
0393: ArrayList this one = (ArrayList) map.get(data
0394: .getPublishedItemId());
0395: if (this one == null)
0396: this one = new ArrayList();
0397: this one.add(data);
0398: map.put(data.getPublishedItemId(), this one);
0399: }
0400: return map;
0401: } catch (Exception e) {
0402: e.printStackTrace();
0403: return new HashMap();
0404: }
0405: }
0406:
0407: public Long add(AssessmentGradingData a) {
0408: int retryCount = PersistenceService.getInstance()
0409: .getRetryCount().intValue();
0410: while (retryCount > 0) {
0411: try {
0412: getHibernateTemplate().save(a);
0413: retryCount = 0;
0414: } catch (Exception e) {
0415: log.warn("problem adding assessmentGrading: "
0416: + e.getMessage());
0417: retryCount = PersistenceService.getInstance()
0418: .retryDeadlock(e, retryCount);
0419: }
0420: }
0421: return a.getAssessmentGradingId();
0422: }
0423:
0424: public int getSubmissionSizeOfPublishedAssessment(
0425: Long publishedAssessmentId) {
0426: List size = getHibernateTemplate()
0427: .find(
0428: "select count(a) from AssessmentGradingData a where a.forGrade=1 and a.publishedAssessmentId=?"
0429: + publishedAssessmentId);
0430: Iterator iter = size.iterator();
0431: if (iter.hasNext()) {
0432: int i = ((Integer) iter.next()).intValue();
0433: return i;
0434: } else {
0435: return 0;
0436: }
0437: }
0438:
0439: public HashMap getSubmissionSizeOfAllPublishedAssessments() {
0440: HashMap h = new HashMap();
0441: List list = getHibernateTemplate()
0442: .find(
0443: "select new PublishedAssessmentData(a.publishedAssessmentId, count(a)) from AssessmentGradingData a where a.forGrade=1 group by a.publishedAssessmentId");
0444: Iterator iter = list.iterator();
0445: while (iter.hasNext()) {
0446: PublishedAssessmentData o = (PublishedAssessmentData) iter
0447: .next();
0448: h.put(o.getPublishedAssessmentId(), new Integer(o
0449: .getSubmissionSize()));
0450: }
0451: return h;
0452: }
0453:
0454: public Long saveMedia(byte[] media, String mimeType) {
0455: log.debug("****" + AgentFacade.getAgentString()
0456: + "saving media...size=" + media.length + " "
0457: + (new Date()));
0458: MediaData mediaData = new MediaData(media, mimeType);
0459: int retryCount = PersistenceService.getInstance()
0460: .getRetryCount().intValue();
0461: while (retryCount > 0) {
0462: try {
0463: getHibernateTemplate().save(mediaData);
0464: retryCount = 0;
0465: } catch (Exception e) {
0466: log.warn("problem saving media with mimeType: "
0467: + e.getMessage());
0468: retryCount = PersistenceService.getInstance()
0469: .retryDeadlock(e, retryCount);
0470: }
0471: }
0472: log.debug("****" + AgentFacade.getAgentString()
0473: + "saved media." + (new Date()));
0474: return mediaData.getMediaId();
0475: }
0476:
0477: public Long saveMedia(MediaData mediaData) {
0478: log.debug("****" + mediaData.getFilename()
0479: + " saving media...size=" + mediaData.getFileSize()
0480: + " " + (new Date()));
0481: int retryCount = PersistenceService.getInstance()
0482: .getRetryCount().intValue();
0483: while (retryCount > 0) {
0484: try {
0485: getHibernateTemplate().save(mediaData);
0486: retryCount = 0;
0487: } catch (Exception e) {
0488: log.warn("problem saving media: " + e.getMessage());
0489: retryCount = PersistenceService.getInstance()
0490: .retryDeadlock(e, retryCount);
0491: }
0492: }
0493: log.debug("****" + mediaData.getFilename() + " saved media."
0494: + (new Date()));
0495: return mediaData.getMediaId();
0496: }
0497:
0498: public void removeMediaById(Long mediaId) {
0499: String mediaLocation = null;
0500: Session session = null;
0501: Connection conn = null;
0502: ResultSet rs = null;
0503: try {
0504: session = getSessionFactory().openSession();
0505: conn = session.connection();
0506: log.debug("****Connection=" + conn);
0507: String query0 = "select LOCATION from SAM_MEDIA_T where MEDIAID=?";
0508: PreparedStatement statement0 = conn
0509: .prepareStatement(query0);
0510: statement0.setLong(1, mediaId.longValue());
0511: rs = statement0.executeQuery();
0512: if (rs.next()) {
0513: mediaLocation = rs.getString("LOCATION");
0514: }
0515: log.debug("****mediaLocation=" + mediaLocation);
0516:
0517: String query = "delete from SAM_MEDIA_T where MEDIAID=?";
0518: PreparedStatement statement = conn.prepareStatement(query);
0519: statement.setLong(1, mediaId.longValue());
0520: statement.executeUpdate();
0521: } catch (Exception e) {
0522: log.warn(e.getMessage());
0523: } finally {
0524: try {
0525: if (session != null)
0526: session.close();
0527: if (conn != null)
0528: conn.close();
0529: if (rs != null)
0530: rs.close();
0531: } catch (Exception ex) {
0532: log.warn(ex.getMessage());
0533: }
0534: }
0535:
0536: try {
0537: if (mediaLocation != null) {
0538: File mediaFile = new File(mediaLocation);
0539: mediaFile.delete();
0540: }
0541: } catch (Exception e) {
0542: log.warn("problem removing file=" + e.getMessage());
0543: }
0544: }
0545:
0546: public MediaData getMedia(Long mediaId) {
0547:
0548: MediaData mediaData = (MediaData) getHibernateTemplate().load(
0549: MediaData.class, mediaId);
0550: if (mediaData != null) {
0551: String mediaLocation = mediaData.getLocation();
0552: if (mediaLocation == null
0553: || (mediaLocation.trim()).equals("")) {
0554: mediaData.setMedia(getMediaStream(mediaId));
0555: }
0556: }
0557: return mediaData;
0558: }
0559:
0560: public ArrayList getMediaArray(final Long itemGradingId) {
0561: log.debug("*** itemGradingId =" + itemGradingId);
0562: ArrayList a = new ArrayList();
0563:
0564: final HibernateCallback hcb = new HibernateCallback() {
0565: public Object doInHibernate(Session session)
0566: throws HibernateException, SQLException {
0567: Query q = session
0568: .createQuery("from MediaData m where m.itemGradingData.itemGradingId=?");
0569: q.setLong(0, itemGradingId.longValue());
0570: return q.list();
0571: };
0572: };
0573: List list = getHibernateTemplate().executeFind(hcb);
0574:
0575: for (int i = 0; i < list.size(); i++) {
0576: a.add((MediaData) list.get(i));
0577: }
0578: log.debug("*** no. of media =" + a.size());
0579: return a;
0580: }
0581:
0582: public ArrayList getMediaArray(ItemGradingData item) {
0583: ArrayList a = new ArrayList();
0584: List list = getHibernateTemplate().find(
0585: "from MediaData m where m.itemGradingData=?", item);
0586: for (int i = 0; i < list.size(); i++) {
0587: a.add((MediaData) list.get(i));
0588: }
0589: log.debug("*** no. of media =" + a.size());
0590: return a;
0591: }
0592:
0593: public List getMediaArray(Long publishedId,
0594: final Long publishedItemId, String which) {
0595: try {
0596: HashMap itemScores = (HashMap) getItemScores(publishedId,
0597: publishedItemId, which);
0598: final List list = (List) itemScores.get(publishedItemId);
0599: log.debug("list size list.size() = " + list.size());
0600:
0601: HibernateCallback hcb = new HibernateCallback() {
0602: public Object doInHibernate(Session session)
0603: throws HibernateException, SQLException {
0604: Criteria criteria = session
0605: .createCriteria(MediaData.class);
0606: Disjunction disjunction = Expression.disjunction();
0607:
0608: /** make list from AssessmentGradingData ids */
0609: List itemGradingIdList = new ArrayList();
0610: for (int i = 0; i < list.size(); i++) {
0611: ItemGradingIfc itemGradingData = (ItemGradingIfc) list
0612: .get(i);
0613: itemGradingIdList.add(itemGradingData
0614: .getItemGradingId());
0615: }
0616:
0617: /** create or disjunctive expression for (in clauses) */
0618: List tempList = new ArrayList();
0619: for (int i = 0; i < itemGradingIdList.size(); i += 50) {
0620: if (i + 50 > itemGradingIdList.size()) {
0621: tempList = itemGradingIdList.subList(i,
0622: itemGradingIdList.size());
0623: disjunction.add(Expression.in(
0624: "itemGradingData.itemGradingId",
0625: tempList));
0626: } else {
0627: tempList = itemGradingIdList.subList(i,
0628: i + 50);
0629: disjunction.add(Expression.in(
0630: "itemGradingData.itemGradingId",
0631: tempList));
0632: }
0633: }
0634: criteria.add(disjunction);
0635: return criteria.setMaxResults(10000).list();
0636: }
0637: };
0638: return (List) getHibernateTemplate().execute(hcb);
0639: } catch (Exception e) {
0640: e.printStackTrace();
0641: return new ArrayList();
0642: }
0643: }
0644:
0645: public ItemGradingData getLastItemGradingDataByAgent(
0646: final Long publishedItemId, final String agentId) {
0647: final HibernateCallback hcb = new HibernateCallback() {
0648: public Object doInHibernate(Session session)
0649: throws HibernateException, SQLException {
0650: Query q = session
0651: .createQuery("from ItemGradingData i where i.publishedItemId=? and i.agentId=?");
0652: q.setLong(0, publishedItemId.longValue());
0653: q.setString(1, agentId);
0654: return q.list();
0655: };
0656: };
0657: List itemGradings = getHibernateTemplate().executeFind(hcb);
0658:
0659: // List itemGradings = getHibernateTemplate().find(
0660: // "from ItemGradingData i where i.publishedItemId=? and i.agentId=?",
0661: // new Object[] { publishedItemId, agentId },
0662: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING });
0663: if (itemGradings.size() == 0)
0664: return null;
0665: return (ItemGradingData) itemGradings.get(0);
0666: }
0667:
0668: public ItemGradingData getItemGradingData(
0669: final Long assessmentGradingId, final Long publishedItemId) {
0670: log.debug("****assessmentGradingId=" + assessmentGradingId);
0671: log.debug("****publishedItemId=" + publishedItemId);
0672:
0673: final HibernateCallback hcb = new HibernateCallback() {
0674: public Object doInHibernate(Session session)
0675: throws HibernateException, SQLException {
0676: Query q = session
0677: .createQuery("from ItemGradingData i where i.assessmentGradingId = ? and i.publishedItemId=?");
0678: q.setLong(0, assessmentGradingId.longValue());
0679: q.setLong(1, publishedItemId.longValue());
0680: return q.list();
0681: };
0682: };
0683: List itemGradings = getHibernateTemplate().executeFind(hcb);
0684:
0685: // List itemGradings = getHibernateTemplate().find(
0686: // "from ItemGradingData i where i.assessmentGradingId = ? and i.publishedItemId=?",
0687: // new Object[] { assessmentGradingId, publishedItemId },
0688: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.LONG });
0689: if (itemGradings.size() == 0)
0690: return null;
0691: return (ItemGradingData) itemGradings.get(0);
0692: }
0693:
0694: public AssessmentGradingData load(Long id) {
0695: AssessmentGradingData gdata = (AssessmentGradingData) getHibernateTemplate()
0696: .load(AssessmentGradingData.class, id);
0697: gdata.setItemGradingSet(getItemGradingSet(gdata
0698: .getAssessmentGradingId()));
0699: return gdata;
0700: }
0701:
0702: public ItemGradingData getItemGrading(Long id) {
0703: return (ItemGradingData) getHibernateTemplate().load(
0704: ItemGradingData.class, id);
0705: }
0706:
0707: public AssessmentGradingData getLastSavedAssessmentGradingByAgentId(
0708: final Long publishedAssessmentId, final String agentIdString) {
0709: AssessmentGradingData ag = null;
0710:
0711: final HibernateCallback hcb = new HibernateCallback() {
0712: public Object doInHibernate(Session session)
0713: throws HibernateException, SQLException {
0714: Query q = session
0715: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate desc");
0716: q.setLong(0, publishedAssessmentId.longValue());
0717: q.setString(1, agentIdString);
0718: q.setBoolean(2, false);
0719: return q.list();
0720: };
0721: };
0722: List assessmentGradings = getHibernateTemplate().executeFind(
0723: hcb);
0724:
0725: // List assessmentGradings = getHibernateTemplate().find(
0726: // "from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate desc",
0727: // new Object[] { publishedAssessmentId, agentIdString, Boolean.FALSE },
0728: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING, Hibernate.BOOLEAN });
0729: if (assessmentGradings.size() != 0) {
0730: ag = (AssessmentGradingData) assessmentGradings.get(0);
0731: ag.setItemGradingSet(getItemGradingSet(ag
0732: .getAssessmentGradingId()));
0733: }
0734: return ag;
0735: }
0736:
0737: public AssessmentGradingData getLastSubmittedAssessmentGradingByAgentId(
0738: final Long publishedAssessmentId, final String agentIdString) {
0739: AssessmentGradingData ag = null;
0740:
0741: final HibernateCallback hcb = new HibernateCallback() {
0742: public Object doInHibernate(Session session)
0743: throws HibernateException, SQLException {
0744: Query q = session
0745: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate desc");
0746: q.setLong(0, publishedAssessmentId.longValue());
0747: q.setString(1, agentIdString);
0748: q.setBoolean(2, true);
0749: return q.list();
0750: };
0751: };
0752: List assessmentGradings = getHibernateTemplate().executeFind(
0753: hcb);
0754:
0755: // List assessmentGradings = getHibernateTemplate().find(
0756: // "from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate desc",
0757: // new Object[] { publishedAssessmentId, agentIdString, Boolean.FALSE },
0758: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING, Hibernate.BOOLEAN });
0759: if (assessmentGradings.size() != 0) {
0760: ag = (AssessmentGradingData) assessmentGradings.get(0);
0761: ag.setItemGradingSet(getItemGradingSet(ag
0762: .getAssessmentGradingId()));
0763: }
0764: return ag;
0765: }
0766:
0767: public AssessmentGradingIfc getLastAssessmentGradingByAgentId(
0768: final Long publishedAssessmentId, final String agentIdString) {
0769: AssessmentGradingData ag = null;
0770:
0771: final HibernateCallback hcb = new HibernateCallback() {
0772: public Object doInHibernate(Session session)
0773: throws HibernateException, SQLException {
0774: Query q = session
0775: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? order by a.submittedDate desc");
0776: q.setLong(0, publishedAssessmentId.longValue());
0777: q.setString(1, agentIdString);
0778: return q.list();
0779: };
0780: };
0781: List assessmentGradings = getHibernateTemplate().executeFind(
0782: hcb);
0783:
0784: // List assessmentGradings = getHibernateTemplate().find(
0785: // "from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? order by a.submittedDate desc",
0786: // new Object[] { publishedAssessmentId, agentIdString },
0787: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING });
0788: if (assessmentGradings.size() != 0) {
0789: ag = (AssessmentGradingData) assessmentGradings.get(0);
0790: ag.setItemGradingSet(getItemGradingSet(ag
0791: .getAssessmentGradingId()));
0792: }
0793: return ag;
0794: }
0795:
0796: public void saveItemGrading(ItemGradingIfc item) {
0797: int retryCount = PersistenceService.getInstance()
0798: .getRetryCount().intValue();
0799: while (retryCount > 0) {
0800: try {
0801: getHibernateTemplate().saveOrUpdate(
0802: (ItemGradingData) item);
0803: retryCount = 0;
0804: } catch (Exception e) {
0805: log.warn("problem saving itemGrading: "
0806: + e.getMessage());
0807: retryCount = PersistenceService.getInstance()
0808: .retryDeadlock(e, retryCount);
0809: }
0810: }
0811: }
0812:
0813: public void saveOrUpdateAssessmentGrading(
0814: AssessmentGradingIfc assessment) {
0815: int retryCount = PersistenceService.getInstance()
0816: .getRetryCount().intValue();
0817: while (retryCount > 0) {
0818: try {
0819: /* for testing the catch block - daisyf
0820: if (retryCount >2)
0821: throw new Exception("uncategorized SQLException for SQL []; SQL state [61000]; error code [60]; ORA-00060: deadlock detected while waiting for resource");
0822: */
0823: getHibernateTemplate().saveOrUpdate(
0824: (AssessmentGradingData) assessment);
0825: retryCount = 0;
0826: } catch (Exception e) {
0827: log
0828: .warn("problem inserting/updating assessmentGrading: "
0829: + e.getMessage());
0830: retryCount = PersistenceService.getInstance()
0831: .retryDeadlock(e, retryCount);
0832: }
0833: }
0834: }
0835:
0836: private byte[] getMediaStream(Long mediaId) {
0837: byte[] b = new byte[4000];
0838: Session session = null;
0839: Connection conn = null;
0840: InputStream in = null;
0841: ResultSet rs = null;
0842: PreparedStatement statement = null;
0843: try {
0844: session = getSessionFactory().openSession();
0845: conn = session.connection();
0846: log.debug("****Connection=" + conn);
0847: String query = "select MEDIA from SAM_MEDIA_T where MEDIAID=?";
0848: statement = conn.prepareStatement(query);
0849: statement.setLong(1, mediaId.longValue());
0850: rs = statement.executeQuery();
0851: if (rs.next()) {
0852: java.lang.Object o = rs.getObject("MEDIA");
0853: if (o != null) {
0854: in = rs.getBinaryStream("MEDIA");
0855: in.mark(0);
0856: int ch;
0857: int len = 0;
0858: while ((ch = in.read()) != -1) {
0859: len++;
0860: }
0861: b = new byte[len];
0862: in.reset();
0863: in.read(b, 0, len);
0864: }
0865: }
0866: } catch (Exception e) {
0867: log.warn(e.getMessage());
0868: } finally {
0869: try {
0870: if (session != null)
0871: session.close();
0872: if (in != null)
0873: in.close();
0874: if (conn != null)
0875: conn.close();
0876: if (rs != null)
0877: rs.close();
0878: if (statement != null)
0879: statement.close();
0880: } catch (Exception ex) {
0881: log.warn(ex.getMessage());
0882: }
0883: }
0884: return b;
0885: }
0886:
0887: public List getAssessmentGradingIds(final Long publishedItemId) {
0888: final HibernateCallback hcb = new HibernateCallback() {
0889: public Object doInHibernate(Session session)
0890: throws HibernateException, SQLException {
0891: Query q = session
0892: .createQuery("select g.assessmentGradingId from "
0893: + " ItemGradingData g where g.publishedItemId=?");
0894: q.setLong(0, publishedItemId.longValue());
0895: return q.list();
0896: };
0897: };
0898: return getHibernateTemplate().executeFind(hcb);
0899:
0900: // return getHibernateTemplate().find(
0901: // "select g.assessmentGradingId from "+
0902: // " ItemGradingData g where g.publishedItemId=?",
0903: // new Object[] { publishedItemId },
0904: // new org.hibernate.type.Type[] { Hibernate.LONG });
0905: }
0906:
0907: public AssessmentGradingIfc getHighestAssessmentGrading(
0908: final Long publishedAssessmentId, final String agentId) {
0909: AssessmentGradingData ag = null;
0910: final String query = "from AssessmentGradingData a "
0911: + " where a.publishedAssessmentId=? and "
0912: + " a.agentId=? order by a.finalScore desc, a.submittedDate desc";
0913:
0914: final HibernateCallback hcb = new HibernateCallback() {
0915: public Object doInHibernate(Session session)
0916: throws HibernateException, SQLException {
0917: Query q = session.createQuery(query);
0918: q.setLong(0, publishedAssessmentId.longValue());
0919: q.setString(1, agentId);
0920: return q.list();
0921: };
0922: };
0923: List assessmentGradings = getHibernateTemplate().executeFind(
0924: hcb);
0925:
0926: // List assessmentGradings = getHibernateTemplate().find(query,
0927: // new Object[] { publishedAssessmentId, agentId },
0928: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING });
0929: if (assessmentGradings.size() != 0) {
0930: ag = (AssessmentGradingData) assessmentGradings.get(0);
0931: ag.setItemGradingSet(getItemGradingSet(ag
0932: .getAssessmentGradingId()));
0933: }
0934: return ag;
0935: }
0936:
0937: public AssessmentGradingIfc getHighestSubmittedAssessmentGrading(
0938: final Long publishedAssessmentId, final String agentId) {
0939: AssessmentGradingData ag = null;
0940: final String query = "from AssessmentGradingData a "
0941: + " where a.publishedAssessmentId=? and a.agentId=? and "
0942: + " a.forGrade=? order by a.finalScore desc, a.submittedDate desc";
0943:
0944: final HibernateCallback hcb = new HibernateCallback() {
0945: public Object doInHibernate(Session session)
0946: throws HibernateException, SQLException {
0947: Query q = session.createQuery(query);
0948: q.setLong(0, publishedAssessmentId.longValue());
0949: q.setString(1, agentId);
0950: q.setBoolean(2, true);
0951: return q.list();
0952: };
0953: };
0954: List assessmentGradings = getHibernateTemplate().executeFind(
0955: hcb);
0956:
0957: // List assessmentGradings = getHibernateTemplate().find(query,
0958: // new Object[] { publishedAssessmentId, agentId },
0959: // new org.hibernate.type.Type[] { Hibernate.LONG, Hibernate.STRING });
0960: if (assessmentGradings.size() != 0) {
0961: ag = (AssessmentGradingData) assessmentGradings.get(0);
0962: ag.setItemGradingSet(getItemGradingSet(ag
0963: .getAssessmentGradingId()));
0964: }
0965: return ag;
0966: }
0967:
0968: public List getLastAssessmentGradingList(
0969: final Long publishedAssessmentId) {
0970: final String query = "from AssessmentGradingData a where a.publishedAssessmentId=? order by a.agentId asc, a.submittedDate desc";
0971:
0972: final HibernateCallback hcb = new HibernateCallback() {
0973: public Object doInHibernate(Session session)
0974: throws HibernateException, SQLException {
0975: Query q = session.createQuery(query);
0976: q.setLong(0, publishedAssessmentId.longValue());
0977: return q.list();
0978: };
0979: };
0980: List assessmentGradings = getHibernateTemplate().executeFind(
0981: hcb);
0982:
0983: // List assessmentGradings = getHibernateTemplate().find(query,
0984: // new Object[] { publishedAssessmentId },
0985: // new org.hibernate.type.Type[] { Hibernate.LONG });
0986:
0987: ArrayList l = new ArrayList();
0988: String currentAgent = "";
0989: for (int i = 0; i < assessmentGradings.size(); i++) {
0990: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
0991: .get(i);
0992: if (!currentAgent.equals(g.getAgentId())) {
0993: l.add(g);
0994: currentAgent = g.getAgentId();
0995: }
0996: }
0997: return l;
0998: }
0999:
1000: public List getLastSubmittedAssessmentGradingList(
1001: final Long publishedAssessmentId) {
1002: final String query = "from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by a.agentId asc, a.submittedDate desc";
1003:
1004: final HibernateCallback hcb = new HibernateCallback() {
1005: public Object doInHibernate(Session session)
1006: throws HibernateException, SQLException {
1007: Query q = session.createQuery(query);
1008: q.setLong(0, publishedAssessmentId.longValue());
1009: q.setBoolean(1, true);
1010: return q.list();
1011: };
1012: };
1013: List assessmentGradings = getHibernateTemplate().executeFind(
1014: hcb);
1015:
1016: ArrayList l = new ArrayList();
1017: String currentAgent = "";
1018: for (int i = 0; i < assessmentGradings.size(); i++) {
1019: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
1020: .get(i);
1021: if (!currentAgent.equals(g.getAgentId())) {
1022: l.add(g);
1023: currentAgent = g.getAgentId();
1024: }
1025: }
1026: return l;
1027: }
1028:
1029: public List getHighestAssessmentGradingList(
1030: final Long publishedAssessmentId) {
1031: final String query = "from AssessmentGradingData a where a.publishedAssessmentId=? order by a.agentId asc, a.finalScore desc";
1032:
1033: final HibernateCallback hcb = new HibernateCallback() {
1034: public Object doInHibernate(Session session)
1035: throws HibernateException, SQLException {
1036: Query q = session.createQuery(query);
1037: q.setLong(0, publishedAssessmentId.longValue());
1038: return q.list();
1039: };
1040: };
1041: List assessmentGradings = getHibernateTemplate().executeFind(
1042: hcb);
1043:
1044: // List assessmentGradings = getHibernateTemplate().find(query,
1045: // new Object[] { publishedAssessmentId },
1046: // new org.hibernate.type.Type[] { Hibernate.LONG });
1047:
1048: ArrayList l = new ArrayList();
1049: String currentAgent = "";
1050: for (int i = 0; i < assessmentGradings.size(); i++) {
1051: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
1052: .get(i);
1053: if (!currentAgent.equals(g.getAgentId())) {
1054: l.add(g);
1055: currentAgent = g.getAgentId();
1056: }
1057: }
1058: return l;
1059: }
1060:
1061: public List getHighestSubmittedAssessmentGradingList(
1062: final Long publishedAssessmentId) {
1063: final String query = "from AssessmentGradingData a where a.publishedAssessmentId=? and a.forGrade=? order by a.agentId asc, a.finalScore desc";
1064:
1065: final HibernateCallback hcb = new HibernateCallback() {
1066: public Object doInHibernate(Session session)
1067: throws HibernateException, SQLException {
1068: Query q = session.createQuery(query);
1069: q.setLong(0, publishedAssessmentId.longValue());
1070: q.setBoolean(1, true);
1071: return q.list();
1072: };
1073: };
1074: List assessmentGradings = getHibernateTemplate().executeFind(
1075: hcb);
1076:
1077: ArrayList l = new ArrayList();
1078: String currentAgent = "";
1079: for (int i = 0; i < assessmentGradings.size(); i++) {
1080: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
1081: .get(i);
1082: if (!currentAgent.equals(g.getAgentId())) {
1083: l.add(g);
1084: currentAgent = g.getAgentId();
1085: }
1086: }
1087: return l;
1088: }
1089:
1090: // build a Hashmap (Long publishedItemId, ArrayList assessmentGradingIds)
1091: // containing the item submission of the last AssessmentGrading
1092: // (regardless of users who submitted it) of a given published assessment
1093: public HashMap getLastAssessmentGradingByPublishedItem(
1094: final Long publishedAssessmentId) {
1095: HashMap h = new HashMap();
1096: final String query = "select new AssessmentGradingData("
1097: + " a.assessmentGradingId, p.itemId, "
1098: + " a.agentId, a.finalScore, a.submittedDate) "
1099: + " from ItemGradingData i, AssessmentGradingData a,"
1100: + " PublishedItemData p where "
1101: + " i.assessmentGradingId = a.assessmentGradingId and i.publishedItemId = p.itemId and "
1102: + " a.publishedAssessmentId=? "
1103: + " order by a.agentId asc, a.submittedDate desc";
1104:
1105: final HibernateCallback hcb = new HibernateCallback() {
1106: public Object doInHibernate(Session session)
1107: throws HibernateException, SQLException {
1108: Query q = session.createQuery(query);
1109: q.setLong(0, publishedAssessmentId.longValue());
1110: return q.list();
1111: };
1112: };
1113: List assessmentGradings = getHibernateTemplate().executeFind(
1114: hcb);
1115:
1116: // List assessmentGradings = getHibernateTemplate().find(query,
1117: // new Object[] { publishedAssessmentId },
1118: // new org.hibernate.type.Type[] { Hibernate.LONG });
1119:
1120: // ArrayList l = new ArrayList();
1121: String currentAgent = "";
1122: Date submittedDate = null;
1123: for (int i = 0; i < assessmentGradings.size(); i++) {
1124: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
1125: .get(i);
1126: Long itemId = g.getPublishedItemId();
1127: Long gradingId = g.getAssessmentGradingId();
1128: log.debug("**** itemId=" + itemId + ", gradingId="
1129: + gradingId + ", agentId=" + g.getAgentId()
1130: + ", score=" + g.getFinalScore());
1131: if (i == 0) {
1132: currentAgent = g.getAgentId();
1133: submittedDate = g.getSubmittedDate();
1134: }
1135: if (currentAgent.equals(g.getAgentId())
1136: && ((submittedDate == null && g.getSubmittedDate() == null) || (submittedDate != null && submittedDate
1137: .equals(g.getSubmittedDate())))) {
1138: Object o = h.get(itemId);
1139: if (o != null)
1140: ((ArrayList) o).add(gradingId);
1141: else {
1142: ArrayList gradingIds = new ArrayList();
1143: gradingIds.add(gradingId);
1144: h.put(itemId, gradingIds);
1145: }
1146: }
1147: if (!currentAgent.equals(g.getAgentId())) {
1148: currentAgent = g.getAgentId();
1149: submittedDate = g.getSubmittedDate();
1150: }
1151: }
1152: return h;
1153: }
1154:
1155: // build a Hashmap (Long publishedItemId, ArrayList assessmentGradingIds)
1156: // containing the item submission of the highest AssessmentGrading
1157: // (regardless of users who submitted it) of a given published assessment
1158: public HashMap getHighestAssessmentGradingByPublishedItem(
1159: final Long publishedAssessmentId) {
1160: HashMap h = new HashMap();
1161: final String query = "select new AssessmentGradingData("
1162: + " a.assessmentGradingId, p.itemId, "
1163: + " a.agentId, a.finalScore, a.submittedDate) "
1164: + " from ItemGradingData i, AssessmentGradingData a, "
1165: + " PublishedItemData p where "
1166: + " i.assessmentGradingId = a.assessmentGradingId and i.publishedItemId = p.itemId and "
1167: + " a.publishedAssessmentId=? "
1168: + " order by a.agentId asc, a.finalScore desc";
1169:
1170: final HibernateCallback hcb = new HibernateCallback() {
1171: public Object doInHibernate(Session session)
1172: throws HibernateException, SQLException {
1173: Query q = session.createQuery(query);
1174: q.setLong(0, publishedAssessmentId.longValue());
1175: return q.list();
1176: };
1177: };
1178: List assessmentGradings = getHibernateTemplate().executeFind(
1179: hcb);
1180:
1181: // List assessmentGradings = getHibernateTemplate().find(query,
1182: // new Object[] { publishedAssessmentId },
1183: // new org.hibernate.type.Type[] { Hibernate.LONG });
1184:
1185: // ArrayList l = new ArrayList();
1186: String currentAgent = "";
1187: Float finalScore = null;
1188: for (int i = 0; i < assessmentGradings.size(); i++) {
1189: AssessmentGradingData g = (AssessmentGradingData) assessmentGradings
1190: .get(i);
1191: Long itemId = g.getPublishedItemId();
1192: Long gradingId = g.getAssessmentGradingId();
1193: log.debug("**** itemId=" + itemId + ", gradingId="
1194: + gradingId + ", agentId=" + g.getAgentId()
1195: + ", score=" + g.getFinalScore());
1196: if (i == 0) {
1197: currentAgent = g.getAgentId();
1198: finalScore = g.getFinalScore();
1199: }
1200: if (currentAgent.equals(g.getAgentId())
1201: && ((finalScore == null && g.getFinalScore() == null) || (finalScore != null && finalScore
1202: .equals(g.getFinalScore())))) {
1203: Object o = h.get(itemId);
1204: if (o != null)
1205: ((ArrayList) o).add(gradingId);
1206: else {
1207: ArrayList gradingIds = new ArrayList();
1208: gradingIds.add(gradingId);
1209: h.put(itemId, gradingIds);
1210: }
1211: }
1212: if (!currentAgent.equals(g.getAgentId())) {
1213: currentAgent = g.getAgentId();
1214: finalScore = g.getFinalScore();
1215: }
1216: }
1217: return h;
1218: }
1219:
1220: public Set getItemGradingSet(final Long assessmentGradingId) {
1221: final String query = "from ItemGradingData i where i.assessmentGradingId=?";
1222:
1223: final HibernateCallback hcb = new HibernateCallback() {
1224: public Object doInHibernate(Session session)
1225: throws HibernateException, SQLException {
1226: Query q = session.createQuery(query);
1227: q.setLong(0, assessmentGradingId.longValue());
1228: return q.list();
1229: };
1230: };
1231: List itemGradings = getHibernateTemplate().executeFind(hcb);
1232:
1233: // List itemGradings = getHibernateTemplate().find(query,
1234: // new Object[] { assessmentGradingId },
1235: // new org.hibernate.type.Type[] { Hibernate.LONG });
1236: HashSet s = new HashSet();
1237: for (int i = 0; i < itemGradings.size(); i++) {
1238: s.add(itemGradings.get(i));
1239: }
1240: return s;
1241: }
1242:
1243: public HashMap getAssessmentGradingByItemGradingId(
1244: final Long publishedAssessmentId) {
1245: List aList = getAllSubmissions(publishedAssessmentId.toString());
1246: HashMap aHash = new HashMap();
1247: for (int j = 0; j < aList.size(); j++) {
1248: AssessmentGradingData a = (AssessmentGradingData) aList
1249: .get(j);
1250: aHash.put(a.getAssessmentGradingId(), a);
1251: }
1252:
1253: final String query = "select new ItemGradingData(i.itemGradingId, a.assessmentGradingId) "
1254: + " from ItemGradingData i, AssessmentGradingData a "
1255: + " where i.assessmentGradingId=a.assessmentGradingId "
1256: + " and a.publishedAssessmentId=?";
1257:
1258: final HibernateCallback hcb = new HibernateCallback() {
1259: public Object doInHibernate(Session session)
1260: throws HibernateException, SQLException {
1261: Query q = session.createQuery(query);
1262: q.setLong(0, publishedAssessmentId.longValue());
1263: return q.list();
1264: };
1265: };
1266: List l = getHibernateTemplate().executeFind(hcb);
1267:
1268: // List l = getHibernateTemplate().find(query,
1269: // new Object[] { publishedAssessmentId },
1270: // new org.hibernate.type.Type[] { Hibernate.LONG });
1271: //System.out.println("****** assessmentGradinghash="+l.size());
1272: HashMap h = new HashMap();
1273: for (int i = 0; i < l.size(); i++) {
1274: ItemGradingData o = (ItemGradingData) l.get(i);
1275: h.put(o.getItemGradingId(), (AssessmentGradingData) aHash
1276: .get(o.getAssessmentGradingId()));
1277: }
1278: return h;
1279: }
1280:
1281: public void deleteAll(Collection c) {
1282: int retryCount = PersistenceService.getInstance()
1283: .getRetryCount().intValue();
1284: while (retryCount > 0) {
1285: try {
1286: getHibernateTemplate().deleteAll(c);
1287: retryCount = 0;
1288: } catch (Exception e) {
1289: log.warn("problem inserting assessmentGrading: "
1290: + e.getMessage());
1291: retryCount = PersistenceService.getInstance()
1292: .retryDeadlock(e, retryCount);
1293: }
1294: }
1295: }
1296:
1297: public void saveOrUpdateAll(Collection c) {
1298: int retryCount = PersistenceService.getInstance()
1299: .getRetryCount().intValue();
1300: while (retryCount > 0) {
1301: try {
1302: getHibernateTemplate().saveOrUpdateAll(c);
1303: retryCount = 0;
1304: } catch (Exception e) {
1305: log.warn("problem inserting assessmentGrading: "
1306: + e.getMessage());
1307: retryCount = PersistenceService.getInstance()
1308: .retryDeadlock(e, retryCount);
1309: }
1310: }
1311: }
1312:
1313: public PublishedAssessmentIfc getPublishedAssessmentByAssessmentGradingId(
1314: final Long assessmentGradingId) {
1315: PublishedAssessmentIfc pub = null;
1316: final String query = "select p from PublishedAssessmentData p, AssessmentGradingData a "
1317: + " where a.publishedAssessmentId=p.publishedAssessmentId and a.assessmentGradingId=?";
1318:
1319: final HibernateCallback hcb = new HibernateCallback() {
1320: public Object doInHibernate(Session session)
1321: throws HibernateException, SQLException {
1322: Query q = session.createQuery(query);
1323: q.setLong(0, assessmentGradingId.longValue());
1324: return q.list();
1325: };
1326: };
1327: List pubList = getHibernateTemplate().executeFind(hcb);
1328:
1329: // List pubList = getHibernateTemplate().find(query,
1330: // new Object[] { assessmentGradingId },
1331: // new org.hibernate.type.Type[] { Hibernate.LONG });
1332: if (pubList != null && pubList.size() > 0)
1333: pub = (PublishedAssessmentIfc) pubList.get(0);
1334:
1335: return pub;
1336: }
1337:
1338: public PublishedAssessmentIfc getPublishedAssessmentByPublishedItemId(
1339: final Long publishedItemId) {
1340: PublishedAssessmentIfc pub = null;
1341: final String query = "select p from PublishedAssessmentData p, PublishedItemData i "
1342: + " where p.publishedAssessmentId=i.section.assessment.publishedAssessmentId and i.itemId=?";
1343:
1344: final HibernateCallback hcb = new HibernateCallback() {
1345: public Object doInHibernate(Session session)
1346: throws HibernateException, SQLException {
1347: Query q = session.createQuery(query);
1348: q.setLong(0, publishedItemId.longValue());
1349: return q.list();
1350: };
1351: };
1352: List pubList = getHibernateTemplate().executeFind(hcb);
1353:
1354: if (pubList != null && pubList.size() > 0)
1355: pub = (PublishedAssessmentIfc) pubList.get(0);
1356:
1357: return pub;
1358: }
1359:
1360: public ArrayList getLastItemGradingDataPosition(
1361: final Long assessmentGradingId, final String agentId) {
1362: ArrayList position = new ArrayList();
1363: try {
1364: final HibernateCallback hcb = new HibernateCallback() {
1365: public Object doInHibernate(Session session)
1366: throws HibernateException, SQLException {
1367: Query q = session
1368: .createQuery("select s.sequence "
1369: + " from ItemGradingData i, PublishedItemData pi, PublishedSectionData s "
1370: + " where i.agentId = ? and i.assessmentGradingId = ? "
1371: + " and pi.itemId = i.publishedItemId "
1372: + " and pi.section.id = s.id "
1373: + " group by i.publishedItemId, s.sequence, pi.sequence "
1374: + " order by s.sequence desc , pi.sequence desc");
1375: q.setString(0, agentId);
1376: q.setLong(1, assessmentGradingId.longValue());
1377: return q.list();
1378: };
1379: };
1380: List list = getHibernateTemplate().executeFind(hcb);
1381: if (list.size() == 0) {
1382: position.add(new Integer(0));
1383: position.add(new Integer(0));
1384: } else {
1385: Integer sequence = (Integer) list.get(0);
1386: Integer nextSequence;
1387: int count = 1;
1388: for (int i = 1; i < list.size(); i++) {
1389: log.debug("i = " + i);
1390: nextSequence = (Integer) list.get(i);
1391: if (sequence.equals(nextSequence)) {
1392: log.debug("equal");
1393: count++;
1394: } else {
1395: break;
1396: }
1397: }
1398: log.debug("sequence = " + sequence);
1399: log.debug("count = " + count);
1400: position.add(sequence);
1401: position.add(new Integer(count));
1402: }
1403: return position;
1404: } catch (Exception e) {
1405: e.printStackTrace();
1406: position.add(new Integer(0));
1407: position.add(new Integer(0));
1408: return position;
1409: }
1410: }
1411:
1412: public List getItemGradingIds(final Long assessmentGradingId) {
1413: final HibernateCallback hcb = new HibernateCallback() {
1414: public Object doInHibernate(Session session)
1415: throws HibernateException, SQLException {
1416: Query q = session
1417: .createQuery("select i.publishedItemId from "
1418: + " ItemGradingData i where i.assessmentGradingId=?");
1419: q.setLong(0, assessmentGradingId.longValue());
1420: return q.list();
1421: };
1422: };
1423: return getHibernateTemplate().executeFind(hcb);
1424: }
1425:
1426: public HashSet getItemSet(final Long publishedAssessmentId,
1427: final Long sectionId) {
1428: HashSet itemSet = new HashSet();
1429:
1430: final HibernateCallback hcb = new HibernateCallback() {
1431: public Object doInHibernate(Session session)
1432: throws HibernateException, SQLException {
1433: Query q = session
1434: .createQuery("select distinct p "
1435: + "from PublishedItemData p, AssessmentGradingData a, ItemGradingData i "
1436: + "where a.publishedAssessmentId=? and a.forGrade=? and p.section.id=? "
1437: + "and i.assessmentGradingId = a.assessmentGradingId "
1438: + "and p.itemId = i.publishedItemId ");
1439: q.setLong(0, publishedAssessmentId.longValue());
1440: q.setBoolean(1, true);
1441: q.setLong(2, sectionId.longValue());
1442: return q.list();
1443: };
1444: };
1445: List assessmentGradings = getHibernateTemplate().executeFind(
1446: hcb);
1447:
1448: Iterator iter = assessmentGradings.iterator();
1449: PublishedItemData publishedItemData;
1450: while (iter.hasNext()) {
1451: publishedItemData = (PublishedItemData) iter.next();
1452: log.debug("itemId = " + publishedItemData.getItemId());
1453: itemSet.add(publishedItemData);
1454: }
1455: return itemSet;
1456: }
1457:
1458: public Long getTypeId(final Long itemGradingId) {
1459: Long typeId = new Long(-1);
1460:
1461: final HibernateCallback hcb = new HibernateCallback() {
1462: public Object doInHibernate(Session session)
1463: throws HibernateException, SQLException {
1464: Query q = session
1465: .createQuery("select p.typeId "
1466: + "from PublishedItemData p, ItemGradingData i "
1467: + "where i.itemGradingId=? "
1468: + "and p.itemId = i.publishedItemId ");
1469: q.setLong(0, itemGradingId.longValue());
1470: return q.list();
1471: };
1472: };
1473: List typeIdList = getHibernateTemplate().executeFind(hcb);
1474:
1475: Iterator iter = typeIdList.iterator();
1476: while (iter.hasNext()) {
1477: typeId = (Long) iter.next();
1478: log.debug("typeId = " + typeId);
1479: }
1480: return typeId;
1481: }
1482:
1483: public List getAllAssessmentGradingByAgentId(
1484: final Long publishedAssessmentId, final String agentIdString) {
1485: final HibernateCallback hcb = new HibernateCallback() {
1486: public Object doInHibernate(Session session)
1487: throws HibernateException, SQLException {
1488: Query q = session
1489: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? order by a.submittedDate desc");
1490: q.setLong(0, publishedAssessmentId.longValue());
1491: q.setString(1, agentIdString);
1492: q.setBoolean(2, true);
1493: return q.list();
1494: };
1495: };
1496: List assessmentGradings = getHibernateTemplate().executeFind(
1497: hcb);
1498:
1499: return assessmentGradings;
1500: }
1501:
1502: public int getActualNumberRetake(final Long publishedAssessmentId,
1503: final String agentIdString) {
1504: final HibernateCallback hcb = new HibernateCallback() {
1505: public Object doInHibernate(Session session)
1506: throws HibernateException, SQLException {
1507: Query q = session
1508: .createQuery("select count(*) from AssessmentGradingData a, StudentGradingSummaryData s "
1509: + " where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? "
1510: + " and a.publishedAssessmentId = s.publishedAssessmentId and a.agentId = s.agentId "
1511: + " and a.submittedDate > s.createdDate"
1512: + " order by a.submittedDate desc");
1513: q.setLong(0, publishedAssessmentId.longValue());
1514: q.setString(1, agentIdString);
1515: q.setBoolean(2, true);
1516: return q.list();
1517: };
1518: };
1519: List countList = getHibernateTemplate().executeFind(hcb);
1520: Iterator iter = countList.iterator();
1521: if (iter.hasNext()) {
1522: int i = ((Integer) iter.next()).intValue();
1523: return i;
1524: } else {
1525: return 0;
1526: }
1527: }
1528:
1529: public List getStudentGradingSummaryData(
1530: final Long publishedAssessmentId, final String agentIdString) {
1531: final HibernateCallback hcb = new HibernateCallback() {
1532: public Object doInHibernate(Session session)
1533: throws HibernateException, SQLException {
1534: Query q = session
1535: .createQuery("select s "
1536: + "from StudentGradingSummaryData s "
1537: + "where s.publishedAssessmentId=? and s.agentId=?");
1538: q.setLong(0, publishedAssessmentId.longValue());
1539: q.setString(1, agentIdString);
1540: return q.list();
1541: };
1542: };
1543: List studentGradingSummaryDataList = getHibernateTemplate()
1544: .executeFind(hcb);
1545:
1546: return studentGradingSummaryDataList;
1547: }
1548:
1549: public int getNumberRetake(final Long publishedAssessmentId,
1550: final String agentIdString) {
1551: final HibernateCallback hcb = new HibernateCallback() {
1552: public Object doInHibernate(Session session)
1553: throws HibernateException, SQLException {
1554: Query q = session
1555: .createQuery("select s.numberRetake "
1556: + "from StudentGradingSummaryData s "
1557: + "where s.publishedAssessmentId=? and s.agentId=?");
1558: q.setLong(0, publishedAssessmentId.longValue());
1559: q.setString(1, agentIdString);
1560: return q.list();
1561: };
1562: };
1563: List numberRetakeList = getHibernateTemplate().executeFind(hcb);
1564:
1565: if (numberRetakeList.size() == 0) {
1566: return 0;
1567: } else {
1568: Integer numberRetake = (Integer) numberRetakeList.get(0);
1569: return numberRetake.intValue();
1570: }
1571: }
1572:
1573: public void saveStudentGradingSummaryData(
1574: StudentGradingSummaryIfc studentGradingSummaryData) {
1575: int retryCount = PersistenceService.getInstance()
1576: .getRetryCount().intValue();
1577: while (retryCount > 0) {
1578: try {
1579: getHibernateTemplate()
1580: .saveOrUpdate(
1581: (StudentGradingSummaryData) studentGradingSummaryData);
1582: retryCount = 0;
1583: } catch (Exception e) {
1584: log.warn("problem saving studentGradingSummaryData: "
1585: + e.getMessage());
1586: retryCount = PersistenceService.getInstance()
1587: .retryDeadlock(e, retryCount);
1588: }
1589: }
1590: }
1591:
1592: public int getLateSubmissionsNumberByAgentId(
1593: final Long publishedAssessmentId,
1594: final String agentIdString, final Date dueDate) {
1595: final HibernateCallback hcb = new HibernateCallback() {
1596: public Object doInHibernate(Session session)
1597: throws HibernateException, SQLException {
1598: Query q = session
1599: .createQuery("from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=? and a.submittedDate>?");
1600: q.setLong(0, publishedAssessmentId.longValue());
1601: q.setString(1, agentIdString);
1602: q.setBoolean(2, true);
1603: q.setDate(3, dueDate);
1604: return q.list();
1605: };
1606: };
1607: List assessmentGradings = getHibernateTemplate().executeFind(
1608: hcb);
1609:
1610: return assessmentGradings.size();
1611: }
1612: }
|