001: /*
002: * Copyright 2003-2004 Michael Franken, Zilverline.
003: *
004: * The contents of this file, or the files included with this file, are subject to
005: * the current version of ZILVERLINE Collaborative Source License for the
006: * Zilverline Search Engine (the "License"); You may not use this file except in
007: * compliance with the License.
008: *
009: * You may obtain a copy of the License at
010: *
011: * http://www.zilverline.org.
012: *
013: * See the License for the rights, obligations and
014: * limitations governing use of the contents of the file.
015: *
016: * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
017: * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
018: * copyrights in the portions it created. All Rights Reserved.
019: *
020: */
021: package org.zilverline.web;
022:
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.springframework.validation.BindException;
031: import org.springframework.validation.ObjectError;
032: import org.springframework.web.bind.RequestUtils;
033: import org.springframework.web.servlet.ModelAndView;
034: import org.zilverline.core.DocumentCollection;
035: import org.zilverline.core.FileSystemCollection;
036: import org.zilverline.core.IMAPCollection;
037: import org.zilverline.core.IndexException;
038:
039: /**
040: * JavaBean form controller that is used to edit or delete a
041: * <code>Collection</code>.
042: *
043: * @author Michael Franken
044: */
045: public class EditCollectionController extends AbstractZilverController {
046:
047: /**
048: * Adjust the CommandClass based on the implementation used for
049: * DocumentCollection.
050: *
051: * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
052: */
053: protected Object formBackingObject(HttpServletRequest request)
054: throws ServletException {
055: // get the Collection (from the collectionManager) referred to by id in
056: // the request
057: DocumentCollection collection = collectionManager
058: .getCollection(new Long(RequestUtils
059: .getRequiredLongParameter(request,
060: "collectionId")));
061: if (collection instanceof IMAPCollection) {
062: setFormView("collectionIMAPForm");
063: setCommandClass(IMAPCollection.class);
064: setValidator(null);
065: return (IMAPCollection) collection;
066: } else if (collection instanceof FileSystemCollection) {
067: setFormView("collectionForm");
068: setCommandClass(FileSystemCollection.class);
069: setValidator(new CollectionValidator());
070: return (FileSystemCollection) collection;
071: }
072:
073: return (FileSystemCollection) collection;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.springframework.web.servlet.mvc.SimpleFormController#suppressValidation(javax.servlet.http.HttpServletRequest)
080: */
081: protected boolean suppressValidation(HttpServletRequest request) {
082: if (request.getParameter("delete") != null) {
083: log.debug(" Suppressing validation for Delete");
084: return true;
085: }
086: return super .suppressValidation(request);
087: }
088:
089: /** Method updates an existing DocumentCollection. */
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
094: * javax.servlet.http.HttpServletResponse, java.lang.Object,
095: * org.springframework.validation.BindException)
096: */
097: protected ModelAndView onSubmit(HttpServletRequest request,
098: HttpServletResponse response, Object command,
099: BindException errors) throws ServletException {
100: DocumentCollection collection = (DocumentCollection) command;
101: try {
102: if (request.getParameter("delete") != null) {
103: // delegate the delete to the service layer
104: collectionManager.deleteCollection(collection);
105: } else {
106: // delegate the update to the service layer
107: collection.init();
108: }
109: collectionManager.store();
110: } catch (IndexException e) {
111: throw new ServletException(
112: "Error initializing updated index in EditCollectionForm",
113: e);
114: }
115: return new ModelAndView(getSuccessView());
116: }
117: }
|