001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/wizard/api-impl/src/java/org/theospi/portfolio/wizard/WizardListGenerator.java $
003: * $Id: WizardListGenerator.java 21798 2007-02-21 16:27:08Z 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.wizard;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.sakaiproject.component.cover.ServerConfigurationService;
029: import org.sakaiproject.metaobj.security.AuthenticationManager;
030: import org.sakaiproject.metaobj.shared.mgt.IdManager;
031: import org.sakaiproject.metaobj.shared.model.Id;
032: import org.sakaiproject.metaobj.worksite.mgt.WorksiteManager;
033: import org.sakaiproject.site.api.Site;
034: import org.sakaiproject.site.api.ToolConfiguration;
035: import org.sakaiproject.tool.cover.SessionManager;
036: import org.sakaiproject.user.api.UserNotDefinedException;
037: import org.theospi.portfolio.list.impl.BaseListGenerator;
038: import org.theospi.portfolio.list.intf.ActionableListGenerator;
039: import org.theospi.portfolio.list.intf.CustomLinkListGenerator;
040: import org.theospi.portfolio.matrix.MatrixFunctionConstants;
041: import org.theospi.portfolio.matrix.MatrixManager;
042: import org.theospi.portfolio.matrix.model.Scaffolding;
043: import org.theospi.portfolio.security.AuthorizationFacade;
044: import org.theospi.portfolio.shared.model.SortableListObject;
045: import org.theospi.portfolio.wizard.mgt.WizardManager;
046: import org.theospi.portfolio.wizard.model.Wizard;
047:
048: public class WizardListGenerator extends BaseListGenerator implements
049: ActionableListGenerator, CustomLinkListGenerator {
050:
051: private static final String SITE_ID_PARAM = "selectedSiteId";
052: private static final String WIZARD_ID_PARAM = "wizardId";
053: private static final String MATRIX_ID_PARAM = "matrixId";
054:
055: private WizardManager wizardManager;
056: private MatrixManager matrixManager;
057: private WorksiteManager worksiteManager;
058: private AuthenticationManager authnManager;
059: private IdManager idManager;
060: private AuthorizationFacade authzManager;
061: private List displayTypes;
062:
063: public void init() {
064: logger.info("init()");
065: super .init();
066: }
067:
068: public WorksiteManager getWorksiteManager() {
069: return worksiteManager;
070: }
071:
072: public AuthenticationManager getAuthnManager() {
073: return authnManager;
074: }
075:
076: public void setAuthnManager(AuthenticationManager authnManager) {
077: this .authnManager = authnManager;
078: }
079:
080: public void setWorksiteManager(WorksiteManager worksiteManager) {
081: this .worksiteManager = worksiteManager;
082: }
083:
084: public List getObjects() {
085: List userSites = getWorksiteManager().getUserSites(null,
086: getListService().getSiteTypeList());
087: List<String> siteIds = new ArrayList<String>(userSites.size());
088: List<Id> siteStrIds = new ArrayList<Id>(userSites.size());
089: Map<String, Site> siteMap = new HashMap<String, Site>();
090:
091: for (Iterator i = userSites.iterator(); i.hasNext();) {
092: Site site = (Site) i.next();
093: String siteId = site.getId();
094: siteIds.add(siteId);
095: siteStrIds.add(idManager.getId(siteId));
096: siteMap.put(siteId, site);
097: }
098:
099: List tempWizardList = new ArrayList();
100: if (getDisplayTypes().contains("wizards"))
101: tempWizardList = getWizardManager().findPublishedWizards(
102: siteIds, true);
103: List tempMatrixList = new ArrayList();
104: if (getDisplayTypes().contains("matrices"))
105: tempMatrixList = getMatrixManager()
106: .findPublishedScaffolding(siteStrIds);
107:
108: List<SortableListObject> objects = new ArrayList<SortableListObject>();
109:
110: objects.addAll(verifyWizards(tempWizardList, siteMap));
111: objects.addAll(verifyMatrices(tempMatrixList, siteMap));
112:
113: return objects;
114: }
115:
116: protected List<SortableListObject> verifyWizards(List allWizards,
117: Map siteMap) {
118: List<SortableListObject> retWizards = new ArrayList<SortableListObject>();
119: Map<String, Boolean> sitePermCache = new HashMap<String, Boolean>();
120:
121: for (Iterator i = allWizards.iterator(); i.hasNext();) {
122: Wizard wizard = (Wizard) i.next();
123: String siteId = wizard.getSiteId();
124: //make sure that the target site gets tested
125: getAuthzManager().pushAuthzGroups(siteId);
126:
127: //Need to make sure the current user can actually have one of their own here,
128: // so only check if they can "use"
129: // But check from the cache first
130: Boolean authzCheck = sitePermCache.get(siteId);
131: if (authzCheck == null) {
132: authzCheck = getAuthzManager().isAuthorized(
133: WizardFunctionConstants.VIEW_WIZARD,
134: idManager.getId(siteId));
135: sitePermCache.put(siteId, authzCheck);
136: logger
137: .debug("Pushing site into cache for WizardListGenerator (wizards): "
138: + siteId);
139: }
140: if (authzCheck) {
141: Site site = (Site) siteMap.get(siteId);
142: try {
143: SortableListObject wiz = new SortableListObject(
144: wizard.getId().getValue(),
145: wizard.getName(), wizard.getDescription(),
146: wizard.getOwner(), site, wizard.getType(),
147: wizard.getModified());
148: retWizards.add(wiz);
149: } catch (UserNotDefinedException e) {
150: logger.warn("User with id "
151: + wizard.getOwner().getId()
152: + " does not exist.");
153: }
154:
155: }
156: }
157: return retWizards;
158: }
159:
160: protected List<SortableListObject> verifyMatrices(List allMatrices,
161: Map siteMap) {
162: List<SortableListObject> retMatrices = new ArrayList<SortableListObject>();
163: Map<String, Boolean> sitePermCache = new HashMap<String, Boolean>();
164:
165: for (Iterator i = allMatrices.iterator(); i.hasNext();) {
166: Scaffolding scaffolding = (Scaffolding) i.next();
167: Id siteId = scaffolding.getWorksiteId();
168: //make sure that the target site gets tested
169: getAuthzManager().pushAuthzGroups(siteId.getValue());
170:
171: //Need to make sure the current user can actually have one of their own here,
172: // so only check if they can "use"
173: // But check from the cache first
174: Boolean authzCheck = sitePermCache.get(siteId.getValue());
175: if (authzCheck == null) {
176: authzCheck = getAuthzManager()
177: .isAuthorized(
178: MatrixFunctionConstants.USE_SCAFFOLDING,
179: siteId);
180: sitePermCache.put(siteId.getValue(), authzCheck);
181: logger
182: .debug("Pushing site into cache for WizardListGenerator (matrices): "
183: + siteId);
184: }
185: if (authzCheck) {
186: Site site = (Site) siteMap.get(siteId.getValue());
187: try {
188: SortableListObject scaff = new SortableListObject(
189: scaffolding.getId().getValue(), scaffolding
190: .getTitle(), scaffolding
191: .getDescription(), scaffolding
192: .getOwner(), site,
193: MatrixFunctionConstants.SCAFFOLDING_PREFIX,
194: null);
195: retMatrices.add(scaff);
196: } catch (UserNotDefinedException e) {
197: logger.warn("User with id "
198: + scaffolding.getOwner().getId()
199: + " does not exist.");
200: }
201:
202: }
203: }
204: return retMatrices;
205: }
206:
207: public boolean isNewWindow(Object entry) {
208: return false;
209: }
210:
211: /**
212: * Create a custom link for enty if it needs
213: * to customize, otherwise, null to use the usual entry
214: *
215: * @param entry
216: * @return link to use or null to use normal redirect link
217: */
218: public String getCustomLink(Object entry) {
219: SortableListObject obj = (SortableListObject) entry;
220: String urlEnd = "";
221: String placement = "";
222:
223: if (obj.getTypeRaw().equals(
224: WizardFunctionConstants.WIZARD_TYPE_HIERARCHICAL)
225: || obj.getTypeRaw().equals(
226: WizardFunctionConstants.WIZARD_TYPE_SEQUENTIAL)) {
227: urlEnd = "/osp.wizard.run.helper/runWizardGuidance?session.CURRENT_WIZARD_ID="
228: + obj.getId()
229: + "&session.WIZARD_USER_ID="
230: + SessionManager.getCurrentSessionUserId()
231: + "&session.WIZARD_RESET_CURRENT=true";
232:
233: ToolConfiguration toolConfig = obj.getSite()
234: .getToolForCommonId("osp.wizard");
235: placement = toolConfig.getId();
236: } else if (obj.getTypeRaw().equals(
237: MatrixFunctionConstants.SCAFFOLDING_PREFIX)) {
238: urlEnd = "/viewMatrix.osp?1=1&scaffolding_id="
239: + obj.getId();
240:
241: ToolConfiguration toolConfig = obj.getSite()
242: .getToolForCommonId("osp.matrix");
243: placement = toolConfig.getId();
244: }
245:
246: String url = ServerConfigurationService.getPortalUrl()
247: + "/directtool/" + placement + urlEnd;
248:
249: return url;
250: }
251:
252: public Map getToolParams(Object entry) {
253: Map<String, Object> params = new HashMap<String, Object>();
254: SortableListObject obj = (SortableListObject) entry;
255:
256: if (obj.getTypeRaw().equals(
257: WizardFunctionConstants.WIZARD_TYPE_HIERARCHICAL)
258: || obj.getTypeRaw().equals(
259: WizardFunctionConstants.WIZARD_TYPE_SEQUENTIAL)) {
260: params.put(WIZARD_ID_PARAM, obj.getId());
261: } else if (obj.getTypeRaw().equals(
262: MatrixFunctionConstants.SCAFFOLDING_PREFIX)) {
263: params.put(MATRIX_ID_PARAM, obj.getId());
264: }
265:
266: params.put(SITE_ID_PARAM, idManager
267: .getId(obj.getSite().getId()));
268: return params;
269: }
270:
271: public ToolConfiguration getToolInfo(Map request) {
272: return null; //To change body of implemented methods use File | Settings | File Templates.
273: }
274:
275: public void setToolState(String toolId, Map request) {
276: //To change body of implemented methods use File | Settings | File Templates.
277: }
278:
279: /**
280: * @return the wizardManager
281: */
282: public WizardManager getWizardManager() {
283: return wizardManager;
284: }
285:
286: /**
287: * @param wizardManager the wizardManager to set
288: */
289: public void setWizardManager(WizardManager wizardManager) {
290: this .wizardManager = wizardManager;
291: }
292:
293: /**
294: * @return the idManager
295: */
296: public IdManager getIdManager() {
297: return idManager;
298: }
299:
300: /**
301: * @param idManager the idManager to set
302: */
303: public void setIdManager(IdManager idManager) {
304: this .idManager = idManager;
305: }
306:
307: /**
308: * @return the authzManager
309: */
310: public AuthorizationFacade getAuthzManager() {
311: return authzManager;
312: }
313:
314: /**
315: * @param authzManager the authzManager to set
316: */
317: public void setAuthzManager(AuthorizationFacade authzManager) {
318: this .authzManager = authzManager;
319: }
320:
321: /**
322: * @return the displayTypes
323: */
324: public List getDisplayTypes() {
325: return displayTypes;
326: }
327:
328: /**
329: * @param displayTypes the displayTypes to set
330: */
331: public void setDisplayTypes(List displayTypes) {
332: this .displayTypes = displayTypes;
333: }
334:
335: /**
336: * @return the matrixManager
337: */
338: public MatrixManager getMatrixManager() {
339: return matrixManager;
340: }
341:
342: /**
343: * @param matrixManager the matrixManager to set
344: */
345: public void setMatrixManager(MatrixManager matrixManager) {
346: this.matrixManager = matrixManager;
347: }
348: }
|