fixing MediaAutoInitializer & fixing bug in AbstractBeanInitializer
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ManagementController.java
index 7905b41d04b8675e47a93f972e031c0f56d332da..d16bec0e5f492bd46cd4a1ad0af8eb747b18801e 100644 (file)
@@ -9,30 +9,27 @@
 */
 package eu.etaxonomy.cdm.remote.controller;
 
+import java.io.IOException;
+import java.net.URL;
 import java.util.Map;
 import java.util.UUID;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.http.protocol.HTTP;
 import org.apache.log4j.Logger;
-import org.hibernate.SessionFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.orm.hibernate3.HibernateTransactionManager;
 import org.springframework.stereotype.Controller;
 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 com.ibm.wsdl.extensions.http.HTTPConstants;
-
-import eu.etaxonomy.cdm.api.service.search.CdmMassIndexer;
 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.controller.util.ProgressMonitorUtil;
 import eu.etaxonomy.cdm.remote.json.JsonpRedirect;
 
 @Controller
@@ -42,7 +39,7 @@ public class ManagementController
     public static final Logger logger = Logger.getLogger(ManagementController.class);
 
 //    @Autowired
-      private DataSourceReloader datasoucrceLoader;
+    private DataSourceReloader datasoucrceLoader;
 
     @Autowired
     public ICdmMassIndexer indexer;
@@ -50,6 +47,13 @@ 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;
 
@@ -80,44 +84,36 @@ public class ManagementController
 
     /**
      *
-     * Reindex all cdm entities litest in {@link ICdmMassIndexer#indexedClasses()}.
+     * 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
      * @param respone
      * @return
      * @throws Exception
      */
     @RequestMapping(value = { "reindex" }, method = RequestMethod.GET)
-    public ModelAndView doReindex(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);
-            }
-        };
-        subThread.start();
-
-        // send redirect "see other"
-        response.setHeader("Location", monitorPath);
-//        response.sendError(303, null);
-        boolean isJSONP = request.getParameter("callback") != null;
-        if(isJSONP){
-            JsonpRedirect jsonpRedirect = new JsonpRedirect(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +  monitorPath);
-            mv.addObject(jsonpRedirect);
-//            response.sendError(303, "{\"redirect\":  \""+ monitorPath + "\"}");
-        } else {
-            response.sendError(303, "Reindexing started, for progress information please see <a href=\"" + monitorPath + "\">" + monitorPath + "</a>");
+    public ModelAndView doReindex(
+             @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
+             HttpServletRequest request, HttpServletResponse response) throws Exception {
+
+
+        String processLabel = "Re-indexing";
+        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
+
+        if(!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
+            indexMonitorUuid = progressUtil.registerNewMonitor();
+            Thread subThread = new Thread(){
+                public void run(){
+                    indexer.reindex(progressMonitorController.getMonitor(indexMonitorUuid));
+                }
+            };
+            subThread.start();
         }
-//        mv.addObject(monitorPath);
-//        mv.setViewName("text");
-        return mv;
+        // send redirect "see other"
+        return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, indexMonitorUuid);
     }
 
     /**
@@ -129,20 +125,28 @@ 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);
+    public ModelAndView doPurge(
+            @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
+            HttpServletRequest request, HttpServletResponse response) throws Exception {
 
-        mv.addObject("done!");
-        mv.setViewName("text");
 
-        return mv;
-    }
+        String processLabel = "Purging";
 
+        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
 
+        if(!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
+            indexMonitorUuid = progressUtil.registerNewMonitor();
+            Thread subThread = new Thread(){
+                public void run(){
+                    indexer.purge(progressMonitorController.getMonitor(indexMonitorUuid));
+                }
+            };
+            subThread.start();
+        }
 
+        // send redirect "see other"
+        return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, indexMonitorUuid);
+    }
 
 }