Project

General

Profile

1
package eu.dnetlib.openaire.community;
2

    
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.cache.annotation.CacheEvict;
7
import org.springframework.cache.annotation.Cacheable;
8
import org.springframework.stereotype.Component;
9

    
10
import java.text.ParseException;
11
import java.util.*;
12

    
13
@Component
14
public class CommunityClientImpl implements CommunityClient {
15

    
16
    private static final Log log = LogFactory.getLog(CommunityClient.class);
17

    
18
//    @Autowired
19
//    private CommunityApiCore communityApiCore;
20

    
21
    @Autowired
22
    private CommunityCommon communityCommon;
23

    
24
//    final Map<String, Set<String>> inverseListMap = new HashMap<>();
25

    
26
    @Override
27
    @Cacheable("community-cache")
28
    public Map<String, Set<String>> getInverseZenodoCommunityMap () {
29
        log.info("Creating the data structure. Not using cache");
30
        final Map<String, Set<String>> inverseListMap = new HashMap<>();
31

    
32
        try {
33
           final List<CommunitySummary> communityList = communityCommon.listCommunities();
34

    
35
            for(CommunitySummary cs :communityList){
36
                final String communityId =  cs.getId();
37
                List<CommunityZenodoCommunity> czc = communityCommon.getCommunityZenodoCommunities(communityId);
38
                for(CommunityZenodoCommunity zc:czc){
39
                    final String zenodoId = zc.getZenodoid();
40
                    if(!inverseListMap.containsKey(zenodoId)) {
41
                        inverseListMap.put(zc.getZenodoid(),new HashSet<>());
42
                    }
43
                    inverseListMap.get(zc.getZenodoid()).add(communityId);
44
                }
45
                final String zenodoMainCommunity = communityCommon.getCommunity(communityId).getZenodoCommunity();
46
                if(!inverseListMap.containsKey(zenodoMainCommunity)) {
47
                    inverseListMap.put(zenodoMainCommunity,new HashSet<>());
48
                }
49

    
50
                inverseListMap.get(zenodoMainCommunity).add(communityId);
51

    
52
            }
53
        } catch (CommunityException e) {
54
            e.printStackTrace();
55
        } catch (CommunityNotFoundException e) {
56
            e.printStackTrace();
57
        }
58
        return inverseListMap;
59
    }
60

    
61
    @Override
62
    @CacheEvict(value="community-cache",allEntries = true)
63
    public void dropCache(){
64
        log.info("dropping cache");
65
    }
66

    
67

    
68
}
(4-4/18)