refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ManagementController.java
index 0382d1f312b508de049d8b94d53d782c9bfd2791..75b85fe4fbff305a1430ffd5ed28f61ad0dbcc67 100644 (file)
@@ -1,16 +1,17 @@
 // $Id$
 /**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
 package eu.etaxonomy.cdm.remote.controller;
 
-import java.net.URL;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 
 import javax.servlet.http.HttpServletRequest;
@@ -19,25 +20,28 @@ import javax.servlet.http.HttpServletResponse;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.servlet.ModelAndView;
 
 import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
-import eu.etaxonomy.cdm.common.monitor.RestServiceProgressMonitor;
 import eu.etaxonomy.cdm.database.DataSourceInfo;
 import eu.etaxonomy.cdm.database.DataSourceReloader;
-import eu.etaxonomy.cdm.remote.json.JsonpRedirect;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.remote.controller.util.ProgressMonitorUtil;
+import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
 
 @Controller
-@RequestMapping(value = {"/manage"})
-public class ManagementController
-{
-    public static final Logger logger = Logger.getLogger(ManagementController.class);
+@RequestMapping(value = { "/manage" })
+public class ManagementController {
+    public static final Logger logger = Logger
+            .getLogger(ManagementController.class);
 
-//    @Autowired
-      private DataSourceReloader datasoucrceLoader;
+    // @Autowired
+    private DataSourceReloader datasoucrceLoader;
 
     @Autowired
     public ICdmMassIndexer indexer;
@@ -45,16 +49,36 @@ public class ManagementController
     @Autowired
     public ProgressMonitorController progressMonitorController;
 
+    /**
+     * There should only be one processes operating on the lucene index
+     * therefore the according progress monitor uuid is stored in this static
+     * field.
+     */
+    private static UUID indexMonitorUuid = null;
 
-    private static final int DEFAULT_PAGE_SIZE = 25;
+    @InitBinder
+    public void initIndexClassBinder(WebDataBinder binder) {
+        binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
+    }
+
+    @InitBinder
+    public void initIndexArrayBinder(WebDataBinder binder) {
+        binder.registerCustomEditor(Class[].class, new CdmTypePropertyEditor());
+    }
 
     /*
      * return page not found http error (404) for unknown or incorrect UUIDs
      * (non-Javadoc)
-     * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+     *
+     * @see
+     * org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
+     * (javax.servlet.http.HttpServletRequest,
+     * javax.servlet.http.HttpServletResponse)
      */
