001: /*******************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/shared/control/XmlControllerBase.java $
003: * $Id: XmlControllerBase.java 21288 2007-02-10 16:50:07Z john.ellis@rsmart.com $
004: * **********************************************************************************
005: *
006: * Copyright (c) 2004, 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.sakaiproject.metaobj.shared.control;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.springframework.validation.Errors;
025: import org.springframework.web.servlet.ModelAndView;
026: import org.sakaiproject.metaobj.shared.model.*;
027: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
028: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactDefinitionManager;
029: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
030: import org.sakaiproject.metaobj.shared.mgt.home.ResourceHelperArtifactHome;
031: import org.sakaiproject.metaobj.shared.FormHelper;
032: import org.sakaiproject.content.api.*;
033: import org.sakaiproject.content.cover.ContentHostingService;
034: import org.sakaiproject.tool.api.ToolSession;
035: import org.sakaiproject.tool.api.SessionManager;
036: import org.sakaiproject.entity.api.Reference;
037: import org.sakaiproject.entity.api.ResourceProperties;
038: import org.sakaiproject.entity.cover.EntityManager;
039:
040: import java.util.*;
041:
042: public class XmlControllerBase {
043: protected final Log logger = LogFactory.getLog(getClass());
044: private HomeFactory homeFactory;
045: private XmlValidator validator = null;
046: private StructuredArtifactDefinitionManager structuredArtifactDefinitionManager;
047: private static final String FILE_ATTACHMENTS_FIELD = "org.sakaiproject.metaobj.shared.control.XmlControllerBase.field";
048:
049: protected ModelAndView handleNonSubmit(ElementBean bean,
050: Map request, Map session, Map application, Errors errors) {
051: return handleNonSubmit(bean, request, session, application,
052: errors, new Hashtable());
053: }
054:
055: protected ModelAndView handleNonSubmit(ElementBean bean,
056: Map request, Map session, Map application, Errors errors,
057: Map model) {
058: if (request.get("backButton") != null) {
059: session.remove(EditedArtifactStorage.STORED_ARTIFACT_FLAG);
060: session
061: .remove(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
062: return new ModelAndView("back");
063: }
064:
065: EditedArtifactStorage sessionBean = (EditedArtifactStorage) session
066: .get(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
067:
068: session.put(EditedArtifactStorage.STORED_ARTIFACT_FLAG, "true");
069:
070: if (sessionBean == null) {
071: StructuredArtifact artifact = (StructuredArtifact) bean;
072: sessionBean = new EditedArtifactStorage(artifact
073: .getCurrentSchema(), artifact);
074: session
075: .put(
076: EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY,
077: sessionBean);
078: }
079:
080: if ((request.get("editButton") != null && request.get(
081: "editButton").toString().length() > 0)
082: || request.get("addButton") != null) {
083: handleEditAdd(bean, sessionBean, request, session,
084: application, errors);
085: } else if (request.get("removeButton") != null
086: && request.get("removeButton").toString().length() > 0) {
087: handleRemove(bean, sessionBean, request, session,
088: application, errors);
089: } else if (request.get("cancelNestedButton") != null) {
090: sessionBean.popCurrentElement(true);
091: sessionBean.popCurrentPath();
092: } else if (request.get("updateNestedButton") != null) {
093: getValidator().validate(sessionBean.getCurrentElement(),
094: errors, true);
095: if (errors.hasErrors()) {
096: return null;
097: }
098: sessionBean.popCurrentElement();
099: sessionBean.popCurrentPath();
100: } else if (request.get("fileHelper") != null) {
101: return processFileAttachments(request, session, bean,
102: errors);
103: }
104:
105: model.put(EditedArtifactStorage.STORED_ARTIFACT_FLAG, "true");
106:
107: if (request.get("parentId") != null) {
108: model.put("parentId", getParentId(request));
109: }
110: return new ModelAndView("subList", model);
111: }
112:
113: protected void handleRemove(ElementBean bean,
114: EditedArtifactStorage sessionBean, Map request,
115: Map session, Map application, Errors errors) {
116: ElementListBean parentList = findList(bean, (String) request
117: .get("childPath"));
118: if (parentList == null) {
119: bean.remove((String) request.get("childPath"));
120: } else {
121: int removeIndex = Integer.parseInt((String) request
122: .get("childIndex"));
123: parentList.remove(removeIndex);
124: }
125: }
126:
127: protected void handleEditAdd(ElementBean bean,
128: EditedArtifactStorage sessionBean, Map request,
129: Map session, Map application, Errors errors) {
130: // find the individual element in question
131: ElementListBean parentList = findList(bean, (String) request
132: .get("childPath"));
133: ElementBean newBean = null;
134:
135: if (parentList == null) {
136: newBean = findSubForm(bean, (String) request
137: .get("childPath"));
138: } else if (request.get("editButton") != null
139: && request.get("editButton").toString().length() > 0) {
140: int index = Integer.parseInt((String) request
141: .get("childIndex"));
142: newBean = (ElementBean) parentList.get(index);
143: } else if (request.get("addButton") != null) {
144: newBean = parentList.createBlank();
145: parentList.add(newBean);
146: }
147:
148: sessionBean.pushCurrentElement(newBean);
149: sessionBean.pushCurrentPath((String) request.get("childPath"));
150:
151: }
152:
153: protected ElementListBean findList(ElementBean bean, String path) {
154: StringTokenizer tok = new StringTokenizer(path, ".");
155: ElementBean current = bean;
156:
157: while (tok.hasMoreTokens()) {
158: Object obj = current.get(tok.nextToken());
159: if (obj instanceof ElementBean) {
160: current = (ElementBean) obj;
161: } else if (obj instanceof ElementListBean) {
162: return (ElementListBean) obj;
163: }
164: }
165:
166: return null;
167: }
168:
169: protected ElementBean findSubForm(ElementBean bean, String path) {
170: StringTokenizer tok = new StringTokenizer(path, ".");
171: ElementBean current = bean;
172:
173: while (tok.hasMoreTokens()) {
174: Object obj = current.get(tok.nextToken());
175: if (obj instanceof ElementBean) {
176: return (ElementBean) obj;
177: }
178: }
179:
180: return null;
181: }
182:
183: protected String getSchemaName(Map session) {
184: Object schemaName = session
185: .get(ResourceEditingHelper.CREATE_SUB_TYPE);
186:
187: if (schemaName == null) {
188: return null;
189: }
190: if (schemaName instanceof String) {
191: return (String) schemaName;
192: } else if (schemaName instanceof String[]) {
193: return ((String[]) schemaName)[0];
194: } else {
195: return schemaName.toString();
196: }
197: }
198:
199: protected StructuredArtifactHomeInterface getSchema(Map session) {
200: if (session.get(FormHelper.PREVIEW_HOME_TAG) != null) {
201: return getStructuredArtifactDefinitionManager()
202: .convertToHome(
203: (StructuredArtifactDefinitionBean) session
204: .get(FormHelper.PREVIEW_HOME_TAG));
205: } else if (session.get(ResourceToolAction.ACTION_PIPE) != null) {
206: ResourceToolActionPipe pipe = (ResourceToolActionPipe) session
207: .get(ResourceToolAction.ACTION_PIPE);
208: String schemaName = getSchemaName(session);
209: if (schemaName == null) {
210: ContentEntity entity = pipe.getContentEntity();
211: schemaName = (String) entity.getProperties().get(
212: ResourceProperties.PROP_STRUCTOBJ_TYPE);
213: }
214:
215: StructuredArtifactHomeInterface home = (StructuredArtifactHomeInterface) getHomeFactory()
216: .getHome(schemaName);
217: return new ResourceHelperArtifactHome(home, pipe);
218: } else {
219: return (StructuredArtifactHomeInterface) getHomeFactory()
220: .getHome(getSchemaName(session));
221: }
222: }
223:
224: protected String getParentId(Map request) {
225: Object parentId = request.get("parentId");
226:
227: if (parentId instanceof String) {
228: return (String) parentId;
229: } else if (parentId instanceof String[]) {
230: return ((String[]) parentId)[0];
231: } else {
232: return parentId.toString();
233: }
234: }
235:
236: public HomeFactory getHomeFactory() {
237: return homeFactory;
238: }
239:
240: public void setHomeFactory(HomeFactory homeFactory) {
241: this .homeFactory = homeFactory;
242: }
243:
244: public XmlValidator getValidator() {
245: return validator;
246: }
247:
248: public void setValidator(XmlValidator validator) {
249: this .validator = validator;
250: }
251:
252: public boolean isCancel(Map request) {
253: return request.get("cancelNestedButton") != null;
254: }
255:
256: public ModelAndView processCancel(Map request, Map session,
257: Map application, Object command, Errors errors)
258: throws Exception {
259: return handleNonSubmit((ElementBean) command, request, session,
260: application, errors);
261: }
262:
263: public ModelAndView processFileAttachments(Map request,
264: Map session, ElementBean currentBean, Errors errors) {
265: String fieldName = (String) request.get("childPath");
266: session.put(FILE_ATTACHMENTS_FIELD, fieldName);
267:
268: //ToolSession toolSession = getSessionManager().getCurrentToolSession();
269: List attachmentRefs = new ArrayList();
270:
271: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
272: FilePickerHelper.CARDINALITY_SINGLE);
273: if (List.class.isAssignableFrom(currentBean.getType(fieldName))) {
274: LimitedList currentIds = (LimitedList) currentBean
275: .get(fieldName);
276: attachmentRefs.addAll(convertToRefList(currentIds));
277: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
278: currentIds.getUpperLimit());
279: } else if (currentBean.get(fieldName) != null) {
280: attachmentRefs.add(convertToRef(currentBean.get(fieldName)
281: .toString()));
282: }
283:
284: session.put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
285: attachmentRefs);
286: session.put(FilePickerHelper.START_HELPER, "true");
287:
288: return new ModelAndView("fileHelper");
289: }
290:
291: public void retrieveFileAttachments(Map request, Map session,
292: ElementBean currentBean) {
293: String fieldName = (String) session.get(FILE_ATTACHMENTS_FIELD);
294:
295: if (session.get(FilePickerHelper.FILE_PICKER_CANCEL) == null
296: && session
297: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
298:
299: List refs = (List) session
300: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
301: List ids = convertRefs(refs);
302: // we may convert later, for now, leave backward compatible.
303: // convertToGuidList(refs);
304:
305: if (List.class.isAssignableFrom(currentBean
306: .getType(fieldName))) {
307: List refList = (List) currentBean.get(fieldName);
308: refList.clear();
309: refList.addAll(ids);
310: } else {
311: if (refs.size() > 0) {
312: currentBean.put(fieldName, ids.get(0));
313: } else {
314: currentBean.put(fieldName, "");
315: }
316: }
317: }
318:
319: session.remove(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
320: session.remove(FilePickerHelper.FILE_PICKER_CANCEL);
321: }
322:
323: protected List convertRefs(List refs) {
324: List ret = new ArrayList();
325: for (Iterator<Reference> i = refs.iterator(); i.hasNext();) {
326: ret.add(convertRef(i.next()));
327: }
328: return ret;
329: }
330:
331: protected String convertRef(Reference reference) {
332: return reference.getId();
333: }
334:
335: /**
336: * not currently used... will use if we move to storing id rather than ref in the
337: * xml form data
338: * @param refs
339: * @return list of converted guids
340: */
341: protected List convertToGuidList(List refs) {
342: List idList = new ArrayList();
343:
344: for (Iterator i = refs.iterator(); i.hasNext();) {
345: Reference ref = (Reference) i.next();
346:
347: idList.add(ContentHostingService.getUuid(ref.getId()));
348: }
349:
350: return idList;
351: }
352:
353: /**
354: * @param id string
355: * @return ref
356: */
357: protected Reference convertToRef(String id) {
358: return EntityManager.newReference(ContentHostingService
359: .getReference(id));
360: }
361:
362: /**
363: * @param refs
364: * @return list of refs
365: */
366: protected List convertToRefList(List refs) {
367: List refList = new ArrayList();
368:
369: for (Iterator<ElementBean> i = refs.iterator(); i.hasNext();) {
370: refList.add(convertToRef(i.next().getBaseElement()
371: .getTextTrim()));
372: }
373:
374: return refList;
375: }
376:
377: public StructuredArtifactDefinitionManager getStructuredArtifactDefinitionManager() {
378: return structuredArtifactDefinitionManager;
379: }
380:
381: public void setStructuredArtifactDefinitionManager(
382: StructuredArtifactDefinitionManager structuredArtifactDefinitionManager) {
383: this.structuredArtifactDefinitionManager = structuredArtifactDefinitionManager;
384: }
385:
386: }
|