View Javadoc

1   /*
2    * Copyright 2003-2004 Michael Franken, Zilverline.
3    *
4    * The contents of this file, or the files included with this file, are subject to
5    * the current version of ZILVERLINE Collaborative Source License for the
6    * Zilverline Search Engine (the "License"); You may not use this file except in
7    * compliance with the License.
8    *
9    * You may obtain a copy of the License at
10   *
11   *     http://www.zilverline.org.
12   *
13   * See the License for the rights, obligations and
14   * limitations governing use of the contents of the file.
15   *
16   * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
17   * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
18   * copyrights in the portions it created. All Rights Reserved.
19   *
20   */
21  package org.zilverline.web;
22  
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.springframework.util.StringUtils;
32  import org.springframework.validation.BindException;
33  import org.springframework.web.bind.RequestUtils;
34  import org.springframework.web.servlet.ModelAndView;
35  
36  import org.zilverline.core.IndexException;
37  
38  /***
39   * JavaBean form controller that is used to update the <code>CollectionManager</code> Extractor Mappings.
40   * 
41   * @author Michael Franken
42   */
43  public class ExtractorMappingsController extends AbstractZilverController {
44      /*
45       * (non-Javadoc)
46       * 
47       * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
48       */
49      protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
50          return collectionManager;
51      }
52  
53      /*** Method updates an existing IndexService's ExtractorFactory. */
54      /*
55       * (non-Javadoc)
56       * 
57       * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
58       *      javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
59       */
60      protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
61          throws ServletException {
62          // reconstruct the extractor map, it contains pairs of (extension,
63          // extractor)
64          // in the request they are related by the fact that the extension is in
65          // a parameter with an integer value
66          // and the extractor contains 'prefix' and corresponding integer value
67  
68          // first get the prefix (posted as hidden field)
69          String prefix = request.getParameter("prefix");
70          if (!StringUtils.hasLength(prefix)) {
71              log.warn("no prefix set");
72              prefix = "select_";
73          }
74  
75          // get keys and values
76          Map reqMap = request.getParameterMap();
77          Iterator iter = reqMap.entrySet().iterator();
78          String[] keys = new String[reqMap.size()];
79          String[] values = new String[reqMap.size()];
80          while (iter.hasNext()) {
81              Map.Entry element = (Map.Entry) iter.next();
82              String key = (String) element.getKey();
83              String value = ((String[]) element.getValue())[0];
84              log.debug("Parsing request for: " + key + ", " + value);
85              try {
86                  if (key.startsWith(prefix)) {
87                      String indexStr = key.substring(prefix.length());
88                      int index = Integer.parseInt(indexStr);
89                      log.debug("Adding " + value + " to values[" + index + "]");
90                      values[index] = value;
91                  } else {
92                      int index = Integer.parseInt(key);
93                      log.debug("Adding " + value + " to keys[" + index + "]");
94                      keys[index] = value;
95                  }
96              }
97              catch (NumberFormatException e) {
98                  // not an extractor related requestParameter
99                  log.debug("Skipping " + key + ", " + value);
100             }
101         }
102 
103         // add the key value pairs to Map, if value contains value
104         Map props = new Properties();
105         for (int i = 0; i < values.length; i++) {
106             if (StringUtils.hasLength(values[i])) {
107                 log.debug("Adding " + keys[i] + "," + values[i] + " to map");
108                 props.put(keys[i], values[i]);
109             } else {
110                 log.debug("Skipping (remove) " + keys[i] + "," + values[i] + " to map");
111             }
112         }
113         collectionManager.getFactory().setCaseSensitive(RequestUtils.getBooleanParameter(request, "casesensitive", false));
114         collectionManager.getFactory().setDefaultFileinfo(RequestUtils.getBooleanParameter(request, "defaultfileinfo", false));
115         collectionManager.getFactory().setMappings(props);
116         try {
117             collectionManager.store();
118         }
119         catch (IndexException e) {
120             throw new ServletException("Error storing new CollectionManager Defaults", e);
121         }
122 
123         return new ModelAndView(getSuccessView());
124     }
125 }