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.Handler;
37  import org.zilverline.core.IndexException;
38  
39  /***
40   * JavaBean form controller that is used to update the <code>IndexService</code> Extractor Mappings.
41   * 
42   * @author Michael Franken
43   */
44  public class HandlersController extends AbstractZilverController {
45  
46      /***
47       * Use service directly in form.
48       * 
49       * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
50       */
51      protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
52          return collectionManager.getArchiveHandler();
53      }
54  
55      /*** Method updates an existing SearchService. */
56      /*
57       * (non-Javadoc)
58       * 
59       * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
60       *      javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
61       */
62      protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
63          throws ServletException {
64          Handler thisForm = (Handler) command;
65  
66          // reconstruct the extractor map, it contains pairs of (extension,
67          // extractor)
68          // in the request they are related by the fact that the extension is in
69          // a parameter with an integer value
70          // and the extractor contains 'prefix' and corresponding integer value
71  
72          // first get the prefix (posted as hidden field)
73          String prefix = request.getParameter("prefix");
74          if (!StringUtils.hasLength(prefix)) {
75              log.warn("no prefix set");
76              prefix = "select_";
77          }
78  
79          // get keys and values
80          Map reqMap = request.getParameterMap();
81          Iterator iter = reqMap.entrySet().iterator();
82          String[] keys = new String[reqMap.size()];
83          String[] values = new String[reqMap.size()];
84          while (iter.hasNext()) {
85              Map.Entry element = (Map.Entry) iter.next();
86              String key = (String) element.getKey();
87              String value = ((String[]) element.getValue())[0];
88              log.debug("Parsing request for: " + key + ", " + value);
89              try {
90                  if (key.startsWith(prefix)) {
91                      String indexStr = key.substring(prefix.length());
92                      int index = Integer.parseInt(indexStr);
93                      log.debug("Adding " + value + " to values[" + index + "]");
94                      values[index] = value;
95                  } else {
96                      int index = Integer.parseInt(key);
97                      log.debug("Adding " + value + " to keys[" + index + "]");
98                      keys[index] = value;
99                  }
100             }
101             catch (NumberFormatException e) {
102                 // not an extractor related requestParameter
103                 log.debug("Skipping " + key + ", " + value);
104             }
105         }
106 
107         // add the key value pairs to Map, if key contains value
108         Map props = new Properties();
109         for (int i = 0; i < values.length; i++) {
110             if (StringUtils.hasLength(keys[i])) {
111                 log.debug("Adding " + keys[i] + "," + values[i] + " to map");
112                 props.put(keys[i], values[i]);
113             } else {
114                 log.debug("Skipping (remove) " + keys[i] + "," + values[i] + " to map");
115             }
116         }
117         // set caseSensitivity before setting the map
118         thisForm.setCaseSensitive(RequestUtils.getBooleanParameter(request, "casesensitive", false));
119         // add the Map
120         thisForm.setMappings(props);
121         collectionManager.getArchiveHandler().setCaseSensitive(RequestUtils.getBooleanParameter(request, "casesensitive", false));
122         collectionManager.getArchiveHandler().setMappings(props);
123         try {
124             collectionManager.store();
125         }
126         catch (IndexException e) {
127             throw new ServletException("Error storing new Search Defaults", e);
128         }
129 
130         return new ModelAndView(getSuccessView());
131     }
132 
133 }