Project

General

Profile

« Previous | Next » 

Revision 56643

View differences:

modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/UserService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import org.springframework.http.ResponseEntity;
4

  
5

  
6
public interface UserService {
7

  
8
    ResponseEntity<Object> login();
9
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/RepositoryService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.domain.data.Repository;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
6
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
7
import eu.dnetlib.repo.manager.shared.*;
8
import org.json.JSONException;
9
import org.springframework.security.core.Authentication;
10
import java.io.IOException;
11
import java.util.List;
12
import java.util.Map;
13

  
14
public interface RepositoryService {
15

  
16

  
17
    Country[] getCountries() ;
18

  
19
    List<RepositorySnippet> getRepositoriesByCountry(String country, String mode, Boolean managed) throws JSONException, IOException;
20

  
21
    List<Repository> getRepositoriesOfUser(String userEmail,
22
                                           String page,
23
                                           String size) throws JSONException;
24

  
25
    Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException;
26

  
27
    List<AggregationDetails> getRepositoryAggregations(String id) throws JSONException;
28

  
29
    Map<String,List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException;
30

  
31
    List<Repository> getRepositoriesByName(String name,
32
                                           String page,
33
                                           String size) throws JSONException;
34

  
35
    List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
36
                                                                String officialName, String requestSortBy, String order,
37
                                                                int page, int pageSize) throws Exception;
38

  
39
    List<RepositoryInterface> getRepositoryInterface(String id) throws JSONException;
40

  
41
    Repository addRepository(String datatype, Repository repository) throws Exception;
42

  
43
    void deleteRepositoryInterface(String id, String registeredBy);
44

  
45
    RepositoryInterface addRepositoryInterface(String datatype,
46
                                               String repoId,
47
                                               String registeredBy,
48
                                               RepositoryInterface iFace) throws JSONException,ResourceNotFoundException;
49

  
50
    List<String> getDnetCountries();
51

  
52
    List<String> getTypologies();
53

  
54
    List<Timezone> getTimezones();
55

  
56
    Repository updateRepository(Repository repository, Authentication authentication) throws Exception;
57

  
58
    List<String> getUrlsOfUserRepos(String user_email,
59
                                    String page,
60
                                    String size) throws JSONException;
61

  
62
    List<String> getDatasourceVocabularies(String mode);
63

  
64
    Map<String, String> getCompatibilityClasses(String mode);
65

  
66
    Map<String, String> getDatasourceClasses(String mode);
67

  
68
    String getCountryName(String countryCode);
69

  
70
    MetricsInfo getMetricsInfoForRepository(String repoId) throws RepositoryServiceException;
71

  
72
    Map<String, String> getListLatestUpdate(String mode) throws RepositoryServiceException, JSONException;
73

  
74
    RepositoryInterface updateRepositoryInterface(String repositoryId, String registeredBy, RepositoryInterface repositoryInterface) throws Exception;
75

  
76
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.repo.manager.service.customHystrixCommands.*;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.stereotype.Service;
7
import org.springframework.web.client.RestTemplate;
8

  
9
import java.util.HashMap;
10
import java.util.Map;
11

  
12
@Service("statsService")
13
public class StatsServiceImpl implements StatsService {
14

  
15
    @Autowired
16
    RestTemplate restTemplate;
17

  
18
    @Value("${search.api.baseAddress}")
19
    private String baseAddress;
20
    @Value("${search.api.usagestats}")
21
    private String usagestatsBaseAddress;
22
    @Value("${search.api.usageEvents}")
23
    private String usagestatsEvents;
24

  
25
    @Override
26
    public Map getStatistics()  {
27

  
28
        UsageStatsTotalHystrixCommand usageStatsTotalHystrixCommand = new UsageStatsTotalHystrixCommand(usagestatsEvents,restTemplate);
29
        DataRepositoriesHystrixCommand dataRepositoriesHystrixCommand = new DataRepositoriesHystrixCommand(baseAddress,restTemplate);
30
        AggregatorsHystrixCommand aggregatorsHystrixCommand = new AggregatorsHystrixCommand(baseAddress,restTemplate);
31
        LiteratureHystrixCommand literatureHystrixCommand = new LiteratureHystrixCommand(baseAddress,restTemplate);
32
        JournalHystrixCommand journalHystrixCommand = new JournalHystrixCommand(baseAddress,restTemplate);
33
        PublicationHystrixCommand publicationHystrixCommand = new PublicationHystrixCommand(baseAddress,restTemplate);
34
        DatasetsHystrixCommand datasetsHystrixCommand = new DatasetsHystrixCommand(baseAddress,restTemplate);
35
        SoftwareHystrixCommand softwareHystrixCommand = new SoftwareHystrixCommand(baseAddress,restTemplate);
36
        LastYearUsageStatsHystrixCommand lastYearUsageStatsHystrixCommand = new LastYearUsageStatsHystrixCommand(usagestatsBaseAddress,restTemplate);
37

  
38
        Map<String,Object> stats = new HashMap<>();
39
        stats.put("aggregators",aggregatorsHystrixCommand.execute());
40
        stats.put("dataRepositories",dataRepositoriesHystrixCommand.execute());
41
        stats.put("literature",literatureHystrixCommand.execute());
42
        stats.put("journal",journalHystrixCommand.execute());
43
        stats.put("publications",publicationHystrixCommand.execute());
44
        stats.put("datasets",datasetsHystrixCommand.execute());
45
        stats.put("software",softwareHystrixCommand.execute());
46
        stats.put("lastYearUsagestats", lastYearUsageStatsHystrixCommand.execute());
47
        stats.put("usagestats",usageStatsTotalHystrixCommand.execute());
48
        return stats;
49
    }
50
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/PiWikService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3

  
4
import eu.dnetlib.domain.data.PiwikInfo;
5
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
6
import org.springframework.http.ResponseEntity;
7

  
8
import java.util.List;
9

  
10

  
11
public interface PiWikService {
12

  
13
    PiwikInfo getPiwikSiteForRepo(String repositoryId);
14

  
15
    PiwikInfo savePiwikInfo(PiwikInfo piwikInfo);
16

  
17
    List<PiwikInfo> getPiwikSitesForRepos();
18

  
19
    ResponseEntity<Object> approvePiwikSite(String repositoryId);
20

  
21
    String getOpenaireId(String repositoryid);
22

  
23
    ResponseEntity<Object> markPiwikSiteAsValidated(String repositoryId) throws RepositoryServiceException;
24

  
25
    PiwikInfo enableMetricsForRepository(String officialName, String repoWebsite, PiwikInfo piwikInfo) throws RepositoryServiceException;
26
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/BrokerService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.repo.manager.shared.BrokerException;
4
import eu.dnetlib.repo.manager.shared.Term;
5
import eu.dnetlib.repo.manager.shared.broker.*;
6
import io.swagger.annotations.Api;
7
import org.json.JSONException;
8
import org.springframework.http.MediaType;
9
import org.springframework.http.ResponseEntity;
10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.RequestMethod;
12
import org.springframework.web.bind.annotation.ResponseBody;
13
import org.springframework.web.bind.annotation.RestController;
14

  
15
import java.io.IOException;
16
import java.util.List;
17
import java.util.Map;
18

  
19

  
20
public interface BrokerService {
21

  
22
    DatasourcesBroker getDatasourcesOfUser(String user, String includeShared, String includeByOthers) throws BrokerException, JSONException;
23

  
24
    List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException;
25

  
26
    EventsPage advancedShowEvents(String page,
27
                                  String size,
28
                                  AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException;
29

  
30
    EventsPage showEvents(String datasourceName,
31
                          String topic,
32
                          String page,
33
                          String size) throws BrokerException, JSONException;
34

  
35
    Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(String userEmail) throws BrokerException;
36

  
37
    Subscription subscribe(OpenaireSubscription obj) throws BrokerException;
38

  
39
    ResponseEntity<Object> unsubscribe(String subscriptionId) throws BrokerException;
40

  
41
    Subscription getSubscription(String subscriptionId) throws BrokerException;
42

  
43
    Map<String, Term> getDnetTopics() throws BrokerException;
44

  
45
    EventsPage getNotificationsBySubscriptionId(String subscriptionId, String page, String size) throws BrokerException;
46

  
47
    Map<String, List<Subscription>> getSubscriptionsOfUser(String userEmail) throws BrokerException;
48
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/ValidatorService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.JobForValidation;
5
import eu.dnetlib.domain.functionality.validator.RuleSet;
6
import eu.dnetlib.domain.functionality.validator.StoredJob;
7
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
8
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
9
import org.json.JSONException;
10
import org.springframework.http.ResponseEntity;
11

  
12
import java.util.List;
13

  
14

  
15

  
16
public interface ValidatorService {
17

  
18

  
19
    JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException;
20

  
21
    ResponseEntity<Object> reSubmitJobForValidation(String email, String jobId) throws JSONException, ValidatorServiceException;
22

  
23
    List<RuleSet> getRuleSets(String mode);
24

  
25
    List<String> getSetsOfRepository(String url);
26

  
27
    boolean identifyRepo(String url);
28

  
29
    RuleSet getRuleSet(String acronym);
30

  
31
    List<StoredJob> getStoredJobsNew(String user,
32
                                     String jobType,
33
                                     String offset,
34
                                     String limit,
35
                                     String dateFrom,
36
                                     String dateTo,
37
                                     String validationStatus) throws ValidatorServiceException;
38

  
39
    int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException;
40

  
41
    InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException;
42
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/AggregatorsHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class AggregatorsHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate;
16
    String baseAddress;
17

  
18
    public AggregatorsHystrixCommand(String baseAddress, RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress + "/resources" +
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " ( datasourcetypename exact Institutional Repository Aggregator " +
30
                "     or datasourcetypename exact Publication Repository Aggregator )";
31

  
32
        UriComponents uriComponents = UriComponentsBuilder
33
                .fromHttpUrl(url)
34
                .queryParam("page", 0)
35
                .queryParam("size", 0)
36
                .queryParam("format", "json")
37
                .build().encode();
38

  
39
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
40
        Map metadata = (Map) ((Map) rs.getBody()).get("meta");
41
        return String.valueOf(metadata.get("total"));
42
    }
43

  
44
    @Override
45
    protected String getFallback() {
46
        return null;
47
    }
48
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/UsageStatsTotalHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class UsageStatsTotalHystrixCommand extends HystrixCommand<Integer> {
14

  
15
    RestTemplate restTemplate ;
16
    String usagestatsEvents;
17

  
18
    public UsageStatsTotalHystrixCommand(String usagestatsEvents,RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.usagestatsEvents = usagestatsEvents;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected Integer run() {
26
        UriComponents uriComponents = UriComponentsBuilder
27
                .fromHttpUrl(usagestatsEvents)
28
                .build().encode();
29

  
30
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
31
        Map metadata = (Map) ((Map)rs.getBody()).get("totals");
32
//        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
33
//        JSONObject resultSet = new JSONObject(rs);
34
//        JSONObject totals = resultSet.getJSONObject("totals");
35
        return (Integer) metadata.get("events");
36
    }
37

  
38
    @Override
39
    protected Integer getFallback() {
40
        return null;
41
    }
42

  
43
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DatasetsHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class DatasetsHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate;
16
    String baseAddress;
17

  
18
    public DatasetsHystrixCommand(String baseAddress,RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.restTemplate = restTemplate;
21
        this.baseAddress = baseAddress;
22
    }
23

  
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress+"/datasets/count";
27

  
28
        UriComponents uriComponents = UriComponentsBuilder
29
                .fromHttpUrl(url)
30
                .queryParam("page", 0)
31
                .queryParam("size", 0)
32
                .queryParam("format", "json")
33
                .build().encode();
34

  
35
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
36
        Map metadata = (Map) (rs.getBody());
37
        return String.valueOf(metadata.get("total"));
38
    }
39

  
40
    @Override
41
    protected String getFallback() {
42
        return null;
43
    }
44
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LastYearUsageStatsHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14

  
15
public class LastYearUsageStatsHystrixCommand extends HystrixCommand<Map> {
16

  
17
    RestTemplate restTemplate;
18
    String usagestatsBaseAddress;
19

  
20
    public LastYearUsageStatsHystrixCommand(String usagestatsBaseAddress,RestTemplate restTemplate) {
21
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
22
        this.usagestatsBaseAddress = usagestatsBaseAddress;
23
        this.restTemplate = restTemplate;
24
    }
25

  
26
    @Override
27
    protected Map run() throws Exception {
28
        UriComponents uriComponents = UriComponentsBuilder
29
                .fromHttpUrl(usagestatsBaseAddress + "/totals")
30
                .build().encode();
31

  
32
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
33

  
34
        List yearly_stats = (List) ((Map)rs.getBody()).get("yearly_stats");
35
        Map lastYear = (Map) yearly_stats.get(yearly_stats.size()-1);
36
        Integer downloads = (Integer) lastYear.get("downloads");
37
        Integer views = (Integer) lastYear.get("views");
38
        Integer year = (Integer) lastYear.get("year");
39

  
40
        Map<String,Object> usagestats = new HashMap<>();
41
        usagestats.put("number",String.valueOf(downloads+views));
42
        usagestats.put("year",year);
43

  
44
        return usagestats;
45
    }
46

  
47
    @Override
48
    protected Map getFallback() {
49
        return null;
50
    }
51

  
52
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/PublicationHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class PublicationHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate;
16
    private String baseAddress;
17

  
18
    public PublicationHystrixCommand(String baseAddress, RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress + "/publications/count";
27

  
28
        UriComponents uriComponents = UriComponentsBuilder
29
                .fromHttpUrl(url)
30
                .queryParam("page", 0)
31
                .queryParam("size", 0)
32
                .queryParam("format", "json")
33
                .build().encode();
34

  
35
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
36
        Map metadata = (Map) (rs.getBody());
37
        return String.valueOf(metadata.get("total"));
38
    }
39

  
40
    @Override
41
    protected String getFallback() {
42
        return null;
43
    }
44

  
45
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/JournalHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class JournalHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate;
16
    private String baseAddress;
17

  
18
    public JournalHystrixCommand(String baseAddress, RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress+"/resources"+
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " datasourcetypename exact Journal";
30

  
31
        UriComponents uriComponents = UriComponentsBuilder
32
                .fromHttpUrl(url)
33
                .queryParam("page", 0)
34
                .queryParam("size", 0)
35
                .queryParam("format", "json")
36
                .build().encode();
37

  
38
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
39
        Map metadata = (Map) ((Map) rs.getBody()).get("meta");
40
        return String.valueOf(metadata.get("total"));
41
    }
42

  
43
    @Override
44
    protected String getFallback() {
45
        return null;
46
    }
47

  
48
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/SoftwareHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class SoftwareHystrixCommand extends HystrixCommand<String> {
14

  
15
    String baseAddress;
16
    RestTemplate restTemplate;
17

  
18
    public SoftwareHystrixCommand(String baseAddress, RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() {
26
        String url = baseAddress + "/software/count";
27

  
28
        UriComponents uriComponents = UriComponentsBuilder
29
                .fromHttpUrl(url)
30
                .queryParam("page", 0)
31
                .queryParam("size", 0)
32
                .queryParam("format", "json")
33
                .build().encode();
34

  
35
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
36
        Map metadata = (Map) (rs.getBody());
37
        return String.valueOf(metadata.get("total"));
38
    }
39

  
40
    @Override
41
    protected String getFallback() {
42
        return null;
43
    }
44
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LiteratureHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class LiteratureHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate;
16
    private String baseAddress;
17

  
18
    public LiteratureHystrixCommand(String baseAddress , RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress+"/resources"+
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " ( datasourcetypename exact Institutional Repository  " +
30
                "     or datasourcetypename exact Publication Repository )";
31

  
32
        UriComponents uriComponents = UriComponentsBuilder
33
                .fromHttpUrl(url)
34
                .queryParam("page", 0)
35
                .queryParam("size", 0)
36
                .queryParam("format", "json")
37
                .build().encode();
38

  
39
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
40
        Map metadata = (Map) ((Map) rs.getBody()).get("meta");
41
        return String.valueOf(metadata.get("total"));
42
    }
43

  
44
    @Override
45
    protected String getFallback() {
46
        return null;
47
    }
48

  
49
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DataRepositoriesHystrixCommand.java
1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

  
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

  
11
import java.util.Map;
12

  
13
public class DataRepositoriesHystrixCommand extends HystrixCommand<String> {
14

  
15
    RestTemplate restTemplate ;
16
    private String baseAddress;
17

  
18
    public DataRepositoriesHystrixCommand(String baseAddress,RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

  
24
    @Override
25
    protected String run() {
26
        String url = baseAddress+"/resources" +
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " datasourcetypename exact Data Repository ";
30

  
31
        UriComponents uriComponents = UriComponentsBuilder
32
                .fromHttpUrl(url)
33
                .queryParam("page",0)
34
                .queryParam("size",0)
35
                .queryParam("format","json")
36
                .build().encode();
37

  
38
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
39
        Map metadata = (Map) ((Map)rs.getBody()).get("meta");
40
        return String.valueOf(metadata.get("total"));
41
    }
42

  
43
    @Override
44
    protected String getFallback() {
45
        return null;
46
    }
47
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/MonitorService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.StoredJob;
5
import eu.dnetlib.repo.manager.shared.JobsOfUser;
6
import io.swagger.annotations.Api;
7
import org.json.JSONException;
8
import org.springframework.http.MediaType;
9
import org.springframework.web.bind.annotation.*;
10

  
11

  
12
public interface MonitorService {
13

  
14

  
15
    JobsOfUser getJobsOfUser(String user,
16
                             String jobType,
17
                             String offset,
18
                             String limit,
19
                             String dateFrom,
20
                             String dateTo,
21
                             String validationStatus,
22
                             String includeJobsTotal) throws JSONException, ValidatorServiceException;
23

  
24
    int getJobsOfUserPerValidationStatus(String user,
25
                                         String jobType,
26
                                         String validationStatus) throws JSONException;
27

  
28

  
29
    StoredJob getJobSummary(String jobId,
30
                            String groupBy) throws JSONException;
31

  
32
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/EmailUtils.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.functionality.validator.JobForValidation;
6
import org.springframework.security.core.Authentication;
7

  
8
public interface EmailUtils {
9

  
10

  
11
    void reportException(Exception exception);
12

  
13
    void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
14

  
15
    void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
16

  
17
    void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
18

  
19
    void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
20

  
21
    void sendAdminRegistrationEmail(Repository repository, Authentication authentication) throws Exception;
22

  
23
    void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception;
24

  
25
    void sendAdminUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception;
26

  
27
    void sendUserUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception;
28

  
29
    void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception;
30
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/DashboardService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.repo.manager.domain.RepositorySummaryInfo;
4
import org.json.JSONException;
5
import org.springframework.web.bind.annotation.PathVariable;
6

  
7
import java.util.List;
8

  
9
public interface DashboardService {
10

  
11
    List<RepositorySummaryInfo> getRepositoriesSummaryInfo(@PathVariable("userEmail") String userEmail,
12
                                                           @PathVariable("page") String page,
13
                                                           @PathVariable("size") String size) throws JSONException;
14
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/SushiliteServiceImpl.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
4
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.core.ParameterizedTypeReference;
8
import org.springframework.http.HttpMethod;
9
import org.springframework.http.ResponseEntity;
10
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
11
import org.springframework.security.access.prepost.PreAuthorize;
12
import org.springframework.stereotype.Service;
13
import org.springframework.web.bind.annotation.PathVariable;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.client.RestClientException;
16
import org.springframework.web.client.RestTemplate;
17
import org.springframework.web.util.UriComponentsBuilder;
18

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

  
22
@Service("sushiliteService")
23
public class SushiliteServiceImpl implements SushiliteService {
24

  
25

  
26
    @Value("${services.repomanager.usagestats.sushiliteEndpoint}")
27
    private String usagestatsSushiliteEndpoint;
28

  
29
    @Autowired
30
    private EmailUtils emailUtils;
31

  
32
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(SushiliteServiceImpl.class);
33

  
34

  
35
    @Override
36
    @PreAuthorize("hasRole('ROLE_USER')")
37
    public ReportResponseWrapper getReportResults(@PathVariable("page") String page,
38
                                                  @PathVariable("pageSize") String pageSize,
39
                                                  @RequestParam(value = "Report") String Report,
40
                                                  @RequestParam(value = "Release",defaultValue="4") String Release,
41
                                                  @RequestParam(value = "RequestorID",required=false,defaultValue="anonymous") String RequestorID,
42
                                                  @RequestParam(value = "BeginDate",required=false,defaultValue="") String BeginDate,
43
                                                  @RequestParam(value = "EndDate",required=false,defaultValue="") String EndDate,
44
                                                  @RequestParam(value = "RepositoryIdentifier") String RepositoryIdentifier,
45
                                                  @RequestParam(value = "ItemIdentifier",required=false,defaultValue="") String ItemIdentifier,
46
                                                  @RequestParam(value = "ItemDataType",required=false,defaultValue="") String ItemDataType,
47
                                                  @RequestParam(value = "Granularity") String Granularity,
48
                                                  @RequestParam(value = "Pretty",required=false,defaultValue="") String Pretty) {
49

  
50
            //build the uri params
51
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/")
52
                                                                .queryParam("Report", Report)
53
                                                                .queryParam("Release", Release)
54
                                                                .queryParam("RequestorID", RequestorID)
55
                                                                .queryParam("BeginDate", BeginDate)
56
                                                                .queryParam("EndDate", EndDate)
57
                                                                .queryParam("RepositoryIdentifier", RepositoryIdentifier)
58
                                                                .queryParam("ItemIdentifier", ItemIdentifier)
59
                                                                .queryParam("ItemDataType", ItemDataType)
60
                                                                .queryParam("Granularity", Granularity)
61
                                                                .queryParam("Pretty", Pretty);
62

  
63
            //create new template engine
64
            RestTemplate template = new RestTemplate();
65
            template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
66

  
67
            ResponseEntity<ReportResponseWrapper> resp;
68
            try {
69
                //communicate with endpoint
70
                resp = template.exchange(
71
                        builder.build().encode().toUri(),
72
                        HttpMethod.GET,
73
                        null,
74
                        new ParameterizedTypeReference<ReportResponseWrapper>() {
75
                        });
76
            } catch (RestClientException e) {
77
                LOGGER.debug("Exception on getReportResults" , e);
78
                emailUtils.reportException(e);
79
                throw e;
80
            }
81

  
82
            // check remote api's response
83
            System.out.println("sushi responded status " + resp.getStatusCode());
84

  
85
            // get the items corresponding to the requested page
86
            List<ReportItem> requestedItemList = new ArrayList<>();
87
            if (resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems() != null) {
88
                try {
89
                    int totalItems = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().size();
90
                    int size = Integer.parseInt(pageSize);
91
                    int offset = Integer.parseInt(page)*size;
92

  
93
                    if (offset < totalItems ) {
94
                        int upperIndex = offset+size;
95
                        if (upperIndex > totalItems) {
96
                            upperIndex = totalItems;
97
                        }
98
                        requestedItemList = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().subList(offset,upperIndex);
99
                    }
100

  
101
                } catch (NumberFormatException e) {
102
                    LOGGER.debug("Exception on getReportResults - trying to cast strings to integers", e);
103
                    //emailUtils.reportException(e);
104
                    throw e;
105
                }
106

  
107
            }
108

  
109
            ReportResponseWrapper newReportResponse = resp.getBody();
110
            newReportResponse.getReportResponse().getReportWrapper().getReport().getCustomer().setReportItems(requestedItemList);
111

  
112

  
113
            return newReportResponse;
114
    }
115

  
116
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/StatsService.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4
import eu.dnetlib.repo.manager.exception.EndPointException;
5
import org.json.JSONException;
6

  
7
import java.util.Map;
8

  
9

  
10
public interface StatsService {
11

  
12
    Map getStatistics() ;
13
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/UserServiceImpl.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.http.HttpStatus;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.security.core.GrantedAuthority;
8
import org.springframework.security.core.context.SecurityContextHolder;
9
import org.springframework.stereotype.Service;
10

  
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.stream.Collectors;
15

  
16
@Service("userService")
17
public class UserServiceImpl implements UserService {
18

  
19
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
20
            .getLogger(UserServiceImpl.class);
21

  
22
    @Value("${oidc.issuer}")
23
    private String oidc_issuer;
24

  
25
    @Override
26
    public ResponseEntity<Object> login() {
27
        OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
28
        LOGGER.debug("User authentication : " + authentication);
29
        Map<String,Object> body = new HashMap<>();
30
        body.put("sub",authentication.getSub());
31
        if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
32
            body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
33
        else
34
            body.put("name",authentication.getUserInfo().getName());
35

  
36
        body.put("email",authentication.getUserInfo().getEmail());
37
        List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
38
        body.put("role",roles);
39

  
40
        return new ResponseEntity<>(body, HttpStatus.OK);
41
    }
42
}
modules/uoa-repository-manager-service/branches/piwikSiteId/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java
1
package eu.dnetlib.repo.manager.service;
2

  
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.fasterxml.jackson.databind.SerializationFeature;
6
import eu.dnetlib.api.functionality.ValidatorServiceException;
7
import eu.dnetlib.domain.data.Repository;
8
import eu.dnetlib.domain.data.RepositoryInterface;
9
import eu.dnetlib.domain.enabling.Vocabulary;
10
import eu.dnetlib.domain.functionality.validator.JobForValidation;
11
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
12
import eu.dnetlib.repo.manager.domain.RequestFilter;
13
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
14
import eu.dnetlib.repo.manager.shared.*;
15
import eu.dnetlib.repo.manager.utils.Converter;
16
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
17
import gr.uoa.di.driver.xml.repository.INTERFACE;
18
import org.apache.commons.codec.digest.DigestUtils;
19
import org.apache.log4j.Logger;
20
import org.json.JSONArray;
21
import org.json.JSONException;
22
import org.json.JSONObject;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Value;
25
import org.springframework.core.ParameterizedTypeReference;
26
import org.springframework.http.*;
27
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
28
import org.springframework.security.core.Authentication;
29
import org.springframework.security.core.context.SecurityContextHolder;
30
import org.springframework.stereotype.Service;
31
import org.springframework.web.bind.annotation.PathVariable;
32
import org.springframework.web.bind.annotation.RequestBody;
33
import org.springframework.web.bind.annotation.RequestParam;
34
import org.springframework.web.client.RestClientException;
35
import org.springframework.web.client.RestTemplate;
36
import org.springframework.web.util.UriComponents;
37
import org.springframework.web.util.UriComponentsBuilder;
38

  
39
import javax.annotation.PostConstruct;
40
import java.io.IOException;
41
import java.sql.Timestamp;
42
import java.util.*;
43
import java.util.concurrent.ConcurrentHashMap;
44
import java.util.stream.Collectors;
45

  
46
@Service("repositoryService")
47
public class RepositoryServiceImpl implements RepositoryService {
48

  
49
    @Value("${api.baseAddress}")
50
    private String baseAddress;
51

  
52
    @Value("${services.repo-manager.adminEmail}")
53
    private String adminEmail;
54

  
55
    @Autowired
56
    RestTemplate restTemplate;
57

  
58
    private HttpHeaders httpHeaders;
59

  
60
    private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"};
61

  
62
    private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class);
63

  
64
    @Value("${services.repomanager.usageStatisticsDiagramsBaseURL}")
65
    private String usageStatisticsDiagramsBaseURL;
66

  
67
    @Value("${services.repomanager.usageStatisticsNumbersBaseURL}")
68
    private String usageStatisticsNumbersBaseURL;
69

  
70
    @Autowired
71
    private VocabularyLoader vocabularyLoader;
72

  
73
    @Autowired
74
    private PiWikService piWikService;
75

  
76
    @Autowired
77
    private EmailUtils emailUtils;
78

  
79
    @Autowired
80
    ValidatorService validatorService;
81

  
82

  
83
    private Map<String, Vocabulary> vocabularyMap = new ConcurrentHashMap<String, Vocabulary>();
84

  
85
    private Map<String, String> countriesMap = new HashMap<>();
86
    private Map<String, String> inverseCountriesMap = new HashMap<>();
87

  
88
    private static Map<String,List<String>> dataSourceClass = new HashMap<String,List<String>>(){{
89
        put("opendoar",new ArrayList<String>(){{ add("pubsrepository::institutional");
90
                                                    add("pubsrepository::thematic");
91
                                                    add("pubsrepository::unknown");
92
                                                    add("pubsrepository::mock");
93
        }});
94
        put("re3data",new ArrayList<String>(){{ add("datarepository::unknown");
95
        }});
96
        put("journal",new ArrayList<String>(){{ add("pubsrepository::journal");
97
        }});
98
        put("aggregator",new ArrayList<String>(){{ add("aggregator::pubsrepository::institutional");
99
                                                        add("aggregator::pubsrepository::journals");
100
                                                        add("aggregator::datarepository");
101
                                                        add("aggregator::pubsrepository::unknown");
102
        }});
103
    }};
104

  
105
    private static Map<String,String> invertedDataSourceClass = new HashMap<String,String>(){{
106
        put("pubsrepository::institutional","opendoar");
107
        put("pubsrepository::thematic","opendoar");
108
        put("pubsrepository::unknown","opendoar");
109
        put("pubsrepository::mock","opendoar");
110

  
111
        put("datarepository::unknown","re3data");
112

  
113
        put("pubsrepository::journal","journal");
114

  
115
        put("aggregator::pubsrepository::institutional","aggregator");
116
        put("aggregator::pubsrepository::journals","aggregator");
117
        put("aggregator::datarepository","aggregator");
118
        put("aggregator::pubsrepository::unknown","aggregator");
119

  
120
    }};
121

  
122

  
123

  
124
    @PostConstruct
125
    private void init() {
126
        LOGGER.debug("Initialization method of repository api!");
127
        LOGGER.debug("Updated version!");
128

  
129
        httpHeaders = new HttpHeaders();
130
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
131

  
132
        for (String vocName : vocabularyNames) {
133
            vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
134
        }
135

  
136
        Country[] countries = getCountries();
137
        for (Country c : countries) {
138
            countriesMap.put(c.getName(), c.getCode());
139
            inverseCountriesMap.put(c.getCode(), c.getName());
140
        }
141

  
142

  
143
    }
144

  
145
    @Override
146
    public Country[] getCountries() {
147
        UriComponents uriComponents = UriComponentsBuilder
148
                .fromHttpUrl(baseAddress + "/ds/countries")
149
                .build().encode();
150
        return restTemplate.getForObject(uriComponents.toUri(), Country[].class);
151
    }
152

  
153

  
154
    @Override
155
    public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
156
                                                            @PathVariable("mode") String mode,
157
                                                            @RequestParam(value = "managed",required=false) Boolean managed) throws JSONException, IOException {
158

  
159
        LOGGER.debug("Getting repositories by country!");
160
        int page = 0;
161
        int size = 100;
162
        List<RepositorySnippet> resultSet = new ArrayList<>();
163
        ObjectMapper mapper = new ObjectMapper();
164

  
165
        String filterKey = "UNKNOWN";
166
        if (mode.equalsIgnoreCase("opendoar"))
167
            filterKey = "openaire____::opendoar";
168
        else if (mode.equalsIgnoreCase("re3data"))
169
            filterKey = "openaire____::re3data";
170

  
171

  
172
        LOGGER.debug("Country code equals : " + country);
173
        LOGGER.debug("Filter mode equals : " + filterKey);
174

  
175
        UriComponents uriComponents = searchSnipperDatasource(String.valueOf(page),String.valueOf(size));
176
        RequestFilter requestFilter = new RequestFilter();
177
        requestFilter.setCountry(country);
178
        requestFilter.setCollectedfrom(filterKey);
179

  
180
        try{
181
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
182
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
183
            while (jsonArray.length() > 0 ) {
184
                resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
185
                        mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
186
                page += 1;
187
                uriComponents = searchSnipperDatasource(String.valueOf(page),String.valueOf(size));
188
                rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
189
                jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
190
            }
191
            return resultSet;
192
        }catch (Exception e){
193
            LOGGER.debug("Exception on getRepositoriesByCountry" , e);
194
//            emailUtils.reportException(e);
195
            throw e;
196
        }
197
    }
198

  
199
    public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
200
            String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception {
201

  
202
        LOGGER.debug("Searching registered repositories");
203

  
204
        List<RepositorySnippet> resultSet = new ArrayList<>();
205
        ObjectMapper mapper = new ObjectMapper();
206

  
207
        UriComponents uriComponents = searchRegisteredDatasource(requestSortBy, order, Integer.toString(page), Integer.toString(pageSize));
208

  
209
        RequestFilter requestFilter = new RequestFilter();
210
        requestFilter.setCountry(country);
211
        requestFilter.setTypology(typology);
212
        requestFilter.setOfficialname(officialName);
213
        requestFilter.setEnglishname(englishName);
214

  
215
        try {
216
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
217
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
218

  
219
            resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),  mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
220

  
221
             return resultSet;
222
        }catch (Exception e){
223
            LOGGER.error("Error searching registered datasources" , e);
224
            throw e;
225
        }
226
    }
227

  
228
    private Repository updateRepositoryInfo(Repository r) throws JSONException {
229

  
230
        /*
231
        * from datasource class
232
        * we get the datasource type form the inverted map
233
        * */
234
        r.setDatasourceType(getRepositoryType(r.getDatasourceClass()));
235
        r.setInterfaces(this.getRepositoryInterface(r.getId()));
236
        r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
237
        r.setCountryName(getCountryName(r.getCountryCode()));
238
        return r;
239
    }
240

  
241

  
242
    private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
243

  
244
        List<Repository> reps = new ArrayList<>();
245
        for (Repository r : rs) {
246
            if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode))
247
                reps.add(r);
248

  
249
        }
250
        return reps;
251
    }
252

  
253
    @Override
254
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
255
                                                  @PathVariable("page") String page,
256
                                                  @PathVariable("size") String size) throws JSONException {
257

  
258
        LOGGER.debug("Retreiving repositories of user : " + userEmail );
259
        UriComponents uriComponents = searchDatasource(page,size);
260
        RequestFilter requestFilter = new RequestFilter();
261
        requestFilter.setRegisteredby(userEmail);
262

  
263
        try{
264
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
265

  
266
            List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
267
            for (Repository r : repos)
268
                this.updateRepositoryInfo(r);
269

  
270
            return repos;
271
        }catch (Exception e){
272
            LOGGER.debug("Exception on getRepositoriesOfUser" , e);
273
            emailUtils.reportException(e);
274
            throw e;
275
        }
276
    }
277

  
278
    @Override
279
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
280

  
281
        LOGGER.debug("Retreiving repositories with id : " + id );
282
        Repository repo = null;
283
        UriComponents uriComponents = searchDatasource("0","100");
284
        RequestFilter requestFilter = new RequestFilter();
285
        requestFilter.setId(id);
286

  
287
        try{
288
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
289
            JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
290

  
291
            if(jsonArray.length() == 0)
292
                throw new ResourceNotFoundException();
293

  
294
            repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
295
            return updateRepositoryInfo(repo);
296
        }catch (JSONException e){
297
            LOGGER.debug("Exception on getRepositoryById" , e);
298
            emailUtils.reportException(e);
299
            throw e;
300
        }
301

  
302
    }
303

  
304

  
305
    @Override
306
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
307

  
308
        LOGGER.debug("Retreiving aggregations for repository with id : " + id );
309
        UriComponents uriComponents = searchDatasource("0","100");
310
        RequestFilter requestFilter = new RequestFilter();
311
        requestFilter.setId(id);
312

  
313
        List<AggregationDetails> aggregationHistory = new ArrayList<>();
314

  
315
        try {
316
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
317
            JSONObject repository = new JSONObject(rs);
318

  
319
            if(repository.getJSONArray("datasourceInfo").length() == 0)
320
                return aggregationHistory;
321
            aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
322
            return aggregationHistory.size() == 0? aggregationHistory : aggregationHistory.stream()
323
                                                    .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
324
                                                    .limit(20)
325
                                                    .collect(Collectors.toList());
326
        } catch (JSONException e) {
327
            LOGGER.debug("Exception on getRepositoryAggregations" , e);
328
            emailUtils.reportException(e);
329
            throw e;
330
        }
331

  
332
    }
333

  
334
    @Override
335
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
336
        LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id );
337
        UriComponents uriComponents = searchDatasource("0","100");
338
        RequestFilter requestFilter = new RequestFilter();
339
        requestFilter.setId(id);
340

  
341
        List<AggregationDetails> aggregationHistory = new ArrayList<>();
342
        Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>();
343
        try {
344
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
345
            JSONObject repository = new JSONObject(rs);
346

  
347
            if(repository.getJSONArray("datasourceInfo").length() == 0)
348
                return aggregationByYear;
349

  
350
            aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
351
            return aggregationHistory.size() == 0? aggregationByYear:createYearMap(aggregationHistory);
352

  
353
        } catch (JSONException e) {
354
            LOGGER.debug("Exception on getRepositoryAggregations" , e);
355
            emailUtils.reportException(e);
356
            throw e;
357
        }
358
    }
359

  
360
    private Map<String,List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) {
361
        Map<String, List<AggregationDetails>> aggregationByYear;
362
        aggregationHistory = aggregationHistory.stream()
363
                            .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
364
                            .collect(Collectors.toList());
365

  
366
       return aggregationHistory.stream()
367
                            .collect(Collectors.groupingBy(AggregationDetails::getYear));
368
    }
369

  
370

  
371
    @Override
372
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
373
                                                  @PathVariable("page") String page,
374
                                                  @PathVariable("size") String size) throws JSONException {
375

  
376
        LOGGER.debug("Retreiving  repositories with official name : " + name );
377
        UriComponents uriComponents = searchDatasource("0","100");
378
        RequestFilter requestFilter = new RequestFilter();
379
        requestFilter.setOfficialname(name);
380

  
381
        try{
382
            String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
383
            List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
384
            for (Repository r : repos)
385
                updateRepositoryInfo(r);
386
            return repos;
387
        }catch (Exception e){
388
            LOGGER.debug("Exception on getRepositoriesByName" , e);
389
            emailUtils.reportException(e);
390
            throw e;
391
        }
392

  
393
    }
394

  
395
    @Override
396
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
397

  
398
        UriComponents uriComponents = UriComponentsBuilder
399
                .fromHttpUrl(baseAddress + "/ds/api/")
400
                .path("/{id}")
401
                .build().expand(id).encode();
402

  
403
        try{
404
            String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
405
            return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
406
        }catch (Exception e ){
407
            LOGGER.debug("Exception on getRepositoryInterface" , e);
408
            emailUtils.reportException(e);
409
            throw e;
410
        }
411

  
412
    }
413

  
414
    @Override
415
    public Repository addRepository(@RequestParam("datatype") String datatype,
416
                                    @RequestBody Repository repository) throws Exception {
417

  
418
        LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
419

  
420
        repository.setCountryCode(countriesMap.get(repository.getCountryName()));
421
        repository.setActivationId(UUID.randomUUID().toString());
422
        repository.setCollectedFrom("infrastruct_::openaire");
423

  
424
        if (datatype.equals("journal")) {
425
            repository.setId("openaire____::issn" + repository.getIssn());
426
            repository.setNamespacePrefix("issn" + repository.getIssn());
427
            this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
428
        }else if (datatype.equals("aggregator")) {
429
            repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName()));
430
            repository.setNamespacePrefix(DigestUtils.md5Hex(repository.getOfficialName()).substring(0,12));
431
            this.storeRepository(repository, SecurityContextHolder.getContext().getAuthentication());
432
        }else {
433
            this.latentUpdate(repository, SecurityContextHolder.getContext().getAuthentication());
434
        }
435

  
436
        return repository;
437
    }
438

  
439
    /* update method acting as add -> send email with registration topic/body*/
440
    private Repository latentUpdate(Repository repository, Authentication authentication) throws Exception {
441
        UriComponents uriComponents = UriComponentsBuilder
442
                .fromHttpUrl(baseAddress + "/ds/update/")
443
                .build()
444
                .encode();
445

  
446
        try {
447
            String json_repository = Converter.repositoryObjectToJson(repository);
448
            LOGGER.debug("JSON to add(update) -> " + json_repository);
449

  
450
//
451
//            // TODO delete these 3 lines
452
//            HttpHeaders temp = new HttpHeaders();
453
//            temp.setContentType(MediaType.APPLICATION_JSON_UTF8);
454

  
455
            HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
456
            ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(),HttpMethod.POST, httpEntity, ResponseEntity.class);
457

  
458
            if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
459
                emailUtils.sendUserRegistrationEmail(repository, authentication);
460
                emailUtils.sendAdminRegistrationEmail(repository, authentication);
461
            } else
462
                LOGGER.debug(responseEntity.getBody().toString());
463

  
464
            return repository;
465
        } catch (Exception e) {
466
            LOGGER.debug("Exception on updateRepository" , e);
467
            emailUtils.reportException(e);
468
            throw e;
469
        }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff