Added 'redict' endpoint to recreate dictionaries
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ManagementController.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.remote.controller;
11
12 import java.util.Map;
13 import java.util.UUID;
14
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.bind.annotation.RequestMethod;
23 import org.springframework.web.bind.annotation.RequestParam;
24 import org.springframework.web.servlet.ModelAndView;
25
26 import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
27 import eu.etaxonomy.cdm.database.DataSourceInfo;
28 import eu.etaxonomy.cdm.database.DataSourceReloader;
29 import eu.etaxonomy.cdm.remote.controller.util.ProgressMonitorUtil;
30
31 @Controller
32 @RequestMapping(value = {"/manage"})
33 public class ManagementController
34 {
35 public static final Logger logger = Logger.getLogger(ManagementController.class);
36
37 // @Autowired
38 private DataSourceReloader datasoucrceLoader;
39
40 @Autowired
41 public ICdmMassIndexer indexer;
42
43 @Autowired
44 public ProgressMonitorController progressMonitorController;
45
46
47 /**
48 * There should only be one processes operating on the lucene index
49 * therefore the according progress monitor uuid is stored in
50 * this static field.
51 */
52 private static UUID indexMonitorUuid = null;
53
54
55 private static final int DEFAULT_PAGE_SIZE = 25;
56
57 /*
58 * return page not found http error (404) for unknown or incorrect UUIDs
59 * (non-Javadoc)
60 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
61 */
62 //@RequestMapping(value = { "/manager/datasources/list" }, method = RequestMethod.GET)
63 protected ModelAndView doList(HttpServletRequest request, HttpServletResponse respone) throws Exception {
64
65 ModelAndView mv = new ModelAndView();
66 Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.test();
67 mv.addObject(dataSourceInfos);
68
69 return mv;
70 }
71
72 //@RequestMapping(value = { "/manager/datasources/reload" }, method = RequestMethod.GET)
73 public ModelAndView doReload(HttpServletRequest request, HttpServletResponse respone) throws Exception {
74
75 ModelAndView mv = new ModelAndView();
76 Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.reload();
77 mv.addObject(dataSourceInfos);
78
79 return mv;
80 }
81
82 /**
83 *
84 * Reindex all cdm entities listed in {@link ICdmMassIndexer#indexedClasses()}.
85 * Re-indexing will not purge the index.
86 * @param frontendBaseUrl if the CDM server is running behind a reverse proxy you need
87 * to supply the base URL of web service front-end which is
88 * provided by the proxy server.
89 * @param request
90 * @param respone
91 * @return
92 * @throws Exception
93 */
94 @RequestMapping(value = { "reindex" }, method = RequestMethod.GET)
95 public ModelAndView doReindex(
96 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
97 HttpServletRequest request, HttpServletResponse response) throws Exception {
98
99
100 String processLabel = "Re-indexing";
101 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
102
103 if(!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
104 indexMonitorUuid = progressUtil.registerNewMonitor();
105 Thread subThread = new Thread(){
106 @Override
107 public void run(){
108 indexer.reindex(progressMonitorController.getMonitor(indexMonitorUuid));
109 }
110 };
111 subThread.start();
112 }
113 // send redirect "see other"
114 return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, indexMonitorUuid);
115 }
116
117 /**
118 *
119 * Create dictionaries for all cdm entities listed in {@link ICdmMassIndexer#dictionaryClasses()}.
120 * Re-dicting will not purge the dictionaries.
121 * @param frontendBaseUrl if the CDM server is running behind a reverse proxy you need
122 * to supply the base URL of web service front-end which is
123 * provided by the proxy server.
124 * @param request
125 * @param respone
126 * @return
127 * @throws Exception
128 */
129 @RequestMapping(value = { "redict" }, method = RequestMethod.GET)
130 public ModelAndView doRedict(
131 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
132 HttpServletRequest request, HttpServletResponse response) throws Exception {
133
134
135 String processLabel = "Re-Dicting";
136 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
137
138 if(!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
139 indexMonitorUuid = progressUtil.registerNewMonitor();
140 Thread subThread = new Thread(){
141 @Override
142 public void run(){
143 indexer.createDictionary(progressMonitorController.getMonitor(indexMonitorUuid));
144 }
145 };
146 subThread.start();
147 }
148 // send redirect "see other"
149 return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, indexMonitorUuid);
150 }
151
152 /**
153 * This will wipe out the index.
154 *
155 * @param request
156 * @param respone
157 * @return
158 * @throws Exception
159 */
160 @RequestMapping(value = { "purge" }, method = RequestMethod.GET)
161 public ModelAndView doPurge(
162 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
163 HttpServletRequest request, HttpServletResponse response) throws Exception {
164
165
166 String processLabel = "Purging";
167
168 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
169
170 if(!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
171 indexMonitorUuid = progressUtil.registerNewMonitor();
172 Thread subThread = new Thread(){
173 @Override
174 public void run(){
175 indexer.purge(progressMonitorController.getMonitor(indexMonitorUuid));
176 }
177 };
178 subThread.start();
179 }
180
181 // send redirect "see other"
182 return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, indexMonitorUuid);
183 }
184
185 }
186