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
;
11 import java
.io
.Serializable
;
12 import java
.util
.List
;
13 import java
.util
.UUID
;
15 import org
.apache
.logging
.log4j
.LogManager
;
16 import org
.apache
.logging
.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
;
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
;
34 public class TestServiceImpl
implements ITestService
{
36 private static final Logger logger
= LogManager
.getLogger();
39 private ITaxonNodeService taxonNodeService
;
42 private IProgressMonitorService progressMonitorService
;
45 @PreAuthorize("hasRole('ROLE_ADMIN')")
46 public void waitFor(long timeToWaitInMs
) throws InterruptedException
{
47 Thread
.sleep(timeToWaitInMs
);
51 public UpdateResult
returnResult(UpdateResult result
) {
56 public UpdateResult
throwException(Exception ex
) {
57 throw new RuntimeException(ex
);
61 @Transactional(readOnly
= false)
62 public UpdateResult
addChild(CdmEntityIdentifier taxonNodeCei
) {
63 TaxonNode taxonNode
= taxonNodeService
.find(taxonNodeCei
.getId());
64 TaxonNode child
= taxonNode
.addChildTaxon(Taxon
.NewInstance(null, null), null, null);
65 taxonNodeService
.saveOrUpdate(child
);
66 UpdateResult result
= new UpdateResult();
67 result
.addUpdatedCdmId(taxonNodeCei
);
68 result
.setStatus(Status
.OK
);
73 public UUID
monitLongRunningMethod(final RuntimeException ex
, final List
<String
> feedbacks
, final long feedbackWaitTimeout
) {
75 RemotingProgressMonitorThread monitorThread
= new RemotingProgressMonitorThread() {
77 public Serializable
doRun(IRemotingProgressMonitor monitor
) {
78 Serializable result
= longRunningMethod(monitor
, ex
, feedbacks
, feedbackWaitTimeout
);
79 if(!monitor
.isCanceled()) {
80 monitor
.addReport("Report");
86 UUID uuid
= progressMonitorService
.registerNewRemotingMonitor(monitorThread
);
87 monitorThread
.setPriority(3);
88 monitorThread
.start();
93 public String
longRunningMethod(IRemotingProgressMonitor monitor
,
95 List
<String
> feedbacks
,
96 long feedbackWaitTimeout
) {
98 int stepToThrowException
= noOfSteps
/ 2;
99 int stepToWaitForFeedback
= noOfSteps
/ 2;
100 monitor
.beginTask("Long Running Task", noOfSteps
);
101 for(int i
=0; i
<noOfSteps
; i
++) {
104 if(i
== stepToThrowException
&& ex
!= null) {
107 if(feedbacks
!= null && feedbacks
.size() > 0 && i
== stepToWaitForFeedback
) {
108 for(String feedback
: feedbacks
) {
109 logger
.warn("Monitor waiting for feedback");
110 if(feedbackWaitTimeout
<= 0) {
111 monitor
.waitForFeedback();
113 monitor
.waitForFeedback(feedbackWaitTimeout
);
115 logger
.warn(" .. feedback received : " + monitor
.getFeedback());
116 monitor
.addReport(feedback
);
119 if(monitor
.isCanceled()) {
122 } catch (InterruptedException e
) {