001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/PrettyScaffoldingController.java $
003: * $Id: PrettyScaffoldingController.java 21737 2007-02-19 20:25:58Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027: import java.util.List;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.content.api.ContentHostingService;
032: import org.sakaiproject.entity.api.EntityManager;
033: import org.sakaiproject.metaobj.security.AuthenticationManager;
034: import org.sakaiproject.metaobj.shared.model.Id;
035: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
036: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
037: import org.sakaiproject.metaobj.worksite.mgt.WorksiteManager;
038: import org.sakaiproject.tool.api.SessionManager;
039: import org.springframework.validation.Errors;
040: import org.springframework.web.servlet.ModelAndView;
041: import org.theospi.portfolio.matrix.model.Criterion;
042: import org.theospi.portfolio.matrix.model.Level;
043: import org.theospi.portfolio.matrix.model.Scaffolding;
044: import org.theospi.portfolio.matrix.model.ScaffoldingCell;
045: import org.theospi.portfolio.matrix.model.Matrix;
046: import org.theospi.portfolio.matrix.model.WizardPage;
047: import org.theospi.portfolio.matrix.model.Cell;
048: import org.theospi.portfolio.review.mgt.ReviewManager;
049:
050: /**
051: * @author chmaurer
052: */
053: public class PrettyScaffoldingController extends
054: BaseScaffoldingController implements FormController,
055: LoadObjectController {
056:
057: protected final Log logger = LogFactory.getLog(getClass());
058: private WorksiteManager worksiteManager = null;
059: private AuthenticationManager authManager = null;
060: private SessionManager sessionManager;
061: private ContentHostingService contentHosting;
062: private EntityManager entityManager;
063: private ReviewManager reviewManager;
064:
065: /* (non-Javadoc)
066: * @see org.theospi.utils.mvc.intf.FormController#referenceData(java.util.Map, java.lang.Object, org.springframework.validation.Errors)
067: */
068: public Map referenceData(Map request, Object command, Errors errors) {
069: Map<String, Object> model = new HashMap<String, Object>();
070:
071: model.put("isInSession",
072: EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG);
073:
074: Scaffolding scaffolding = null;
075: if (command instanceof Scaffolding)
076: scaffolding = (Scaffolding) command;
077:
078: if (scaffolding != null)
079: model.put("isMatrixUsed", scaffolding.isPublished()
080: && isMatrixUsed(scaffolding.getId()));
081: else
082: model.put("isMatrixUsed", false);
083:
084: return model;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.theospi.utils.mvc.intf.LoadObjectController#fillBackingObject(java.lang.Object, java.util.Map, java.util.Map, java.util.Map)
089: */
090: public Object fillBackingObject(Object incomingModel, Map request,
091: Map session, Map application) throws Exception {
092:
093: MatrixGridBean grid = (MatrixGridBean) incomingModel;
094:
095: Id sId = getIdManager().getId(
096: (String) request.get("scaffolding_id"));
097: Scaffolding scaffolding = getMatrixManager()
098: .getScaffolding(sId);
099:
100: List levels = scaffolding.getLevels();
101: List criteria = scaffolding.getCriteria();
102: List<List> matrixContents = new ArrayList<List>();
103: Criterion criterion = new Criterion();
104: Level level = new Level();
105: List<ScaffoldingCell> row = new ArrayList<ScaffoldingCell>();
106:
107: Set cells = scaffolding.getScaffoldingCells();
108:
109: for (Iterator criteriaIterator = criteria.iterator(); criteriaIterator
110: .hasNext();) {
111: row.clear(); // = new ArrayList<ScaffoldingCell>();
112: criterion = (Criterion) criteriaIterator.next();
113: for (Iterator levelsIterator = levels.iterator(); levelsIterator
114: .hasNext();) {
115: level = (Level) levelsIterator.next();
116: ScaffoldingCell scaffoldingCell = getScaffoldingCell(
117: cells, criterion, level);
118:
119: row.add(scaffoldingCell);
120: }
121: matrixContents.add(row);
122: }
123:
124: grid.setScaffolding(scaffolding);
125: grid.setColumnLabels(levels);
126: grid.setRowLabels(criteria);
127: grid.setMatrixContents(matrixContents);
128:
129: //Make sure these are not in session.
130: session
131: .remove(EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG);
132: session
133: .remove(EditedScaffoldingStorage.EDITED_SCAFFOLDING_STORAGE_SESSION_KEY);
134:
135: return incomingModel;
136: }
137:
138: private ScaffoldingCell getScaffoldingCell(Set cells,
139: Criterion criterion, Level level) {
140: for (Iterator iter = cells.iterator(); iter.hasNext();) {
141: ScaffoldingCell scaffoldingCell = (ScaffoldingCell) iter
142: .next();
143: if (scaffoldingCell.getRootCriterion().getId().getValue()
144: .equals(criterion.getId().getValue())
145: && scaffoldingCell.getLevel().getId().getValue()
146: .equals(level.getId().getValue())) {
147: return scaffoldingCell;
148: }
149: }
150: return null;
151: }
152:
153: /* (non-Javadoc)
154: * @see org.theospi.utils.mvc.intf.Controller#handleRequest(java.lang.Object, java.util.Map, java.util.Map, java.util.Map, org.springframework.validation.Errors)
155: */
156: public ModelAndView handleRequest(Object requestModel, Map request,
157: Map session, Map application, Errors errors) {
158: String action = (String) request.get("action");
159: if (action == null)
160: action = (String) request.get("submitAction");
161: String generateAction = (String) request.get("generateAction");
162: String cancelAction = (String) request.get("cancelAction");
163:
164: Id worksiteId = worksiteManager.getCurrentWorksiteId();
165: Map<String, Object> model = new HashMap<String, Object>();
166:
167: EditedScaffoldingStorage sessionBean = (EditedScaffoldingStorage) session
168: .get(EditedScaffoldingStorage.EDITED_SCAFFOLDING_STORAGE_SESSION_KEY);
169: Scaffolding scaffolding = sessionBean.getScaffolding();
170: scaffolding.setWorksiteId(worksiteId);
171:
172: scaffolding.setOwner(authManager.getAgent());
173:
174: if (generateAction != null) {
175: if (scaffolding.isPublished()) {
176: return new ModelAndView("editScaffoldingConfirm");
177: }
178:
179: scaffolding = saveScaffolding(scaffolding);
180: session
181: .remove(EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG);
182: session
183: .remove(EditedScaffoldingStorage.EDITED_SCAFFOLDING_STORAGE_SESSION_KEY);
184: model.put("scaffolding_id", scaffolding.getId());
185: return new ModelAndView("view", model);
186: }
187: if (cancelAction != null) {
188: return new ModelAndView("return");
189: }
190:
191: if (action != null) {
192: if (action.equals("forward")) {
193: String forwardView = (String) request.get("dest");
194: model.put("label", request.get("label"));
195: model.put("finalDest", request.get("finalDest"));
196: model.put("displayText", request.get("displayText"));
197: String params = (String) request.get("params");
198: model.put("params", params);
199: if (!params.equals("")) {
200: String[] paramsList = params.split(":");
201: for (int i = 0; i < paramsList.length; i++) {
202: String[] pair = paramsList[i].split("=");
203: String val = null;
204: if (pair.length > 1)
205: val = pair[1];
206: model.put(pair[0], val);
207: }
208: }
209: //matrixManager.storeScaffolding(scaffolding);
210:
211: //touchAllCells(scaffolding);
212: sessionBean.setScaffolding(scaffolding);
213: model.put("scaffolding_id", scaffolding.getId());
214:
215: return new ModelAndView(forwardView, model);
216:
217: }
218: }
219: return new ModelAndView("success");
220: }
221:
222: /*
223: private void touchAllScaffolding(Scaffolding scaffolding) {
224: scaffolding.getLevels().size();
225: scaffolding.getCriteria().size();
226: for (Iterator iter = scaffolding.getScaffoldingCells().iterator(); iter.hasNext();) {
227: ScaffoldingCell sCell = (ScaffoldingCell) iter.next();
228: sCell.getCells().size();
229: //sCell.getExpectations().size();
230: }
231: }
232: */
233: protected void touchAllCells(Scaffolding scaffolding) {
234: for (Iterator iter = scaffolding.getScaffoldingCells()
235: .iterator(); iter.hasNext();) {
236: ScaffoldingCell sCell = (ScaffoldingCell) iter.next();
237: sCell.getCells().size();
238: }
239:
240: }
241:
242: /**
243: * @return Returns the worksiteManager.
244: */
245: public WorksiteManager getWorksiteManager() {
246: return worksiteManager;
247: }
248:
249: /**
250: * @param worksiteManager The worksiteManager to set.
251: */
252: public void setWorksiteManager(WorksiteManager worksiteManager) {
253: this .worksiteManager = worksiteManager;
254: }
255:
256: /**
257: * @return Returns the authManager.
258: */
259: public AuthenticationManager getAuthManager() {
260: return authManager;
261: }
262:
263: /**
264: * @param authManager The authManager to set.
265: */
266: public void setAuthManager(AuthenticationManager authManager) {
267: this .authManager = authManager;
268: }
269:
270: public SessionManager getSessionManager() {
271: return sessionManager;
272: }
273:
274: public void setSessionManager(SessionManager sessionManager) {
275: this .sessionManager = sessionManager;
276: }
277:
278: public ContentHostingService getContentHosting() {
279: return contentHosting;
280: }
281:
282: public void setContentHosting(ContentHostingService contentHosting) {
283: this .contentHosting = contentHosting;
284: }
285:
286: public EntityManager getEntityManager() {
287: return entityManager;
288: }
289:
290: public void setEntityManager(EntityManager entityManager) {
291: this .entityManager = entityManager;
292: }
293:
294: /**
295: * @return Returns the reviewManager.
296: */
297: public ReviewManager getReviewManager() {
298: return reviewManager;
299: }
300:
301: /**
302: * @param reviewManager The reviewManager to set.
303: */
304: public void setReviewManager(ReviewManager reviewManager) {
305: this .reviewManager = reviewManager;
306: }
307:
308: /**
309: ** Determine if any matrix with the specified scaffoldingId has been 'used'
310: ** (e.g. containing reflections and/or added form items)
311: **/
312: private boolean isMatrixUsed(Id scaffoldingId) {
313: List matrices = getMatrixManager().getMatrices(scaffoldingId);
314:
315: for (Iterator matrixIt = matrices.iterator(); matrixIt
316: .hasNext();) {
317: Matrix matrix = (Matrix) matrixIt.next();
318: Set cells = matrix.getCells();
319:
320: for (Iterator cellIt = cells.iterator(); cellIt.hasNext();) {
321: Cell cell = (Cell) cellIt.next();
322: WizardPage wizardPage = cell.getWizardPage();
323: String pageId = wizardPage.getId().getValue();
324: if (wizardPage.getReflections() != null
325: && wizardPage.getReflections().size() > 0)
326: return true;
327: if (wizardPage.getPageForms() != null
328: && wizardPage.getPageForms().size() > 0)
329: return true;
330: if (wizardPage.getAttachments() != null
331: && wizardPage.getAttachments().size() > 0)
332: return true;
333: if (reviewManager.getReviewsByParent(pageId) != null
334: && reviewManager.getReviewsByParent(pageId)
335: .size() > 0)
336: return true;
337: // note: wizardPage.[get|set]Feedback() does not appear to be used
338: }
339: }
340:
341: return false;
342: }
343: }
|