c2a2eb8cdc8818ad4460aa597ad84118e13d72ca
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / RemoteInvocationTermCacher.java
1 /**
2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.service;
10
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.remoting.support.RemoteInvocation;
19 import org.springframework.remoting.support.RemoteInvocationResult;
20
21 import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
22 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
23 import eu.etaxonomy.cdm.model.term.TermType;
24
25
26 public class RemoteInvocationTermCacher implements IRemoteInvocationTermCacher {
27
28 private static final Logger logger = Logger.getLogger(RemoteInvocationTermCacher.class);
29
30 private static Map<TermType, RemoteInvocationResult> termTypeMap = new HashMap<>();
31
32 private static CdmServiceCacher cdmServiceCacher;
33
34 public static void setDefaultCacher(CdmServiceCacher csc) {
35 cdmServiceCacher = csc;
36 }
37
38 @Override
39 public void cacheTerms(RemoteInvocation ri, RemoteInvocationResult rir) {
40 if(cdmServiceCacher != null) {
41 if(ri.getMethodName().equals("listByTermType")) {
42 if(ri.getArguments()[1] == null) {
43 Set<DefinedTermBase<?>> terms = new HashSet<>();
44 if(rir.getValue() != null) {
45 terms.addAll((List<DefinedTermBase<?>>)rir.getValue());
46
47 for(DefinedTermBase<?> term : terms) {
48 cdmServiceCacher.load(term);
49 }
50 termTypeMap.put((TermType)ri.getArguments()[0], rir);
51 }
52 }
53 }
54 } else {
55 logger.info("Default CdmServiceCacher is null. Cannot cache terms");
56 }
57 }
58
59 @Override
60 public RemoteInvocationResult termsFromCache(RemoteInvocation ri) {
61 return termTypeMap.get(ri.getArguments()[0]);
62 }
63 }