Project

General

Profile

Download (9.11 KB) Statistics
| Branch: | Tag: | Revision:
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 io.swagger.annotations.Api;
13

    
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.UUID;
18

    
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21

    
22
import org.apache.log4j.Logger;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.stereotype.Controller;
25
import org.springframework.web.bind.WebDataBinder;
26
import org.springframework.web.bind.annotation.InitBinder;
27
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMethod;
29
import org.springframework.web.bind.annotation.RequestParam;
30
import org.springframework.web.servlet.ModelAndView;
31

    
32
import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
33
import eu.etaxonomy.cdm.database.DataSourceInfo;
34
import eu.etaxonomy.cdm.database.DataSourceReloader;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.remote.controller.util.ProgressMonitorUtil;
37
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
38

    
39
@Controller
40
@Api("manage")
41
@RequestMapping(value = { "/manage" })
42
public class ManagementController {
43
    public static final Logger logger = Logger
44
            .getLogger(ManagementController.class);
45

    
46
    // @Autowired
47
    private DataSourceReloader datasoucrceLoader;
48

    
49
    @Autowired
50
    public ICdmMassIndexer indexer;
51

    
52
    @Autowired
53
    public ProgressMonitorController progressMonitorController;
54

    
55
    /**
56
     * There should only be one processes operating on the lucene index
57
     * therefore the according progress monitor uuid is stored in this static
58
     * field.
59
     */
60
    private static UUID indexMonitorUuid = null;
61

    
62
    @InitBinder
63
    public void initIndexClassBinder(WebDataBinder binder) {
64
        binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
65
    }
66

    
67
    @InitBinder
68
    public void initIndexArrayBinder(WebDataBinder binder) {
69
        binder.registerCustomEditor(Class[].class, new CdmTypePropertyEditor());
70
    }
71

    
72
    /*
73
     * return page not found http error (404) for unknown or incorrect UUIDs
74
     * (non-Javadoc)
75
     *
76
     * @see
77
     * org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
78
     * (javax.servlet.http.HttpServletRequest,
79
     * javax.servlet.http.HttpServletResponse)
80
     */
81
    // @RequestMapping(value = { "/manager/datasources/list" }, method =
82
    // RequestMethod.GET)
83
    protected ModelAndView doList(HttpServletRequest request,
84
            HttpServletResponse respone) throws Exception {
85

    
86
        ModelAndView mv = new ModelAndView();
87
        Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader.test();
88
        mv.addObject(dataSourceInfos);
89

    
90
        return mv;
91
    }
92

    
93
    // @RequestMapping(value = { "/manager/datasources/reload" }, method =
94
    // RequestMethod.GET)
95
    public ModelAndView doReload(HttpServletRequest request,
96
            HttpServletResponse respone) throws Exception {
97

    
98
        ModelAndView mv = new ModelAndView();
99
        Map<String, DataSourceInfo> dataSourceInfos = datasoucrceLoader
100
                .reload();
101
        mv.addObject(dataSourceInfos);
102

    
103
        return mv;
104
    }
105

    
106
    /**
107
     *
108
     * Reindex all cdm entities listed in
109
     * {@link ICdmMassIndexer#indexedClasses()}. Re-indexing will not purge the
110
     * index.
111
     *
112
     * @param frontendBaseUrl
113
     *            if the CDM server is running behind a reverse proxy you need
114
     *            to supply the base URL of web service front-end which is
115
     *            provided by the proxy server.
116
     * @param request
117
     * @param respone
118
     * @return
119
     * @throws Exception
120
     */
121
    @RequestMapping(value = { "reindex" }, method = RequestMethod.GET)
122
    public synchronized ModelAndView doReindex(
123
             @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
124
             @RequestParam(value = "type", required = false) Class<? extends CdmBase>[] types,
125
             @RequestParam(value = "priority", required = false) Integer priority,
126
            HttpServletRequest request, HttpServletResponse response)
127
            throws Exception {
128

    
129
        final List<Class<? extends CdmBase>> typeSet = asList(types);
130

    
131
        String processLabel = "Re-indexing";
132

    
133
        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
134
                progressMonitorController);
135

    
136
        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
137
            indexMonitorUuid = progressUtil.registerNewMonitor();
138
            Thread subThread = new Thread() {
139
                @Override
140
                public void run() {
141
                    indexer.reindex(typeSet, progressMonitorController.getMonitor(indexMonitorUuid));
142
                }
143
            };
144
            if (priority == null) {
145
                priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
146
            }
147
            subThread.setPriority(priority);
148
            subThread.start();
149
        }
