001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/AddTemplateController.java $
003: * $Id:AddTemplateController.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.control;
021:
022: import java.text.SimpleDateFormat;
023: import java.util.*;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.sakaiproject.component.cover.ComponentManager;
029: import org.sakaiproject.content.api.ContentHostingService;
030: import org.sakaiproject.content.api.FilePickerHelper;
031: import org.sakaiproject.entity.api.EntityManager;
032: import org.sakaiproject.entity.api.Reference;
033: import org.sakaiproject.metaobj.security.AuthenticationManager;
034: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
035: import org.sakaiproject.metaobj.shared.mgt.IdManager;
036: import org.sakaiproject.metaobj.shared.mgt.ReadableObjectHome;
037: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactDefinitionManager;
038: import org.sakaiproject.metaobj.shared.model.Agent;
039: import org.sakaiproject.metaobj.shared.model.Id;
040: import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
041: import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;
042: import org.sakaiproject.metaobj.utils.xml.SchemaFactory;
043: import org.sakaiproject.metaobj.utils.xml.SchemaInvalidException;
044: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
045: import org.sakaiproject.metaobj.worksite.mgt.WorksiteManager;
046: import org.sakaiproject.tool.api.Placement;
047: import org.sakaiproject.tool.api.SessionManager;
048: import org.sakaiproject.tool.api.ToolSession;
049: import org.sakaiproject.tool.cover.ToolManager;
050: import org.springframework.beans.propertyeditors.CustomDateEditor;
051: import org.springframework.validation.BindException;
052: import org.springframework.validation.Errors;
053: import org.springframework.web.bind.ServletRequestDataBinder;
054: import org.springframework.web.servlet.ModelAndView;
055: import org.springframework.web.servlet.mvc.AbstractWizardFormController;
056: import org.theospi.portfolio.presentation.PresentationFunctionConstants;
057: import org.theospi.portfolio.presentation.PresentationManager;
058: import org.theospi.portfolio.presentation.model.PresentationItemDefinition;
059: import org.theospi.portfolio.presentation.model.PresentationTemplate;
060: import org.theospi.portfolio.presentation.model.TemplateFileRef;
061: import org.theospi.portfolio.security.AuthorizationFacade;
062: import org.theospi.portfolio.shared.model.Node;
063: import org.theospi.portfolio.shared.model.CommonFormBean;
064:
065: public class AddTemplateController extends AbstractWizardFormController {
066: final public static int DESCRIBE_PAGE = 0;
067: final public static int TEMPLATE_PAGE = 1;
068: final public static int CONTENT_PAGE = 2;
069: final public static int FILES_PAGE = 3;
070: final public static int PICKER_PAGE = 4;
071:
072: public static final String TEMPLATE_RENDERER = "osp.presentation.template.renderer";
073: public static final String TEMPLATE_PROPERTYFILE = "osp.presentation.template.propertyFile";
074: public static final String TEMPLATE_SUPPORTFILE = "osp.presentation.template.supportFile";
075: public static final String TEMPLATE_PICKER = "osp.presentation.template.picker";
076: private static final String STARTING_PAGE = "osp.presentation.template.startingPage";
077:
078: private WorksiteManager worksiteManager;
079: private AuthenticationManager authManager;
080: private PresentationManager presentationManager;
081: private List customTypedEditors;
082: private AuthorizationFacade authzManager;
083: private IdManager idManager;
084: private HomeFactory homeFactory;
085: private Collection mimeTypes;
086: private SessionManager sessionManager;
087: private ContentHostingService contentHosting;
088: private EntityManager entityManager;
089: private StructuredArtifactDefinitionManager structuredArtifactDefinitionManager;
090:
091: public static Comparator worksiteHomesComparator;
092: static {
093: worksiteHomesComparator = new Comparator() {
094: public int compare(Object o1, Object o2) {
095: return ((ReadableObjectHome) o1)
096: .getType()
097: .getDescription()
098: .toLowerCase()
099: .compareTo(
100: ((ReadableObjectHome) o2).getType()
101: .getDescription().toLowerCase());
102: }
103: };
104: }
105:
106: protected ModelAndView processFinish(HttpServletRequest request,
107: HttpServletResponse response, Object o, BindException e)
108: throws Exception {
109: PresentationTemplate template = (PresentationTemplate) o;
110: Agent agent = getAuthManager().getAgent();
111: template.setOwner(agent);
112: template.setSiteId(ToolManager.getCurrentPlacement()
113: .getContext());
114:
115: // remove id's from new dependent object, so hibernate doesn't freak out
116: removeTemporaryIds(template);
117:
118: template = getPresentationManager().storeTemplate(template);
119:
120: Map model = new Hashtable();
121: model.put("newPresentationTemplateId", template.getId()
122: .getValue());
123:
124: return new ModelAndView("listTemplateRedirect", model);
125: }
126:
127: protected void removeTemporaryIds(PresentationTemplate template) {
128: PresentationTemplate oldTemplate = new PresentationTemplate();
129: if (template.getId() != null
130: && template.getId().getValue().length() > 0) {
131: oldTemplate = getPresentationManager()
132: .getPresentationTemplate(template.getId());
133: }
134:
135: for (Iterator i = template.getItems().iterator(); i.hasNext();) {
136: PresentationItemDefinition item = (PresentationItemDefinition) i
137: .next();
138: if (!oldTemplate.getItems().contains(item)) {
139: item.setId(null);
140: }
141: }
142:
143: for (Iterator i = template.getFiles().iterator(); i.hasNext();) {
144: TemplateFileRef file = (TemplateFileRef) i.next();
145: if (!oldTemplate.getFiles().contains(file)) {
146: file.setId(null);
147: }
148: }
149: }
150:
151: public Object formBackingObject(HttpServletRequest request)
152: throws Exception {
153: PresentationTemplate template = new PresentationTemplate();
154:
155: // this is an edit, load model
156: if (request.getParameter("id") != null) {
157: Id id = getIdManager().getId(request.getParameter("id"));
158: getAuthzManager().checkPermission(
159: PresentationFunctionConstants.EDIT_TEMPLATE, id);
160: template = getPresentationManager()
161: .getPresentationTemplate(id);
162: } else {
163: getAuthzManager().checkPermission(
164: PresentationFunctionConstants.CREATE_TEMPLATE,
165: getIdManager().getId(
166: ToolManager.getCurrentPlacement()
167: .getContext()));
168: template.setNewObject(true);
169: }
170: ToolSession session = getSessionManager()
171: .getCurrentToolSession();
172: if (session.getAttribute("SessionPresentationTemplate") != null) {
173: template = (PresentationTemplate) session
174: .getAttribute("SessionPresentationTemplate");
175: session.removeAttribute("SessionPresentationTemplate");
176: request.setAttribute(STARTING_PAGE, Integer
177: .valueOf((String) session
178: .getAttribute(STARTING_PAGE)));
179: session.removeAttribute(STARTING_PAGE);
180: }
181:
182: return template;
183: }
184:
185: protected ModelAndView processCancel(HttpServletRequest request,
186: HttpServletResponse response, Object command,
187: BindException errors) {
188: PresentationTemplate template = (PresentationTemplate) command;
189: Map model = new Hashtable();
190: if (template.getId() != null) {
191: model.put("newPresentationTemplateId", template.getId()
192: .getValue());
193: }
194:
195: return new ModelAndView("listTemplateRedirect", model);
196: }
197:
198: protected void onBindAndValidate(
199: javax.servlet.http.HttpServletRequest request,
200: java.lang.Object command, BindException errors, int page)
201: throws java.lang.Exception {
202:
203: }
204:
205: protected void validatePage(Object model, Errors errors, int page) {
206: PresentationValidator validator = (PresentationValidator) getValidator();
207: switch (page) {
208: case DESCRIBE_PAGE:
209: validator.validateTemplateFirstPage(model, errors);
210: break;
211: case TEMPLATE_PAGE:
212: if (((PresentationTemplate) model).isValidate()) {
213: validator.validateTemplateSecondPage(model, errors);
214: }
215: break;
216: case CONTENT_PAGE:
217: validator.validateTemplateThirdPage(model, errors);
218: break;
219: case FILES_PAGE:
220: validator.validateTemplateFourthPage(model, errors);
221: break;
222: }
223: }
224:
225: protected Collection getFormsForSelect(String type) {
226: Placement placement = ToolManager.getCurrentPlacement();
227: String currentSiteId = placement.getContext();
228: Collection commentForms = getAvailableForms(currentSiteId, type);
229:
230: List retForms = new ArrayList();
231: for (Iterator iter = commentForms.iterator(); iter.hasNext();) {
232: StructuredArtifactDefinitionBean sad = (StructuredArtifactDefinitionBean) iter
233: .next();
234: retForms.add(new CommonFormBean(sad.getId().getValue(), sad
235: .getDecoratedDescription(), "form", sad.getOwner()
236: .getName(), sad.getModified()));
237: }
238:
239: Collections.sort(retForms, CommonFormBean.beanComparator);
240: return retForms;
241: }
242:
243: protected Collection getAvailableForms(String siteId, String type) {
244: return getStructuredArtifactDefinitionManager().findHomes(
245: getIdManager().getId(siteId), true);
246: }
247:
248: protected Map referenceData(HttpServletRequest request,
249: Object command, Errors errors, int page) throws Exception {
250: Map model = new HashMap();
251: PresentationTemplate template = (PresentationTemplate) command;
252: model.put("currentPage", new Integer(page + 1));
253: model.put("totalPages", new Integer(4));
254: model.put("template", template);
255: ToolSession session = getSessionManager()
256: .getCurrentToolSession();
257:
258: model.put("STARTING_PAGE", STARTING_PAGE);
259:
260: switch (page) {
261: case DESCRIBE_PAGE:
262: break;
263: case TEMPLATE_PAGE:
264: model.put("TEMPLATE_RENDERER", TEMPLATE_RENDERER);
265: model.put("TEMPLATE_PROPERTYFILE", TEMPLATE_PROPERTYFILE);
266: //ToolSession session = getSessionManager().getCurrentToolSession();
267: if (session
268: .getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
269: && session
270: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
271: // here is where we setup the id
272: List refs = (List) session
273: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
274:
275: Id nodeId = null;
276: String nodeName = "";
277:
278: if (refs.size() == 1) {
279: Reference ref = (Reference) refs.get(0);
280: Node node = getPresentationManager().getNode(ref);
281: nodeId = node.getId();
282: nodeName = node.getDisplayName();
283: }
284: if (session.getAttribute(TEMPLATE_PICKER).equals(
285: TEMPLATE_RENDERER)) {
286: template.setRendererName(nodeName);
287: template.setRenderer(nodeId);
288: } else {
289: template.setPropertyPageName(nodeName);
290: template.setPropertyPage(nodeId);
291: }
292:
293: session.removeAttribute(TEMPLATE_PICKER);
294: session
295: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
296: session
297: .removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
298: }
299:
300: if (template.getRenderer() != null) {
301: Node artifact = (Node) getPresentationManager()
302: .getNode(template.getRenderer());
303: model.put("rendererName", artifact.getDisplayName());
304: }
305: if (template.getPropertyPage() != null) {
306: Node artifact = (Node) getPresentationManager()
307: .getNode(template.getPropertyPage());
308: SchemaNode schemaNode;
309: try {
310: schemaNode = SchemaFactory.getInstance().getSchema(
311: artifact.getInputStream());
312: model.put("propertyPageName", artifact
313: .getDisplayName());
314: model.put("elements", schemaNode.getRootChildren());
315: } catch (SchemaInvalidException e) {
316: template.setPropertyPage(null);
317: String errorMessage = "Invalid outline properties file: "
318: + e.getMessage();
319: errors.rejectValue("propertyPage", errorMessage,
320: errorMessage);
321: }
322: }
323:
324: model.put("propertyFormTypes", getFormsForSelect(null));
325: break;
326: case CONTENT_PAGE:
327: Collection mimeTypes = getMimeTypes();
328: model
329: .put("mimeTypeListSize", new Integer(mimeTypes
330: .size()));
331: model.put("mimeTypeList", mimeTypes);
332: model.put("homes", getHomes());
333: break;
334: case FILES_PAGE:
335: model.put("TEMPLATE_SUPPORTFILE", TEMPLATE_SUPPORTFILE);
336:
337: if (session
338: .getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
339: && session
340: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
341: // here is where we setup the id
342:
343: String fileId = "";
344: String nodeName = "";
345: String fileType = "";
346: List refs = (List) session
347: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
348: if (refs.size() == 1) {
349: Reference ref = (Reference) refs.get(0);
350: Node node = getPresentationManager().getNode(ref);
351: fileId = node.getId().getValue();
352: nodeName = node.getDisplayName();
353: fileType = node.getFileType();
354: }
355: if (session.getAttribute(TEMPLATE_PICKER).equals(
356: TEMPLATE_SUPPORTFILE)) {
357: template.getFileRef().setFileId(fileId);
358: template.getFileRef().setArtifactName(nodeName);
359: template.getFileRef().setFileType(fileType);
360: }
361:
362: session.removeAttribute(TEMPLATE_PICKER);
363: session
364: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
365: session
366: .removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
367: }
368: break;
369: case PICKER_PAGE:
370: session.setAttribute(TEMPLATE_PICKER, request
371: .getParameter("pickerField"));
372: session.setAttribute("SessionPresentationTemplate",
373: template);
374: session.setAttribute(STARTING_PAGE, request
375: .getParameter("returnPage"));
376:
377: List files = new ArrayList();
378: String filter = "";
379:
380: String pickField = (String) request
381: .getParameter("pickerField");
382: String id = "";
383: if (pickField.equals(TEMPLATE_RENDERER)) {
384: filter = "org.sakaiproject.content.api.ContentResourceFilter.xslFile";
385: if (template.getRenderer() != null) {
386: id = getContentHosting().resolveUuid(
387: template.getRenderer().getValue());
388: }
389: } else if (pickField.equals(TEMPLATE_PROPERTYFILE)) {
390: filter = "org.sakaiproject.content.api.ContentResourceFilter.metaobjFile";
391: if (template.getPropertyPage() != null) {
392: id = getContentHosting().resolveUuid(
393: template.getPropertyPage().getValue());
394: }
395: } else if (pickField.equals(TEMPLATE_SUPPORTFILE)
396: && template.getFileRef() != null
397: && template.getFileRef().getFileId() != null) {
398: id = getContentHosting().resolveUuid(
399: template.getFileRef().getFileId());
400: }
401: if (id != null && !id.equals("")) {
402: Reference ref = getEntityManager().newReference(
403: getContentHosting().getResource(id)
404: .getReference());
405: files.add(ref);
406: session
407: .setAttribute(
408: FilePickerHelper.FILE_PICKER_ATTACHMENTS,
409: files);
410: }
411:
412: if (!filter.equals(""))
413: session.setAttribute(
414: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
415: ComponentManager.get(filter));
416: else
417: session
418: .removeAttribute(FilePickerHelper.FILE_PICKER_RESOURCE_FILTER);
419:
420: session.setAttribute(
421: FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
422: new Integer(1));
423:
424: break;
425: }
426: return model;
427:
428: }
429:
430: protected Collection getHomes() {
431: ArrayList list = new ArrayList();
432: Map homeMap = getHomeFactory().getWorksiteHomes(
433: getWorksiteManager().getCurrentWorksiteId(), true);
434: for (Iterator i = homeMap.values().iterator(); i.hasNext();) {
435: list.add(i.next());
436: }
437: Collections.sort(list, worksiteHomesComparator);
438: return list;
439: }
440:
441: protected void initBinder(HttpServletRequest request,
442: ServletRequestDataBinder binder) {
443: SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
444: dateFormat.setLenient(false);
445: binder.registerCustomEditor(Date.class, null,
446: new CustomDateEditor(dateFormat, true));
447:
448: for (Iterator i = getCustomTypedEditors().iterator(); i
449: .hasNext();) {
450: TypedPropertyEditor editor = (TypedPropertyEditor) i.next();
451: binder.registerCustomEditor(editor.getType(), editor);
452: }
453: }
454:
455: protected int getTargetPage(HttpServletRequest request,
456: Object command, Errors errors, int currentPage) {
457: int retVal = super .getTargetPage(request, command, errors,
458: currentPage);
459: if (isFormSubmission(request)) {
460: onSubmit(request, command, errors, currentPage);
461: }
462: return retVal;
463: }
464:
465: protected int getInitialPage(HttpServletRequest request,
466: Object command) {
467: Integer startingPage = (Integer) request
468: .getAttribute(STARTING_PAGE);
469: if (startingPage != null) {
470: request.removeAttribute(STARTING_PAGE);
471: return startingPage.intValue();
472: } else {
473: return super .getInitialPage(request, command);
474: }
475: }
476:
477: /**
478: * perform page specific business logic after bind and validate
479: * @param request
480: * @param command
481: * @param errors
482: * @param currentPage - page just submitted
483: */
484: protected void onSubmit(HttpServletRequest request, Object command,
485: Errors errors, int currentPage) {
486: PresentationTemplate template = (PresentationTemplate) command;
487: switch (currentPage) {
488: case CONTENT_PAGE:
489: // save add item to backing object
490: if (template.getItem().getAction() != null
491: && template.getItem().getAction().equalsIgnoreCase(
492: "addItem") && !errors.hasErrors()) {
493:
494: PresentationItemDefinition itemDefinition = template
495: .getItem();
496: if (itemDefinition.getId() == null
497: || itemDefinition.getId().getValue().length() == 0) {
498: itemDefinition.setId(getIdManager().createId());
499: }
500: itemDefinition.setPresentationTemplate(template);
501: template.getItemDefinitions().remove(itemDefinition);
502: if (itemDefinition.getSequence() == -1) {
503: itemDefinition.setSequence(Integer.MAX_VALUE);
504: }
505: template.getItemDefinitions().add(itemDefinition);
506: template.setItem(new PresentationItemDefinition());
507: template.orderItemDefs();
508: }
509: break;
510: case FILES_PAGE:
511: if (template.getFileRef().getAction() != null
512: && template.getFileRef().getAction()
513: .equalsIgnoreCase("addFile")
514: && !errors.hasErrors()) {
515:
516: TemplateFileRef file = (TemplateFileRef) template
517: .getFileRef();
518: file.setPresentationTemplate(template);
519: if (file.getId() == null
520: || file.getId().getValue().length() == 0) {
521: file.setId(getIdManager().createId());
522: }
523: template.getFiles().remove(file);
524: template.getFiles().add(file);
525: template.setFileRef(new TemplateFileRef());
526: }
527: break;
528: }
529: }
530:
531: public String getFormAttributeName() {
532: return getFormSessionAttributeName();
533: }
534:
535: protected boolean isFormSubmission(HttpServletRequest request) {
536: if (request.getParameter("formSubmission") != null
537: && request.getParameter("formSubmission")
538: .equalsIgnoreCase("true")) {
539: return true;
540: }
541: return super .isFormSubmission(request);
542: }
543:
544: public Collection getMimeTypes() {
545: return mimeTypes;
546: }
547:
548: public void setMimeTypes(Collection mimeTypes) {
549: this .mimeTypes = mimeTypes;
550: }
551:
552: public HomeFactory getHomeFactory() {
553: return homeFactory;
554: }
555:
556: public void setHomeFactory(HomeFactory homeFactory) {
557: this .homeFactory = homeFactory;
558: }
559:
560: public IdManager getIdManager() {
561: return idManager;
562: }
563:
564: public void setIdManager(IdManager idManager) {
565: this .idManager = idManager;
566: }
567:
568: public AuthorizationFacade getAuthzManager() {
569: return authzManager;
570: }
571:
572: public void setAuthzManager(AuthorizationFacade authzManager) {
573: this .authzManager = authzManager;
574: }
575:
576: public WorksiteManager getWorksiteManager() {
577: return worksiteManager;
578: }
579:
580: public List getCustomTypedEditors() {
581: return customTypedEditors;
582: }
583:
584: public void setCustomTypedEditors(List customTypedEditors) {
585: this .customTypedEditors = customTypedEditors;
586: }
587:
588: public void setWorksiteManager(WorksiteManager worksiteManager) {
589: this .worksiteManager = worksiteManager;
590: }
591:
592: public AuthenticationManager getAuthManager() {
593: return authManager;
594: }
595:
596: public void setAuthManager(AuthenticationManager authManager) {
597: this .authManager = authManager;
598: }
599:
600: public PresentationManager getPresentationManager() {
601: return presentationManager;
602: }
603:
604: public void setPresentationManager(
605: PresentationManager presentationManager) {
606: this .presentationManager = presentationManager;
607: }
608:
609: public SessionManager getSessionManager() {
610: return sessionManager;
611: }
612:
613: public void setSessionManager(SessionManager sessionManager) {
614: this .sessionManager = sessionManager;
615: }
616:
617: public ContentHostingService getContentHosting() {
618: return contentHosting;
619: }
620:
621: public void setContentHosting(ContentHostingService contentHosting) {
622: this .contentHosting = contentHosting;
623: }
624:
625: public EntityManager getEntityManager() {
626: return entityManager;
627: }
628:
629: public void setEntityManager(EntityManager entityManager) {
630: this .entityManager = entityManager;
631: }
632:
633: /**
634: * @return the structuredArtifactDefinitionManager
635: */
636: public StructuredArtifactDefinitionManager getStructuredArtifactDefinitionManager() {
637: return structuredArtifactDefinitionManager;
638: }
639:
640: /**
641: * @param structuredArtifactDefinitionManager the structuredArtifactDefinitionManager to set
642: */
643: public void setStructuredArtifactDefinitionManager(
644: StructuredArtifactDefinitionManager structuredArtifactDefinitionManager) {
645: this.structuredArtifactDefinitionManager = structuredArtifactDefinitionManager;
646: }
647: }
|