001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/portfolio/guidance/tool/GuidanceTool.java $
003: * $Id:GuidanceTool.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.portfolio.guidance.tool;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031:
032: import org.sakaiproject.content.api.FilePickerHelper;
033: import org.sakaiproject.content.api.ResourceEditingHelper;
034: import org.sakaiproject.content.api.ContentCollection;
035: import org.sakaiproject.content.cover.ContentHostingService;
036: import org.sakaiproject.entity.api.Reference;
037: import org.sakaiproject.entity.cover.EntityManager;
038: import org.sakaiproject.tool.api.Placement;
039: import org.sakaiproject.tool.api.ToolSession;
040: import org.sakaiproject.tool.cover.SessionManager;
041: import org.sakaiproject.tool.cover.ToolManager;
042: import org.sakaiproject.metaobj.shared.FormHelper;
043: import org.sakaiproject.exception.TypeException;
044: import org.sakaiproject.exception.IdUnusedException;
045: import org.sakaiproject.exception.PermissionException;
046: import org.sakaiproject.user.api.User;
047: import org.sakaiproject.user.cover.UserDirectoryService;
048: import org.sakaiproject.site.cover.SiteService;
049: import org.theospi.portfolio.guidance.mgt.GuidanceHelper;
050: import org.theospi.portfolio.guidance.mgt.GuidanceManager;
051: import org.theospi.portfolio.guidance.model.Guidance;
052: import org.theospi.portfolio.guidance.model.GuidanceItem;
053: import org.theospi.portfolio.guidance.model.GuidanceItemAttachment;
054: import org.theospi.portfolio.shared.tool.HelperToolBase;
055:
056: /**
057: * Created by IntelliJ IDEA.
058: * User: John Ellis
059: * Date: Nov 11, 2005
060: * Time: 3:33:43 PM
061: * To change this template use File | Settings | File Templates.
062: */
063: public class GuidanceTool extends HelperToolBase {
064:
065: private DecoratedGuidance current = null;
066: private String formTypeId = null;
067: private String formId = null;
068: private String formDisplayName = null;
069:
070: private boolean showExamples = true;
071: private boolean showInstructions = true;
072: private boolean showRationale = true;
073:
074: private GuidanceManager guidanceManager;
075: public static final String ATTACHMENT_TYPE = "org.theospi.portfolio.guidance.attachmentType";
076:
077: public GuidanceManager getGuidanceManager() {
078: return guidanceManager;
079: }
080:
081: public void setGuidanceManager(GuidanceManager guidanceManager) {
082: this .guidanceManager = guidanceManager;
083: }
084:
085: public String getGuidanceInstructions() {
086: return getCurrent().getBase().getDescription();
087: }
088:
089: public DecoratedGuidance getCurrent() {
090: ToolSession session = SessionManager.getCurrentToolSession();
091:
092: if (session.getAttribute(GuidanceManager.CURRENT_GUIDANCE_ID) != null) {
093: String id = (String) session
094: .getAttribute(GuidanceManager.CURRENT_GUIDANCE_ID);
095: current = new DecoratedGuidance(this , getGuidanceManager()
096: .getGuidance(id));
097: session
098: .removeAttribute(GuidanceManager.CURRENT_GUIDANCE_ID);
099: } else if (session
100: .getAttribute(GuidanceManager.CURRENT_GUIDANCE) != null) {
101: current = new DecoratedGuidance(this , (Guidance) session
102: .getAttribute(GuidanceManager.CURRENT_GUIDANCE));
103: session.removeAttribute(GuidanceManager.CURRENT_GUIDANCE);
104: }
105:
106: return current;
107: }
108:
109: public String processActionManageAttachments(String type) {
110: ExternalContext context = FacesContext.getCurrentInstance()
111: .getExternalContext();
112: ToolSession session = SessionManager.getCurrentToolSession();
113: session.setAttribute(FilePickerHelper.FILE_PICKER_ATTACH_LINKS,
114: new Boolean(true).toString());
115: session.setAttribute(GuidanceTool.ATTACHMENT_TYPE, type);
116: GuidanceItem item = getCurrent().getBase().getItem(type);
117:
118: List attachments = item.getAttachments();
119: List attachmentRefs = EntityManager.newReferenceList();
120:
121: for (Iterator i = attachments.iterator(); i.hasNext();) {
122: GuidanceItemAttachment attachment = (GuidanceItemAttachment) i
123: .next();
124: attachmentRefs.add(attachment.getBaseReference().getBase());
125: }
126: session.setAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
127: attachmentRefs);
128:
129: try {
130: context.redirect("sakai.filepicker.helper/tool");
131: } catch (IOException e) {
132: throw new RuntimeException("Failed to redirect to helper",
133: e);
134: }
135: return null;
136: }
137:
138: public String processActionSave() {
139: getGuidanceManager().saveGuidance(getCurrent().getBase());
140:
141: ToolSession session = SessionManager.getCurrentToolSession();
142:
143: session.setAttribute(GuidanceManager.CURRENT_GUIDANCE,
144: getCurrent().getBase());
145: cleanup(session);
146:
147: return returnToCaller();
148: }
149:
150: public String processActionCancel() {
151: ToolSession session = SessionManager.getCurrentToolSession();
152: session.removeAttribute(GuidanceManager.CURRENT_GUIDANCE);
153: session.removeAttribute(GuidanceManager.CURRENT_GUIDANCE_ID);
154: cleanup(session);
155: current = null;
156: return returnToCaller();
157: }
158:
159: protected void cleanup(ToolSession toolSession) {
160: toolSession.removeAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG);
161: toolSession
162: .removeAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG);
163: toolSession.removeAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG);
164: }
165:
166: public Reference decorateReference(String reference) {
167: return getGuidanceManager().decorateReference(
168: getCurrent().getBase(), reference);
169: }
170:
171: /**
172: * sample
173: * @return
174: */
175: public String getLastSavedId() {
176: ToolSession session = SessionManager.getCurrentToolSession();
177: Guidance guidance = (Guidance) session
178: .getAttribute(GuidanceManager.CURRENT_GUIDANCE);
179: if (guidance != null) {
180: session.removeAttribute(GuidanceManager.CURRENT_GUIDANCE);
181: return guidance.getId().getValue();
182: }
183: return "none";
184: }
185:
186: /**
187: * sample
188: * @return
189: */
190: public List getSampleGuidances() {
191: Placement placement = ToolManager.getCurrentPlacement();
192: String currentSiteId = placement.getContext();
193: List returned = new ArrayList();
194: List orig = getGuidanceManager().listGuidances(currentSiteId);
195:
196: for (Iterator i = orig.iterator(); i.hasNext();) {
197: Guidance guidance = (Guidance) i.next();
198: returned.add(new DecoratedGuidance(this , guidance));
199: }
200: return returned;
201: }
202:
203: public boolean isInstructionsRendered() {
204: //boolean showInstructions = true;
205: if (getAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG) != null) {
206: if (getAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG) instanceof Boolean) {
207: showInstructions = ((Boolean) getAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG))
208: .booleanValue();
209: } else {
210: showInstructions = "true"
211: .equalsIgnoreCase((String) getAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG));
212: }
213: removeAttribute(GuidanceHelper.SHOW_INSTRUCTION_FLAG);
214: }
215: return showInstructions;
216: }
217:
218: public boolean isExamplesRendered() {
219: //boolean showExamples = true;
220: if (getAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG) != null) {
221: if (getAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG) instanceof Boolean) {
222: showExamples = ((Boolean) getAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG))
223: .booleanValue();
224: } else {
225: showExamples = "true"
226: .equalsIgnoreCase((String) getAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG));
227: }
228: removeAttribute(GuidanceHelper.SHOW_EXAMPLE_FLAG);
229: }
230: return showExamples;
231: }
232:
233: public boolean isRationaleRendered() {
234: //boolean showRationale = true;
235: if (getAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG) != null) {
236: if (getAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG) instanceof Boolean) {
237: showRationale = ((Boolean) getAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG))
238: .booleanValue();
239: } else {
240: showRationale = "true"
241: .equalsIgnoreCase((String) getAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG));
242: }
243: removeAttribute(GuidanceHelper.SHOW_RATIONALE_FLAG);
244: }
245: return showRationale;
246: }
247:
248: /**
249: * sample
250: * @param guidance
251: * @return
252: */
253: public String processActionEdit(Guidance guidance) {
254: guidance = getGuidanceManager().getGuidance(guidance.getId());
255: invokeTool(guidance, new HashMap());
256: return null;
257: }
258:
259: public String processActionEditInstruction(Guidance guidance) {
260: guidance = getGuidanceManager().getGuidance(guidance.getId());
261:
262: Map typeFlags = new HashMap();
263:
264: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "true");
265: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "false");
266: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "false");
267: invokeTool(guidance, typeFlags);
268: return null;
269: }
270:
271: public String processActionEditExample(Guidance guidance) {
272: guidance = getGuidanceManager().getGuidance(guidance.getId());
273:
274: Map typeFlags = new HashMap();
275:
276: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "false");
277: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "false");
278: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "true");
279: invokeTool(guidance, typeFlags);
280: return null;
281: }
282:
283: public String processActionEditRationale(Guidance guidance) {
284: guidance = getGuidanceManager().getGuidance(guidance.getId());
285:
286: Map typeFlags = new HashMap();
287:
288: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "false");
289: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "true");
290: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "false");
291: invokeTool(guidance, typeFlags);
292: return null;
293: }
294:
295: /**
296: * sample
297: * @param guidance
298: * @return
299: */
300: public String processActionDelete(Guidance guidance) {
301: getGuidanceManager().deleteGuidance(guidance);
302: current = null;
303: return "list";
304: }
305:
306: /**
307: * sample
308: * @param guidance
309: * @return
310: */
311: public String processActionView(Guidance guidance) {
312: guidance = getGuidanceManager().getGuidance(guidance.getId());
313: invokeToolView(guidance.getId().getValue());
314: return null;
315: }
316:
317: /**
318: * sample
319: * @return
320: */
321: public String processActionNew() {
322: Placement placement = ToolManager.getCurrentPlacement();
323: String currentSite = placement.getContext();
324: Guidance newGuidance = getGuidanceManager().createNew(
325: "Sample Guidance", currentSite, null, "", "");
326:
327: invokeTool(newGuidance, new HashMap());
328:
329: return null;
330: }
331:
332: public String processActionNewInstruction() {
333: Placement placement = ToolManager.getCurrentPlacement();
334: String currentSite = placement.getContext();
335: Guidance newGuidance = getGuidanceManager().createNew(
336: "Sample Guidance", currentSite, null, "", "");
337:
338: Map typeFlags = new HashMap();
339:
340: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "true");
341: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "false");
342: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "false");
343: invokeTool(newGuidance, typeFlags);
344:
345: return null;
346: }
347:
348: public String processActionNewExample() {
349: Placement placement = ToolManager.getCurrentPlacement();
350: String currentSite = placement.getContext();
351: Guidance newGuidance = getGuidanceManager().createNew(
352: "Sample Guidance", currentSite, null, "", "");
353:
354: Map typeFlags = new HashMap();
355:
356: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "true");
357: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "false");
358: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "false");
359: invokeTool(newGuidance, typeFlags);
360:
361: return null;
362: }
363:
364: public String processActionNewRationale() {
365: Placement placement = ToolManager.getCurrentPlacement();
366: String currentSite = placement.getContext();
367: Guidance newGuidance = getGuidanceManager().createNew(
368: "Sample Guidance", currentSite, null, "", "");
369:
370: Map typeFlags = new HashMap();
371:
372: typeFlags.put(GuidanceHelper.SHOW_RATIONALE_FLAG, "true");
373: typeFlags.put(GuidanceHelper.SHOW_EXAMPLE_FLAG, "false");
374: typeFlags.put(GuidanceHelper.SHOW_INSTRUCTION_FLAG, "false");
375: invokeTool(newGuidance, typeFlags);
376:
377: return null;
378: }
379:
380: /**
381: * sample
382: * @param guidance
383: */
384: protected void invokeTool(Guidance guidance, Map typeFlags) {
385: ExternalContext context = FacesContext.getCurrentInstance()
386: .getExternalContext();
387: ToolSession session = SessionManager.getCurrentToolSession();
388:
389: session
390: .setAttribute(GuidanceManager.CURRENT_GUIDANCE,
391: guidance);
392:
393: for (Iterator iter = typeFlags.keySet().iterator(); iter
394: .hasNext();) {
395: String key = (String) iter.next();
396: session.setAttribute(key, typeFlags.get(key));
397: }
398:
399: try {
400: context.redirect("osp.guidance.helper/tool");
401: } catch (IOException e) {
402: throw new RuntimeException("Failed to redirect to helper",
403: e);
404: }
405: }
406:
407: /**
408: * sample
409: * @param id
410: */
411: protected void invokeToolView(String id) {
412: ExternalContext context = FacesContext.getCurrentInstance()
413: .getExternalContext();
414: ToolSession session = SessionManager.getCurrentToolSession();
415:
416: session.setAttribute(GuidanceManager.CURRENT_GUIDANCE_ID, id);
417:
418: try {
419: context.redirect("osp.guidance.helper/view");
420: } catch (IOException e) {
421: throw new RuntimeException("Failed to redirect to helper",
422: e);
423: }
424: }
425:
426: public String processTestResourceHelper() {
427: ExternalContext context = FacesContext.getCurrentInstance()
428: .getExternalContext();
429: ToolSession session = SessionManager.getCurrentToolSession();
430: session.removeAttribute(ResourceEditingHelper.ATTACHMENT_ID);
431: session.setAttribute(ResourceEditingHelper.CREATE_TYPE,
432: ResourceEditingHelper.CREATE_TYPE_FORM);
433: session.setAttribute(ResourceEditingHelper.CREATE_SUB_TYPE,
434: formTypeId);
435: session.setAttribute(FormHelper.NEW_FORM_DISPLAY_NAME_TAG,
436: getFormDisplayName());
437:
438: try {
439: session.setAttribute(FormHelper.PARENT_ID_TAG,
440: getUserCollection().getId());
441: context.redirect("sakai.metaobj.form.helper/formHelper");
442: } catch (IOException e) {
443: throw new RuntimeException("Failed to redirect to helper",
444: e);
445: } catch (IdUnusedException e) {
446: throw new RuntimeException("Failed to redirect to helper",
447: e);
448: } catch (TypeException e) {
449: throw new RuntimeException("Failed to redirect to helper",
450: e);
451: } catch (PermissionException e) {
452: throw new RuntimeException("Failed to redirect to helper",
453: e);
454: }
455:
456: return null;
457: }
458:
459: public String processTestResourceEditHelper() {
460: ExternalContext context = FacesContext.getCurrentInstance()
461: .getExternalContext();
462: ToolSession session = SessionManager.getCurrentToolSession();
463: session.removeAttribute(ResourceEditingHelper.CREATE_TYPE);
464: session.removeAttribute(ResourceEditingHelper.CREATE_SUB_TYPE);
465: session.removeAttribute(ResourceEditingHelper.CREATE_PARENT);
466: session.setAttribute(ResourceEditingHelper.CREATE_TYPE,
467: ResourceEditingHelper.CREATE_TYPE_FORM);
468: session.setAttribute(ResourceEditingHelper.ATTACHMENT_ID,
469: getFormId());
470:
471: try {
472: context
473: .redirect("sakai.metaobj.form.helper/formEditHelper");
474: } catch (IOException e) {
475: throw new RuntimeException("Failed to redirect to helper",
476: e);
477: }
478:
479: return null;
480: }
481:
482: public String processTestResourceViewHelper() {
483: ExternalContext context = FacesContext.getCurrentInstance()
484: .getExternalContext();
485: ToolSession session = SessionManager.getCurrentToolSession();
486: session.removeAttribute(ResourceEditingHelper.CREATE_TYPE);
487: session.removeAttribute(ResourceEditingHelper.CREATE_SUB_TYPE);
488: session.removeAttribute(ResourceEditingHelper.CREATE_PARENT);
489: session.setAttribute(ResourceEditingHelper.CREATE_TYPE,
490: ResourceEditingHelper.CREATE_TYPE_FORM);
491: session.setAttribute(ResourceEditingHelper.ATTACHMENT_ID,
492: getFormId());
493:
494: try {
495: context
496: .redirect("sakai.metaobj.formView.helper/formView.osp");
497: } catch (IOException e) {
498: throw new RuntimeException("Failed to redirect to helper",
499: e);
500: }
501:
502: return null;
503: }
504:
505: public String getFormTypeId() {
506: return formTypeId;
507: }
508:
509: public void setFormTypeId(String formTypeId) {
510: this .formTypeId = formTypeId;
511: }
512:
513: public String getFormId() {
514: return formId;
515: }
516:
517: public void setFormId(String formId) {
518: this .formId = formId;
519: }
520:
521: protected ContentCollection getUserCollection()
522: throws TypeException, IdUnusedException,
523: PermissionException {
524: User user = UserDirectoryService.getCurrentUser();
525: String userId = user.getId();
526: String wsId = SiteService.getUserSiteId(userId);
527: String wsCollectionId = ContentHostingService
528: .getSiteCollection(wsId);
529: ContentCollection collection = ContentHostingService
530: .getCollection(wsCollectionId);
531: return collection;
532: }
533:
534: public String getFormDisplayName() {
535: return formDisplayName;
536: }
537:
538: public void setFormDisplayName(String formDisplayName) {
539: this.formDisplayName = formDisplayName;
540: }
541:
542: }
|