001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/matrix/tool/src/java/org/theospi/portfolio/matrix/control/ViewMatrixController.java $
003: * $Id:ViewMatrixController.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 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.Collection;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.authz.api.Member;
035: import org.sakaiproject.exception.IdUnusedException;
036: import org.sakaiproject.metaobj.security.FunctionConstants;
037: import org.sakaiproject.metaobj.shared.model.Agent;
038: import org.sakaiproject.metaobj.shared.model.Id;
039: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
040: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
041: import org.sakaiproject.site.api.Group;
042: import org.sakaiproject.site.api.Site;
043: import org.sakaiproject.site.api.ToolConfiguration;
044: import org.sakaiproject.site.cover.SiteService;
045: import org.sakaiproject.tool.api.Placement;
046: import org.sakaiproject.tool.api.ToolManager;
047: import org.sakaiproject.tool.api.ToolSession;
048: import org.sakaiproject.tool.cover.SessionManager;
049: import org.sakaiproject.user.api.UserNotDefinedException;
050: import org.sakaiproject.user.cover.UserDirectoryService;
051: import org.springframework.validation.Errors;
052: import org.springframework.web.servlet.ModelAndView;
053: import org.theospi.portfolio.matrix.MatrixFunctionConstants;
054: import org.theospi.portfolio.matrix.MatrixManager;
055: import org.theospi.portfolio.matrix.model.Cell;
056: import org.theospi.portfolio.matrix.model.Criterion;
057: import org.theospi.portfolio.matrix.model.Level;
058: import org.theospi.portfolio.matrix.model.Matrix;
059: import org.theospi.portfolio.matrix.model.Scaffolding;
060: import org.theospi.portfolio.matrix.model.ScaffoldingCell;
061:
062: public class ViewMatrixController extends AbstractMatrixController
063: implements FormController, LoadObjectController {
064:
065: protected final Log logger = LogFactory.getLog(getClass());
066:
067: public static final String VIEW_USER = "view_user";
068:
069: private ToolManager toolManager;
070:
071: public Object fillBackingObject(Object incomingModel, Map request,
072: Map session, Map application) throws Exception {
073:
074: MatrixGridBean grid = (MatrixGridBean) incomingModel;
075: String strScaffoldingId = (String) request
076: .get("scaffolding_id");
077:
078: if (strScaffoldingId == null) {
079: Placement placement = getToolManager()
080: .getCurrentPlacement();
081: strScaffoldingId = placement.getPlacementConfig()
082: .getProperty(MatrixManager.EXPOSED_MATRIX_KEY);
083: }
084:
085: Id scaffoldingId = getIdManager().getId(strScaffoldingId);
086: Scaffolding scaffolding = getMatrixManager().getScaffolding(
087: scaffoldingId);
088: // Check for invalid scaffolding (could happen if scaffolding is deleted)
089: if (scaffolding == null) {
090: logger.warn("Unable to find scaffolding: " + scaffoldingId);
091: return incomingModel;
092: }
093:
094: Agent currentAgent = getAuthManager().getAgent();
095: boolean createAuthz = false;
096:
097: String userRequest = (String) request.get(VIEW_USER);
098: String userSession = (String) session.get(VIEW_USER);
099: if (userRequest != null) {
100: currentAgent = getAgentManager().getAgent(
101: getIdManager().getId(userRequest));
102: createAuthz = true;
103: } else if (userSession != null) {
104: userRequest = userSession;
105: currentAgent = getAgentManager().getAgent(
106: getIdManager().getId(userSession));
107: // The authorize was already created by this point
108: }
109: session.put(VIEW_USER, userRequest);
110:
111: Matrix matrix = getMatrixManager().getMatrix(scaffoldingId,
112: currentAgent.getId());
113: if (matrix == null) {
114: if (currentAgent != null && !currentAgent.equals("")) {
115: //Don't create a matrix unless the scaffolding has been published
116: // and the user has permission to use a matrix.
117: if (scaffolding.isPublished()
118: || scaffolding.isPreview()) {
119: matrix = getMatrixManager().createMatrix(
120: currentAgent, scaffolding);
121: } else {
122: grid.setScaffolding(scaffolding);
123: return incomingModel;
124: }
125: }
126: }
127: scaffolding = matrix.getScaffolding();
128: if (createAuthz) {
129: getAuthzManager().createAuthorization(
130: getAuthManager().getAgent(),
131: FunctionConstants.READ_MATRIX, matrix.getId());
132: }
133:
134: List levels = scaffolding.getLevels();
135: List criteria = scaffolding.getCriteria();
136: List matrixContents = new ArrayList();
137: Criterion criterion = new Criterion();
138: Level level = new Level();
139: List row = new ArrayList();
140:
141: Set cells = matrix.getCells();
142:
143: for (Iterator criteriaIterator = criteria.iterator(); criteriaIterator
144: .hasNext();) {
145: row = new ArrayList();
146: criterion = (Criterion) criteriaIterator.next();
147: for (Iterator levelsIterator = levels.iterator(); levelsIterator
148: .hasNext();) {
149: level = (Level) levelsIterator.next();
150: CellFormBean cellBean = new CellFormBean();
151:
152: Cell cell = getCell(cells, criterion, level);
153: if (cell == null) {
154: cell = new Cell();
155: cell.getWizardPage().setOwner(matrix.getOwner());
156: cell.setMatrix(matrix);
157: ScaffoldingCell scaffoldingCell = getMatrixManager()
158: .getScaffoldingCell(criterion, level);
159: cell.setScaffoldingCell(scaffoldingCell);
160: cell.setStatus(scaffoldingCell.getInitialStatus());
161: getMatrixManager().storeCell(cell);
162: }
163: List nodeList = new ArrayList(getMatrixManager()
164: .getPageContents(cell.getWizardPage()));
165: nodeList.addAll(getMatrixManager().getPageForms(
166: cell.getWizardPage()));
167: cellBean.setCell(cell);
168: cellBean.setNodes(nodeList);
169: row.add(cellBean);
170: }
171: matrixContents.add(row);
172: }
173:
174: grid.setMatrixId(matrix.getId());
175: grid.setMatrixOwner(matrix.getOwner());
176: grid.setScaffolding(scaffolding);
177: grid.setColumnLabels(levels);
178: grid.setRowLabels(criteria);
179: grid.setMatrixContents(matrixContents);
180:
181: return incomingModel;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.theospi.utils.mvc.intf.FormController#referenceData(java.util.Map, java.lang.Object, org.springframework.validation.Errors)
186: */
187: public Map referenceData(Map request, Object command, Errors errors) {
188: Map model = new HashMap();
189: String worksiteId = getWorksiteManager().getCurrentWorksiteId()
190: .getValue();
191:
192: model.put("worksite", getWorksiteManager().getSite(worksiteId));
193: model.put("tool", getToolManager().getCurrentPlacement());
194: model.put("isMaintainer", isMaintainer());
195:
196: List userList = new ArrayList(getUserList(worksiteId));
197: Collections.sort(userList);
198: model.put("members", userList);
199:
200: MatrixGridBean grid = (MatrixGridBean) command;
201: Agent owner = grid.getMatrixOwner();
202: Boolean readOnly = Boolean.valueOf(false);
203:
204: if ((owner != null && !owner
205: .equals(getAuthManager().getAgent()))
206: || !getAuthzManager().isAuthorized(
207: MatrixFunctionConstants.USE_SCAFFOLDING,
208: getWorksiteManager().getCurrentWorksiteId()))
209: readOnly = Boolean.valueOf(true);
210:
211: model.put("matrixOwner", owner);
212: model.put("readOnlyMatrix", readOnly);
213:
214: if (grid.getScaffolding() != null
215: && getCurrentSitePageId().equals(
216: grid.getScaffolding().getExposedPageId())) {
217: model.put("isExposedPage", Boolean.valueOf(true));
218: }
219:
220: return model;
221: }
222:
223: /**
224: * Extract the site page id from the current request.
225: *
226: * @return The site page id implied from the current request.
227: */
228: protected String getCurrentSitePageId() {
229: ToolSession ts = SessionManager.getCurrentToolSession();
230: if (ts != null) {
231: ToolConfiguration tool = SiteService.findTool(ts
232: .getPlacementId());
233: if (tool != null) {
234: return tool.getPageId();
235: }
236: }
237:
238: return null;
239:
240: } // getCurrentSitePageId
241:
242: private Set getUserList(String worksiteId) {
243: Set members = new HashSet();
244: Set users = new HashSet();
245:
246: try {
247: Site site = SiteService.getSite(worksiteId);
248: if (site.hasGroups()) {
249: String currentUser = SessionManager
250: .getCurrentSessionUserId();
251: Collection groups = site
252: .getGroupsWithMember(currentUser);
253: for (Iterator iter = groups.iterator(); iter.hasNext();) {
254: Group group = (Group) iter.next();
255: members.addAll(group.getMembers());
256: }
257: } else {
258: members.addAll(site.getMembers());
259: }
260:
261: for (Iterator memb = members.iterator(); memb.hasNext();) {
262: try {
263: Member member = (Member) memb.next();
264: users.add(UserDirectoryService.getUser(member
265: .getUserId()));
266: } catch (UserNotDefinedException e) {
267: logger.warn("Unable to find user: " + e.getId()
268: + " " + e.toString());
269: }
270: }
271: } catch (IdUnusedException e) {
272: logger.error("", e);
273: }
274: return users;
275: }
276:
277: private Cell getCell(Collection cells, Criterion criterion,
278: Level level) {
279: for (Iterator iter = cells.iterator(); iter.hasNext();) {
280: Cell cell = (Cell) iter.next();
281: if (cell.getScaffoldingCell().getRootCriterion().getId()
282: .getValue().equals(criterion.getId().getValue())
283: && cell.getScaffoldingCell().getLevel().getId()
284: .getValue()
285: .equals(level.getId().getValue())) {
286: return cell;
287: }
288: }
289: return null;
290: }
291:
292: public ModelAndView handleRequest(Object requestModel, Map request,
293: Map session, Map application, Errors errors) {
294: Map model = new HashMap();
295: //model.put("view_user", request.get("view_user"));
296: return new ModelAndView("success", model);
297: }
298:
299: public ToolManager getToolManager() {
300: return toolManager;
301: }
302:
303: public void setToolManager(ToolManager toolManager) {
304: this.toolManager = toolManager;
305: }
306: }
|