001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/CellFormPickerController.java $
003: * $Id: CellFormPickerController.java 21439 2007-02-14 21:25:46Z jmpease@syr.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006, 2007 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.matrix.control;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.ResourceBundle;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.authz.api.SecurityService;
031: import org.sakaiproject.content.api.ContentCollection;
032: import org.sakaiproject.content.api.ContentCollectionEdit;
033: import org.sakaiproject.content.api.ContentHostingService;
034: import org.sakaiproject.content.api.FilePickerHelper;
035: import org.sakaiproject.content.api.ResourceEditingHelper;
036: import org.sakaiproject.entity.api.EntityManager;
037: import org.sakaiproject.entity.api.Reference;
038: import org.sakaiproject.entity.api.ResourceProperties;
039: import org.sakaiproject.exception.IdUnusedException;
040: import org.sakaiproject.exception.IdUsedException;
041: import org.sakaiproject.exception.PermissionException;
042: import org.sakaiproject.exception.TypeException;
043: import org.sakaiproject.metaobj.shared.FormHelper;
044: import org.sakaiproject.metaobj.shared.model.Id;
045: import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
046: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
047: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
048: import org.sakaiproject.site.cover.SiteService;
049: import org.sakaiproject.tool.api.Placement;
050: import org.sakaiproject.tool.cover.ToolManager;
051: import org.sakaiproject.user.api.User;
052: import org.sakaiproject.user.cover.UserDirectoryService;
053: import org.springframework.validation.Errors;
054: import org.springframework.web.servlet.ModelAndView;
055: import org.theospi.portfolio.matrix.model.WizardPage;
056: import org.theospi.portfolio.matrix.model.WizardPageForm;
057: import org.theospi.portfolio.security.AllowMapSecurityAdvisor;
058: import org.theospi.portfolio.shared.model.Node;
059: import org.theospi.portfolio.shared.tool.BaseFormResourceFilter;
060:
061: public class CellFormPickerController extends CellController implements
062: FormController, LoadObjectController {
063:
064: protected final Log logger = LogFactory.getLog(getClass());
065: private ContentHostingService contentHosting;
066: private EntityManager entityManager;
067: private SecurityService securityService = null;
068:
069: public static final String HELPER_CREATOR = "filepicker.helper.creator";
070: public static final String HELPER_PICKER = "filepicker.helper.picker";
071:
072: /*
073: public Map referenceData(Map request, Object command, Errors errors) {
074:
075:
076: ToolSession session = getSessionManager().getCurrentToolSession();
077: String pageId = (String) request.get("page_id");
078: if (pageId == null) {
079: pageId = (String)session.getAttribute("page_id");
080: }
081: WizardPage page = getMatrixManager().getWizardPage(getIdManager().getId(pageId));
082:
083: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null &&
084: session.getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
085: // here is where we setup the id
086: List refs = (List)session.getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
087: page.getPageForms().clear();
088: for (Iterator iter = refs.iterator(); iter.hasNext();) {
089: Reference ref = (Reference) iter.next();
090: String strId = getMatrixManager().getNode(ref).getId().getValue();
091: page.getPageForms().add(strId);
092: }
093: getMatrixManager().storePage(page);
094:
095: session.removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
096: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
097: }
098: return null;
099: }
100: */
101:
102: public Object fillBackingObject(Object incomingModel, Map request,
103: Map session, Map application) throws Exception {
104:
105: String pageId = (String) request.get("page_id");
106: if (pageId == null) {
107: pageId = (String) session.get("page_id");
108: }
109: WizardPage page = getMatrixManager().getWizardPage(
110: getIdManager().getId(pageId));
111: if (page == null) // error should already be logged
112: return null;
113:
114: if ((session.get(FilePickerHelper.FILE_PICKER_CANCEL) == null && session
115: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null)
116: || (FormHelper.RETURN_ACTION_SAVE
117: .equals((String) session
118: .get(FormHelper.RETURN_ACTION_TAG)) && session
119: .get(FormHelper.RETURN_REFERENCE_TAG) != null)) {
120:
121: if (session.get(FilePickerHelper.FILE_PICKER_CANCEL) == null
122: && session
123: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
124: // here is where we setup the id
125: List refs = (List) session
126: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
127: //if (session.get(WHICH_HELPER_KEY).equals(HELPER_PICKER))
128: if (HELPER_PICKER.equals((String) session
129: .get(WHICH_HELPER_KEY))
130: && !"true".equals((String) session
131: .get(KEEP_HELPER_LIST)))
132: page.getPageForms().clear();
133:
134: for (Iterator iter = refs.iterator(); iter.hasNext();) {
135: Reference ref = (Reference) iter.next();
136: Node node = getMatrixManager().getNode(ref);
137: processPageForm(node, page);
138: }
139: //getMatrixManager().storePage(page);
140:
141: session
142: .remove(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
143: session.remove(FilePickerHelper.FILE_PICKER_CANCEL);
144:
145: }
146: if (FormHelper.RETURN_ACTION_SAVE.equals((String) session
147: .get(FormHelper.RETURN_ACTION_TAG))
148: && session.get(FormHelper.RETURN_REFERENCE_TAG) != null) {
149: String artifactId = (String) session
150: .get(FormHelper.RETURN_REFERENCE_TAG);
151: Node node = getMatrixManager().getNode(
152: getIdManager().getId(artifactId));
153: processPageForm(node, page);
154:
155: session.remove(FormHelper.RETURN_REFERENCE_TAG);
156: session.remove(FormHelper.RETURN_ACTION_TAG);
157:
158: }
159: getMatrixManager().storePage(page);
160: session.remove(ResourceEditingHelper.CREATE_TYPE);
161:
162: session.remove(ResourceEditingHelper.CREATE_PARENT);
163: session.remove(ResourceEditingHelper.CREATE_SUB_TYPE);
164: session.remove(ResourceEditingHelper.ATTACHMENT_ID);
165: session.remove(WHICH_HELPER_KEY);
166: session.remove(KEEP_HELPER_LIST);
167: }
168: return null;
169: }
170:
171: protected void processPageForm(Node node, WizardPage page) {
172: Id id = node.getId();
173: WizardPageForm wpf = new WizardPageForm();
174: wpf.setArtifactId(id);
175: wpf.setFormType(node.getResource().getProperties().getProperty(
176: node.getResource().getProperties()
177: .getNamePropStructObjType()));
178: wpf.setWizardPage(page);
179: wpf.setNewId(getIdManager().createId());
180: page.getPageForms().add(wpf);
181: }
182:
183: public ModelAndView handleRequest(Object requestModel, Map request,
184: Map session, Map application, Errors errors) {
185: String attachFormAction = (String) request
186: .get("attachFormAction");
187: String createFormAction = (String) request
188: .get("createFormAction");
189: String viewFormAction = (String) request.get("viewFormAction");
190: String pageId = (String) request.get("page_id");
191: if (pageId == null) {
192: pageId = (String) session.get("page_id");
193: session.remove("page_id");
194: }
195: WizardPage page = getMatrixManager().getWizardPage(
196: getIdManager().getId(pageId));
197: String pageTitle = page.getPageDefinition().getTitle();
198:
199: if (attachFormAction != null) {
200: //session.setAttribute(TEMPLATE_PICKER, request.getParameter("pickerField"));
201: //session.setAttribute("SessionPresentationTemplate", template);
202: //session.setAttribute(STARTING_PAGE, request.getParameter("returnPage"));
203:
204: List<Reference> files = new ArrayList<Reference>();
205:
206: //String pickField = (String)request.get("formType");
207: String id = "";
208: for (Iterator iter = page.getPageForms().iterator(); iter
209: .hasNext();) {
210: WizardPageForm wpf = (WizardPageForm) iter.next();
211: if (attachFormAction.equals(wpf.getFormType())) {
212: id = getContentHosting().resolveUuid(
213: wpf.getArtifactId().getValue());
214: Reference ref = getEntityManager().newReference(
215: getContentHosting().getReference(id));
216: files.add(ref);
217: }
218: }
219: BaseFormResourceFilter crf = new BaseFormResourceFilter();
220:
221: crf.getFormTypes().add(attachFormAction);
222: session.put(FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
223: crf);
224: session.put("page_id", pageId);
225: session
226: .put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
227: files);
228: session.put(WHICH_HELPER_KEY, HELPER_PICKER);
229: session.put(KEEP_HELPER_LIST, "false");
230:
231: //Start in user's resources area
232: //osp-ui-05
233: String siteId = SiteService
234: .getUserSiteId(getSessionManager()
235: .getCurrentSessionUserId());
236: String collectionId = getContentHosting()
237: .getSiteCollection(siteId);
238: session.put(FilePickerHelper.DEFAULT_COLLECTION_ID,
239: collectionId);
240:
241: return new ModelAndView("formPicker");
242:
243: } else if (createFormAction != null) {
244: String view = setupSessionInfo(request, session, pageId,
245: pageTitle, createFormAction);
246: session.put(WHICH_HELPER_KEY, HELPER_CREATOR);
247: return new ModelAndView(view);
248: } else if (viewFormAction != null) {
249: setupSessionInfo(request, session, pageId, pageTitle,
250: viewFormAction);
251: getSecurityService().pushAdvisor(
252: new AllowMapSecurityAdvisor(
253: ContentHostingService.EVENT_RESOURCE_READ,
254: (String) request.get("current_form_id")));
255: return new ModelAndView("formViewer");
256: }
257: session.remove(FilePickerHelper.FILE_PICKER_RESOURCE_FILTER);
258: return new ModelAndView("page", "page_id", pageId);
259: }
260:
261: protected String setupSessionInfo(Map request,
262: Map<String, Object> session, String pageId,
263: String pageTitle, String formTypeId) {
264: String retView = "formCreator";
265: //session.put(ResourceEditingHelper.CREATE_TYPE,
266: // ResourceEditingHelper.CREATE_TYPE_FORM);
267: session.put("page_id", pageId);
268:
269: if (request.get("current_form_id") == null) {
270: session.remove(ResourceEditingHelper.ATTACHMENT_ID);
271: session.put(ResourceEditingHelper.CREATE_TYPE,
272: ResourceEditingHelper.CREATE_TYPE_FORM);
273: session.put(ResourceEditingHelper.CREATE_SUB_TYPE,
274: formTypeId);
275:
276: String objectId = (String) request.get("objectId");
277: String objectTitle = (String) request.get("objectTitle");
278: String objectDesc = (String) request.get("objectDesc");
279:
280: StructuredArtifactDefinitionBean bean = getStructuredArtifactDefinitionManager()
281: .loadHome(formTypeId);
282: ResourceBundle myResources = ResourceBundle
283: .getBundle("org.theospi.portfolio.matrix.bundle.Messages");
284: try {
285: String folderBase = getUserCollection().getId();
286:
287: Placement placement = ToolManager.getCurrentPlacement();
288: String currentSite = placement.getContext();
289:
290: String rootDisplayName = myResources
291: .getString("portfolioInteraction.displayName");
292: String rootDescription = myResources
293: .getString("portfolioInteraction.description");
294:
295: String folderPath = createFolder(folderBase,
296: "portfolio-interaction", rootDisplayName,
297: rootDescription);
298: folderPath = createFolder(folderPath, currentSite,
299: SiteService.getSiteDisplay(currentSite), null);
300: folderPath = createFolder(folderPath, objectId,
301: objectTitle, objectDesc);
302: folderPath = createFolder(folderPath, formTypeId, bean
303: .getDescription(), null);
304:
305: session.put(FormHelper.PARENT_ID_TAG, folderPath);
306: } catch (TypeException e) {
307: throw new RuntimeException(
308: "Failed to redirect to helper", e);
309: } catch (IdUnusedException e) {
310: throw new RuntimeException(
311: "Failed to redirect to helper", e);
312: } catch (PermissionException e) {
313: throw new RuntimeException(
314: "Failed to redirect to helper", e);
315: }
316:
317: //CWM OSP-UI-09 - for auto naming
318: session.put(FormHelper.NEW_FORM_DISPLAY_NAME_TAG,
319: getFormDisplayName(pageTitle, objectTitle, bean
320: .getDescription()));
321: } else {
322: //session.put(ResourceEditingHelper.ATTACHMENT_ID, request.get("current_form_id"));
323: session.remove(ResourceEditingHelper.CREATE_TYPE);
324: session.remove(ResourceEditingHelper.CREATE_SUB_TYPE);
325: session.remove(ResourceEditingHelper.CREATE_PARENT);
326: session.put(ResourceEditingHelper.CREATE_TYPE,
327: ResourceEditingHelper.CREATE_TYPE_FORM);
328: session.put(ResourceEditingHelper.ATTACHMENT_ID, request
329: .get("current_form_id"));
330: retView = "formEditor";
331: }
332: return retView;
333: }
334:
335: protected String getFormDisplayName(String pageTitle,
336: String objectTitle, String formTypeName) {
337: String includePageTitle = "";
338: if (pageTitle != null && pageTitle.length() > 0)
339: includePageTitle = pageTitle + "-";
340:
341: return objectTitle + "-" + includePageTitle + formTypeName;
342: }
343:
344: protected String createFolder(String base, String append,
345: String appendDisplay, String appendDescription) {
346: //String folder = "/user/" +
347: //SessionManager.getCurrentSessionUserId() +
348: //PresentationManager.PRESENTATION_PROPERTIES_FOLDER_PATH;
349: String folder = base + append + "/";
350:
351: try {
352: ContentCollectionEdit propFolder = getContentHosting()
353: .addCollection(folder);
354: propFolder.getPropertiesEdit()
355: .addProperty(ResourceProperties.PROP_DISPLAY_NAME,
356: appendDisplay);
357: propFolder.getPropertiesEdit().addProperty(
358: ResourceProperties.PROP_DESCRIPTION,
359: appendDescription);
360: getContentHosting().commitCollection(propFolder);
361: return propFolder.getId();
362: } catch (IdUsedException e) {
363: // ignore... it is already there.
364: } catch (Exception e) {
365: throw new RuntimeException(e);
366: }
367: return folder;
368: }
369:
370: protected ContentCollection getUserCollection()
371: throws TypeException, IdUnusedException,
372: PermissionException {
373: User user = UserDirectoryService.getCurrentUser();
374: String userId = user.getId();
375: String wsId = SiteService.getUserSiteId(userId);
376: String wsCollectionId = getContentHosting().getSiteCollection(
377: wsId);
378: ContentCollection collection = getContentHosting()
379: .getCollection(wsCollectionId);
380: return collection;
381: }
382:
383: /**
384: * @return Returns the contentHosting.
385: */
386: public ContentHostingService getContentHosting() {
387: return contentHosting;
388: }
389:
390: /**
391: * @param contentHosting The contentHosting to set.
392: */
393: public void setContentHosting(ContentHostingService contentHosting) {
394: this .contentHosting = contentHosting;
395: }
396:
397: /**
398: * @return Returns the entityManager.
399: */
400: public EntityManager getEntityManager() {
401: return entityManager;
402: }
403:
404: /**
405: * @param entityManager The entityManager to set.
406: */
407: public void setEntityManager(EntityManager entityManager) {
408: this .entityManager = entityManager;
409: }
410:
411: public SecurityService getSecurityService() {
412: return securityService;
413: }
414:
415: public void setSecurityService(SecurityService securityService) {
416: this.securityService = securityService;
417: }
418:
419: }
|