0b7ca355d920d5e66afbbb4e57caba5715c974d4
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / CdmServiceRequestExecutor.java
1 package eu.etaxonomy.taxeditor.service;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 import org.apache.log4j.Logger;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
9 import org.springframework.remoting.support.RemoteInvocation;
10 import org.springframework.remoting.support.RemoteInvocationResult;
11 import org.springframework.stereotype.Component;
12
13 import eu.etaxonomy.cdm.api.service.UpdateResult;
14 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
15
16 @Component
17 public class CdmServiceRequestExecutor extends CdmAuthenticatedHttpInvokerRequestExecutor {
18
19 private static final Logger logger = Logger.getLogger(CdmServiceRequestExecutor.class);
20
21 @Autowired
22 private ICdmEntitySessionManager cdmEntitySessionManager;
23
24 private RemoteInvocation currentRemoteInvocation;
25
26 @Override
27 protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException {
28 currentRemoteInvocation = invocation;
29 super.writeRemoteInvocation(invocation, os);
30 }
31
32 @Override
33 protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config,
34 java.io.ByteArrayOutputStream baos)
35 throws java.io.IOException,
36 java.lang.ClassNotFoundException {
37 RemoteInvocationResult rir = fromCache(currentRemoteInvocation);
38
39 // if rir is not null at this point we assume that the
40 // the object has already been loaded in the cache and
41 // does not need to be reloaded
42
43 if(rir == null) {
44 logger.info("Remote invoking : " + currentRemoteInvocation.getMethodName() + "@" + config.getServiceUrl());
45 rir = super.doExecuteRequest(config, baos);
46 if(rir.getValue() != null && !rir.hasException()) {
47 if("merge".equals(currentRemoteInvocation.getMethodName()) ||
48 "save".equals(currentRemoteInvocation.getMethodName()) ||
49 "findWithUpdate".equals(currentRemoteInvocation.getMethodName())) {
50 rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), true));
51 } else if(rir.getValue() instanceof UpdateResult){
52 UpdateResult result = (UpdateResult)rir.getValue();
53 if(result.isOk()){
54 cdmEntitySessionManager.load(result, true);
55 }
56 } else {
57 rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), false));
58 }
59 }
60 cache(currentRemoteInvocation, rir);
61 }
62 currentRemoteInvocation = null;
63
64 return rir;
65 }
66
67 public void cache(RemoteInvocation ri, RemoteInvocationResult rir) {
68
69 }
70
71 public RemoteInvocationResult fromCache(RemoteInvocation ri) {
72 return null;
73 }
74 }