150
        // send redirect "see other"
151
        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
152
                response, processLabel, indexMonitorUuid);
153
    }
154

    
155
    /**
156
     * @param types
157
     */
158
    private List<Class<? extends CdmBase>> asList(Class<? extends CdmBase>[] types) {
159

    
160
    	List<Class<? extends CdmBase>> typeList = null;
161
        if(types != null) {
162
            typeList = new ArrayList<Class<? extends CdmBase>>();
163
            for (Class<? extends CdmBase> type : types) {
164
                if(type != null && ! typeList.contains(type)) {
165
                    typeList.add(type);
166
                }
167
            }
168
        }
169
        return typeList;
170
    }
171

    
172
    /**
173
     *
174
     * Create dictionaries for all cdm entities listed in
175
     * {@link ICdmMassIndexer#dictionaryClasses()}. Re-dicting will not purge
176
     * the dictionaries.
177
     *
178
     * @param frontendBaseUrl
179
     *            if the CDM server is running behind a reverse proxy you need
180
    *            to supply the base URL of web service front-end which is
181
    *            provided by the proxy server.
182
    * @param request
183
    * @param respone
184
    * @return
185
    * @throws Exception
186
    */
187
   @RequestMapping(value = { "redict" }, method = RequestMethod.GET)
188
    public synchronized ModelAndView doRedict(
189
            @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
190
            @RequestParam(value = "priority", required = false) Integer priority,
191
            HttpServletRequest request, HttpServletResponse response)
192
            throws Exception {
193

    
194
       String processLabel = "Re-Dicting";
195
        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
196
                progressMonitorController);
197

    
198
        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
199
           indexMonitorUuid = progressUtil.registerNewMonitor();
200
            Thread subThread = new Thread() {
201
               @Override
202
                public void run() {
203
                    indexer.createDictionary(progressMonitorController
204
                            .getMonitor(indexMonitorUuid));
205
               }
206
           };
207
            if (priority == null) {
208
               priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
209
           }
210
           subThread.setPriority(priority);
211
           subThread.start();
212
       }
213
       // send redirect "see other"
214
        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
215
                response, processLabel, indexMonitorUuid);
216
   }
217

    
218
    /**
219
     * This will wipe out the index.
220
     *
221
     * @param request
222
     * @param respone
223
     * @return
224
     * @throws Exception
225
     */
226
    @RequestMapping(value = { "purge" }, method = RequestMethod.GET)
227
    public synchronized ModelAndView doPurge(
228
            @RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
229
            @RequestParam(value = "priority", required = false) Integer priority,
230
            HttpServletRequest request, HttpServletResponse response)
231
            throws Exception {
232

    
233
        String processLabel = "Purging";
234

    
235
        ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(
236
                progressMonitorController);
237

    
238
        if (!progressMonitorController.isMonitorRunning(indexMonitorUuid)) {
239
            indexMonitorUuid = progressUtil.registerNewMonitor();
240
            Thread subThread = new Thread() {
241
                @Override
242
                public void run() {
243
                    indexer.purge(progressMonitorController
244
                            .getMonitor(indexMonitorUuid));
245
                }
246
            };
247
            if (priority == null) {
248
                priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
249
            }
250
            subThread.setPriority(priority);
251
            subThread.start();
252
        }
253

    
254
        // send redirect "see other"
255
        return progressUtil.respondWithMonitor(frontendBaseUrl, request,
256
                response, processLabel, indexMonitorUuid);
257
    }
258

    
259

    
260

    
261
}
(33-33/63)