Project

General

Profile

Download (2.29 KB) Statistics
| Branch: | Tag: | Revision:
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.logging.log4j.LogManager;import org.apache.logging.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
/**
27
 * Note by AM (2022-06-15): This caches terms. But it seems that only the method
28
 * "listByTermType" from term service is cached.
29
 */
30
public class RemoteInvocationTermCacher implements IRemoteInvocationTermCacher {
31

    
32
    private static final Logger logger = LogManager.getLogger(RemoteInvocationTermCacher.class);
33

    
34
	private static Map<TermType, RemoteInvocationResult> termTypeMap = new HashMap<>();
35

    
36
	private static CdmServiceCacher cdmServiceCacher;
37

    
38
	public static void setDefaultCacher(CdmServiceCacher csc) {
39
        cdmServiceCacher = csc;
40
    }
41

    
42
	@Override
43
	public void cacheTerms(RemoteInvocation remoteInvoc, RemoteInvocationResult remoteInvocResul) {
44
	    if(cdmServiceCacher != null) {
45
	        if(remoteInvoc.getMethodName().equals("listByTermType")) {
46
	            if(remoteInvoc.getArguments()[1] == null) {
47
	                Set<DefinedTermBase<?>> terms = new HashSet<>();
48
	                if(remoteInvocResul.getValue() != null) {
49
	                    terms.addAll((List<DefinedTermBase<?>>)remoteInvocResul.getValue());
50

    
51
	                    for(DefinedTermBase<?> term : terms) {
52
	                        cdmServiceCacher.load(term);
53
	                    }
54
	                    termTypeMap.put((TermType)remoteInvoc.getArguments()[0], remoteInvocResul);
55
	                }
56
	            }
57
	        }
58
	    } else {
59
	        logger.info("Default CdmServiceCacher is null. Cannot cache terms");
60
	    }
61
	}
62

    
63
	@Override
64
	public  RemoteInvocationResult termsFromCache(RemoteInvocation ri) {
65
		return termTypeMap.get(ri.getArguments()[0]);
66
	}
67
}
(6-6/7)