-    //@RequestMapping(value = { "/manager/datasources/list" }, method = RequestMethod.GET)
-    protected ModelAndView doList(HttpServletRequest request, HttpServletResponse respone) throws Exception {
+    // @RequestMapping(value = { "//manager/datasources/list" }, method =
+    // RequestMethod.GET)
+    protected ModelAndView doList(HttpServletRequest request,
+            HttpServletResponse respone) throws Exception {
 
         ModelAndView mv = new ModelAndView();
         Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.test();
@@ -63,11 +87,14 @@ public class ManagementController
         return mv;
     }
 
-    //@RequestMapping(value = { "/manager/datasources/reload" }, method = RequestMethod.GET)
-    public ModelAndView doReload(HttpServletRequest request, HttpServletResponse respone) throws Exception {
+    // @RequestMapping(value = { "//manager/datasources/reload" }, method =
+    // RequestMethod.GET)
+    public ModelAndView doReload(HttpServletRequest request,
+            HttpServletResponse respone) throws Exception {
 
         ModelAndView mv = new ModelAndView();
-        Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.reload();
+        Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader
+                .reload();
         mv.addObject(dataSourceInfos);
 
         return mv;
@@ -75,9 +102,12 @@ public class ManagementController
 
     /**
      *
-     * Reindex all cdm entities litest in {@link ICdmMassIndexer#indexedClasses()}.
-     * Re-indexing will not purge the index.
-     * @param frontendBaseUrl if the CDM server is running behind a reverse proxy you need
+     * Reindex all cdm entities listed in
+     * {@link ICdmMassIndexer#indexedClasses()}. Re-indexing will not purge the
+     * index.
+     *
+     * @param frontendBaseUrl
+     *            if the CDM server is running behind a reverse proxy you need
      *            to supply the base URL of web service front-end which is
      *            provided by the proxy server.
      * @param request
@@ -86,41 +116,100 @@ public class ManagementController
      * @throws Exception
      */
     @RequestMapping(value = { "reindex" }, method = RequestMethod.GET)
-    public ModelAndView doReindex(
+    public synchronized ModelAndView doReindex(
              @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
-             HttpServletRequest request, HttpServletResponse response) throws Exception {
-
-        ModelAndView mv = new ModelAndView();
-
-        final RestServiceProgressMonitor monitor = new RestServiceProgressMonitor();
-        UUID monitorUuid = progressMonitorController.registerMonitor(monitor);
-        String monitorPath = progressMonitorController.pathFor(request, monitorUuid);
-
-        Thread subThread = new Thread(){
-            public void run(){
-                indexer.reindex(monitor);
+             @RequestParam(value = "type", required = false) Class<? extends CdmBase>[] types,
+             @RequestParam(value = "priority", required = false) Integer priority,
+            HttpServletRequest request, HttpServletResponse response)
+            throws Exception {
+
+        final Set<Class<? extends CdmBase>> typeSet = asList(types);
+
+        String processLabel = "Re-indexing";
+        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
+                progressMonitorController);
+
+        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
+            indexMonitorUuid = progressUtil.registerNewMonitor();
+            Thread subThread = new Thread() {
+                @Override
+                public void run() {
+                    indexer.reindex(typeSet, progressMonitorController.getMonitor(indexMonitorUuid));
+                }
+            };
+            if (priority == null) {
+                priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
             }
-        };
-        subThread.start();
-
+            subThread.setPriority(priority);
+            subThread.start();
+        }
         // send redirect "see other"
-        response.setHeader("Location", monitorPath);
-        boolean isJSONP = request.getParameter("callback") != null;
-        if(isJSONP){
-            JsonpRedirect jsonpRedirect;
-            if(frontendBaseUrl != null){
-                jsonpRedirect = new JsonpRedirect(frontendBaseUrl, monitorPath);
-            } else {
-                jsonpRedirect = new JsonpRedirect(request, monitorPath);
-            }
-            mv.addObject(jsonpRedirect);
+        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
+                response, processLabel, indexMonitorUuid);
+    }
 
-        } else {
-            response.sendError(303, "Reindexing started, for progress information please see <a href=\"" + monitorPath + "\">" + monitorPath + "</a>");
+    /**
+     * @param types
+     */
+    private Set<Class<? extends CdmBase>> asList(Class<? extends CdmBase>[] types) {
+        Set<Class<? extends CdmBase>> typeSet = null;
+        if(types != null) {
+            typeSet = new HashSet<Class<? extends CdmBase>>();
+            for (Class<? extends CdmBase> type : types) {
+                if(type != null) {
+                    typeSet.add(type);
+                }
+            }
         }
-        return mv;
+        return typeSet;
     }
 
+    /**
+     *
+     * Create dictionaries for all cdm entities listed in
+     * {@link ICdmMassIndexer#dictionaryClasses()}. Re-dicting will not purge
+     * the dictionaries.
+     *
+     * @param frontendBaseUrl
+     *            if the CDM server is running behind a reverse proxy you need
+    *            to supply the base URL of web service front-end which is
+    *            provided by the proxy server.
+    * @param request
+    * @param respone
+    * @return
+    * @throws Exception
+    */
+   @RequestMapping(value = { "redict" }, method = RequestMethod.GET)
+    public synchronized ModelAndView doRedict(
+            @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
+            @RequestParam(value = "priority", required = false) Integer priority,
+            HttpServletRequest request, HttpServletResponse response)
+            throws Exception {
+
+       String processLabel = "Re-Dicting";
+        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
+                progressMonitorController);
+
+        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
+           indexMonitorUuid = progressUtil.registerNewMonitor();
+            Thread subThread = new Thread() {
+               @Override
+                public void run() {
+                    indexer.createDictionary(progressMonitorController
+                            .getMonitor(indexMonitorUuid));
+               }
+           };
+            if (priority == null) {
+               priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
+           }
+           subThread.setPriority(priority);
+           subThread.start();
+       }
+       // send redirect "see other"
+        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
+                response, processLabel, indexMonitorUuid);
+   }
+
     /**
      * This will wipe out the index.
      *
@@ -130,20 +219,38 @@ public class ManagementController
      * @throws Exception
      */
     @RequestMapping(value = { "purge" }, method = RequestMethod.GET)
-    public ModelAndView doPurge(HttpServletRequest request, HttpServletResponse respone) throws Exception {
-
-        ModelAndView mv = new ModelAndView();
-
-        indexer.purge(null);
-
-        mv.addObject("done!");
-        mv.setViewName("text");
+    public synchronized ModelAndView doPurge(
+            @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
+            @RequestParam(value = "priority", required = false) Integer priority,
+            HttpServletRequest request, HttpServletResponse response)
+            throws Exception {
+
+        String processLabel = "Purging";
+
+        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
+                progressMonitorController);
+
+        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
+            indexMonitorUuid = progressUtil.registerNewMonitor();
+            Thread subThread = new Thread() {
+                @Override
+                public void run() {
+                    indexer.purge(progressMonitorController
+                            .getMonitor(indexMonitorUuid));
+                }
+            };
+            if (priority == null) {
+                priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
+            }
+            subThread.setPriority(priority);
+            subThread.start();
+        }
 
-        return mv;
+        // send redirect "see other"
+        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
+                response, processLabel, indexMonitorUuid);
     }
 
 
 
-
 }
-