1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 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.api.service;
|
11
|
|
12
|
import java.io.Serializable;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.security.access.prepost.PreAuthorize;
|
19
|
import org.springframework.stereotype.Service;
|
20
|
import org.springframework.transaction.annotation.Transactional;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.UpdateResult.Status;
|
23
|
import eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier;
|
24
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
25
|
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28
|
|
29
|
/**
|
30
|
* @author cmathew
|
31
|
* @date 26 Jun 2015
|
32
|
*
|
33
|
*/
|
34
|
@Service
|
35
|
public class TestServiceImpl implements ITestService {
|
36
|
|
37
|
private static final Logger logger = Logger.getLogger(TestServiceImpl.class);
|
38
|
|
39
|
@Autowired
|
40
|
ITaxonNodeService taxonNodeService;
|
41
|
|
42
|
@Autowired
|
43
|
IProgressMonitorService progressMonitorService;
|
44
|
|
45
|
/* (non-Javadoc)
|
46
|
* @see eu.etaxonomy.cdm.api.service.ITestService#wait(int)
|
47
|
*/
|
48
|
@Override
|
49
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
50
|
public void waitFor(long timeToWaitInMs) throws InterruptedException {
|
51
|
Thread.sleep(timeToWaitInMs);
|
52
|
}
|
53
|
|
54
|
/* (non-Javadoc)
|
55
|
* @see eu.etaxonomy.cdm.api.service.ITestService#returnResult(eu.etaxonomy.cdm.api.service.UpdateResult)
|
56
|
*/
|
57
|
@Override
|
58
|
public UpdateResult returnResult(UpdateResult result) {
|
59
|
return result;
|
60
|
}
|
61
|
|
62
|
/* (non-Javadoc)
|
63
|
* @see eu.etaxonomy.cdm.api.service.ITestService#throwException(java.lang.Exception)
|
64
|
*/
|
65
|
@Override
|
66
|
public UpdateResult throwException(Exception ex) {
|
67
|
throw new RuntimeException(ex);
|
68
|
}
|
69
|
|
70
|
/* (non-Javadoc)
|
71
|
* @see eu.etaxonomy.cdm.api.service.ITestService#addChild(eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier)
|
72
|
*/
|
73
|
@Override
|
74
|
@Transactional(readOnly = false)
|
75
|
public UpdateResult addChild(CdmEntityIdentifier taxonNodeCei) {
|
76
|
TaxonNode taxonNode = taxonNodeService.find(taxonNodeCei.getId());
|
77
|
taxonNode.addChildTaxon(Taxon.NewInstance(null, null), null, null);
|
78
|
UpdateResult result = new UpdateResult();
|
79
|
result.addUpdatedCdmId(taxonNodeCei);
|
80
|
result.setStatus(Status.OK);
|
81
|
return result;
|
82
|
}
|
83
|
|
84
|
|
85
|
@Override
|
86
|
public UUID monitLongRunningMethod(final RuntimeException ex, final List<String> feedbacks, final long feedbackWaitTimeout) {
|
87
|
|
88
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
89
|
@Override
|
90
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
91
|
Serializable result = longRunningMethod(monitor, ex, feedbacks, feedbackWaitTimeout);
|
92
|
if(!monitor.isCanceled()) {
|
93
|
monitor.addReport("Report");
|
94
|
}
|
95
|
return result;
|
96
|
}
|
97
|
};
|
98
|
|
99
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
100
|
monitorThread.setPriority(3);
|
101
|
monitorThread.start();
|
102
|
return uuid;
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public String longRunningMethod(IRemotingProgressMonitor monitor,
|
107
|
RuntimeException ex,
|
108
|
List<String> feedbacks,
|
109
|
long feedbackWaitTimeout) {
|
110
|
int noOfSteps = 10;
|
111
|
int stepToThrowException = noOfSteps / 2;
|
112
|
int stepToWaitForFeedback = noOfSteps / 2;
|
113
|
monitor.beginTask("Long Running Task", noOfSteps);
|
114
|
for(int i=0; i<noOfSteps; i++) {
|
115
|
try {
|
116
|
Thread.sleep(1000);
|
117
|
if(i == stepToThrowException && ex != null) {
|
118
|
throw ex;
|
119
|
}
|
120
|
if(feedbacks != null && feedbacks.size() > 0 && i == stepToWaitForFeedback) {
|
121
|
for(String feedback : feedbacks) {
|
122
|
logger.warn("Monitor waiting for feedback");
|
123
|
if(feedbackWaitTimeout <= 0) {
|
124
|
monitor.waitForFeedback();
|
125
|
} else {
|
126
|
monitor.waitForFeedback(feedbackWaitTimeout);
|
127
|
}
|
128
|
logger.warn(" .. feedback received : " + monitor.getFeedback());
|
129
|
monitor.addReport(feedback);
|
130
|
}
|
131
|
}
|
132
|
if(monitor.isCanceled()) {
|
133
|
return "Cancelled";
|
134
|
}
|
135
|
} catch (InterruptedException e) {
|
136
|
throw ex;
|
137
|
}
|
138
|
monitor.worked(1);
|
139
|
}
|
140
|
monitor.done();
|
141
|
return "Success";
|
142
|
}
|
143
|
|
144
|
}
|