001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/services/ItemService.java $
003: * $Id: ItemService.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.services;
021:
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.sakaiproject.tool.assessment.facade.ItemFacade;
032: import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
033: import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
034: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
035: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
036: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback;
037: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
038: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
039:
040: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
041: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
042: import org.sakaiproject.content.api.ContentResource;
043:
044: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
045:
046: /**
047: * The ItemService calls persistent service locator to reach the
048: * manager on the back end.
049: */
050: public class ItemService {
051: private static Log log = LogFactory.getLog(ItemService.class);
052:
053: /**
054: * Creates a new ItemService object.
055: */
056: public ItemService() {
057: }
058:
059: /**
060: * Get a particular item from the backend, with all questions.
061: */
062: public ItemFacade getItem(Long itemId, String agentId) {
063: ItemFacade item = null;
064: try {
065: item = PersistenceService.getInstance()
066: .getItemFacadeQueries().getItem(itemId, agentId);
067: } catch (Exception e) {
068: log.error(e);
069: throw new RuntimeException(e);
070: }
071:
072: return item;
073: }
074:
075: /**
076: * Delete a item
077: */
078: public void deleteItem(Long itemId, String agentId) {
079: try {
080: //ItemFacade item= PersistenceService.getInstance().
081: //getItemFacadeQueries().getItem(itemId, agentId);
082:
083: /* do not check for owner, anyone who has maintain role can modify items see SAK-2214
084: // you are not allowed to delete item if you are not the owner
085: if (!item.getData().getCreatedBy().equals(agentId))
086: throw new RuntimeException("you are not allowed to delete item if you are not the owner");
087: */
088: PersistenceService.getInstance().getItemFacadeQueries()
089: .deleteItem(itemId, agentId);
090: } catch (Exception e) {
091: log.error(e);
092: throw new RuntimeException(e);
093: }
094: }
095:
096: /**
097: * Delete itemtextset for an item, used for modify
098: */
099: public void deleteItemContent(Long itemId, String agentId) {
100: try {
101: //ItemFacade item= PersistenceService.getInstance().
102: //getItemFacadeQueries().getItem(itemId, agentId);
103:
104: /* do not check for owner, anyone who has maintain role can modify items see SAK-2214
105: // you are not allowed to delete item if you are not the owner
106: if (!item.getData().getCreatedBy().equals(agentId))
107: throw new RuntimeException("you are not allowed to delete item if you are not the owner");
108: */
109: PersistenceService.getInstance().getItemFacadeQueries()
110: .deleteItemContent(itemId, agentId);
111: } catch (Exception e) {
112: log.error(e);
113: throw new RuntimeException(e);
114: }
115: }
116:
117: /**
118: * Delete metadata for an item, used for modify
119: * param: itemid, label, agentId
120: */
121: public void deleteItemMetaData(Long itemId, String label,
122: String agentId) {
123: try {
124: //ItemFacade item= PersistenceService.getInstance().
125: //getItemFacadeQueries().getItem(itemId, agentId);
126:
127: /* do not check for owner, anyone who has maintain role can modify items see SAK-2214
128: // you are not allowed to delete item if you are not the owner
129: if (!item.getData().getCreatedBy().equals(agentId))
130: throw new Error(new Exception());
131: */
132: PersistenceService.getInstance().getItemFacadeQueries()
133: .deleteItemMetaData(itemId, label);
134: } catch (Exception e) {
135: log.error(e);
136: throw new RuntimeException(e);
137: }
138: }
139:
140: /**
141: * Add metadata for an item, used for modify
142: * param: itemid, label, value, agentId
143: */
144: public void addItemMetaData(Long itemId, String label,
145: String value, String agentId) {
146: try {
147: //ItemFacade item= PersistenceService.getInstance().
148: //getItemFacadeQueries().getItem(itemId, agentId);
149:
150: /* do not check for owner, anyone who has maintain role can modify items see SAK-2214
151: // you are not allowed to delete item if you are not the owner
152: if (!item.getData().getCreatedBy().equals(agentId))
153: throw new Error(new Exception());
154: */
155: PersistenceService.getInstance().getItemFacadeQueries()
156: .addItemMetaData(itemId, label, value);
157: } catch (Exception e) {
158: log.error(e);
159: throw new RuntimeException(e);
160: }
161: }
162:
163: /**
164: * Save a question item.
165: */
166: public ItemFacade saveItem(ItemFacade item) {
167: try {
168: return PersistenceService.getInstance()
169: .getItemFacadeQueries().saveItem(item);
170: } catch (Exception e) {
171: log.error(e);
172:
173: return item;
174: }
175: }
176:
177: public ItemFacade getItem(String itemId) {
178: try {
179: return PersistenceService.getInstance()
180: .getItemFacadeQueries().getItem(new Long(itemId));
181: } catch (Exception e) {
182: log.error(e);
183: throw new RuntimeException(e);
184: }
185: }
186:
187: public HashMap getItemsByKeyword(String keyword) {
188: keyword = "%" + keyword + "%";
189: HashMap map = null;
190: map = PersistenceService.getInstance().getItemFacadeQueries()
191: .getItemsByKeyword(keyword);
192: return map;
193:
194: }
195:
196: public Long getItemTextId(Long publishedItemId) {
197: try {
198: return PersistenceService.getInstance()
199: .getItemFacadeQueries().getItemTextId(
200: publishedItemId);
201: } catch (Exception e) {
202: log.error(e);
203: throw new RuntimeException(e);
204: }
205: }
206:
207: public ItemData cloneItem(ItemDataIfc item) {
208: ItemData cloned = new ItemData(item.getSection(), item
209: .getSequence(), item.getDuration(), item
210: .getInstruction(), item.getDescription(), item
211: .getTypeId(), item.getGrade(), item.getScore(), item
212: .getHint(), item.getHasRationale(), item.getStatus(),
213: item.getCreatedBy(), item.getCreatedDate(), item
214: .getLastModifiedBy(), item
215: .getLastModifiedDate(), null, null, null, item
216: .getTriesAllowed());
217:
218: // perform deep copy, set ItemTextSet, itemMetaDataSet and itemFeedbackSet
219: Set newItemTextSet = copyItemTextSet(cloned, item
220: .getItemTextSet());
221: Set newItemMetaDataSet = copyItemMetaDataSet(cloned, item
222: .getItemMetaDataSet());
223: Set newItemFeedbackSet = copyItemFeedbackSet(cloned, item
224: .getItemFeedbackSet());
225: Set newItemAttachmentSet = copyItemAttachmentSet(cloned, item
226: .getItemAttachmentSet());
227: cloned.setItemTextSet(newItemTextSet);
228: cloned.setItemMetaDataSet(newItemMetaDataSet);
229: cloned.setItemFeedbackSet(newItemFeedbackSet);
230: cloned.setItemAttachmentSet(newItemAttachmentSet);
231:
232: return cloned;
233: }
234:
235: private Set copyItemTextSet(ItemData cloned, Set itemTextSet) {
236: HashSet h = new HashSet();
237: Iterator k = itemTextSet.iterator();
238: while (k.hasNext()) {
239: ItemText itemText = (ItemText) k.next();
240: ItemText newItemText = new ItemText(cloned, itemText
241: .getSequence(), itemText.getText(), null);
242: Set newAnswerSet = copyAnswerSet(newItemText, itemText
243: .getAnswerSet());
244: newItemText.setAnswerSet(newAnswerSet);
245: h.add(newItemText);
246: }
247: return h;
248: }
249:
250: private Set copyAnswerSet(ItemText newItemText, Set answerSet) {
251: HashSet h = new HashSet();
252: Iterator l = answerSet.iterator();
253: while (l.hasNext()) {
254: Answer answer = (Answer) l.next();
255: Answer newAnswer = new Answer(newItemText,
256: answer.getText(), answer.getSequence(), answer
257: .getLabel(), answer.getIsCorrect(), answer
258: .getGrade(), answer.getScore(), null);
259: Set newAnswerFeedbackSet = copyAnswerFeedbackSet(newAnswer,
260: answer.getAnswerFeedbackSet());
261: newAnswer.setAnswerFeedbackSet(newAnswerFeedbackSet);
262: h.add(newAnswer);
263: }
264: return h;
265: }
266:
267: private Set copyAnswerFeedbackSet(Answer newAnswer,
268: Set answerFeedbackSet) {
269: HashSet h = new HashSet();
270: Iterator m = answerFeedbackSet.iterator();
271: while (m.hasNext()) {
272: AnswerFeedback answerFeedback = (AnswerFeedback) m.next();
273: AnswerFeedback newAnswerFeedback = new AnswerFeedback(
274: newAnswer, answerFeedback.getTypeId(),
275: answerFeedback.getText());
276: h.add(newAnswerFeedback);
277: }
278: return h;
279: }
280:
281: private Set copyItemMetaDataSet(ItemData cloned, Set itemMetaDataSet) {
282: HashSet h = new HashSet();
283: Iterator n = itemMetaDataSet.iterator();
284: while (n.hasNext()) {
285: ItemMetaData itemMetaData = (ItemMetaData) n.next();
286: ItemMetaData newItemMetaData = new ItemMetaData(cloned,
287: itemMetaData.getLabel(), itemMetaData.getEntry());
288: h.add(newItemMetaData);
289: }
290: return h;
291: }
292:
293: private Set copyItemFeedbackSet(ItemData cloned, Set itemFeedbackSet) {
294: HashSet h = new HashSet();
295: Iterator o = itemFeedbackSet.iterator();
296: while (o.hasNext()) {
297: ItemFeedback itemFeedback = (ItemFeedback) o.next();
298: ItemFeedback newItemFeedback = new ItemFeedback(cloned,
299: itemFeedback.getTypeId(), itemFeedback.getText());
300: h.add(newItemFeedback);
301: }
302: return h;
303: }
304:
305: private Set copyItemAttachmentSet(ItemData cloned,
306: Set itemAttachmentSet) {
307: AssessmentService service = new AssessmentService();
308: HashSet h = new HashSet();
309: Iterator n = itemAttachmentSet.iterator();
310: while (n.hasNext()) {
311: ItemAttachmentIfc itemAttachment = (ItemAttachmentIfc) n
312: .next();
313: ContentResource cr_copy = service
314: .createCopyOfContentResource(itemAttachment
315: .getResourceId(), itemAttachment
316: .getFilename());
317: ItemAttachmentIfc newItemAttachment = new ItemAttachment(
318: null, cr_copy.getId(),
319: itemAttachment.getFilename(), itemAttachment
320: .getMimeType(), itemAttachment
321: .getFileSize(), itemAttachment
322: .getDescription(), cr_copy.getUrl(),
323: itemAttachment.getIsLink(), itemAttachment
324: .getStatus(),
325: itemAttachment.getCreatedBy(), itemAttachment
326: .getCreatedDate(), itemAttachment
327: .getLastModifiedBy(), itemAttachment
328: .getLastModifiedDate());
329: newItemAttachment.setItem(cloned);
330: h.add(newItemAttachment);
331: }
332: return h;
333: }
334:
335: }
|