001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/syllabus/tags/sakai_2-4-1/syllabus-impl/src/java/org/sakaiproject/component/app/syllabus/SyllabusManagerImpl.java $
003: * $Id: SyllabusManagerImpl.java 15690 2006-10-05 14:28:35Z cwen@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 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.component.app.syllabus;
021:
022: import java.sql.SQLException;
023: import java.util.Set;
024: import java.util.TreeSet;
025:
026: import org.hibernate.Criteria;
027: import org.hibernate.FetchMode;
028: import org.hibernate.Hibernate;
029: import org.hibernate.HibernateException;
030: import org.hibernate.Query;
031: import org.hibernate.Session;
032: import org.hibernate.criterion.Expression;
033:
034: import org.sakaiproject.api.app.syllabus.SyllabusAttachment;
035: import org.sakaiproject.api.app.syllabus.SyllabusData;
036: import org.sakaiproject.api.app.syllabus.SyllabusItem;
037: import org.sakaiproject.api.app.syllabus.SyllabusManager;
038: import org.sakaiproject.content.api.ContentResource;
039: import org.sakaiproject.content.cover.ContentHostingService;
040: import org.sakaiproject.user.cover.UserDirectoryService;
041: import org.sakaiproject.user.api.User;
042:
043: import org.springframework.orm.hibernate3.HibernateCallback;
044: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
045:
046: /**
047: * SyllabusManagerImpl provides convenience functions to query the database
048: *
049: * @author <a href="mailto:jlannan@iupui.edu">Jarrod Lannan </a>
050: * @version $Id:
051: */
052: public class SyllabusManagerImpl extends HibernateDaoSupport implements
053: SyllabusManager {
054:
055: private static final String QUERY_BY_USERID_AND_CONTEXTID = "findSyllabusItemByUserAndContextIds";
056: private static final String QUERY_BY_CONTEXTID = "findSyllabusItemByContextId";
057: private static final String QUERY_LARGEST_POSITION = "findLargestSyllabusPosition";
058: private static final String USER_ID = "userId";
059: private static final String CONTEXT_ID = "contextId";
060: private static final String SURROGATE_KEY = "surrogateKey";
061: private static final String SYLLABI = "syllabi";
062: private static final String FOREIGN_KEY = "foreignKey";
063: private static final String QUERY_BY_SYLLABUSDATAID = "findSyllabusDataByDataIds";
064: private static final String DATA_KEY = "syllabusId";
065: private static final String SYLLABUS_DATA_ID = "syllabusId";
066: private static final String ATTACHMENTS = "attachments";
067:
068: /**
069: * createSyllabusItem creates a new SyllabusItem
070: * @param userId
071: * @param contextId
072: * @param redirectURL
073: *
074: */
075: public SyllabusItem createSyllabusItem(String userId,
076: String contextId, String redirectURL) {
077: if (userId == null || contextId == null) {
078: throw new IllegalArgumentException("Null Argument");
079: } else {
080: // construct a new SyllabusItem
081: SyllabusItem item = new SyllabusItemImpl(userId, contextId,
082: redirectURL);
083: saveSyllabusItem(item);
084: return item;
085: }
086: }
087:
088: /**
089: * getSyllabiForSyllabusItem returns the collection of syllabi
090: * @param syllabusItem
091: */
092: public Set getSyllabiForSyllabusItem(final SyllabusItem syllabusItem) {
093: if (syllabusItem == null) {
094: throw new IllegalArgumentException("Null Argument");
095: } else {
096: HibernateCallback hcb = new HibernateCallback() {
097: public Object doInHibernate(Session session)
098: throws HibernateException, SQLException {
099: // get syllabi in an eager fetch mode
100: Criteria crit = session.createCriteria(
101: SyllabusItemImpl.class).add(
102: Expression.eq(SURROGATE_KEY, syllabusItem
103: .getSurrogateKey())).setFetchMode(
104: SYLLABI, FetchMode.EAGER);
105:
106: SyllabusItem syllabusItem = (SyllabusItem) crit
107: .uniqueResult();
108:
109: if (syllabusItem != null) {
110: return syllabusItem.getSyllabi();
111: }
112: return new TreeSet();
113: }
114: };
115: return (Set) getHibernateTemplate().execute(hcb);
116: }
117: }
118:
119: /**
120: * createSyllabusData creates a persistent SyllabusData object
121: * @param title
122: * @param position
123: * @param assetId
124: * @param view
125: * @param status
126: * @param emailNotification
127: */
128: public SyllabusData createSyllabusDataObject(String title,
129: Integer position, String asset, String view, String status,
130: String emailNotification) {
131: if (position == null) {
132: throw new IllegalArgumentException("Null Argument");
133: } else {
134: // construct a new SyllabusData persistent object
135: SyllabusData data = new SyllabusDataImpl();
136: data.setTitle(title);
137: data.setPosition(position);
138: data.setAsset(asset);
139: data.setView(view);
140: data.setStatus(status);
141: data.setEmailNotification(emailNotification);
142:
143: saveSyllabus(data);
144: return data;
145: }
146: }
147:
148: /**
149: * removes a syllabus data object (on form cancel action)
150: * @see org.sakaiproject.api.app.syllabus.SyllabusManager#removeSyllabusDataObject(org.sakaiproject.api.app.syllabus.SyllabusData)
151: */
152: public void removeSyllabusDataObject(SyllabusData o) {
153: getHibernateTemplate().delete(o);
154: }
155:
156: /**
157: * swapSyllabusDataPositions swaps positions for two SyllabusData objects
158: * @param syllabusItem
159: * @param d1
160: * @param d2
161: */
162: public void swapSyllabusDataPositions(
163: final SyllabusItem syllabusItem, final SyllabusData d1,
164: final SyllabusData d2) {
165: if (syllabusItem == null || d1 == null || d2 == null) {
166: throw new IllegalArgumentException("Null Argument");
167: } else {
168: HibernateCallback hcb = new HibernateCallback() {
169: public Object doInHibernate(Session session)
170: throws HibernateException, SQLException {
171: // load objects from hibernate
172: SyllabusItem item = (SyllabusItem) session.get(
173: SyllabusItemImpl.class, syllabusItem
174: .getSurrogateKey());
175: SyllabusData data1 = (SyllabusData) session.get(
176: SyllabusDataImpl.class, d1.getSyllabusId());
177: SyllabusData data2 = (SyllabusData) session.get(
178: SyllabusDataImpl.class, d2.getSyllabusId());
179:
180: Integer temp = data1.getPosition();
181: data1.setPosition(data2.getPosition());
182: data2.setPosition(temp);
183:
184: return null;
185: }
186: };
187: getHibernateTemplate().execute(hcb);
188: }
189: }
190:
191: /**
192: * findLargestSyllabusPosition finds the largest syllabus data position for an item
193: * @param syllabusItem
194: */
195: public Integer findLargestSyllabusPosition(
196: final SyllabusItem syllabusItem) {
197: if (syllabusItem == null) {
198: throw new IllegalArgumentException("Null Argument");
199: } else {
200: HibernateCallback hcb = new HibernateCallback() {
201: public Object doInHibernate(Session session)
202: throws HibernateException, SQLException {
203: Query q = session
204: .getNamedQuery(QUERY_LARGEST_POSITION);
205: q.setParameter(FOREIGN_KEY, syllabusItem
206: .getSurrogateKey(), Hibernate.LONG);
207:
208: Integer position = (Integer) q.uniqueResult();
209:
210: if (position == null) {
211: return new Integer(0);
212: } else {
213: return position;
214: }
215:
216: }
217: };
218: return (Integer) getHibernateTemplate().execute(hcb);
219: }
220: }
221:
222: /**
223: * getSyllabusItemByContextId finds a SyllabusItem
224: * @param contextId
225: * @return SyllabusItem
226: *
227: */
228: public SyllabusItem getSyllabusItemByContextId(
229: final String contextId) {
230: if (contextId == null) {
231: throw new IllegalArgumentException("Null Argument");
232: }
233:
234: HibernateCallback hcb = new HibernateCallback() {
235: public Object doInHibernate(Session session)
236: throws HibernateException, SQLException {
237: Query q = session.getNamedQuery(QUERY_BY_CONTEXTID);
238: q.setParameter(CONTEXT_ID, contextId, Hibernate.STRING);
239: return q.uniqueResult();
240: }
241: };
242:
243: return (SyllabusItem) getHibernateTemplate().execute(hcb);
244: }
245:
246: /**
247: * getSyllabusItemByUserAndContextIds finds a SyllabusItem
248: * @param userId
249: * @param contextId
250: * @return SyllabusItem
251: *
252: */
253: public SyllabusItem getSyllabusItemByUserAndContextIds(
254: final String userId, final String contextId) {
255: if (userId == null || contextId == null) {
256: throw new IllegalArgumentException("Null Argument");
257: }
258:
259: HibernateCallback hcb = new HibernateCallback() {
260: public Object doInHibernate(Session session)
261: throws HibernateException, SQLException {
262: Query q = session
263: .getNamedQuery(QUERY_BY_USERID_AND_CONTEXTID);
264: q.setParameter(USER_ID, userId, Hibernate.STRING);
265: q.setParameter(CONTEXT_ID, contextId, Hibernate.STRING);
266: return q.uniqueResult();
267: }
268: };
269:
270: return (SyllabusItem) getHibernateTemplate().execute(hcb);
271: }
272:
273: /**
274: * addSyllabusToSyllabusItem adds a SyllabusData object to syllabi collection
275: * @param syllabusItem
276: * @param syllabusData
277: * @return Set
278: */
279: public void addSyllabusToSyllabusItem(
280: final SyllabusItem syllabusItem,
281: final SyllabusData syllabusData) {
282:
283: if (syllabusItem == null || syllabusData == null) {
284: throw new IllegalArgumentException("Null Argument");
285: }
286:
287: HibernateCallback hcb = new HibernateCallback() {
288: public Object doInHibernate(Session session)
289: throws HibernateException, SQLException {
290:
291: SyllabusItem returnedItem = (SyllabusItem) session.get(
292: SyllabusItemImpl.class, syllabusItem
293: .getSurrogateKey());
294: if (returnedItem != null) {
295: returnedItem.getSyllabi().add(syllabusData);
296: session.save(returnedItem);
297: }
298: return null;
299: }
300: };
301: getHibernateTemplate().execute(hcb);
302: }
303:
304: /**
305: * removeSyllabusToSyllabusItem loads many side of the relationship
306: * @param syllabusItem
307: * @param syllabusData
308: * @return Set
309: */
310: public void removeSyllabusFromSyllabusItem(
311: final SyllabusItem syllabusItem,
312: final SyllabusData syllabusData) {
313:
314: if (syllabusItem == null || syllabusData == null) {
315: throw new IllegalArgumentException("Null Argument");
316: }
317:
318: HibernateCallback hcb = new HibernateCallback() {
319: public Object doInHibernate(Session session)
320: throws HibernateException, SQLException {
321: SyllabusItem returnedItem = (SyllabusItem) session.get(
322: SyllabusItemImpl.class, syllabusItem
323: .getSurrogateKey());
324: if (returnedItem != null) {
325: returnedItem.getSyllabi().remove(syllabusData);
326: session.saveOrUpdate(returnedItem);
327: }
328: return null;
329: }
330: };
331: getHibernateTemplate().execute(hcb);
332: }
333:
334: /**
335: * saveSyllabusItem persists a SyllabusItem
336: * @param item
337: */
338: public void saveSyllabusItem(SyllabusItem item) {
339: getHibernateTemplate().saveOrUpdate(item);
340: }
341:
342: /**
343: * saveSyllabus persists a SyllabusData object
344: * @param item
345: */
346: public void saveSyllabus(SyllabusData data) {
347: getHibernateTemplate().saveOrUpdate(data);
348: }
349:
350: public SyllabusData getSyllabusData(final String dataId) {
351: if (dataId == null) {
352: throw new IllegalArgumentException("Null Argument");
353: } else {
354: HibernateCallback hcb = new HibernateCallback() {
355: public Object doInHibernate(Session session)
356: throws HibernateException, SQLException {
357: Long longObj = new Long(dataId);
358: SyllabusData returnedData = (SyllabusData) session
359: .get(SyllabusDataImpl.class, longObj);
360: return returnedData;
361: }
362: };
363: return (SyllabusData) getHibernateTemplate().execute(hcb);
364: }
365:
366: }
367:
368: public SyllabusAttachment createSyllabusAttachmentObject(
369: String attachId, String name) {
370: try {
371: SyllabusAttachment attach = new SyllabusAttachmentImpl();
372:
373: attach.setAttachmentId(attachId);
374:
375: attach.setName(name);
376:
377: ContentResource cr = ContentHostingService
378: .getResource(attachId);
379: attach.setSize((new Integer(cr.getContentLength()))
380: .toString());
381: User creator = UserDirectoryService.getUser(cr
382: .getProperties().getProperty(
383: cr.getProperties().getNamePropCreator()));
384: attach.setCreatedBy(creator.getDisplayName());
385: User modifier = UserDirectoryService
386: .getUser(cr.getProperties().getProperty(
387: cr.getProperties().getNamePropModifiedBy()));
388: attach.setLastModifiedBy(modifier.getDisplayName());
389: attach.setType(cr.getContentType());
390: String tempString = cr.getUrl();
391: String newString = new String();
392: char[] oneChar = new char[1];
393: for (int i = 0; i < tempString.length(); i++) {
394: if (tempString.charAt(i) != ' ') {
395: oneChar[0] = tempString.charAt(i);
396: String concatString = new String(oneChar);
397: newString = newString.concat(concatString);
398: } else {
399: newString = newString.concat("%20");
400: }
401: }
402: //tempString.replaceAll(" ", "%20");
403: attach.setUrl(newString);
404:
405: saveSyllabusAttachment(attach);
406:
407: return attach;
408: } catch (Exception e) {
409: e.printStackTrace();
410: return null;
411: }
412: }
413:
414: public void saveSyllabusAttachment(SyllabusAttachment attach) {
415: getHibernateTemplate().saveOrUpdate(attach);
416: }
417:
418: public void addSyllabusAttachToSyllabusData(
419: final SyllabusData syllabusData,
420: final SyllabusAttachment syllabusAttach) {
421:
422: if (syllabusData == null || syllabusAttach == null) {
423: throw new IllegalArgumentException("Null Argument");
424: }
425:
426: HibernateCallback hcb = new HibernateCallback() {
427: public Object doInHibernate(Session session)
428: throws HibernateException, SQLException {
429: SyllabusData returnedData = (SyllabusData) session.get(
430: SyllabusDataImpl.class, syllabusData
431: .getSyllabusId());
432: if (returnedData != null) {
433: returnedData.getAttachments().add(syllabusAttach);
434: session.save(returnedData);
435: }
436: return null;
437: }
438: };
439: getHibernateTemplate().execute(hcb);
440: }
441:
442: public void removeSyllabusAttachmentObject(SyllabusAttachment o) {
443: getHibernateTemplate().delete(o);
444: }
445:
446: public void removeSyllabusAttachSyllabusData(
447: final SyllabusData syllabusData,
448: final SyllabusAttachment syllabusAttach) {
449:
450: if (syllabusData == null || syllabusAttach == null) {
451: throw new IllegalArgumentException("Null Argument");
452: }
453:
454: HibernateCallback hcb = new HibernateCallback() {
455: public Object doInHibernate(Session session)
456: throws HibernateException, SQLException {
457: SyllabusData returnedData = (SyllabusData) session.get(
458: SyllabusDataImpl.class, syllabusData
459: .getSyllabusId());
460: if (returnedData != null) {
461: returnedData.getAttachments()
462: .remove(syllabusAttach);
463: session.saveOrUpdate(returnedData);
464: }
465: return null;
466: }
467: };
468: getHibernateTemplate().execute(hcb);
469: }
470:
471: public Set getSyllabusAttachmentsForSyllabusData(
472: final SyllabusData syllabusData) {
473: if (syllabusData == null) {
474: throw new IllegalArgumentException("Null Argument");
475: } else {
476: HibernateCallback hcb = new HibernateCallback() {
477: public Object doInHibernate(Session session)
478: throws HibernateException, SQLException {
479: Criteria crit = session.createCriteria(
480: SyllabusDataImpl.class).add(
481: Expression.eq(SYLLABUS_DATA_ID,
482: syllabusData.getSyllabusId()))
483: .setFetchMode(ATTACHMENTS, FetchMode.EAGER);
484:
485: SyllabusData syllabusData = (SyllabusData) crit
486: .uniqueResult();
487:
488: if (syllabusData != null) {
489: return syllabusData.getAttachments();
490: }
491: return new TreeSet();
492: }
493: };
494: return (Set) getHibernateTemplate().execute(hcb);
495: }
496: }
497:
498: public SyllabusAttachment getSyllabusAttachment(
499: final String syllabusAttachId) {
500: if (syllabusAttachId == null) {
501: throw new IllegalArgumentException("Null Argument");
502: } else {
503: HibernateCallback hcb = new HibernateCallback() {
504: public Object doInHibernate(Session session)
505: throws HibernateException, SQLException {
506: Long longObj = new Long(syllabusAttachId);
507: SyllabusAttachment returnedAttach = (SyllabusAttachment) session
508: .get(SyllabusAttachmentImpl.class, longObj);
509: return returnedAttach;
510: }
511: };
512: return (SyllabusAttachment) getHibernateTemplate().execute(
513: hcb);
514: }
515:
516: }
517:
518: /* public SyllabusAttachment creatSyllabusAttachmentResource(String attachId, String name)
519: {
520: SyllabusAttachment attach = new SyllabusAttachmentImpl();
521:
522: attach.setAttachmentId(attachId);
523:
524: attach.setName(name);
525:
526: return attach;
527: }*/
528: }
|