Project

General

Profile

Download (2.26 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;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.remoting.support.RemoteInvocation;
20
import org.springframework.remoting.support.RemoteInvocationResult;
21

    
22
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
23
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
24
import eu.etaxonomy.cdm.model.term.TermType;
25

    
26

    
27
/**
28
 * Note by AM (2022-06-15): This caches terms. But it seems that only the method
29
 * "listByTermType" from term service is cached.
30
 */
31
public class RemoteInvocationTermCacher implements IRemoteInvocationTermCacher {
32

    
33
    private static final Logger logger = LogManager.getLogger();
34

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

    
37
	private static CdmServiceCacher cdmServiceCacher;
38

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

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

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

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