1 |
933331e0
|
Cherian Mathew
|
// $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 |
eeedb131
|
Cherian Mathew
|
import java.io.Serializable;
|
13 |
|
|
import java.util.List;
|
14 |
3087df1b
|
Cherian Mathew
|
import java.util.UUID;
|
15 |
|
|
|
16 |
eeedb131
|
Cherian Mathew
|
import org.apache.log4j.Logger;
|
17 |
3fda6c39
|
Cherian Mathew
|
import org.springframework.beans.factory.annotation.Autowired;
|
18 |
3087df1b
|
Cherian Mathew
|
import org.springframework.security.access.prepost.PreAuthorize;
|
19 |
933331e0
|
Cherian Mathew
|
import org.springframework.stereotype.Service;
|
20 |
3fda6c39
|
Cherian Mathew
|
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 |
3087df1b
|
Cherian Mathew
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
25 |
ac7c4c35
|
Cherian Mathew
|
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
|
26 |
3fda6c39
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28 |
933331e0
|
Cherian Mathew
|
|
29 |
|
|
/**
|
30 |
|
|
* @author cmathew
|
31 |
|
|
* @date 26 Jun 2015
|
32 |
|
|
*
|
33 |
|
|
*/
|
34 |
|
|
@Service
|
35 |
|
|
public class TestServiceImpl implements ITestService {
|
36 |
|
|
|
37 |
eeedb131
|
Cherian Mathew
|
private static final Logger logger = Logger.getLogger(TestServiceImpl.class);
|
38 |
|
|
|
39 |
3fda6c39
|
Cherian Mathew
|
@Autowired
|
40 |
|
|
ITaxonNodeService taxonNodeService;
|
41 |
|
|
|
42 |
3087df1b
|
Cherian Mathew
|
@Autowired
|
43 |
ac7c4c35
|
Cherian Mathew
|
IProgressMonitorService progressMonitorService;
|
44 |
3087df1b
|
Cherian Mathew
|
|
45 |
933331e0
|
Cherian Mathew
|
/* (non-Javadoc)
|
46 |
|
|
* @see eu.etaxonomy.cdm.api.service.ITestService#wait(int)
|
47 |
|
|
*/
|
48 |
|
|
@Override
|
49 |
3087df1b
|
Cherian Mathew
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
50 |
933331e0
|
Cherian Mathew
|
public void waitFor(long timeToWaitInMs) throws InterruptedException {
|
51 |
|
|
Thread.sleep(timeToWaitInMs);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
610855da
|
Cherian Mathew
|
/* (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 |
3fda6c39
|
Cherian Mathew
|
/* (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 |
3087df1b
|
Cherian Mathew
|
|
85 |
|
|
@Override
|
86 |
ac7c4c35
|
Cherian Mathew
|
public UUID monitLongRunningMethod(final RuntimeException ex, final List<String> feedbacks, final long feedbackWaitTimeout) {
|
87 |
|
|
|
88 |
|
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
89 |
3087df1b
|
Cherian Mathew
|
@Override
|
90 |
eeedb131
|
Cherian Mathew
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
91 |
ac7c4c35
|
Cherian Mathew
|
Serializable result = longRunningMethod(monitor, ex, feedbacks, feedbackWaitTimeout);
|
92 |
1c7b9181
|
Cherian Mathew
|
if(!monitor.isCanceled()) {
|
93 |
|
|
monitor.addReport("Report");
|
94 |
|
|
}
|
95 |
3087df1b
|
Cherian Mathew
|
return result;
|
96 |
|
|
}
|
97 |
|
|
};
|
98 |
|
|
|
99 |
ac7c4c35
|
Cherian Mathew
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
100 |
|
|
monitorThread.setPriority(3);
|
101 |
|
|
monitorThread.start();
|
102 |
3087df1b
|
Cherian Mathew
|
return uuid;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
@Override
|
106 |
ac7c4c35
|
Cherian Mathew
|
public String longRunningMethod(IRemotingProgressMonitor monitor,
|
107 |
|
|
RuntimeException ex,
|
108 |
|
|
List<String> feedbacks,
|
109 |
|
|
long feedbackWaitTimeout) {
|
110 |
3087df1b
|
Cherian Mathew
|
int noOfSteps = 10;
|
111 |
|
|
int stepToThrowException = noOfSteps / 2;
|
112 |
eeedb131
|
Cherian Mathew
|
int stepToWaitForFeedback = noOfSteps / 2;
|
113 |
3087df1b
|
Cherian Mathew
|
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 |
eeedb131
|
Cherian Mathew
|
if(feedbacks != null && feedbacks.size() > 0 && i == stepToWaitForFeedback) {
|
121 |
|
|
for(String feedback : feedbacks) {
|
122 |
|
|
logger.warn("Monitor waiting for feedback");
|
123 |
ac7c4c35
|
Cherian Mathew
|
if(feedbackWaitTimeout <= 0) {
|
124 |
|
|
monitor.waitForFeedback();
|
125 |
|
|
} else {
|
126 |
|
|
monitor.waitForFeedback(feedbackWaitTimeout);
|
127 |
|
|
}
|
128 |
eeedb131
|
Cherian Mathew
|
logger.warn(" .. feedback received : " + monitor.getFeedback());
|
129 |
|
|
monitor.addReport(feedback);
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
1c7b9181
|
Cherian Mathew
|
if(monitor.isCanceled()) {
|
133 |
|
|
return "Cancelled";
|
134 |
|
|
}
|
135 |
3087df1b
|
Cherian Mathew
|
} catch (InterruptedException e) {
|
136 |
|
|
throw ex;
|
137 |
|
|
}
|
138 |
|
|
monitor.worked(1);
|
139 |
|
|
}
|
140 |
|
|
monitor.done();
|
141 |
|
|
return "Success";
|
142 |
|
|
}
|
143 |
|
|
|
144 |
933331e0
|
Cherian Mathew
|
}
|