001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/BaseScaffoldingController.java $
003: * $Id: BaseScaffoldingController.java 29140 2007-04-18 23:29:54Z 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.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.assignment.taggable.api.TaggableActivity;
030: import org.sakaiproject.assignment.taggable.api.TaggingManager;
031: import org.sakaiproject.assignment.taggable.api.TaggingProvider;
032: import org.sakaiproject.content.api.LockManager;
033: import org.sakaiproject.metaobj.shared.mgt.IdManager;
034: import org.sakaiproject.metaobj.shared.model.Id;
035: import org.theospi.portfolio.matrix.MatrixFunctionConstants;
036: import org.theospi.portfolio.matrix.MatrixManager;
037: import org.theospi.portfolio.matrix.model.Criterion;
038: import org.theospi.portfolio.matrix.model.Level;
039: import org.theospi.portfolio.matrix.model.Scaffolding;
040: import org.theospi.portfolio.matrix.model.ScaffoldingCell;
041: import org.theospi.portfolio.matrix.model.WizardPageForm;
042: import org.theospi.portfolio.matrix.model.Attachment;
043: import org.theospi.portfolio.matrix.model.Matrix;
044: import org.theospi.portfolio.matrix.model.Cell;
045: import org.theospi.portfolio.matrix.model.WizardPage;
046: import org.theospi.portfolio.security.AuthorizationFacade;
047: import org.theospi.portfolio.wizard.taggable.api.WizardActivityProducer;
048:
049: public class BaseScaffoldingController {
050:
051: protected final Log logger = LogFactory.getLog(getClass());
052:
053: private AuthorizationFacade authzManager;
054: private MatrixManager matrixManager;
055: private IdManager idManager;
056: private LockManager lockManager = null;
057: private TaggingManager taggingManager;
058: private WizardActivityProducer wizardActivityProducer;
059:
060: /* (non-Javadoc)
061: * @see org.theospi.utils.mvc.intf.CustomCommandController#formBackingObject(java.util.Map, java.util.Map, java.util.Map)
062: */
063: public Object formBackingObject(Map request, Map session,
064: Map application) {
065: Scaffolding scaffolding;
066: if (request
067: .get(EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG) == null
068: && session
069: .get(EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG) == null) {
070:
071: if (request.get("scaffolding_id") != null
072: && !request.get("scaffolding_id").equals("")) {
073: Id id = getIdManager().getId(
074: (String) request.get("scaffolding_id"));
075: scaffolding = getMatrixManager().getScaffolding(id);
076: } else {
077: scaffolding = getMatrixManager()
078: .createDefaultScaffolding();
079: }
080: EditedScaffoldingStorage sessionBean = new EditedScaffoldingStorage(
081: scaffolding);
082: session
083: .put(
084: EditedScaffoldingStorage.EDITED_SCAFFOLDING_STORAGE_SESSION_KEY,
085: sessionBean);
086:
087: } else {
088: EditedScaffoldingStorage sessionBean = (EditedScaffoldingStorage) session
089: .get(EditedScaffoldingStorage.EDITED_SCAFFOLDING_STORAGE_SESSION_KEY);
090: scaffolding = sessionBean.getScaffolding();
091: session
092: .remove(EditedScaffoldingStorage.STORED_SCAFFOLDING_FLAG);
093: }
094: //Traversing the collections to un-lazily load
095: scaffolding.getLevels().size();
096: scaffolding.getCriteria().size();
097: traverseScaffoldingCells(scaffolding);
098:
099: return scaffolding;
100: }
101:
102: protected void traverseScaffoldingCells(Scaffolding scaffolding) {
103: scaffolding.getScaffoldingCells().size();
104: for (Iterator iter = scaffolding.getScaffoldingCells()
105: .iterator(); iter.hasNext();) {
106: ScaffoldingCell sCell = (ScaffoldingCell) iter.next();
107: sCell.getCells().size();
108: }
109: }
110:
111: protected boolean isDirtyProgression(Scaffolding scaffolding) {
112: int newProgression = scaffolding.getWorkflowOption();
113: if (scaffolding.getId() == null)
114: return true;
115:
116: Scaffolding origScaff = matrixManager
117: .getScaffolding(scaffolding.getId());
118: int origProgression = origScaff.getWorkflowOption();
119:
120: return (newProgression != origProgression);
121: }
122:
123: protected Scaffolding saveScaffolding(Scaffolding scaffolding) {
124: boolean isDirty = isDirtyProgression(scaffolding);
125:
126: // Check for Hibernate error, which could happen if:
127: // (1) admin starts to revises published but unused matrix
128: // (2) user 'uses' matrix by adding forms/reflections
129: // (3) admin now tries to save changes
130: try {
131: // if taggable, remove tags for any page defs that have been removed
132: if (getTaggingManager().isTaggable()
133: && scaffolding.getId() != null
134: && wizardActivityProducer != null) {
135: Scaffolding origScaffolding = getMatrixManager()
136: .getScaffolding(scaffolding.getId());
137: Set<ScaffoldingCell> storedCells = origScaffolding
138: .getScaffoldingCells();
139: for (ScaffoldingCell cell : storedCells) {
140: if (!scaffolding.getScaffoldingCells().contains(
141: cell)) {
142: for (TaggingProvider provider : getTaggingManager()
143: .getProviders()) {
144: TaggableActivity activity = wizardActivityProducer
145: .getActivity(cell
146: .getWizardPageDefinition());
147: provider.removeTags(activity);
148: }
149: }
150: }
151: }
152:
153: scaffolding = getMatrixManager().storeScaffolding(
154: scaffolding);
155: } catch (Exception e) {
156: logger.error(e.toString());
157: // tbd how to report error back to admin user?
158: }
159:
160: //regen the cells
161: regenerateScaffoldingCells(scaffolding, isDirty);
162:
163: if (isDirty)
164: regenerateMatrixCellStatus(scaffolding);
165:
166: return scaffolding;
167: }
168:
169: /**
170: ** Update the status of all matrix cells to match the initial status
171: ** defined for the corresponding scaffolding cell.
172: **/
173: protected void regenerateMatrixCellStatus(Scaffolding scaffolding) {
174: List matrices = getMatrixManager().getMatrices(
175: scaffolding.getId());
176:
177: for (Iterator matrixIt = matrices.iterator(); matrixIt
178: .hasNext();) {
179: Matrix matrix = (Matrix) matrixIt.next();
180: Set cells = matrix.getCells();
181:
182: for (Iterator cellIt = cells.iterator(); cellIt.hasNext();) {
183: Cell cell = (Cell) cellIt.next();
184: WizardPage page = cell.getWizardPage();
185: String status = page.getStatus();
186: String initialStatus = cell.getScaffoldingCell()
187: .getInitialStatus();
188:
189: if (status != initialStatus) {
190: page.setStatus(initialStatus);
191: getMatrixManager().storePage(page);
192:
193: // If status was locked and is now ready, then unlock resources
194: if (initialStatus
195: .equals(MatrixFunctionConstants.READY_STATUS)
196: && status
197: .equals(MatrixFunctionConstants.LOCKED_STATUS)) {
198: for (Iterator iter = page.getAttachments()
199: .iterator(); iter.hasNext();) {
200: Attachment att = (Attachment) iter.next();
201: lockManager.removeLock(att.getArtifactId()
202: .getValue(), page.getId()
203: .getValue());
204: }
205: for (Iterator iter2 = page.getPageForms()
206: .iterator(); iter2.hasNext();) {
207: WizardPageForm form = (WizardPageForm) iter2
208: .next();
209: lockManager.removeLock(form.getArtifactId()
210: .getValue(), page.getId()
211: .getValue());
212: }
213: }
214: }
215: }
216: }
217: }
218:
219: protected void regenerateScaffoldingCells(Scaffolding scaffolding,
220: boolean dirtyProgression) {
221: List levels = scaffolding.getLevels();
222: List criteria = scaffolding.getCriteria();
223: Criterion criterion = new Criterion();
224: Level level = new Level();
225: Set cells = scaffolding.getScaffoldingCells();
226: boolean firstRow = true;
227: boolean firstColumn = true;
228:
229: for (Iterator criteriaIterator = criteria.iterator(); criteriaIterator
230: .hasNext();) {
231: criterion = (Criterion) criteriaIterator.next();
232: for (Iterator levelsIterator = levels.iterator(); levelsIterator
233: .hasNext();) {
234: level = (Level) levelsIterator.next();
235: ScaffoldingCell scaffoldingCell = getScaffoldingCell(
236: cells, criterion, level);
237: String status = MatrixFunctionConstants.READY_STATUS;
238: if ((scaffolding.getWorkflowOption() == Scaffolding.HORIZONTAL_PROGRESSION && !firstColumn)
239: || (scaffolding.getWorkflowOption() == Scaffolding.VERTICAL_PROGRESSION && !firstRow)
240: || (scaffolding.getWorkflowOption() == Scaffolding.MANUAL_PROGRESSION)) {
241: status = MatrixFunctionConstants.LOCKED_STATUS;
242: }
243: if (scaffoldingCell == null) {
244: scaffoldingCell = new ScaffoldingCell(criterion,
245: level, status, scaffolding);
246: scaffoldingCell.getWizardPageDefinition()
247: .setSiteId(
248: scaffolding.getWorksiteId()
249: .getValue());
250: scaffoldingCell.getWizardPageDefinition().setTitle(
251: getDefaultTitle(scaffolding, criterion,
252: level));
253: getMatrixManager().storeScaffoldingCell(
254: scaffoldingCell);
255: } else if (dirtyProgression) {
256: scaffoldingCell.setInitialStatus(status);
257: getMatrixManager().storeScaffoldingCell(
258: scaffoldingCell);
259: }
260: firstColumn = false;
261: }
262: firstRow = false;
263: //Need to reset firstColumn when moving to the next row
264: firstColumn = true;
265: }
266: }
267:
268: private ScaffoldingCell getScaffoldingCell(Set cells,
269: Criterion criterion, Level level) {
270: for (Iterator iter = cells.iterator(); iter.hasNext();) {
271: ScaffoldingCell scaffoldingCell = (ScaffoldingCell) iter
272: .next();
273: if (scaffoldingCell.getRootCriterion().getId().getValue()
274: .equals(criterion.getId().getValue())
275: && scaffoldingCell.getLevel().getId().getValue()
276: .equals(level.getId().getValue())) {
277: return scaffoldingCell;
278: }
279: }
280: return null;
281: }
282:
283: protected String getDefaultTitle(Scaffolding scaffolding,
284: Criterion criterion, Level level) {
285: String title = scaffolding.getRowLabel() + ": "
286: + criterion.getDescription() + "; "
287: + scaffolding.getColumnLabel() + ": "
288: + level.getDescription();
289:
290: return title;
291: }
292:
293: public AuthorizationFacade getAuthzManager() {
294: return authzManager;
295: }
296:
297: public void setAuthzManager(AuthorizationFacade authzManager) {
298: this .authzManager = authzManager;
299: }
300:
301: public MatrixManager getMatrixManager() {
302: return matrixManager;
303: }
304:
305: public void setMatrixManager(MatrixManager matrixManager) {
306: this .matrixManager = matrixManager;
307: }
308:
309: public IdManager getIdManager() {
310: return idManager;
311: }
312:
313: public void setIdManager(IdManager idManager) {
314: this .idManager = idManager;
315: }
316:
317: public LockManager getLockManager() {
318: return lockManager;
319: }
320:
321: public void setLockManager(LockManager lockManager) {
322: this .lockManager = lockManager;
323: }
324:
325: public TaggingManager getTaggingManager() {
326: return taggingManager;
327: }
328:
329: public void setTaggingManager(TaggingManager taggingManager) {
330: this .taggingManager = taggingManager;
331: }
332:
333: public WizardActivityProducer getWizardActivityProducer() {
334: return wizardActivityProducer;
335: }
336:
337: public void setWizardActivityProducer(
338: WizardActivityProducer wizardActivityProducer) {
339: this.wizardActivityProducer = wizardActivityProducer;
340: }
341: }
|