Project

General

Profile

Download (9.49 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.cdm.remote.controller;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.UUID;
15

    
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpServletResponse;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Controller;
22
import org.springframework.web.bind.WebDataBinder;
23
import org.springframework.web.bind.annotation.CrossOrigin;
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
import io.swagger.annotations.Api;
37

    
38
@Controller
39
@Api("manage")
40
@CrossOrigin(origins="*")
41
@RequestMapping(value = { "/manage" })
42
public class ManagementController {
43

    
44
    public static final Logger logger = Logger.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
     *
75
     * @see
76
     * org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
77
     * (javax.servlet.http.HttpServletRequest,
78
     * javax.servlet.http.HttpServletResponse)
79
     */
80
    // @RequestMapping(value = { "/manager/datasources/list" }, method =
81
    // RequestMethod.GET)
82
    protected ModelAndView doList(HttpServletRequest request,
83
            HttpServletResponse respone) throws Exception {
84

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

    
89
        return mv;
90
    }
91

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

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

    
102
        return mv;
103
    }
104

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

    
155
    private List<Class<? extends CdmBase>> asList(Class<? extends CdmBase>[] types) {
156

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

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

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

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

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

    
232
        String processLabel = "Purging";
233

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

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

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

    
258
}
(30-30/76)