001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/services/QuestionPoolService.java $
003: * $Id: QuestionPoolService.java 9343 2006-05-12 23:30:02Z lydial@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.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.sakaiproject.tool.assessment.data.dao.questionpool.QuestionPoolItemData;
030: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
031: import org.sakaiproject.tool.assessment.data.model.Tree;
032: import org.sakaiproject.tool.assessment.facade.QuestionPoolFacade;
033: import org.sakaiproject.tool.assessment.facade.QuestionPoolIteratorFacade;
034:
035: //import osid.assessment.Item;
036:
037: /**
038: * The QuestionPoolService calls the service locator to reach the
039: * manager on the back end.
040: * @author Rachel Gollub <rgollub@stanford.edu>
041: */
042: public class QuestionPoolService {
043: private static Log log = LogFactory
044: .getLog(QuestionPoolService.class);
045:
046: /**
047: * Creates a new QuestionPoolService object.
048: */
049: public QuestionPoolService() {
050: }
051:
052: /**
053: * Get all pools from the back end.
054: */
055: public QuestionPoolIteratorFacade getAllPools(String agentId) {
056: QuestionPoolIteratorFacade results = null;
057: results = (QuestionPoolIteratorFacade) PersistenceService
058: .getInstance().getQuestionPoolFacadeQueries()
059: .getAllPools(agentId);
060: return results;
061: }
062:
063: /**
064: * Get basic info for pools(just id and title) for displaying in pulldown .
065: */
066: public ArrayList getBasicInfoOfAllPools(String agentId) {
067: ArrayList results = null;
068: results = PersistenceService.getInstance()
069: .getQuestionPoolFacadeQueries().getBasicInfoOfAllPools(
070: agentId);
071: return results;
072: }
073:
074: /**
075: * Get a particular pool from the backend, with all questions.
076: */
077: public QuestionPoolFacade getPool(Long poolId, String agentId) {
078: QuestionPoolFacade pool = null;
079: try {
080: pool = PersistenceService.getInstance()
081: .getQuestionPoolFacadeQueries().getPool(poolId,
082: agentId);
083: } catch (Exception e) {
084: log.error(e);
085: throw new RuntimeException(e);
086: }
087:
088: return pool;
089: }
090:
091: /**
092: * Get a list of pools that have a specific Agent
093: */
094: public List getPoolIdsByItem(String itemId) {
095: List idList = null;
096: try {
097: idList = PersistenceService.getInstance()
098: .getQuestionPoolFacadeQueries().getPoolIdsByItem(
099: itemId);
100: } catch (Exception e) {
101: log.error(e);
102: throw new RuntimeException(e);
103: }
104:
105: return idList;
106: }
107:
108: public boolean hasItem(String itemId, Long poolId) {
109: List poollist = null;
110: boolean found = false;
111: try {
112: poollist = getPoolIdsByItem(itemId);
113: if (poollist != null) {
114: found = poollist.contains(poolId);
115: } else {
116: found = false;
117: }
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121: return found;
122:
123: }
124:
125: /**
126: * Get pool id's by agent.
127: */
128: public List getPoolIdsByAgent(String agentId) {
129: List idList = null;
130: try {
131: idList = PersistenceService.getInstance()
132: .getQuestionPoolFacadeQueries().getPoolIdsByAgent(
133: agentId);
134: } catch (Exception e) {
135: log.error(e);
136: throw new RuntimeException(e);
137: }
138:
139: return idList;
140: }
141:
142: /**
143: * Get a list of pools that have a specific parent
144: */
145: public List getSubPools(Long poolId) {
146: List poolList = null;
147: try {
148: poolList = PersistenceService.getInstance()
149: .getQuestionPoolFacadeQueries().getSubPools(poolId);
150: } catch (Exception e) {
151: log.error(e);
152: throw new RuntimeException(e);
153: }
154:
155: return poolList;
156: }
157:
158: /**
159: * Get the size of a subpool.
160: */
161: public int getSubPoolSize(Long poolId) {
162: int poolSize = 0;
163: try {
164: poolSize = PersistenceService.getInstance()
165: .getQuestionPoolFacadeQueries().getSubPoolSize(
166: poolId);
167: } catch (Exception e) {
168: log.error(e);
169: throw new RuntimeException(e);
170: }
171: return poolSize;
172: }
173:
174: /**
175: * Checks to see if a pool has subpools
176: */
177: public boolean hasSubPools(Long poolId) {
178: boolean result = false;
179: try {
180: result = PersistenceService.getInstance()
181: .getQuestionPoolFacadeQueries().hasSubPools(poolId);
182: } catch (Exception e) {
183: log.error(e);
184: throw new RuntimeException(e);
185: }
186:
187: return result;
188: }
189:
190: /**
191: * Get all items sorted by orderby
192: */
193: /*
194: public ArrayList getAllItemsSorted(Long poolId, String orderBy)
195: {
196: ArrayList results = null;
197: try {
198: if ("text".equals(orderBy)) {
199: results = (ArrayList) PersistenceService.getInstance().
200: getQuestionPoolFacadeQueries().getAllItemFacadesOrderByItemText(poolId, "instruction");
201: }
202: else if ("keyword".equals(orderBy)) {
203: results = (ArrayList) PersistenceService.getInstance().
204: getQuestionPoolFacadeQueries().getAllItemFacadesOrderByItemType(poolId, orderBy);
205: }
206:
207: } catch (Exception e) {
208: e.printStackTrace();
209: }
210: return results;
211: }
212: */
213:
214: public ArrayList getAllItemsSorted(Long poolId, String orderBy,
215: String ascending) {
216: ArrayList results = null;
217: try {
218: if ("text".equals(orderBy)) {
219: results = new ArrayList(PersistenceService
220: .getInstance().getQuestionPoolFacadeQueries()
221: .getAllItemFacadesOrderByItemText(poolId,
222: "instruction", ascending));
223: } else if ("keyword".equals(orderBy)) {
224: results = new ArrayList(PersistenceService
225: .getInstance().getQuestionPoolFacadeQueries()
226: .getAllItemFacadesOrderByItemType(poolId,
227: orderBy, ascending));
228: }
229: } catch (Exception e) {
230: e.printStackTrace();
231: }
232: return results;
233: }
234:
235: /**
236: * Get all scores for a published assessment from the back end.
237: */
238: public ArrayList getAllItems(Long poolId) {
239: ArrayList results = null;
240: try {
241: results = new ArrayList(PersistenceService.getInstance()
242: .getQuestionPoolFacadeQueries().getAllItemFacades(
243: poolId));
244: } catch (Exception e) {
245: e.printStackTrace();
246: }
247: return results;
248: }
249:
250: /**
251: * Save a question to a pool.
252: */
253: public void addItemToPool(String itemId, Long poolId) {
254: try {
255: PersistenceService.getInstance()
256: .getQuestionPoolFacadeQueries().addItemToPool(
257: new QuestionPoolItemData(poolId, itemId));
258: } catch (Exception e) {
259: log.error(e);
260: throw new RuntimeException(e);
261: }
262: }
263:
264: /**
265: * Move a question to a pool.
266: */
267: public void moveItemToPool(String itemId, Long sourceId, Long destId) {
268: try {
269: PersistenceService.getInstance()
270: .getQuestionPoolFacadeQueries().moveItemToPool(
271: itemId, sourceId, destId);
272: } catch (Exception e) {
273: log.error(e);
274: throw new RuntimeException(e);
275: }
276: }
277:
278: /**
279: * Is a pool a descendant of the other?
280: */
281: public boolean isDescendantOf(Long poolA, Long poolB, String agentId) {
282: try {
283: Long tempPoolId = poolA;
284: while ((tempPoolId != null)
285: && (tempPoolId.toString().compareTo("0") > 0)) {
286: QuestionPoolFacade tempPool = getPool(tempPoolId,
287: agentId);
288: if (tempPool.getParentPoolId().toString().compareTo(
289: poolB.toString()) == 0)
290: return true;
291: tempPoolId = tempPool.getParentPoolId();
292: }
293: return false;
294:
295: } catch (Exception e) {
296: e.printStackTrace();
297: log.error(e);
298: return false;
299: }
300: }
301:
302: /**
303: * Move a subpool to a pool.
304: */
305: public void movePool(String agentId, Long sourceId, Long destId) {
306: try {
307: if (!isDescendantOf(destId, sourceId, agentId)) {
308: if (!sourceId.equals(destId)) {
309:
310: PersistenceService.getInstance()
311: .getQuestionPoolFacadeQueries().movePool(
312: agentId, sourceId, destId);
313: } else {
314: log
315: .warn("Illegal Move: Can not move a pool to itself.");
316: }
317: } else {
318: log
319: .warn("Illegal Move: Can not move a pool to its descendant.");
320: }
321: } catch (Exception e) {
322: log.error(e);
323: throw new RuntimeException(e);
324: }
325:
326: }
327:
328: /**
329: * Delete a pool
330: */
331: public void deletePool(Long poolId, String agentId, Tree tree) {
332: try {
333: QuestionPoolFacade qp = PersistenceService.getInstance()
334: .getQuestionPoolFacadeQueries().getPool(poolId,
335: agentId);
336:
337: // you are not allowed to delete pool if you are not the owner
338: if (!qp.getOwnerId().equals(agentId))
339: throw new Exception(
340: "You are not allowed to delete pool if you are not the owner");
341: PersistenceService.getInstance()
342: .getQuestionPoolFacadeQueries().deletePool(poolId,
343: agentId, tree);
344: } catch (Exception e) {
345: log.error(e);
346: throw new RuntimeException(e);
347: }
348: }
349:
350: /**
351: * removes a Question from the question pool. This does not *delete* the question itself
352: */
353: public void removeQuestionFromPool(String questionId, Long poolId) {
354: try {
355: PersistenceService.getInstance()
356: .getQuestionPoolFacadeQueries().removeItemFromPool(
357: questionId, poolId);
358: } catch (Exception e) {
359: log.error(e);
360: throw new RuntimeException(e);
361: }
362: }
363:
364: /**
365: * Copy a subpool to a pool.
366: */
367: public void copyPool(Tree tree, String agentId, Long sourceId,
368: Long destId, String prependString1, String prependString2) {
369: try {
370: if (!isDescendantOf(destId, sourceId, agentId)) {
371: PersistenceService.getInstance()
372: .getQuestionPoolFacadeQueries().copyPool(tree,
373: agentId, sourceId, destId,
374: prependString1, prependString2);
375: } else {
376: log
377: .warn("Illegal Copy: Can not copy a pool to its descendant!");
378: }
379:
380: } catch (Exception e) {
381: log.error(e);
382: throw new RuntimeException(e);
383: }
384: }
385:
386: /**
387: * Copy a question to a pool
388: */
389: public void copyQuestion(osid.shared.Id questionId,
390: osid.shared.Id destId) {
391: try {
392: //TODO must call the Service.
393: //questionPoolService.copyQuestion(questionId, destId );
394: } catch (Exception e) {
395: log.error(e);
396: throw new RuntimeException(e);
397: }
398: }
399:
400: /**
401: * Copy a question to a pool
402: */
403: public void copyQuestion(osid.shared.Id questionId,
404: osid.shared.Id destId, boolean duplicateCopy) {
405: try {
406: //TODO must call the Service.
407: //questionPoolService.copyQuestion(questionId, destId ,duplicateCopy);
408: } catch (Exception e) {
409: log.error(e);
410: throw new RuntimeException(e);
411: }
412: }
413:
414: /*
415: * Exports a Question as an Xml file
416: */
417: public String exportQuestion(osid.shared.Id questionId) {
418: try {
419: return "";//questionPoolService.exportQuestion(questionId);
420: } catch (Exception e) {
421: log.debug("Exception in exportQuestion");
422: log.error(e);
423: return null;
424: }
425: }
426:
427: /**
428: * Save a question pool.
429: */
430: public QuestionPoolFacade savePool(QuestionPoolFacade pool) {
431: try {
432: return PersistenceService.getInstance()
433: .getQuestionPoolFacadeQueries().savePool(pool);
434: } catch (Exception e) {
435: log.error(e);
436:
437: return pool;
438: }
439: }
440:
441: public HashMap getQuestionPoolItemMap() {
442: return PersistenceService.getInstance()
443: .getQuestionPoolFacadeQueries()
444: .getQuestionPoolItemMap();
445: }
446:
447: public boolean poolIsUnique(String questionPoolId, String title,
448: String parentPoolId, String agentId) {
449: return PersistenceService.getInstance()
450: .getQuestionPoolFacadeQueries().poolIsUnique(
451: new Long(questionPoolId), title,
452: new Long(parentPoolId), agentId);
453: }
454:
455: public Long copyItemFacade(ItemDataIfc itemData) {
456: return PersistenceService.getInstance()
457: .getQuestionPoolFacadeQueries()
458: .copyItemFacade(itemData);
459: }
460: }
|