001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/SectionFacadeQueries.java $
003: * $Id: SectionFacadeQueries.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
021:
022: import java.sql.SQLException;
023: import java.util.Date;
024: import java.util.List;
025:
026: import org.hibernate.Hibernate;
027: import org.hibernate.HibernateException;
028: import org.hibernate.Query;
029: import org.hibernate.Session;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData;
034: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
035: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
036: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
037: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData;
038: import org.springframework.orm.hibernate3.HibernateCallback;
039: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
040: import org.sakaiproject.tool.assessment.services.PersistenceService;
041: import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
042:
043: public class SectionFacadeQueries extends HibernateDaoSupport implements
044: SectionFacadeQueriesAPI {
045: private static Log log = LogFactory
046: .getLog(SectionFacadeQueries.class);
047:
048: public SectionFacadeQueries() {
049: }
050:
051: public IdImpl getId(String id) {
052: return new IdImpl(id);
053: }
054:
055: public IdImpl getId(Long id) {
056: return new IdImpl(id);
057: }
058:
059: public IdImpl getId(long id) {
060: return new IdImpl(id);
061: }
062:
063: public static void main(String[] args) throws DataFacadeException {
064: SectionFacadeQueriesAPI instance = new SectionFacadeQueries();
065: // add an assessmentTemplate
066: if (args[0].equals("add")) {
067: Long assessmentId = new Long(args[1]);
068: Long sectionId = instance.addSection(assessmentId);
069: SectionFacade section = instance.get(sectionId);
070: print(section);
071: }
072: if (args[0].equals("remove")) {
073: instance.remove(new Long(args[1]));
074: }
075: if (args[0].equals("load")) {
076: SectionFacade s = (SectionFacade) instance.get(new Long(
077: args[1]));
078: print(s);
079: }
080: System.exit(0);
081: }
082:
083: public static void print(SectionFacade section) {
084: //log.debug("**sectionId #" + section.getId());
085: //log.debug("**Section Title = " + section.getTitle());
086: //log.debug("**Item = " + section.getItemSet());
087: }
088:
089: public Long addSection(Long assessmentId) {
090: // take default submission model
091: SectionData section = new SectionData();
092: AssessmentBaseData assessment = (AssessmentBaseData) getHibernateTemplate()
093: .load(AssessmentBaseData.class, assessmentId);
094: //section.setAssessmentId(assessmentId);
095: section.setAssessment((AssessmentData) assessment);
096: section.setDuration(new Integer(30));
097: section.setSequence(new Integer(1));
098: section.setTitle("section title");
099: section.setDescription("section description");
100: section.setTypeId(TypeFacade.DEFAULT_SECTION);
101: section.setStatus(new Integer(1));
102: section.setCreatedBy("1");
103: section.setCreatedDate(new Date());
104: section.setLastModifiedBy("1");
105: section.setLastModifiedDate(new Date());
106: ItemManager itemManager = new ItemManager();
107: ItemData item = itemManager.prepareItem();
108: item.setSection(section);
109: section.addItem(item);
110:
111: getHibernateTemplate().save(section);
112: return section.getSectionId();
113: }
114:
115: public void remove(Long sectionId) {
116: SectionFacade section = (SectionFacade) getHibernateTemplate()
117: .load(SectionData.class, sectionId);
118: int retryCount = PersistenceService.getInstance()
119: .getRetryCount().intValue();
120: while (retryCount > 0) {
121: try {
122: getHibernateTemplate().delete(section);
123: retryCount = 0;
124: } catch (Exception e) {
125: log.warn("problem removing section: " + e.getMessage());
126: retryCount = PersistenceService.getInstance()
127: .retryDeadlock(e, retryCount);
128: }
129: }
130: }
131:
132: public SectionFacade get(Long sectionId) {
133: SectionData section = (SectionData) getHibernateTemplate()
134: .load(SectionData.class, sectionId);
135: return new SectionFacade(section);
136: }
137:
138: public SectionData load(Long sectionId) {
139: return (SectionData) getHibernateTemplate().load(
140: SectionData.class, sectionId);
141: }
142:
143: public void addSectionMetaData(Long sectionId, String label,
144: String value) {
145: SectionData section = (SectionData) getHibernateTemplate()
146: .load(SectionData.class, sectionId);
147: if (section != null) {
148:
149: SectionMetaData sectionmetadata = new SectionMetaData(
150: section, label, value);
151: int retryCount = PersistenceService.getInstance()
152: .getRetryCount().intValue();
153: while (retryCount > 0) {
154: try {
155: getHibernateTemplate().save(sectionmetadata);
156: retryCount = 0;
157: } catch (Exception e) {
158: log.warn("problem add section metadata: "
159: + e.getMessage());
160: retryCount = PersistenceService.getInstance()
161: .retryDeadlock(e, retryCount);
162: }
163: }
164: }
165: }
166:
167: public void deleteSectionMetaData(final Long sectionId,
168: final String label) {
169: final String query = "from SectionMetaData imd where imd.section.sectionId=? and imd.label= ? ";
170:
171: final HibernateCallback hcb = new HibernateCallback() {
172: public Object doInHibernate(Session session)
173: throws HibernateException, SQLException {
174: Query q = session.createQuery(query);
175: q.setLong(0, sectionId.longValue());
176: q.setString(1, label);
177: return q.list();
178: };
179: };
180: List sectionmetadatalist = getHibernateTemplate().executeFind(
181: hcb);
182:
183: // List sectionmetadatalist = getHibernateTemplate().find(query,
184: // new Object[] { sectionId, label },
185: // new org.hibernate.type.Type[] { Hibernate.LONG , Hibernate.STRING });
186: int retryCount = PersistenceService.getInstance()
187: .getRetryCount().intValue();
188: while (retryCount > 0) {
189: try {
190: getHibernateTemplate().deleteAll(sectionmetadatalist);
191: retryCount = 0;
192: } catch (Exception e) {
193: log.warn("problem delete section metadata: "
194: + e.getMessage());
195: retryCount = PersistenceService.getInstance()
196: .retryDeadlock(e, retryCount);
197: }
198: }
199: }
200:
201: }
|