001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/matrix/tool/src/java/org/theospi/portfolio/matrix/control/CellController.java $
003: * $Id:CellController.java 9134 2006-05-08 20:28:42Z chmaurer@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 org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.assignment.taggable.api.TaggableActivity;
025: import org.sakaiproject.assignment.taggable.api.TaggableItem;
026: import org.sakaiproject.assignment.taggable.api.TaggingHelperInfo;
027: import org.sakaiproject.assignment.taggable.api.TaggingManager;
028: import org.sakaiproject.assignment.taggable.api.TaggingProvider;
029: import org.sakaiproject.content.api.FilePickerHelper;
030: import org.sakaiproject.content.api.ResourceEditingHelper;
031: import org.sakaiproject.metaobj.security.AuthenticationManager;
032: import org.sakaiproject.metaobj.shared.mgt.IdManager;
033: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactDefinitionManager;
034: import org.sakaiproject.metaobj.shared.model.Agent;
035: import org.sakaiproject.metaobj.shared.model.Id;
036: import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
037: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
038: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
039: import org.sakaiproject.tool.api.SessionManager;
040: import org.sakaiproject.tool.api.ToolSession;
041: import org.springframework.validation.Errors;
042: import org.springframework.web.servlet.ModelAndView;
043: import org.springframework.web.servlet.view.RedirectView;
044: import org.theospi.portfolio.guidance.mgt.GuidanceManager;
045: import org.theospi.portfolio.matrix.MatrixManager;
046: import org.theospi.portfolio.matrix.WizardPageHelper;
047: import org.theospi.portfolio.matrix.MatrixFunctionConstants;
048: import org.theospi.portfolio.matrix.model.Cell;
049: import org.theospi.portfolio.matrix.model.Scaffolding;
050: import org.theospi.portfolio.matrix.model.ScaffoldingCell;
051: import org.theospi.portfolio.matrix.model.WizardPage;
052: import org.theospi.portfolio.matrix.model.impl.MatrixContentEntityProducer;
053: import org.theospi.portfolio.matrix.taggable.tool.DecoratedTaggingProvider;
054: import org.theospi.portfolio.matrix.taggable.tool.DecoratedTaggingProvider.Pager;
055: import org.theospi.portfolio.matrix.taggable.tool.DecoratedTaggingProvider.Sort;
056: import org.theospi.portfolio.review.ReviewHelper;
057: import org.theospi.portfolio.review.mgt.ReviewManager;
058: import org.theospi.portfolio.review.model.Review;
059: import org.theospi.portfolio.shared.model.Node;
060: import org.theospi.portfolio.shared.model.CommonFormBean;
061: import org.theospi.portfolio.style.model.Style;
062: import org.theospi.portfolio.security.AuthorizationFacade;
063: import org.theospi.portfolio.wizard.taggable.api.WizardActivityProducer;
064:
065: import java.util.*;
066:
067: public class CellController implements FormController,
068: LoadObjectController {
069:
070: protected final Log logger = LogFactory.getLog(getClass());
071:
072: private MatrixManager matrixManager;
073:
074: private AuthenticationManager authManager = null;
075:
076: private IdManager idManager = null;
077:
078: private ReviewManager reviewManager;
079:
080: private StructuredArtifactDefinitionManager structuredArtifactDefinitionManager;
081:
082: private AuthorizationFacade authzManager = null;
083:
084: private TaggingManager taggingManager;
085:
086: private WizardActivityProducer wizardActivityProducer;
087:
088: private SessionManager sessionManager;
089:
090: private List<String> ratingProviderIds;
091:
092: public static final String WHICH_HELPER_KEY = "filepicker.helper.key";
093:
094: public static final String KEEP_HELPER_LIST = "filepicker.helper.keeplist";
095:
096: protected static final int METADATA_ID_INDEX = 0;
097:
098: protected static final int METADATA_TITLE_INDEX = 1;
099:
100: protected static final int METADATA_DESC_INDEX = 2;
101:
102: protected static final String PROVIDERS_PARAM = "providers";
103:
104: public Map referenceData(Map request, Object command, Errors errors) {
105: ToolSession session = getSessionManager()
106: .getCurrentToolSession();
107:
108: CellFormBean cell = (CellFormBean) command;
109: Map model = new HashMap();
110:
111: model.put("isMatrix", "true");
112: model.put("currentUser", getSessionManager()
113: .getCurrentSessionUserId());
114: model.put("CURRENT_GUIDANCE_ID_KEY", "session."
115: + GuidanceManager.CURRENT_GUIDANCE_ID);
116:
117: model.put("isEvaluation", "false");
118:
119: // This is the tool session so evaluation tool gets "is_eval_page_id"
120: // and the matrix/wizard does not
121: if (session.getAttribute("is_eval_page_id") != null) {
122: String eval_page_id = (String) session
123: .getAttribute("is_eval_page_id");
124: model.put("isEvaluation", "true");
125: }
126:
127: model.put("pageTitleKey", "view_cell");
128:
129: // Check for cell being deleted while user was attempting to view
130: if (cell == null || cell.getCell() == null) {
131: clearSession(session);
132: return model;
133: }
134:
135: String pageId = cell.getCell().getWizardPage().getId()
136: .getValue();
137: String siteId = cell.getCell().getWizardPage()
138: .getPageDefinition().getSiteId();
139:
140: model.put("reviews", getReviewManager()
141: .getReviewsByParentAndType(pageId,
142: Review.FEEDBACK_TYPE, siteId,
143: getEntityProducer()));
144: model.put("evaluations", getReviewManager()
145: .getReviewsByParentAndType(pageId,
146: Review.EVALUATION_TYPE, siteId,
147: getEntityProducer()));
148: model.put("reflections", getReviewManager()
149: .getReviewsByParentAndType(pageId,
150: Review.REFLECTION_TYPE, siteId,
151: getEntityProducer()));
152:
153: model.put("cellFormDefs", processAdditionalForms(cell.getCell()
154: .getScaffoldingCell().getAdditionalForms()));
155:
156: model.put("cellForms", getMatrixManager().getPageForms(
157: cell.getCell().getWizardPage()));
158:
159: Boolean readOnly = new Boolean(false);
160:
161: if (cell.getCell().getMatrix() != null) {
162: Agent owner = cell.getCell().getMatrix().getOwner();
163: readOnly = isReadOnly(owner, cell.getCell().getMatrix()
164: .getScaffolding().getWorksiteId());
165: }
166: model.put("readOnlyMatrix", readOnly);
167:
168: String[] objectMetadata = getObjectMetadata(pageId, request);
169: model.put("objectId", objectMetadata[METADATA_ID_INDEX]);
170: model.put("objectTitle", objectMetadata[METADATA_TITLE_INDEX]);
171: model.put("objectDesc", objectMetadata[METADATA_DESC_INDEX]);
172:
173: Style style = cell.getCell().getWizardPage()
174: .getPageDefinition().getStyle();
175:
176: if (style != null) {
177: Id fileId = style.getStyleFile();
178: Node node = getMatrixManager().getNode(fileId);
179: model.put("styleUrl", node.getExternalUri());
180: }
181:
182: Style defaultStyle = getDefaultStyle(getIdManager().getId(
183: pageId));
184: if (defaultStyle != null) {
185: Id fileId = defaultStyle.getStyleFile();
186: Node node = getMatrixManager().getNode(fileId);
187: model.put("defaultStyleUrl", node.getExternalUri());
188: }
189:
190: if (taggingManager.isTaggable()) {
191: TaggableItem item = wizardActivityProducer.getItem(cell
192: .getCell().getWizardPage());
193: model.put("taggable", "true");
194: ToolSession toolSession = getSessionManager()
195: .getCurrentToolSession();
196: List<DecoratedTaggingProvider> providers = (List) toolSession
197: .getAttribute(PROVIDERS_PARAM);
198: if (providers == null) {
199: providers = getDecoratedProviders(item.getActivity());
200: toolSession.setAttribute(PROVIDERS_PARAM, providers);
201: }
202: model.put("helperInfoList", getHelperInfo(item));
203: model.put("providers", providers);
204: }
205:
206: clearSession(session);
207: return model;
208: }
209:
210: /**
211: *
212: * @param pageId
213: * Id representation of the wizard page id
214: * @return The Style object used on the wizard page with the passed id.
215: */
216: protected Style getDefaultStyle(Id pageId) {
217: // Get the scaffolding default style
218: WizardPage wp = getMatrixManager().getWizardPage(pageId);
219: ScaffoldingCell sCell = getMatrixManager()
220: .getScaffoldingCellByWizardPageDef(
221: wp.getPageDefinition().getId());
222: return sCell.getScaffolding().getStyle();
223: }
224:
225: protected String getEntityProducer() {
226: return MatrixContentEntityProducer.MATRIX_PRODUCER;
227: }
228:
229: protected Boolean isReadOnly(Agent owner, Id id) {
230: if ((owner != null && owner.equals(getAuthManager().getAgent()))
231: && (id == null || getAuthzManager().isAuthorized(
232: MatrixFunctionConstants.USE_SCAFFOLDING, id)))
233: return new Boolean(false);
234: return new Boolean(true);
235: }
236:
237: /**
238: *
239: * @param pageId
240: * String representation of the wizard page id
241: * @param request
242: * Map containing all of the request variables
243: * @return String[] containing the id, title, and description of the object
244: * (matrix or wizard)
245: */
246: protected String[] getObjectMetadata(String pageId, Map request) {
247: String[] objectMetadata = new String[3];
248:
249: Cell cell = getMatrixManager().getCellFromPage(
250: getIdManager().getId(pageId));
251: Scaffolding scaffolding = cell.getMatrix().getScaffolding();
252: objectMetadata[METADATA_ID_INDEX] = scaffolding.getId()
253: .getValue();
254: objectMetadata[METADATA_TITLE_INDEX] = scaffolding.getTitle();
255: objectMetadata[METADATA_DESC_INDEX] = scaffolding
256: .getDescription();
257: return objectMetadata;
258: }
259:
260: public Object fillBackingObject(Object incomingModel, Map request,
261: Map session, Map application) throws Exception {
262: // coming from matrix cell, not helper
263: session.remove(WizardPageHelper.WIZARD_PAGE);
264:
265: CellFormBean cellBean = (CellFormBean) incomingModel;
266:
267: String strId = (String) request.get("page_id");
268: if (strId == null) {
269: strId = (String) session.get("page_id");
270: session.remove("page_id");
271: }
272:
273: Cell cell;
274: Id id = getIdManager().getId(strId);
275:
276: // Check if the cell has been removed, which can happen if:
277: // (1) user views matrix
278: // (2) owner removes column or row (the code verifies that no one has
279: // modified the matrix)
280: // (3) user selects a cell that has just been removed with the column or
281: // row
282: try {
283: cell = matrixManager.getCellFromPage(id);
284:
285: cellBean.setCell(cell);
286:
287: List nodeList = new ArrayList(matrixManager
288: .getPageContents(cell.getWizardPage()));
289: cellBean.setNodes(nodeList);
290:
291: if (request.get("view_user") != null)
292: session.put("view_user", cell.getWizardPage()
293: .getOwner().getId().getValue());
294: } catch (Exception e) {
295: logger.error("Error with cell: " + strId + " "
296: + e.toString());
297: // tbd how to report error back to user?
298: }
299:
300: clearSession(getSessionManager().getCurrentToolSession());
301: return cellBean;
302: }
303:
304: private String ListToString(String[] strArray) {
305: String result = "";
306: if (strArray != null) {
307: for (int i = 0; i < strArray.length; i++) {
308: if (i == 0)
309: result = strArray[i];
310: else
311: result = result.concat(",").concat(strArray[i]);
312: }
313: }
314: return result;
315: }
316:
317: public ModelAndView handleRequest(Object requestModel, Map request,
318: Map session, Map application, Errors errors) {
319: CellFormBean cellBean = (CellFormBean) requestModel;
320: Cell cell = cellBean.getCell();
321:
322: // Check for cell being deleted while user was attempting to view
323: if (cell == null)
324: return new ModelAndView("matrixError");
325:
326: // String action = (String)request.get("action");
327: String submit = (String) request.get("submit");
328: String matrixAction = (String) request.get("matrix");
329: String submitAction = (String) request.get("submitAction");
330:
331: if ("tagItem".equals(submitAction)) {
332: return tagItem(cell, request, session);
333: } else if ("sortList".equals(submitAction)) {
334: return sortList(request, session);
335: } else if ("pageList".equals(submitAction)) {
336: return pageList(request, session);
337: }
338:
339: if (submit != null) {
340: Map map = new HashMap();
341: map.put("page_id", cell.getWizardPage().getId());
342: map.put("selectedArtifacts", ListToString(cellBean
343: .getSelectedArtifacts()));
344: map.put("cellBean", cellBean);
345: // cwm change this to use the reflection submission confirmation
346: return new ModelAndView("confirm", map);
347: }
348:
349: if (matrixAction != null) {
350: String scaffId = "";
351:
352: if (getTaggingManager().isTaggable()) {
353: session.remove(PROVIDERS_PARAM);
354: }
355:
356: if (cell.getMatrix() != null)
357: scaffId = cell.getMatrix().getScaffolding().getId()
358: .getValue();
359:
360: if (session.get("is_eval_page_id") != null) {
361: String eval_page_id = (String) session
362: .get("is_eval_page_id");
363: String pageId = cell.getWizardPage().getId().getValue();
364: if (eval_page_id.equals(pageId))
365: return new ModelAndView("cancelEvaluation");
366: }
367:
368: return new ModelAndView("cancel", "scaffolding_id", scaffId);
369: }
370:
371: return new ModelAndView("success", "cellBean", cellBean);
372: }
373:
374: protected ModelAndView tagItem(Cell cell, Map request, Map session) {
375: ModelAndView view = null;
376: // Get appropriate helperInfo
377: for (TaggingHelperInfo info : getHelperInfo(wizardActivityProducer
378: .getItem(cell.getWizardPage()))) {
379: if (info.getProvider().getId().equals(
380: request.get("providerId"))) {
381: // Add parameters to session
382: for (String key : info.getParameterMap().keySet()) {
383: session.put(key, info.getParameterMap().get(key));
384: }
385: session.put("page_id", (String) request.get("page_id"));
386: view = new ModelAndView(new RedirectView(info
387: .getHelperId()
388: + ".helper"));
389: break;
390: }
391: }
392: return view;
393: }
394:
395: protected ModelAndView sortList(Map request, Map session) {
396: String providerId = (String) request.get("providerId");
397: String criteria = (String) request.get("criteria");
398:
399: List<DecoratedTaggingProvider> providers = (List) getSessionManager()
400: .getCurrentToolSession().getAttribute(PROVIDERS_PARAM);
401: for (DecoratedTaggingProvider dtp : providers) {
402: if (dtp.getProvider().getId().equals(providerId)) {
403: Sort sort = dtp.getSort();
404: if (sort.getSort().equals(criteria)) {
405: sort
406: .setAscending(sort.isAscending() ? false
407: : true);
408: } else {
409: sort.setSort(criteria);
410: sort.setAscending(true);
411: }
412: break;
413: }
414: }
415: session.put("page_id", (String) request.get("page_id"));
416: return new ModelAndView(new RedirectView((String) request
417: .get("view")));
418: }
419:
420: protected ModelAndView pageList(Map request, Map session) {
421: String page = (String) request.get("page");
422: String pageSize = (String) request.get("pageSize");
423: String providerId = (String) request.get("providerId");
424:
425: List<DecoratedTaggingProvider> providers = (List) getSessionManager()
426: .getCurrentToolSession().getAttribute(PROVIDERS_PARAM);
427: for (DecoratedTaggingProvider dtp : providers) {
428: if (dtp.getProvider().getId().equals(providerId)) {
429: Pager pager = dtp.getPager();
430: pager.setPageSize(Integer.valueOf(pageSize));
431: if (Pager.FIRST.equals(page)) {
432: pager.setFirstItem(0);
433: } else if (Pager.PREVIOUS.equals(page)) {
434: pager.setFirstItem(pager.getFirstItem()
435: - pager.getPageSize());
436: } else if (Pager.NEXT.equals(page)) {
437: pager.setFirstItem(pager.getFirstItem()
438: + pager.getPageSize());
439: } else if (Pager.LAST.equals(page)) {
440: pager.setFirstItem((pager.getTotalItems() / pager
441: .getPageSize())
442: * pager.getPageSize());
443: }
444: break;
445: }
446: }
447: session.put("page_id", (String) request.get("page_id"));
448: return new ModelAndView(new RedirectView((String) request
449: .get("view")));
450: }
451:
452: protected List processAdditionalForms(List formTypes) {
453: List retList = new ArrayList();
454: for (Iterator iter = formTypes.iterator(); iter.hasNext();) {
455: String strFormDefId = (String) iter.next();
456: StructuredArtifactDefinitionBean bean = getStructuredArtifactDefinitionManager()
457: .loadHome(strFormDefId);
458: bean.getDescription();
459: // cwm use a different bean below, as the name has implications
460: retList.add(new CommonFormBean(strFormDefId, bean
461: .getDecoratedDescription(), strFormDefId, bean
462: .getOwner().getName(), bean.getModified()));
463: }
464: return retList;
465: }
466:
467: protected void clearSession(ToolSession session) {
468: session
469: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
470: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
471: session
472: .removeAttribute(FilePickerHelper.FILE_PICKER_RESOURCE_FILTER);
473:
474: session.removeAttribute(ResourceEditingHelper.CREATE_TYPE);
475: session.removeAttribute(ResourceEditingHelper.CREATE_PARENT);
476: session.removeAttribute(ResourceEditingHelper.CREATE_SUB_TYPE);
477: session.removeAttribute(ResourceEditingHelper.ATTACHMENT_ID);
478:
479: session.removeAttribute(ReviewHelper.REVIEW_TYPE);
480: session.removeAttribute(ReviewHelper.REVIEW_TYPE_KEY);
481:
482: session.removeAttribute(WHICH_HELPER_KEY);
483: session.removeAttribute(KEEP_HELPER_LIST);
484:
485: }
486:
487: protected List<TaggingHelperInfo> getHelperInfo(TaggableItem item) {
488: List<TaggingHelperInfo> infoList = new ArrayList<TaggingHelperInfo>();
489: if (taggingManager.isTaggable()) {
490: for (TaggingProvider provider : taggingManager
491: .getProviders()) {
492: // Only get helpers for accepted rating providers
493: if (ratingProviderIds.contains(provider.getId())) {
494: TaggingHelperInfo info = provider
495: .getItemHelperInfo(item.getReference());
496: if (info != null) {
497: infoList.add(info);
498: }
499: }
500: }
501: }
502: return infoList;
503: }
504:
505: protected List<DecoratedTaggingProvider> getDecoratedProviders(
506: TaggableActivity activity) {
507: List<DecoratedTaggingProvider> providers = new ArrayList<DecoratedTaggingProvider>();
508: for (TaggingProvider provider : getTaggingManager()
509: .getProviders()) {
510: providers.add(new DecoratedTaggingProvider(activity,
511: provider));
512: }
513: return providers;
514: }
515:
516: /**
517: * @return
518: */
519: public AuthenticationManager getAuthManager() {
520: return authManager;
521: }
522:
523: /**
524: * @param manager
525: */
526: public void setAuthManager(AuthenticationManager manager) {
527: authManager = manager;
528: }
529:
530: /**
531: * @return
532: */
533: public IdManager getIdManager() {
534: return idManager;
535: }
536:
537: /**
538: * @param manager
539: */
540: public void setIdManager(IdManager manager) {
541: idManager = manager;
542: }
543:
544: /**
545: * @return Returns the matrixManager.
546: */
547: public MatrixManager getMatrixManager() {
548: return matrixManager;
549: }
550:
551: /**
552: * @param matrixManager
553: * The matrixManager to set.
554: */
555: public void setMatrixManager(MatrixManager matrixManager) {
556: this .matrixManager = matrixManager;
557: }
558:
559: /**
560: * @return Returns the reviewManager.
561: */
562: public ReviewManager getReviewManager() {
563: return reviewManager;
564: }
565:
566: /**
567: * @param reviewManager
568: * The reviewManager to set.
569: */
570: public void setReviewManager(ReviewManager reviewManager) {
571: this .reviewManager = reviewManager;
572: }
573:
574: /**
575: * @return Returns the structuredArtifactDefinitionManager.
576: */
577: public StructuredArtifactDefinitionManager getStructuredArtifactDefinitionManager() {
578: return structuredArtifactDefinitionManager;
579: }
580:
581: /**
582: * @param structuredArtifactDefinitionManager
583: * The structuredArtifactDefinitionManager to set.
584: */
585: public void setStructuredArtifactDefinitionManager(
586: StructuredArtifactDefinitionManager structuredArtifactDefinitionManager) {
587: this .structuredArtifactDefinitionManager = structuredArtifactDefinitionManager;
588: }
589:
590: public AuthorizationFacade getAuthzManager() {
591: return authzManager;
592: }
593:
594: public void setAuthzManager(AuthorizationFacade authzManager) {
595: this .authzManager = authzManager;
596: }
597:
598: public TaggingManager getTaggingManager() {
599: return taggingManager;
600: }
601:
602: public void setTaggingManager(TaggingManager taggingManager) {
603: this .taggingManager = taggingManager;
604: }
605:
606: public WizardActivityProducer getWizardActivityProducer() {
607: return wizardActivityProducer;
608: }
609:
610: public void setWizardActivityProducer(
611: WizardActivityProducer wizardActivityProducer) {
612: this .wizardActivityProducer = wizardActivityProducer;
613: }
614:
615: public List<String> getRatingProviderIds() {
616: return ratingProviderIds;
617: }
618:
619: public void setRatingProviderIds(List<String> ratingProviderIds) {
620: this .ratingProviderIds = ratingProviderIds;
621: }
622:
623: public SessionManager getSessionManager() {
624: return sessionManager;
625: }
626:
627: public void setSessionManager(SessionManager sessionManager) {
628: this.sessionManager = sessionManager;
629: }
630: }
|