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