Project

General

Profile

1
package eu.dnetlib.data.objectstore.modular;
2

    
3
import com.google.common.base.Function;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.Sets;
6
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
7
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
8
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
9
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Controller;
15
import org.springframework.ui.ModelMap;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.ResponseBody;
18

    
19
import java.util.List;
20
import java.util.Set;
21

    
22

    
23
@Controller
24
public class ObjectStoreConsistency {
25

    
26
    private static final Log log = LogFactory.getLog(ObjectStoreConsistency.class);
27

    
28
    @Autowired
29
    private ObjectStoreDao objectStoreDao;
30

    
31
    @Autowired
32
    private UniqueServiceLocator serviceLocator;
33

    
34
    private ObjectStoreIntegrityInfo integrityInfo;
35

    
36

    
37
    @RequestMapping(value = "/ui/objectStore/infoConsistency.do")
38
    @ResponseBody
39
    public ObjectStoreIntegrityInfo getConsistencyInfo(final ModelMap map) {
40
        return getIntegrityInfo();
41
    }
42

    
43
    public void refreshConsistency() {
44
        try {
45
            Set<String> profiles = listProfileIds();
46
            Set<String> objectStore = listObjectStoreIds();
47
            Set<String> objectStoreNotInProfile = Sets.newHashSet(objectStore);
48
            objectStoreNotInProfile.removeAll(profiles);
49
            Set<String> profilesNotInObjectStore = Sets.newHashSet(profiles);
50
            profilesNotInObjectStore.removeAll(objectStore);
51
            ObjectStoreIntegrityInfo info = new ObjectStoreIntegrityInfo();
52
            info.setObjectStoreWithoutProfile(objectStoreNotInProfile);
53
            info.setProfileWithoutObjectStore(profilesNotInObjectStore);
54
            this.integrityInfo = info;
55
        } catch (ISLookUpException e) {
56
            log.error("Error on refreshing consistency of objectStore ", e);
57
        }
58

    
59

    
60
    }
61

    
62

    
63
    private Set<String> listObjectStoreIds() {
64
        return Sets.newHashSet(
65
                Lists.transform(objectStoreDao.listObjectStores(), new Function<String, String>() {
66
                    @Override
67
                    public String apply(String input) {
68
                        return StringUtils.substringBefore(input, "_");
69
                    }
70
                }));
71
    }
72

    
73

    
74
    private Set<String> listProfileIds() throws ISLookUpException {
75

    
76
        final ISLookUpService lookupService = serviceLocator.getService(ISLookUpService.class);
77
        final String query = "for $x in collection('/db/DRIVER/ObjectStoreDSResources/ObjectStoreDSResourceType') return $x//RESOURCE_IDENTIFIER/@value/string()";
78

    
79
        List<String> resultList = lookupService.quickSearchProfile(query);
80

    
81

    
82
        return Sets.newHashSet(Lists.transform(resultList, new Function<String, String>() {
83
            @Override
84
            public String apply(String input) {
85
                return StringUtils.substringBefore(input, "_");
86
            }
87
        }));
88

    
89
    }
90

    
91

    
92
    public ObjectStoreIntegrityInfo getIntegrityInfo() {
93
        if (integrityInfo == null) {
94
            refreshConsistency();
95
        }
96
        return integrityInfo;
97
    }
98

    
99
    public void setIntegrityInfo(ObjectStoreIntegrityInfo integrityInfo) {
100
        this.integrityInfo = integrityInfo;
101
    }
102
}
(12-12/15)