2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.service
.longrunningService
;
11 import java
.io
.Serializable
;
12 import java
.util
.Collection
;
14 import java
.util
.UUID
;
16 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
17 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
18 import org
.springframework
.stereotype
.Service
;
19 import org
.springframework
.transaction
.annotation
.Transactional
;
21 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptiveDataSetService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
26 import eu
.etaxonomy
.cdm
.api
.service
.config
.CacheUpdaterConfigurator
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteDescriptiveDataSetConfigurator
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.ForSubtreeConfiguratorBase
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.PublishForSubtreeConfigurator
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.SecundumForSubtreeConfigurator
;
31 import eu
.etaxonomy
.cdm
.api
.service
.config
.SortIndexUpdaterConfigurator
;
32 import eu
.etaxonomy
.cdm
.api
.service
.description
.DescriptionAggregationBase
;
33 import eu
.etaxonomy
.cdm
.api
.service
.description
.DescriptionAggregationConfigurationBase
;
34 import eu
.etaxonomy
.cdm
.api
.service
.dto
.SpecimenRowWrapperDTO
;
35 import eu
.etaxonomy
.cdm
.common
.JvmLimitsException
;
36 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
37 import eu
.etaxonomy
.cdm
.common
.monitor
.RemotingProgressMonitorThread
;
43 @Service("longRunningTasksService")
44 @Transactional(readOnly
= false)
45 public class LongRunningTasksService
implements ILongRunningTasksService
{
48 private ITaxonNodeService taxonNodeService
;
51 private IDescriptiveDataSetService descriptiveDataSetService
;
54 private IProgressMonitorService progressMonitorService
;
57 private CacheUpdater updater
;
60 private SortIndexUpdaterWrapper sortIndexUpdater
;
63 @Qualifier("cdmRepository")
64 private ICdmRepository repository
;
67 public UUID
monitGetRowWrapper(UUID descriptiveDataSetUuid
) {
68 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
70 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
71 return descriptiveDataSetService
.getRowWrapper(descriptiveDataSetUuid
, monitor
);
74 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
75 monitorThread
.setPriority(3);
76 monitorThread
.start();
82 public <T
extends DescriptionAggregationBase
<T
,C
>, C
extends DescriptionAggregationConfigurationBase
<T
>>
83 UUID
invoke(C config
){
85 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
87 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
88 T task
= config
.getTaskInstance();
89 UpdateResult updateResult
= null;
91 updateResult
= task
.invoke(config
, repository
);
92 for(Exception e
: updateResult
.getExceptions()) {
93 monitor
.addReport(e
.getMessage());
95 } catch (JvmLimitsException e
) {
96 monitor
.warning("Memory problem. Java Virtual Machine limits exceeded. Task was interrupted", e
);
98 monitor
.setResult(updateResult
);
103 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
104 monitorThread
.setPriority(2);
105 monitorThread
.start();
111 public UUID
addRowWrapperToDataset(Collection
<SpecimenRowWrapperDTO
> wrapper
, UUID datasetUuid
){
112 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
114 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
115 UpdateResult updateResult
= descriptiveDataSetService
.addRowWrapperToDataset(wrapper
, datasetUuid
);
116 for(Exception e
: updateResult
.getExceptions()) {
117 monitor
.addReport(e
.getMessage());
119 monitor
.setResult(updateResult
);
123 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
124 monitorThread
.setPriority(2);
125 monitorThread
.start();
130 public UUID
generatePolytomousKey(UUID datasetUuid
, UUID taxonUuid
){
131 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
133 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
134 UpdateResult updateResult
= descriptiveDataSetService
.generatePolytomousKey(datasetUuid
, taxonUuid
);
135 for(Exception e
: updateResult
.getExceptions()) {
136 monitor
.addReport(e
.getMessage());
138 monitor
.setResult(updateResult
);
143 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
144 monitorThread
.setPriority(2);
145 monitorThread
.start();
150 public UUID
deleteDescriptiveDataset(UUID datasetUuid
, DeleteDescriptiveDataSetConfigurator config
){
151 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
153 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
154 UpdateResult updateResult
= descriptiveDataSetService
.delete(datasetUuid
, config
, monitor
);
155 for(Exception e
: updateResult
.getExceptions()) {
156 monitor
.addReport(e
.getMessage());
158 monitor
.setResult(updateResult
);
163 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
164 monitorThread
.setPriority(2);
165 monitorThread
.start();
170 public UUID
monitLongRunningTask(ForSubtreeConfiguratorBase config
) {
172 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
174 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
175 config
.setMonitor(monitor
);
177 UpdateResult result
= updateData(config
);
178 for(Exception e
: result
.getExceptions()) {
179 monitor
.addReport(e
.getMessage());
181 monitor
.setResult(result
);
185 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
186 monitorThread
.setPriority(2);
187 monitorThread
.start();
191 private UpdateResult
updateData(ForSubtreeConfiguratorBase config
){
192 if (config
instanceof SecundumForSubtreeConfigurator
){
193 return taxonNodeService
.setSecundumForSubtree((SecundumForSubtreeConfigurator
)config
);
195 return taxonNodeService
.setPublishForSubtree((PublishForSubtreeConfigurator
) config
);
200 public UUID
monitLongRunningTask(Set
<UUID
> movingUuids
, UUID targetTreeNodeUuid
, int movingType
) {
202 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
204 public Serializable
doRun(IRemotingProgressMonitor remotingMonitor
) {
207 result
= taxonNodeService
.moveTaxonNodes(movingUuids
,targetTreeNodeUuid
, movingType
, remotingMonitor
);
208 for(Exception e
: result
.getExceptions()) {
209 remotingMonitor
.addReport(e
.getMessage());
211 remotingMonitor
.setResult(result
);
215 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
216 monitorThread
.setPriority(2);
217 monitorThread
.start();
222 public UUID
monitLongRunningTask(CacheUpdaterConfigurator configurator
) {
223 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
225 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
228 configurator
.setMonitor(monitor
);
230 result
= updater
.doInvoke(configurator
);
232 for(Exception e
: result
.getExceptions()) {
233 monitor
.addReport(e
.getMessage());
235 monitor
.setResult(result
);
239 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
240 monitorThread
.setPriority(2);
241 monitorThread
.start();
246 public UUID
monitLongRunningTask(SortIndexUpdaterConfigurator configurator
) {
247 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
249 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
251 configurator
.setMonitor(monitor
);
252 UpdateResult result
= sortIndexUpdater
.doInvoke(configurator
);
254 for(Exception e
: result
.getExceptions()) {
255 monitor
.addReport(e
.getMessage());
257 monitor
.setResult(result
);
261 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
262 monitorThread
.setPriority(2);
263 monitorThread
.start();