001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/wizard/tool/src/java/org/theospi/portfolio/wizard/tool/DecoratedWizard.java $
003: * $Id: DecoratedWizard.java 22351 2007-03-09 14:11:59Z bkirschn@umich.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.wizard.tool;
021:
022: import org.theospi.portfolio.security.AuthorizationFailedException;
023:
024: import org.sakaiproject.tool.api.ToolSession;
025: import org.sakaiproject.tool.cover.SessionManager;
026: import org.theospi.portfolio.guidance.model.Guidance;
027: import org.theospi.portfolio.guidance.model.GuidanceItem;
028: import org.theospi.portfolio.style.StyleHelper;
029: import org.theospi.portfolio.style.model.Style;
030: import org.theospi.portfolio.wizard.mgt.WizardManager;
031: import org.theospi.portfolio.wizard.model.Wizard;
032: import org.theospi.portfolio.wizard.model.CompletedWizard;
033:
034: import javax.faces.context.ExternalContext;
035: import javax.faces.context.FacesContext;
036: import java.io.IOException;
037: import java.io.UnsupportedEncodingException;
038: import java.net.URLEncoder;
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.Iterator;
042:
043: /**
044: * Created by IntelliJ IDEA.
045: * User: John Ellis
046: * Date: Nov 14, 2005
047: * Time: 4:52:55 PM
048: * To change this template use File | Settings | File Templates.
049: */
050: public class DecoratedWizard implements DecoratedListInterface {
051: private Wizard base;
052: private WizardTool parent;
053: private DecoratedCategory rootCategory = null;
054: private DecoratedWizard next;
055: private DecoratedWizard prev;
056: private boolean newWizard = false;
057:
058: private DecoratedCompletedWizard runningWizard;
059:
060: private DecoratedCompletedWizard usersWizard;
061:
062: private int totalPages;
063:
064: public DecoratedWizard(WizardTool tool, Wizard base) {
065: this .base = base;
066: this .parent = tool;
067: rootCategory = new DecoratedCategory(base.getRootCategory(),
068: tool);
069: usersWizard = null;
070: }
071:
072: public DecoratedWizard(WizardTool tool, Wizard base,
073: boolean newWizard) {
074: this .newWizard = newWizard;
075: this .base = base;
076: this .parent = tool;
077: rootCategory = new DecoratedCategory(base.getRootCategory(),
078: tool);
079: usersWizard = null;
080: }
081:
082: public Wizard getBase() {
083: return base;
084: }
085:
086: public void setBase(Wizard base) {
087: this .base = base;
088: }
089:
090: public String getDescription() {
091: return getConcatDescription();
092: }
093:
094: /**
095: * This returns the concat description string. This is currently acceptable
096: * because the wizard description is not html
097: * @return String
098: */
099: public String getConcatDescription() {
100: String s = getBase().getDescription();
101: if (s == null) {
102: s = "";
103: }
104:
105: if (s.length() > 100) {
106: s = s.substring(0, 100) + "...";
107: }
108: return s;
109: }
110:
111: public boolean getExposeAsTool() {
112: if (base.getExposeAsTool() == null) {
113: return false;
114: } else {
115: return base.getExposeAsTool().booleanValue();
116: }
117: }
118:
119: public void setExposeAsTool(boolean exposeAsTool) {
120: base.setExposeAsTool(new Boolean(exposeAsTool));
121: }
122:
123: public boolean getCanPublish() {
124: return parent.getCanPublish(base);
125: }
126:
127: public boolean getCanDelete() {
128: return parent.getCanDelete(base);
129: }
130:
131: public boolean getCanEdit() {
132: return parent.getCanEdit(base);
133: }
134:
135: public boolean getCanExport() {
136: return parent.getCanExport(base);
137: }
138:
139: public boolean getCanOperateOnWizardInstance() {
140: boolean rethrow = false;
141: boolean isPublishedOrPreview = getBase().isPublished()
142: || getBase().isPreview();
143: Exception exc = null;
144:
145: boolean canOperate = false;
146: try {
147: canOperate = parent.getCanOperate(base);
148: } catch (AuthorizationFailedException e) {
149: canOperate = false;
150: exc = e;
151: }
152:
153: boolean isOwner = parent.getCurrentUserId().equals(
154: base.getOwner().getId().getValue());
155:
156: boolean can = (isPublishedOrPreview) && (canOperate || isOwner);
157:
158: if (!can && rethrow && exc != null)
159: throw new RuntimeException("couldn't authorize", exc);
160:
161: return can;
162: }
163:
164: public String getCurrentExportLink() {
165:
166: try {
167: return "repository/"
168: + "manager=org.theospi.portfolio.wizard.mgt.WizardManager&"
169: + WizardManager.WIZARD_PARAM_ID
170: + "="
171: + URLEncoder.encode(getBase().getId().getValue(),
172: "UTF-8")
173: + "/"
174: + URLEncoder.encode(getBase().getName()
175: + " Wizard.zip", "UTF-8");
176: } catch (UnsupportedEncodingException e) {
177: throw new RuntimeException(e);
178: }
179: }
180:
181: public String processActionEdit() {
182: return parent.processActionEdit(base);
183: }
184:
185: public String processActionDelete() {
186: return parent.processActionDelete(base);
187: }
188:
189: public String processActionConfirmDelete() {
190: return parent.processActionConfirmDelete(base);
191: }
192:
193: public String processActionPublish() {
194: return parent.processActionPublish(base);
195: }
196:
197: public String processActionPreview() {
198: return parent.processActionPreview(base);
199: }
200:
201: public String getStyleName() {
202: ToolSession session = SessionManager.getCurrentToolSession();
203: if (session.getAttribute(StyleHelper.CURRENT_STYLE) != null) {
204: Style style = (Style) session
205: .getAttribute(StyleHelper.CURRENT_STYLE);
206: base.setStyle(style);
207: } else if (session.getAttribute(StyleHelper.UNSELECTED_STYLE) != null) {
208: base.setStyle(null);
209: session.removeAttribute(StyleHelper.UNSELECTED_STYLE);
210: return "";
211: }
212:
213: if (base.getStyle() != null) {
214: return base.getStyle().getName();
215: }
216: return "";
217: }
218:
219: public String processActionSelectStyle() {
220: getParent().clearInterface();
221: ExternalContext context = FacesContext.getCurrentInstance()
222: .getExternalContext();
223: ToolSession session = SessionManager.getCurrentToolSession();
224: session.removeAttribute(StyleHelper.CURRENT_STYLE);
225: session.removeAttribute(StyleHelper.CURRENT_STYLE_ID);
226:
227: session.setAttribute(StyleHelper.STYLE_SELECTABLE, "true");
228:
229: Wizard wizard = getBase();
230:
231: if (wizard.getStyle() != null) {
232: session.setAttribute(StyleHelper.CURRENT_STYLE_ID, wizard
233: .getStyle().getId().getValue());
234: }
235:
236: try {
237: context.redirect("osp.style.helper/listStyle");
238: } catch (IOException e) {
239: throw new RuntimeException("Failed to redirect to helper",
240: e);
241: }
242: return null;
243: }
244:
245: public WizardTool getParent() {
246: return parent;
247: }
248:
249: public void setParent(WizardTool parent) {
250: this .parent = parent;
251: }
252:
253: public DecoratedCategory getRootCategory() {
254: return rootCategory;
255: }
256:
257: public void setRootCategory(DecoratedCategory rootCategory) {
258: this .rootCategory = rootCategory;
259: }
260:
261: public boolean isFirst() {
262: return getPrev() == null;
263: }
264:
265: public boolean isLast() {
266: return getNext() == null;
267: }
268:
269: public String moveUp() {
270: return switchSeq(getPrev());
271: }
272:
273: public String moveDown() {
274: return switchSeq(getNext());
275: }
276:
277: protected String switchSeq(DecoratedWizard other) {
278: int otherSeq = other.getBase().getSequence();
279: int this Seq = getBase().getSequence();
280: other.getBase().setSequence(this Seq);
281: getBase().setSequence(otherSeq);
282: getParent().getWizardManager().saveWizard(getBase());
283: getParent().getWizardManager().saveWizard(other.getBase());
284: return null;
285: }
286:
287: public DecoratedWizard getNext() {
288: return next;
289: }
290:
291: public void setNext(DecoratedWizard next) {
292: this .next = next;
293: }
294:
295: public DecoratedWizard getPrev() {
296: return prev;
297: }
298:
299: public void setPrev(DecoratedWizard prev) {
300: this .prev = prev;
301: }
302:
303: public boolean isOwner() {
304: String userId = SessionManager.getCurrentSessionUserId();
305: if (userId != null) {
306: return userId.equals(getBase().getOwner().getId()
307: .getValue());
308: }
309: return false;
310: }
311:
312: public String processActionRunWizard() {
313: getParent().clearInterface();
314: ExternalContext context = FacesContext.getCurrentInstance()
315: .getExternalContext();
316: //ToolSession session = SessionManager.getCurrentToolSession();
317:
318: setBase(parent.getWizardManager().getWizard(getBase().getId()));
319: rootCategory = new DecoratedCategory(base.getRootCategory(),
320: parent);
321: getParent().setCurrent(this );
322: setRunningWizard(new DecoratedCompletedWizard(getParent(),
323: this , parent.getWizardManager().getCompletedWizard(
324: getBase(), getParent().getCurrentUserId())));
325:
326: //return "runWizard";
327:
328: try {
329: context.redirect("osp.wizard.run.helper/runWizardGuidance");
330: } catch (IOException e) {
331: throw new RuntimeException("Failed to redirect to helper",
332: e);
333: }
334: return null;
335: }
336:
337: public String processActionEditInstructions() {
338: parent.processActionGuidanceHelper(getBase(), 1);
339: return null;
340: }
341:
342: public String processActionEditRationale() {
343: parent.processActionGuidanceHelper(getBase(), 2);
344: return null;
345: }
346:
347: public String processActionEditExamples() {
348: parent.processActionGuidanceHelper(getBase(), 4);
349: return null;
350: }
351:
352: public DecoratedCompletedWizard getRunningWizard() {
353: return runningWizard;
354: }
355:
356: public void setRunningWizard(DecoratedCompletedWizard runningWizard) {
357: this .runningWizard = runningWizard;
358: }
359:
360: public GuidanceItem getInstruction() {
361: if (getBase().getGuidance() == null) {
362: return null;
363: }
364: return getBase().getGuidance().getInstruction();
365: }
366:
367: public GuidanceItem getExample() {
368: if (getBase().getGuidance() == null) {
369: return null;
370: }
371: return getBase().getGuidance().getExample();
372: }
373:
374: public GuidanceItem getRationale() {
375: if (getBase().getGuidance() == null) {
376: return null;
377: }
378: return getBase().getGuidance().getRationale();
379: }
380:
381: public boolean isGuidanceAvailable() {
382: return getBase().getGuidance() != null;
383: }
384:
385: protected String limitString(String s, int max) {
386: if (s == null) {
387: return "";
388: }
389: if (s.length() > max) {
390: s = s.substring(0, max) + "...";
391: }
392: return s;
393: }
394:
395: private void assureAttachmentAccess(Guidance guidance) {
396: if (getParent().getAuthzManager().isAuthorized(
397: guidance.getSecurityEditFunction(),
398: guidance.getSecurityQualifier())
399: || getParent().getAuthzManager().isAuthorized(
400: guidance.getSecurityViewFunction(),
401: guidance.getSecurityQualifier())) {
402: getParent().getGuidanceManager().assureAccess(guidance);
403: }
404: }
405:
406: public String getGuidanceInstructions() {
407: Guidance guidance = getBase().getGuidance();
408: if (guidance == null) {
409: return null;
410: }
411: GuidanceItem item = guidance.getInstruction();
412: if (item == null) {
413: return null;
414: }
415: return limitString(item.getText(), 100);
416: }
417:
418: public List getGuidanceInstructionsAttachments() {
419: Guidance guidance = getBase().getGuidance();
420: if (guidance == null) {
421: return new ArrayList();
422: }
423: GuidanceItem item = guidance.getInstruction();
424: if (item == null) {
425: return new ArrayList();
426: }
427: assureAttachmentAccess(guidance);
428: return item.getAttachments();
429: }
430:
431: public String getGuidanceRationale() {
432: Guidance guidance = getBase().getGuidance();
433: if (guidance == null) {
434: return "";
435: }
436: GuidanceItem item = guidance.getRationale();
437: if (item == null) {
438: return "";
439: }
440: return limitString(item.getText(), 100);
441: }
442:
443: public List getGuidanceRationaleAttachments() {
444: Guidance guidance = getBase().getGuidance();
445: if (guidance == null) {
446: return new ArrayList();
447: }
448: GuidanceItem item = guidance.getRationale();
449: if (item == null) {
450: return new ArrayList();
451: }
452: assureAttachmentAccess(guidance);
453: return item.getAttachments();
454: }
455:
456: public String getGuidanceExamples() {
457: Guidance guidance = getBase().getGuidance();
458: if (guidance == null) {
459: return "";
460: }
461: GuidanceItem item = guidance.getExample();
462: if (item == null) {
463: return "";
464: }
465: return limitString(item.getText(), 100);
466: }
467:
468: public List getGuidanceExamplesAttachments() {
469: Guidance guidance = getBase().getGuidance();
470: if (guidance == null) {
471: return new ArrayList();
472: }
473: GuidanceItem item = guidance.getExample();
474: if (item == null) {
475: return new ArrayList();
476: }
477: assureAttachmentAccess(guidance);
478: return item.getAttachments();
479: }
480:
481: public List getEvaluators() {
482: return parent.getEvaluators(getBase());
483: }
484:
485: public boolean isNewWizard() {
486: return newWizard;
487: }
488:
489: public void setNewWizard(boolean newWizard) {
490: this .newWizard = newWizard;
491: }
492:
493: public DecoratedCategory getCategory() {
494: return null;
495: }
496:
497: public String getIndentString() {
498: return "";
499: }
500:
501: public String getTitle() {
502: return getBase().getName();
503: }
504:
505: public boolean isMoveTarget() {
506: return false;
507: }
508:
509: public boolean getHasChildren() {
510: return false;
511: }
512:
513: public boolean isWizard() {
514: return true;
515: }
516:
517: public String getDeleteMessage() {
518: return getParent().getMessageFromBundle(
519: "delete_wizard_message",
520: new Object[] { base.getName() });
521: }
522:
523: public DecoratedCompletedWizard getUsersWizard() {
524: if (usersWizard == null) {
525: setUsersWizard(new DecoratedCompletedWizard(getParent(),
526: this , parent.getWizardManager().getCompletedWizard(
527: getBase(), getParent().getCurrentUserId(),
528: false)));
529: }
530: return usersWizard;
531: }
532:
533: public void setUsersWizard(DecoratedCompletedWizard usersWizard) {
534: this .usersWizard = usersWizard;
535: }
536:
537: public int getTotalPages() {
538: return totalPages;
539: }
540:
541: public void setTotalPages(int totalPages) {
542: this .totalPages = totalPages;
543: }
544:
545: public boolean getIsWizardUsed() {
546: // preview mode is never considered 'in use' (edits should not be prohibited)
547: if (base.isPreview())
548: return false;
549:
550: String wizardId = base.getId().getValue();
551: List completedWizards = parent.getWizardManager()
552: .getCompletedWizardsByWizardId(wizardId);
553: for (Iterator i = completedWizards.iterator(); i.hasNext();) {
554: CompletedWizard cw = (CompletedWizard) i.next();
555:
556: List reviews = parent.getReviewManager()
557: .getReviewsByParent(cw.getId().getValue());
558: if (reviews.size() > 0)
559: return true;
560: else
561: return false;
562: }
563: return false;
564: }
565: }
|