001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/tool/FreeFormTool.java $
003: * $Id:FreeFormTool.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.presentation.tool;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031: import javax.faces.event.ActionEvent;
032: import javax.faces.event.ValueChangeEvent;
033: import javax.faces.model.SelectItem;
034:
035: import org.sakaiproject.content.api.ContentHostingService;
036: import org.sakaiproject.content.api.FilePickerHelper;
037: import org.sakaiproject.entity.api.Reference;
038: import org.sakaiproject.entity.cover.EntityManager;
039: import org.sakaiproject.metaobj.shared.mgt.IdManager;
040: import org.sakaiproject.site.cover.SiteService;
041: import org.sakaiproject.tool.api.Tool;
042: import org.sakaiproject.tool.api.ToolSession;
043: import org.sakaiproject.tool.cover.SessionManager;
044: import org.sakaiproject.tool.cover.ToolManager;
045: import org.theospi.jsf.intf.XmlTagFactory;
046: import org.theospi.portfolio.presentation.PresentationManager;
047: import org.theospi.portfolio.presentation.intf.FreeFormHelper;
048: import org.theospi.portfolio.presentation.model.Presentation;
049: import org.theospi.portfolio.presentation.model.PresentationItem;
050: import org.theospi.portfolio.presentation.model.PresentationPage;
051: import org.theospi.portfolio.shared.model.Node;
052: import org.theospi.portfolio.shared.tool.HelperToolBase;
053: import org.theospi.portfolio.style.StyleHelper;
054: import org.theospi.portfolio.style.model.Style;
055:
056: /**
057: * Created by IntelliJ IDEA.
058: * User: John Ellis
059: * Date: Dec 31, 2005
060: * Time: 9:23:29 PM
061: * To change this template use File | Settings | File Templates.
062: */
063: public class FreeFormTool extends HelperToolBase {
064:
065: private PresentationManager presentationManager;
066: private IdManager idManager;
067: private XmlTagFactory factory;
068: private ContentHostingService contentHosting;
069:
070: private Presentation presentation = null;
071:
072: private DecoratedPage currentPage = null;
073: private List pageList;
074: private List attachableItems = null;
075: private List listableItems = null;
076: private List layouts = null;
077: private String nextPageId = null;
078: private int step = 1;
079: private int pageCount;
080:
081: public String processActionBack() {
082: if (!validPages()) {
083: return null;
084: }
085: setAttribute(FreeFormHelper.FREE_FORM_ACTION,
086: FreeFormHelper.ACTION_BACK);
087: return returnToCaller();
088: }
089:
090: protected boolean validPages() {
091: if (getPageList() == null || getPageList().size() == 0) {
092: FacesContext.getCurrentInstance().addMessage(
093: null,
094: getFacesMessageFromBundle("one_page_required",
095: new Object[] {}));
096: return false;
097: }
098:
099: return true;
100: }
101:
102: public String processActionContinue() {
103: if (!validPages()) {
104: return null;
105: }
106: setAttribute(FreeFormHelper.FREE_FORM_ACTION,
107: FreeFormHelper.ACTION_CONTINUE);
108: return returnToCaller();
109: }
110:
111: public String processActionSave() {
112: if (!validPages()) {
113: return null;
114: }
115: setAttribute(FreeFormHelper.FREE_FORM_ACTION,
116: FreeFormHelper.ACTION_SAVE);
117: return returnToCaller();
118: }
119:
120: public String processActionCancel() {
121: initValues();
122: setAttribute(FreeFormHelper.FREE_FORM_ACTION,
123: FreeFormHelper.ACTION_CANCEL);
124: return returnToCaller();
125: }
126:
127: public String processActionCancelPage() {
128: if (getCurrentPage().isNewPage()) {
129: deletePage(getCurrentPage());
130: }
131: cancelBoundValues();
132: return "main";
133: }
134:
135: protected void initValues() {
136: currentPage = null;
137: pageList = null;
138: attachableItems = null;
139: listableItems = null;
140: }
141:
142: public PresentationManager getPresentationManager() {
143: return presentationManager;
144: }
145:
146: public void setPresentationManager(
147: PresentationManager presentationManager) {
148: this .presentationManager = presentationManager;
149: }
150:
151: public Presentation getPresentation() {
152: Presentation sessionPresentation = (Presentation) getAttribute(FreeFormHelper.FREE_FORM_PREFIX
153: + "presentation");
154: if (sessionPresentation != null) {
155: removeAttribute(FreeFormHelper.FREE_FORM_PREFIX
156: + "presentation");
157: presentation = sessionPresentation;
158: List pages = presentation.getPages();
159: if (pages == null) {
160: pages = getPresentationManager()
161: .getPresentationPagesByPresentation(
162: presentation.getId());
163: presentation.setPages(pages);
164: }
165: initValues();
166: }
167: return presentation;
168: }
169:
170: public DecoratedPage getCurrentPage() {
171: return currentPage;
172: }
173:
174: public void setCurrentPage(DecoratedPage currentPage) {
175: nextPageId = null;
176: this .currentPage = currentPage;
177: }
178:
179: public void processPageSelectChange(ValueChangeEvent event) {
180:
181: }
182:
183: public List getPageList() {
184: Presentation presentation = getPresentation();
185: if (pageList == null) {
186: List pages = presentation.getPages();
187:
188: pageList = new ArrayList();
189: for (Iterator i = pages.iterator(); i.hasNext();) {
190: pageList.add(new DecoratedPage((PresentationPage) i
191: .next(), this ));
192: }
193: }
194: return pageList;
195: }
196:
197: public void setPageList(List pageList) {
198: this .pageList = pageList;
199: }
200:
201: public XmlTagFactory getFactory() {
202: return factory;
203: }
204:
205: public void setFactory(XmlTagFactory factory) {
206: this .factory = factory;
207: }
208:
209: public void processActionManageItems(ActionEvent event) {
210: ExternalContext context = FacesContext.getCurrentInstance()
211: .getExternalContext();
212: ToolSession session = SessionManager.getCurrentToolSession();
213: session.setAttribute(FilePickerHelper.FILE_PICKER_ATTACH_LINKS,
214: new Boolean(true).toString());
215:
216: List attachments = new ArrayList(getPresentation().getItems());
217: List attachmentRefs = EntityManager.newReferenceList();
218:
219: for (Iterator i = attachments.iterator(); i.hasNext();) {
220: PresentationItem attachment = (PresentationItem) i.next();
221: Node item = getPresentationManager().getNode(
222: attachment.getArtifactId());
223: attachmentRefs.add(EntityManager.newReference(item
224: .getResource().getReference()));
225: }
226:
227: session.setAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
228: attachmentRefs);
229:
230: //Start in user's resources area
231: //osp-ui-05
232: String siteId = SiteService.getUserSiteId(SessionManager
233: .getCurrentSessionUserId());
234: String collectionId = getContentHosting().getSiteCollection(
235: siteId);
236: session.setAttribute(FilePickerHelper.DEFAULT_COLLECTION_ID,
237: collectionId);
238:
239: try {
240: context.redirect("sakai.filepicker.helper/tool");
241: } catch (IOException e) {
242: throw new RuntimeException("Failed to redirect to helper",
243: e);
244: }
245: }
246:
247: public Set getItems() {
248: checkUpdateItems();
249: if (getPresentation().getItems() != null) {
250: return getPresentation().getItems();
251: }
252: getPresentation().setItems(new HashSet());
253: return getPresentation().getItems();
254: }
255:
256: protected void checkUpdateItems() {
257: ToolSession session = SessionManager.getCurrentToolSession();
258: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
259: && session
260: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
261:
262: List refs = (List) session
263: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
264: List newAttachments = new ArrayList();
265:
266: for (int i = 0; i < refs.size(); i++) {
267: Reference ref = (Reference) refs.get(i);
268: PresentationItem item = new PresentationItem();
269: Node node = getPresentationManager().getNode(ref);
270: item.setArtifactId(node.getId());
271: newAttachments.add(item);
272: }
273: session
274: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
275: getPresentation().getItems().clear();
276: getPresentation().getItems().addAll(newAttachments);
277: attachableItems = null;
278: listableItems = null;
279: }
280:
281: }
282:
283: public List getAttachableItems() {
284: checkUpdateItems();
285: if (attachableItems == null) {
286: attachableItems = new ArrayList();
287: for (Iterator i = getListableItems().iterator(); i
288: .hasNext();) {
289: DecoratedItem item = (DecoratedItem) i.next();
290: Node node = item.getNode();
291: attachableItems.add(createSelect(node.getExternalUri(),
292: node.getDisplayName()));
293: }
294: }
295:
296: List pages = getPageList();
297: for (Iterator i = pages.iterator(); i.hasNext();) {
298: DecoratedPage decoratedPage = (DecoratedPage) i.next();
299: PresentationPage page = decoratedPage.getBase();
300: SelectItem si = (SelectItem) createSelect(page.getUrl(),
301: page.getTitle());
302:
303: boolean present = false;
304: for (Iterator j = attachableItems.iterator(); j.hasNext();) {
305: SelectItem isi = (SelectItem) j.next();
306: if (isi.getLabel() != null && isi.getValue() != null)
307: if (isi.getLabel().equals(si.getLabel())
308: && isi.getValue().equals(si.getValue())) {
309: present = true;
310: break;
311: }
312: }
313: if (!present)
314: attachableItems.add(si);
315: }
316:
317: return attachableItems;
318: }
319:
320: public List getListableItems() {
321: checkUpdateItems();
322: if (listableItems == null) {
323: listableItems = new ArrayList();
324: for (Iterator i = getItems().iterator(); i.hasNext();) {
325: PresentationItem item = (PresentationItem) i.next();
326: listableItems.add(new DecoratedItem(item, this ));
327: }
328: }
329:
330: return listableItems;
331: }
332:
333: public String getCurrentPageId() {
334: return getCurrentPage().getBase().getId().getValue();
335: }
336:
337: public void setCurrentPageId(String pageId) {
338: nextPageId = pageId;
339: }
340:
341: public List getPageDropList() {
342: List base = getPageList();
343: List returned = new ArrayList();
344: for (Iterator i = base.iterator(); i.hasNext();) {
345: DecoratedPage page = (DecoratedPage) i.next();
346: returned.add(createSelect(
347: page.getBase().getId().getValue(), page.getBase()
348: .getTitle()));
349: }
350: return returned;
351: }
352:
353: public void setLayouts(List layouts) {
354: this .layouts = layouts;
355: }
356:
357: public IdManager getIdManager() {
358: return idManager;
359: }
360:
361: public void setIdManager(IdManager idManager) {
362: this .idManager = idManager;
363: }
364:
365: public String processActionNewPage() {
366: PresentationPage page = new PresentationPage();
367:
368: page.setNewObject(true);
369: page.setId(getIdManager().createId());
370: page.setPresentation(getPresentation());
371: page.setRegions(new HashSet());
372: getPresentation().getPages().add(page);
373: reorderPages();
374: DecoratedPage decoratedPage = new DecoratedPage(page, this );
375: decoratedPage.setNewPage(true);
376: setCurrentPage(decoratedPage);
377: return "edit";
378: }
379:
380: protected void reorderPages() {
381: int index = 0;
382: for (Iterator i = presentation.getPages().iterator(); i
383: .hasNext();) {
384: PresentationPage page = (PresentationPage) i.next();
385: page.setSequence(index);
386: index++;
387: }
388: pageList = null;
389: attachableItems = null; // make sure list gets re-created in order
390: }
391:
392: public String processRemoveSelectedPages() {
393: List localPageList = pageList;
394:
395: for (Iterator i = localPageList.iterator(); i.hasNext();) {
396: DecoratedPage page = (DecoratedPage) i.next();
397: if (page.isSelected()) {
398: deletePage(page);
399: }
400: }
401:
402: reorderPages();
403: return "main";
404: }
405:
406: public void deletePage(DecoratedPage page) {
407: getPresentation().getPages().remove(page.getBase());
408: pageList = null;
409: attachableItems = null; // make sure list gets re-created in order
410: }
411:
412: public String processChangeCurrentPage() {
413: List base = getPageList();
414: for (Iterator i = base.iterator(); i.hasNext();) {
415: DecoratedPage page = (DecoratedPage) i.next();
416: if (page.getBase().getId().getValue().equals(nextPageId)) {
417: setCurrentPage(page);
418: break;
419: }
420: }
421: return "arrange";
422: }
423:
424: public String processActionSelectStyle() {
425:
426: ExternalContext context = FacesContext.getCurrentInstance()
427: .getExternalContext();
428: ToolSession session = SessionManager.getCurrentToolSession();
429: session.removeAttribute(StyleHelper.CURRENT_STYLE);
430: session.removeAttribute(StyleHelper.CURRENT_STYLE_ID);
431:
432: session.setAttribute(StyleHelper.STYLE_SELECTABLE, "true");
433: if (presentation.getStyle() != null)
434: session.setAttribute(StyleHelper.CURRENT_STYLE_ID,
435: presentation.getStyle().getId().getValue());
436:
437: try {
438: context.redirect("osp.style.helper/listStyle");
439: } catch (IOException e) {
440: throw new RuntimeException("Failed to redirect to helper",
441: e);
442: }
443: return null;
444: }
445:
446: public int getStep() {
447: return step;
448: }
449:
450: public String getStepString() {
451: return "" + (step);
452: }
453:
454: public int getPageCount() {
455: return getPageList().size();
456: }
457:
458: public String getStyleName() {
459: ToolSession session = SessionManager.getCurrentToolSession();
460: if (session.getAttribute(StyleHelper.CURRENT_STYLE) != null) {
461: Style style = (Style) session
462: .getAttribute(StyleHelper.CURRENT_STYLE);
463: presentation.setStyle(style);
464: } else if (session.getAttribute(StyleHelper.UNSELECTED_STYLE) != null) {
465: presentation.setStyle(null);
466: session.removeAttribute(StyleHelper.UNSELECTED_STYLE);
467: return "";
468: }
469:
470: if (presentation.getStyle() != null)
471: return presentation.getStyle().getName();
472: return "";
473: }
474:
475: /**
476: * @return the contentHosting
477: */
478: public ContentHostingService getContentHosting() {
479: return contentHosting;
480: }
481:
482: /**
483: * @param contentHosting the contentHosting to set
484: */
485: public void setContentHosting(ContentHostingService contentHosting) {
486: this .contentHosting = contentHosting;
487: }
488:
489: /**
490: * @return The url (String) to the portfolio preivew
491: */
492: public String getPreviewUrl() {
493: Tool tool = ToolManager.getCurrentTool();
494: ToolSession session = SessionManager.getCurrentToolSession();
495: // Making a significant assumption here... is there a better way to get the url for this?
496: return (String) session.getAttribute(tool.getId()
497: + Tool.HELPER_DONE_URL)
498: + "/../viewPresentation.osp";
499: }
500:
501: }
|