refactoring taxon controllers, reducing code duplication
[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.HashSet;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Controller;
23 import org.springframework.web.bind.WebDataBinder;
24 import org.springframework.web.bind.annotation.InitBinder;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestMethod;
27 import org.springframework.web.bind.annotation.RequestParam;
28 import org.springframework.web.servlet.ModelAndView;
29
30 import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
31 import eu.etaxonomy.cdm.database.DataSourceInfo;
32 import eu.etaxonomy.cdm.database.DataSourceReloader;
33 import eu.etaxonomy.cdm.model.common.CdmBase;
34 import eu.etaxonomy.cdm.remote.controller.util.ProgressMonitorUtil;
35 import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
36
37 @Controller
38 @RequestMapping(value = { "/manage" })
39 public class ManagementController {
40 public static final Logger logger = Logger
41 .getLogger(ManagementController.class);
42
43 // @Autowired
44 private DataSourceReloader datasoucrceLoader;
45
46 @Autowired
47 public ICdmMassIndexer indexer;
48
49 @Autowired
50 public ProgressMonitorController progressMonitorController;
51
52 /**
53 * There should only be one processes operating on the lucene index
54 * therefore the according progress monitor uuid is stored in this static
55 * field.
56 */
57 private static UUID indexMonitorUuid = null;
58
59 @InitBinder
60 public void initIndexClassBinder(WebDataBinder binder) {
61 binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
62 }
63
64 @InitBinder
65 public void initIndexArrayBinder(WebDataBinder binder) {
66 binder.registerCustomEditor(Class[].class, new CdmTypePropertyEditor());
67 }
68
69 /*
70 * return page not found http error (404) for unknown or incorrect UUIDs
71 * (non-Javadoc)
72 *
73 * @see
74 * org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
75 * (javax.servlet.http.HttpServletRequest,
76 * javax.servlet.http.HttpServletResponse)
77 */
78 // @RequestMapping(value = { "//manager/datasources/list" }, method =
79 // RequestMethod.GET)
80 protected ModelAndView doList(HttpServletRequest request,
81 HttpServletResponse respone) throws Exception {
82
83 ModelAndView mv = new ModelAndView();
84 Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.test();
85 mv.addObject(dataSourceInfos);
86
87 return mv;
88 }
89
90 // @RequestMapping(value = { "//manager/datasources/reload" }, method =
91 // RequestMethod.GET)
92 public ModelAndView doReload(HttpServletRequest request,
93 HttpServletResponse respone) throws Exception {
94
95 ModelAndView mv = new ModelAndView();
96 Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader
97 .reload();
98 mv.addObject(dataSourceInfos);
99
100 return mv;
101 }
102
103 /**
104 *
105 * Reindex all cdm entities listed in
106 * {@link ICdmMassIndexer#indexedClasses()}. Re-indexing will not purge the
107 * index.
108 *
109 * @param frontendBaseUrl
110 * if the CDM server is running behind a reverse proxy you need
111 * to supply the base URL of web service front-end which is
112 * provided by the proxy server.
113 * @param request
114 * @param respone
115 * @return
116 * @throws Exception
117 */
118 @RequestMapping(value = { "reindex" }, method = RequestMethod.GET)
119 public synchronized ModelAndView doReindex(
120 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
121 @RequestParam(value = "type", required = false) Class<? extends CdmBase>[] types,
122 @RequestParam(value = "priority", required = false) Integer priority,
123 HttpServletRequest request, HttpServletResponse response)
124 throws Exception {
125
126 final Set<Class<? extends CdmBase>> typeSet = asList(types);
127
128 String processLabel = "Re-indexing";
129 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
130 progressMonitorController);
131
132 if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
133 indexMonitorUuid = progressUtil.registerNewMonitor();
134 Thread subThread = new Thread() {
135 @Override
136 public void run() {
137 indexer.reindex(typeSet, progressMonitorController.getMonitor(indexMonitorUuid));
138 }
139 };
140 if (priority == null) {
141 priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
142 }
143 subThread.setPriority(priority);
144 subThread.start();
145 }
146 // send redirect "see other"
147 return progressUtil.respondWithMonitor(frontendBaseUrl, request,
148 response, processLabel, indexMonitorUuid);
149 }
150
151 /**
152 * @param types
153 */
154 private Set<Class<? extends CdmBase>> asList(Class<? extends CdmBase>[] types) {
155 Set<Class<? extends CdmBase>> typeSet = null;
156 if(types != null) {
157 typeSet = new HashSet<Class<? extends CdmBase>>();
158 for (Class<? extends CdmBase> type : types) {
159 if(type != null) {
160 typeSet.add(type);
161 }
162 }
163 }
164 return typeSet;
165 }
166
167 /**
168 *
169 * Create dictionaries for all cdm entities listed in
170 * {@link ICdmMassIndexer#dictionaryClasses()}. Re-dicting will not purge
171 * the dictionaries.
172 *
173 * @param frontendBaseUrl
174 * if the CDM server is running behind a reverse proxy you need
175 * to supply the base URL of web service front-end which is
176 * provided by the proxy server.
177 * @param request
178 * @param respone
179 * @return
180 * @throws Exception
181 */
182 @RequestMapping(value = { "redict" }, method = RequestMethod.GET)
183 public synchronized ModelAndView doRedict(
184 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
185 @RequestParam(value = "priority", required = false) Integer priority,
186 HttpServletRequest request, HttpServletResponse response)
187 throws Exception {
188
189 String processLabel = "Re-Dicting";
190 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
191 progressMonitorController);
192
193 if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
194 indexMonitorUuid = progressUtil.registerNewMonitor();
195 Thread subThread = new Thread() {
196 @Override
197 public void run() {
198 indexer.createDictionary(progressMonitorController
199 .getMonitor(indexMonitorUuid));
200 }
201 };
202 if (priority == null) {
203 priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
204 }
205 subThread.setPriority(priority);
206 subThread.start();
207 }
208 // send redirect "see other"
209 return progressUtil.respondWithMonitor(frontendBaseUrl, request,
210 response, processLabel, indexMonitorUuid);
211 }
212
213 /**
214 * This will wipe out the index.
215 *
216 * @param request
217 * @param respone
218 * @return
219 * @throws Exception
220 */
221 @RequestMapping(value = { "purge" }, method = RequestMethod.GET)
222 public synchronized ModelAndView doPurge(
223 @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
224 @RequestParam(value = "priority", required = false) Integer priority,
225 HttpServletRequest request, HttpServletResponse response)
226 throws Exception {
227
228 String processLabel = "Purging";
229
230 ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
231 progressMonitorController);
232
233 if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
234 indexMonitorUuid = progressUtil.registerNewMonitor();
235 Thread subThread = new Thread() {
236 @Override
237 public void run() {
238 indexer.purge(progressMonitorController
239 .getMonitor(indexMonitorUuid));
240 }
241 };
242 if (priority == null) {
243 priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
244 }
245 subThread.setPriority(priority);
246 subThread.start();
247 }
248
249 // send redirect "see other"
250 return progressUtil.respondWithMonitor(frontendBaseUrl, request,
251 response, processLabel, indexMonitorUuid);
252 }
253
254
255
256 }