001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/matrix/tool/src/java/org/theospi/portfolio/matrix/control/AttachArtifactController.java $
003: * $Id:AttachArtifactController.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.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
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.content.api.FilePickerHelper;
033: import org.sakaiproject.entity.api.EntityManager;
034: import org.sakaiproject.entity.api.Reference;
035: import org.sakaiproject.metaobj.shared.mgt.IdManager;
036: import org.sakaiproject.metaobj.shared.model.Id;
037: import org.sakaiproject.metaobj.utils.mvc.intf.CancelableController;
038: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
039: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
040: import org.sakaiproject.site.cover.SiteService;
041: import org.sakaiproject.tool.api.SessionManager;
042: import org.sakaiproject.tool.api.ToolSession;
043: import org.springframework.validation.Errors;
044: import org.springframework.web.servlet.ModelAndView;
045: import org.theospi.portfolio.matrix.MatrixManager;
046: import org.theospi.portfolio.matrix.WizardPageHelper;
047: import org.theospi.portfolio.matrix.model.Attachment;
048: import org.theospi.portfolio.matrix.model.WizardPage;
049:
050: public class AttachArtifactController implements Controller,
051: LoadObjectController, CancelableController {
052: protected final transient Log logger = LogFactory
053: .getLog(getClass());
054: private SessionManager sessionManager;
055: private MatrixManager matrixManager;
056: private IdManager idManager;
057: private ContentHostingService contentHosting;
058: private EntityManager entityManager;
059: public static final Object ATTACH_ARTIFACT_FORM = "attachArtifactForm";
060:
061: public Object fillBackingObject(Object incomingModel, Map request,
062: Map session, Map application) throws Exception {
063: CellAndNodeForm form = (CellAndNodeForm) incomingModel;
064: String page_id = (String) request.get("page_id");
065: if (page_id != null) {
066: form.setPage_id(page_id);
067: session.put(getModelName(), form);
068:
069: WizardPage page = matrixManager.getWizardPage(idManager
070: .getId(page_id));
071:
072: Set attachments = page.getAttachments();
073: List files = new ArrayList();
074: for (Iterator iter = attachments.iterator(); iter.hasNext();) {
075: Attachment att = (Attachment) iter.next();
076: String id = getContentHosting().resolveUuid(
077: att.getArtifactId().getValue());
078: Reference ref = getEntityManager().newReference(
079: contentHosting.getReference(id));
080: files.add(ref);
081: }
082:
083: session
084: .put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
085: files);
086:
087: //Start in user's resources area
088: //osp-ui-05
089: String siteId = SiteService
090: .getUserSiteId(getSessionManager()
091: .getCurrentSessionUserId());
092: String collectionId = getContentHosting()
093: .getSiteCollection(siteId);
094: session.put(FilePickerHelper.DEFAULT_COLLECTION_ID,
095: collectionId);
096: } else {
097: form = (CellAndNodeForm) session.get(getModelName());
098: }
099: return form;
100: }
101:
102: protected String getModelName() {
103: return this .getClass().getName() + ".model";
104: }
105:
106: public ModelAndView handleRequest(Object requestModel, Map request,
107: Map session, Map application, Errors errors) {
108: CellAndNodeForm form = (CellAndNodeForm) requestModel;
109: WizardPage page = (WizardPage) session
110: .get(WizardPageHelper.WIZARD_PAGE);
111: boolean sessionPage = true;
112: if (page == null) {
113: sessionPage = false;
114: //page = getMatrixManager().getWizardPage(getIdManager().getId(form.getPage_id()));
115: }
116: page = getMatrixManager().getWizardPage(
117: getIdManager().getId(form.getPage_id()));
118: attachArtifacts(session, page);
119: if (sessionPage) {
120: session.put(WizardPageHelper.WIZARD_PAGE,
121: getMatrixManager().getWizardPage(page.getId()));
122: }
123: session.remove(getModelName());
124: request.put(ATTACH_ARTIFACT_FORM, form);
125: // track all the attachments here...
126: return new ModelAndView("success", "page_id", page.getId());
127: }
128:
129: protected void attachArtifacts(Map session, WizardPage page) {
130: List files = (List) session
131: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
132: Set attachments = page.getAttachments();
133: List existing = new ArrayList();
134: for (Iterator i = attachments.iterator(); i.hasNext();) {
135: Attachment attach = (Attachment) i.next();
136: existing.add(attach.getArtifactId());
137: }
138:
139: for (Iterator i = files.iterator(); i.hasNext();) {
140: Object node = i.next();
141: Reference ref = null;
142: if (node instanceof Reference) {
143: ref = (Reference) node;
144: }
145: Attachment attachment = getMatrixManager().attachArtifact(
146: page.getId(), ref);
147: existing.remove(attachment.getArtifactId());
148: }
149:
150: for (Iterator i = existing.iterator(); i.hasNext();) {
151: Id oldAttachment = (Id) i.next();
152: getMatrixManager().detachArtifact(page.getId(),
153: oldAttachment);
154: }
155:
156: session.remove(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
157: }
158:
159: public boolean isCancel(Map request) {
160: ToolSession toolSession = getSessionManager()
161: .getCurrentToolSession();
162: boolean returned = toolSession
163: .getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) != null;
164: return returned;
165: }
166:
167: public ModelAndView processCancel(Map request, Map session,
168: Map application, Object command, Errors errors)
169: throws Exception {
170: CellAndNodeForm form = (CellAndNodeForm) command;
171: session.remove(getModelName());
172:
173: Map model = new Hashtable();
174: model.put("page_id", form.getPage_id());
175: model.put("readOnlyMatrix", new Boolean("false"));
176:
177: return new ModelAndView("cancel", model);
178: }
179:
180: public SessionManager getSessionManager() {
181: return sessionManager;
182: }
183:
184: public void setSessionManager(SessionManager sessionManager) {
185: this .sessionManager = sessionManager;
186: }
187:
188: public ContentHostingService getContentHosting() {
189: return contentHosting;
190: }
191:
192: public void setContentHosting(ContentHostingService contentHosting) {
193: this .contentHosting = contentHosting;
194: }
195:
196: public IdManager getIdManager() {
197: return idManager;
198: }
199:
200: public void setIdManager(IdManager idManager) {
201: this .idManager = idManager;
202: }
203:
204: public MatrixManager getMatrixManager() {
205: return matrixManager;
206: }
207:
208: public void setMatrixManager(MatrixManager matrixManager) {
209: this .matrixManager = matrixManager;
210: }
211:
212: public EntityManager getEntityManager() {
213: return entityManager;
214: }
215:
216: public void setEntityManager(EntityManager entityManager) {
217: this.entityManager = entityManager;
218: }
219: }
|