001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/ReviewHelperController.java $
003: * $Id: ReviewHelperController.java 29139 2007-04-18 23:27:24Z ajpoland@iupui.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.Arrays;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.ResourceBundle;
028: import java.util.Set;
029:
030: import org.sakaiproject.content.api.ContentCollection;
031: import org.sakaiproject.content.api.ContentCollectionEdit;
032: import org.sakaiproject.content.api.ContentHostingService;
033: import org.sakaiproject.content.api.LockManager;
034: import org.sakaiproject.content.api.ResourceEditingHelper;
035: import org.sakaiproject.entity.api.ResourceProperties;
036: import org.sakaiproject.exception.IdUnusedException;
037: import org.sakaiproject.exception.IdUsedException;
038: import org.sakaiproject.exception.PermissionException;
039: import org.sakaiproject.exception.TypeException;
040: import org.sakaiproject.metaobj.shared.FormHelper;
041: import org.sakaiproject.metaobj.shared.mgt.IdManager;
042: import org.sakaiproject.metaobj.shared.model.Id;
043: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
044: import org.sakaiproject.site.cover.SiteService;
045: import org.sakaiproject.tool.api.Placement;
046: import org.sakaiproject.tool.cover.ToolManager;
047: import org.sakaiproject.user.api.User;
048: import org.sakaiproject.user.cover.UserDirectoryService;
049: import org.springframework.validation.Errors;
050: import org.springframework.web.servlet.ModelAndView;
051: import org.theospi.portfolio.matrix.MatrixManager;
052: import org.theospi.portfolio.matrix.model.WizardPage;
053: import org.theospi.portfolio.review.ReviewHelper;
054: import org.theospi.portfolio.review.mgt.ReviewManager;
055: import org.theospi.portfolio.review.model.Review;
056: import org.theospi.portfolio.shared.model.Node;
057: import org.theospi.portfolio.shared.model.ObjectWithWorkflow;
058: import org.theospi.portfolio.wizard.mgt.WizardManager;
059: import org.theospi.portfolio.wizard.model.CompletedWizard;
060: import org.theospi.portfolio.workflow.model.Workflow;
061:
062: public class ReviewHelperController implements Controller {
063:
064: private MatrixManager matrixManager;
065: private IdManager idManager = null;
066: private ReviewManager reviewManager;
067: private WizardManager wizardManager;
068: private LockManager lockManager;
069: private ContentHostingService contentHosting;
070:
071: public ModelAndView handleRequest(Object requestModel, Map request,
072: Map session, Map application, Errors errors) {
073: String strId = null;
074: String lookupId = null;
075: String returnView = "return";
076: String manager = "";
077:
078: if (request.get("process_type_key") != null) {
079: session.put("process_type_key", request
080: .get("process_type_key"));
081: session.put(ReviewHelper.REVIEW_TYPE_KEY, request
082: .get(ReviewHelper.REVIEW_TYPE_KEY));
083: }
084:
085: String processTypeKey = (String) session
086: .get("process_type_key");
087:
088: if (processTypeKey != null
089: && !processTypeKey.equals(WizardPage.PROCESS_TYPE_KEY)) {
090: lookupId = processTypeKey;
091: returnView = "helperDone";
092: manager = "org.theospi.portfolio.wizard.mgt.WizardManager";
093: } else if (processTypeKey != null) {
094: lookupId = processTypeKey;
095: manager = "matrixManager";
096: }
097: strId = (String) request.get(lookupId);
098: if (strId == null) {
099: strId = (String) session.get(lookupId);
100: }
101:
102: //
103: // If this is the second pass,
104: // then we are creating a new [feedback | evaluation | reflection] review
105: //
106: String secondPass = (String) session.get("secondPass");
107: if (secondPass != null) {
108: strId = (String) session.get(lookupId);
109: String formType = (String) session
110: .get(ResourceEditingHelper.CREATE_SUB_TYPE);
111: String itemId = (String) session
112: .get(ReviewHelper.REVIEW_ITEM_ID);
113:
114: Map<String, Object> model = new HashMap<String, Object>();
115: model.put(lookupId, strId);
116:
117: Placement placement = ToolManager.getCurrentPlacement();
118: String currentSite = placement.getContext();
119:
120: Review review = getReviewManager().createNew("New Review",
121: currentSite);
122: review.setDeviceId(formType);
123: review.setParent(strId);
124: review.setItemId(itemId);
125: String strType = (String) session
126: .get(ReviewHelper.REVIEW_TYPE);
127: review.setType(Integer.parseInt(strType));
128:
129: session.remove(ResourceEditingHelper.CREATE_TYPE);
130: session.remove(ResourceEditingHelper.CREATE_SUB_TYPE);
131: session.remove(ReviewHelper.REVIEW_TYPE);
132: session.remove(ResourceEditingHelper.CREATE_PARENT);
133: session.remove(ReviewHelper.REVIEW_ITEM_ID);
134: session.remove(lookupId);
135: //session.remove("process_type_key");
136: session.remove("secondPass");
137:
138: if (FormHelper.RETURN_ACTION_SAVE.equals((String) session
139: .get(FormHelper.RETURN_ACTION_TAG))
140: && session.get(FormHelper.RETURN_REFERENCE_TAG) != null) {
141: String artifactId = (String) session
142: .get(FormHelper.RETURN_REFERENCE_TAG);
143: Node node = getMatrixManager().getNode(
144: getIdManager().getId(artifactId));
145:
146: review.setReviewContentNode(node);
147: review.setReviewContent(node.getId());
148: getReviewManager().saveReview(review);
149:
150: session.remove(FormHelper.RETURN_REFERENCE_TAG);
151: session.remove(FormHelper.RETURN_ACTION_TAG);
152:
153: if (review.getType() == Review.EVALUATION_TYPE
154: || review.getType() == Review.FEEDBACK_TYPE)
155: getLockManager().lockObject(
156: review.getReviewContent().getValue(),
157: strId, "evals and review always locked",
158: true);
159:
160: if (session
161: .get(ReviewHelper.REVIEW_POST_PROCESSOR_WORKFLOWS) != null) {
162: Set workflows = (Set) session
163: .get(ReviewHelper.REVIEW_POST_PROCESSOR_WORKFLOWS);
164: List wfList = Arrays.asList(workflows.toArray());
165: Collections.sort(wfList, Workflow.getComparator());
166: model.put("workflows", wfList);
167: model.put("manager", manager);
168: model.put("obj_id", strId);
169: return new ModelAndView("postProcessor", model);
170: }
171: }
172: session.remove(FormHelper.RETURN_ACTION_TAG);
173: return new ModelAndView(returnView, model);
174: }
175:
176: //
177: // This is the first pass,
178: // so we are presenting the form to create a new [feedback | evaluation | reflection] review
179: //
180: String ownerEid = null;
181: String pageTitle = null;
182: Id id = getIdManager().getId(strId);
183: ObjectWithWorkflow obj = null;
184: if (lookupId.equals(WizardPage.PROCESS_TYPE_KEY)) {
185: WizardPage page = matrixManager.getWizardPage(id);
186: obj = page.getPageDefinition();
187: pageTitle = page.getPageDefinition().getTitle();
188: ownerEid = page.getOwner().getEid().getValue();
189: } else {
190: CompletedWizard cw = wizardManager.getCompletedWizard(id);
191: obj = cw.getWizard();
192: ownerEid = cw.getOwner().getEid().getValue();
193: }
194:
195: String type = (String) session
196: .get(ReviewHelper.REVIEW_TYPE_KEY);
197: session.remove(ReviewHelper.REVIEW_TYPE_KEY);
198: int intType = Integer.parseInt(type);
199:
200: String formTypeId = "";
201: String formTypeTitleKey = "";
202: session.remove(ReviewHelper.REVIEW_POST_PROCESSOR_WORKFLOWS);
203: switch (intType) {
204: case Review.FEEDBACK_TYPE:
205: formTypeId = obj.getReviewDevice().getValue();
206: formTypeTitleKey = "osp.reviewType." + Review.FEEDBACK_TYPE;
207: break;
208: case Review.EVALUATION_TYPE:
209: formTypeId = obj.getEvaluationDevice().getValue();
210: session.put(ReviewHelper.REVIEW_POST_PROCESSOR_WORKFLOWS,
211: obj.getEvalWorkflows());
212: formTypeTitleKey = "osp.reviewType."
213: + Review.EVALUATION_TYPE;
214: break;
215: case Review.REFLECTION_TYPE:
216: formTypeId = obj.getReflectionDevice().getValue();
217: // set ownerEid to null since we don't need it for the reflection
218: ownerEid = null;
219: formTypeTitleKey = "osp.reviewType."
220: + Review.REFLECTION_TYPE;
221: break;
222: }
223:
224: String formView = "formCreator";
225: session.put(ReviewHelper.REVIEW_TYPE, type);
226: session.put(ResourceEditingHelper.CREATE_TYPE,
227: ResourceEditingHelper.CREATE_TYPE_FORM);
228:
229: formView = setupSessionInfo(request, session, pageTitle,
230: formTypeId, formTypeTitleKey, ownerEid);
231: session.put("page_id", strId);
232: session.put("secondPass", "true");
233: return new ModelAndView(formView);
234:
235: }
236:
237: /**
238: *
239: * @param request
240: * @param session
241: * @param pageTitle
242: * @param formTypeId
243: * @param formTypeTitleKey
244: * @param ownerEid The eid of the user that owns the object in question (wizard or page)
245: * @return
246: */
247: protected String setupSessionInfo(Map request,
248: Map<String, Object> session, String pageTitle,
249: String formTypeId, String formTypeTitleKey, String ownerEid) {
250: String retView = "formCreator";
251:
252: if (request.get("current_review_id") == null) {
253: session.remove(ResourceEditingHelper.ATTACHMENT_ID);
254: session.put(ResourceEditingHelper.CREATE_TYPE,
255: ResourceEditingHelper.CREATE_TYPE_FORM);
256: session.put(ResourceEditingHelper.CREATE_SUB_TYPE,
257: formTypeId);
258:
259: String objectId = (String) request.get("objectId");
260: String objectTitle = (String) request.get("objectTitle");
261: String objectDesc = (String) request.get("objectDesc");
262:
263: String itemId = (String) request.get("itemId");
264: session.put(ReviewHelper.REVIEW_ITEM_ID, itemId);
265:
266: ResourceBundle myResources = ResourceBundle
267: .getBundle("org.theospi.portfolio.matrix.bundle.Messages");
268: String formTypeTitle = myResources
269: .getString(formTypeTitleKey);
270:
271: try {
272: String folderBase = getUserCollection().getId();
273:
274: Placement placement = ToolManager.getCurrentPlacement();
275: String currentSite = placement.getContext();
276:
277: String rootDisplayName = myResources
278: .getString("portfolioInteraction.displayName");
279: String rootDescription = myResources
280: .getString("portfolioInteraction.description");
281:
282: String folderPath = createFolder(folderBase,
283: "portfolio-interaction", rootDisplayName,
284: rootDescription);
285: folderPath = createFolder(folderPath, currentSite,
286: SiteService.getSiteDisplay(currentSite), null);
287: folderPath = createFolder(folderPath, objectId,
288: objectTitle, objectDesc);
289: folderPath = createFolder(folderPath, formTypeId,
290: formTypeTitle, null);
291:
292: session.put(FormHelper.PARENT_ID_TAG, folderPath);
293: } catch (TypeException e) {
294: throw new RuntimeException(
295: "Failed to redirect to helper", e);
296: } catch (IdUnusedException e) {
297: throw new RuntimeException(
298: "Failed to redirect to helper", e);
299: } catch (PermissionException e) {
300: throw new RuntimeException(
301: "Failed to redirect to helper", e);
302: }
303:
304: //CWM OSP-UI-09 - for auto naming
305: session.put(FormHelper.NEW_FORM_DISPLAY_NAME_TAG,
306: getFormDisplayName(objectTitle, pageTitle,
307: formTypeTitle, ownerEid));
308: } else {
309: session.remove(ResourceEditingHelper.CREATE_TYPE);
310: session.remove(ResourceEditingHelper.CREATE_SUB_TYPE);
311: session.remove(ResourceEditingHelper.CREATE_PARENT);
312: session.put(ResourceEditingHelper.CREATE_TYPE,
313: ResourceEditingHelper.CREATE_TYPE_FORM);
314: session.put(ResourceEditingHelper.ATTACHMENT_ID, request
315: .get("current_review_id"));
316: retView = "formEditor";
317: }
318: return retView;
319: }
320:
321: /**
322: *
323: * @param objectTitle
324: * @param pageTitle
325: * @param formTypeName
326: * @param ownerEid
327: * @return
328: */
329: protected String getFormDisplayName(String objectTitle,
330: String pageTitle, String formTypeName, String ownerEid) {
331: String includePageTitle = "";
332: String includeOwner = "";
333:
334: if (pageTitle != null && pageTitle.length() > 0)
335: includePageTitle = pageTitle + "-";
336:
337: if (ownerEid != null && ownerEid.length() > 0)
338: includeOwner = ownerEid + "-";
339:
340: return objectTitle + "-" + includePageTitle + includeOwner
341: + formTypeName;
342: }
343:
344: /**
345: *
346: * @param base
347: * @param append
348: * @param appendDisplay
349: * @param appendDescription
350: * @return
351: */
352: protected String createFolder(String base, String append,
353: String appendDisplay, String appendDescription) {
354: String folder = base + append + "/";
355:
356: try {
357: ContentCollectionEdit propFolder = getContentHosting()
358: .addCollection(folder);
359: propFolder.getPropertiesEdit()
360: .addProperty(ResourceProperties.PROP_DISPLAY_NAME,
361: appendDisplay);
362: propFolder.getPropertiesEdit().addProperty(
363: ResourceProperties.PROP_DESCRIPTION,
364: appendDescription);
365: getContentHosting().commitCollection(propFolder);
366: return propFolder.getId();
367: } catch (IdUsedException e) {
368: // ignore... it is already there.
369: } catch (Exception e) {
370: throw new RuntimeException(e);
371: }
372: return folder;
373: }
374:
375: /**
376: *
377: * @return
378: * @throws TypeException
379: * @throws IdUnusedException
380: * @throws PermissionException
381: */
382: protected ContentCollection getUserCollection()
383: throws TypeException, IdUnusedException,
384: PermissionException {
385: User user = UserDirectoryService.getCurrentUser();
386: String userId = user.getId();
387: String wsId = SiteService.getUserSiteId(userId);
388: String wsCollectionId = getContentHosting().getSiteCollection(
389: wsId);
390: ContentCollection collection = getContentHosting()
391: .getCollection(wsCollectionId);
392: return collection;
393: }
394:
395: /**
396: * @return Returns the idManager.
397: */
398: public IdManager getIdManager() {
399: return idManager;
400: }
401:
402: /**
403: * @param idManager The idManager to set.
404: */
405: public void setIdManager(IdManager idManager) {
406: this .idManager = idManager;
407: }
408:
409: /**
410: * @return Returns the matrixManager.
411: */
412: public MatrixManager getMatrixManager() {
413: return matrixManager;
414: }
415:
416: /**
417: * @param matrixManager The matrixManager to set.
418: */
419: public void setMatrixManager(MatrixManager matrixManager) {
420: this .matrixManager = matrixManager;
421: }
422:
423: /**
424: * @return Returns the reviewManager.
425: */
426: public ReviewManager getReviewManager() {
427: return reviewManager;
428: }
429:
430: /**
431: * @param reviewManager The reviewManager to set.
432: */
433: public void setReviewManager(ReviewManager reviewManager) {
434: this .reviewManager = reviewManager;
435: }
436:
437: /**
438: * @return Returns the wizardManager.
439: */
440: public WizardManager getWizardManager() {
441: return wizardManager;
442: }
443:
444: /**
445: * @param wizardManager The wizardManager to set.
446: */
447: public void setWizardManager(WizardManager wizardManager) {
448: this .wizardManager = wizardManager;
449: }
450:
451: public LockManager getLockManager() {
452: return lockManager;
453: }
454:
455: public void setLockManager(LockManager lockManager) {
456: this .lockManager = lockManager;
457: }
458:
459: /**
460: * @return the contentHosting
461: */
462: public ContentHostingService getContentHosting() {
463: return contentHosting;
464: }
465:
466: /**
467: * @param contentHosting the contentHosting to set
468: */
469: public void setContentHosting(ContentHostingService contentHosting) {
470: this.contentHosting = contentHosting;
471: }
472:
473: }
|