minor refactoring

This commit is contained in:
Konstantinos Spyrou 2021-07-14 10:15:49 +00:00
parent b8c1e18d27
commit d2c45c2db7
6 changed files with 130 additions and 159 deletions

View File

@ -5,7 +5,7 @@ import eu.dnetlib.domain.data.RepositoryInterface;
import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
import eu.dnetlib.repo.manager.service.RepositoryService;
import eu.dnetlib.repo.manager.service.security.AuthorizationService;
import eu.dnetlib.repo.manager.utils.JsonUtils;
import io.swagger.annotations.Api;
@ -28,24 +28,24 @@ import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/repository")
@Api(description = "Repository API", tags = {"repository"})
@RequestMapping(value = "/repositories")
@Api(description = "Repository API", tags = {"repositories"})
public class RepositoryController {
private static final Logger logger = Logger.getLogger(RepositoryController.class);
private final RepositoryServiceImpl repositoryService; //TODO: why not RepositoryService??
private final RepositoryService repositoryService;
private final AuthorizationService authorizationService;
@Autowired
RepositoryController(RepositoryServiceImpl repositoryService,
RepositoryController(RepositoryService repositoryService,
AuthorizationService authorizationService) {
this.repositoryService = repositoryService;
this.authorizationService = authorizationService;
}
@RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/countries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Country[] getCountries() {
return repositoryService.getCountries();
@ -60,24 +60,12 @@ public class RepositoryController {
return repositoryService.getRepositoriesByCountry(country, mode, managed);
}
@RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}", method = RequestMethod.GET,
@RequestMapping(value = "/snippets/user", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasAuthority('REGISTERED_USER')")
public List<RepositorySnippet> getRepositoriesSnippetOfUser(
@PathVariable("page") String page,
@PathVariable("size") String size) throws Exception {
return repositoryService.getRepositoriesSnippetOfUser(page, size);
}
@RequestMapping(value = "/user/repositories/{page}/{size}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasAuthority('REGISTERED_USER')")
public List<Repository> getRepositoriesOfUser(
@PathVariable("page") String page,
@PathVariable("size") String size) throws JSONException, IOException {
return repositoryService.getRepositoriesOfUser(page, size);
public List<RepositorySnippet> getRepositoriesSnippetsOfUser() throws Exception {
return repositoryService.getRepositoriesSnippetOfUser("0", "100");
}
@RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,

View File

@ -8,13 +8,13 @@ import java.util.Date;
public class RepositorySnippet {
private String id;
private String officialname;
private String englishname;
private String websiteurl;
private String officialName;
private String englishName;
private String websiteUrl;
private String typology;
private String registeredby;
private String registeredBy;
private Organization[] organizations;
private String registrationdate;
private String registrationDate;
private String logoUrl;
private String description;
private String fullTextDownload;
@ -24,7 +24,8 @@ public class RepositorySnippet {
private PiwikInfo piwikInfo;
public RepositorySnippet() {}
public RepositorySnippet() {
}
public String getId() {
return id;
@ -34,36 +35,36 @@ public class RepositorySnippet {
this.id = id;
}
public String getOfficialname() {
return officialname;
public String getOfficialName() {
return officialName;
}
public void setOfficialname(String officialname) {
this.officialname = officialname;
public void setOfficialName(String officialName) {
this.officialName = officialName;
}
public String getEnglishname() {
return englishname;
public String getEnglishName() {
return englishName;
}
public void setEnglishname(String englishname) {
this.englishname = englishname;
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public String getWebsiteurl() {
return websiteurl;
public String getWebsiteUrl() {
return websiteUrl;
}
public void setWebsiteurl(String websiteurl) {
this.websiteurl = websiteurl;
public void setWebsiteUrl(String websiteUrl) {
this.websiteUrl = websiteUrl;
}
public String getRegisteredby() {
return registeredby;
public String getRegisteredBy() {
return registeredBy;
}
public void setRegisteredby(String registeredby) {
this.registeredby = registeredby;
public void setRegisteredBy(String registeredBy) {
this.registeredBy = registeredBy;
}
public String getTypology() {
@ -82,12 +83,12 @@ public class RepositorySnippet {
this.organizations = organizations;
}
public String getRegistrationdate() {
return registrationdate;
public String getRegistrationDate() {
return registrationDate;
}
public void setRegistrationdate(String registrationdate) {
this.registrationdate = registrationdate;
public void setRegistrationDate(String registrationDate) {
this.registrationDate = registrationDate;
}
public PiwikInfo getPiwikInfo() {

View File

@ -2,7 +2,6 @@ package eu.dnetlib.repo.manager.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.repo.manager.domain.BrokerException;
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
import eu.dnetlib.repo.manager.domain.Term;
@ -44,11 +43,11 @@ public class BrokerServiceImpl implements BrokerService {
.getLogger(BrokerServiceImpl.class);
@Autowired
RestTemplate restTemplate ;
RestTemplate restTemplate;
private HttpHeaders httpHeaders;
private HashMap<String,Term> topics = new HashMap<String, Term>();
private HashMap<String, Term> topics = new HashMap<String, Term>();
@Autowired
private EmailUtils emailUtils;
@ -60,29 +59,29 @@ public class BrokerServiceImpl implements BrokerService {
httpHeaders.set("Content-Type", "application/json");
LOGGER.debug("Init dnet topics!");
try (InputStream is = new URL(topicsURL).openStream() ){
try (InputStream is = new URL(topicsURL).openStream()) {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(is);
for (JsonNode term : root.path("terms") )
for (JsonNode term : root.path("terms"))
topics.put(term.path("code").textValue(), parseTerm(term));
} catch (IOException e) {
LOGGER.debug("Exception on initDnetTopicsMap" , e);
LOGGER.debug("Exception on initDnetTopicsMap", e);
emailUtils.reportException(e);
}
}
private Term parseTerm(JsonNode term) {
return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
term.path("encoding").textValue(),term.path("code").textValue());
return new Term(term.path("englishName").textValue(), term.path("nativeName").textValue(),
term.path("encoding").textValue(), term.path("code").textValue());
}
@Override
public DatasourcesBroker getDatasourcesOfUser(String user,String includeShared,String includeByOthers) throws JSONException {
public DatasourcesBroker getDatasourcesOfUser(String user, String includeShared, String includeByOthers) throws JSONException {
long start = System.currentTimeMillis();
DatasourcesBroker ret = new DatasourcesBroker();
try {
ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetOfUser(user,"0","100")));
ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetOfUser(user, "0", "100")));
//TODO fix bug when values are true
// if (Boolean.parseBoolean(includeShared)) {
// List<String> sharedDatasourceIds = new ArrayList<String>();
@ -93,11 +92,11 @@ public class BrokerServiceImpl implements BrokerService {
// ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
// }
} catch (Exception e) {
LOGGER.debug("Exception on getDatasourcesOfUser" , e);
LOGGER.debug("Exception on getDatasourcesOfUser", e);
emailUtils.reportException(e);
}
long end = System.currentTimeMillis();
System.out.println("Getting datasources of user in " + (end-start)+"ms");
System.out.println("Getting datasources of user in " + (end - start) + "ms");
return ret;
}
@ -117,7 +116,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<List<BrowseEntry>>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on getTopicsForDatasource" , e);
LOGGER.debug("Exception on getTopicsForDatasource", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -128,7 +127,7 @@ public class BrokerServiceImpl implements BrokerService {
@Override
public EventsPage advancedShowEvents(String page,
String size,
AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
AdvQueryObject advQueryObject) throws BrokerException, JSONException, IOException {
final String service = "/events/{page}/{pageSize}";
@ -151,7 +150,7 @@ public class BrokerServiceImpl implements BrokerService {
}
);
} catch (RestClientException e) {
LOGGER.debug("Exception on advancedShowEvents" , e);
LOGGER.debug("Exception on advancedShowEvents", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -166,9 +165,9 @@ public class BrokerServiceImpl implements BrokerService {
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
for (RepositorySnippet repo : repositories) {
BrowseEntry temp = new BrowseEntry();
temp.setValue(repo.getOfficialname());
temp.setValue(repo.getOfficialName());
temp.setSize(new Long(0));
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) {
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
temp.setSize(temp.getSize() + e.getSize());
}
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
@ -183,31 +182,11 @@ public class BrokerServiceImpl implements BrokerService {
}
});
long stop = System.currentTimeMillis();
System.out.println("getDatasourcesOfUserType returned in " + (stop-start) + "ms ");
System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms ");
return entries;
}
private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
int page = 0;
int size = 50;
List<Repository> rs ;
List<Repository> resultSet = new ArrayList<>();
while (true){
rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
resultSet.addAll(rs);
page+=1;
if(rs.size() == 0) break;
}
return resultSet;
}
private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
return null;
}
@Override
public EventsPage showEvents(String datasourceName,
String topic,
@ -230,7 +209,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<EventsPage>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on showEvents" , e);
LOGGER.debug("Exception on showEvents", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -256,7 +235,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on getSimpleSubscriptionsOfUser" , e);
LOGGER.debug("Exception on getSimpleSubscriptionsOfUser", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -292,7 +271,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<Subscription>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on OpenaireSubscription" , e);
LOGGER.debug("Exception on OpenaireSubscription", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -316,15 +295,15 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<Void>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on unsubscribe" , e);
LOGGER.debug("Exception on unsubscribe", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
return new ResponseEntity<>("OK",HttpStatus.OK);
return new ResponseEntity<>("OK", HttpStatus.OK);
}
@Override
public Subscription getSubscription( String subscriptionId) throws BrokerException {
public Subscription getSubscription(String subscriptionId) throws BrokerException {
final String service = "/subscriptions/" + subscriptionId;
//build the uri params
@ -340,7 +319,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<Subscription>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on getSubscription" , e);
LOGGER.debug("Exception on getSubscription", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -360,7 +339,7 @@ public class BrokerServiceImpl implements BrokerService {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(openairePath + "/notifications/")
.path("/{id}/{page}/{size}/")
.build().expand(subscriptionId,page, size).encode();
.build().expand(subscriptionId, page, size).encode();
ResponseEntity<EventsPage> resp;
try {
@ -371,7 +350,7 @@ public class BrokerServiceImpl implements BrokerService {
new ParameterizedTypeReference<EventsPage>() {
});
} catch (RestClientException e) {
LOGGER.debug("Exception on getNotificationsBySubscriptionId" , e);
LOGGER.debug("Exception on getNotificationsBySubscriptionId", e);
emailUtils.reportException(e);
throw new BrokerException(e);
}
@ -383,16 +362,16 @@ public class BrokerServiceImpl implements BrokerService {
throws BrokerException {
Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
Map<String,List<Subscription>> subs = new HashMap<>();
Map<String, List<Subscription>> subs = new HashMap<>();
List<Subscription> subscriptions = null;
for(String s:simpleSubs.keySet()){
for (String s : simpleSubs.keySet()) {
List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
for (SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
subscriptions = new ArrayList<>();
subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
}
subs.put(s,subscriptions);
subs.put(s, subscriptions);
}
return subs;
}

View File

@ -1,6 +1,5 @@
package eu.dnetlib.repo.manager.service;
import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.domain.broker.BrowseEntry;
import org.apache.log4j.Logger;
@ -27,58 +26,58 @@ public class DashboardServiceImpl implements DashboardService {
@Override
public List<RepositorySummaryInfo> getRepositoriesSummaryInfo(String userEmail,
String page,
String size){
String size) {
List<RepositorySummaryInfo> repositorySummaryInfoList = new ArrayList<>();
try {
List<Repository> repositoriesOfUser = repositoryService.getRepositoriesOfUser(userEmail, page, size);
for(Repository repository: repositoriesOfUser) {
RepositorySummaryInfo repositorySummaryInfo = new RepositorySummaryInfo();
repositorySummaryInfo.setId(repository.getId());
repositorySummaryInfo.setRepositoryName(repository.getOfficialName());
repositorySummaryInfo.setLogoURL(repository.getLogoUrl());
List<RepositorySnippet> repositoriesOfUser = repositoryService.getRepositoriesSnippetOfUser(userEmail, page, size);
for (RepositorySnippet repository : repositoriesOfUser) {
RepositorySummaryInfo repositorySummaryInfo = new RepositorySummaryInfo();
repositorySummaryInfo.setId(repository.getId());
repositorySummaryInfo.setRepositoryName(repository.getOfficialName());
repositorySummaryInfo.setLogoURL(repository.getLogoUrl());
//TODO getRepositoryAggregations returns only the 20 more recent items. Is it positive that we will find an indexed version there?
long start = System.currentTimeMillis();
List<AggregationDetails> aggregationDetailsList = repositoryService.getRepositoryAggregations(repository.getId(),0,20);
for(AggregationDetails aggregationDetails: aggregationDetailsList) {
if(aggregationDetails.getIndexedVersion()) {
repositorySummaryInfo.setRecordsCollected(aggregationDetails.getNumberOfRecords());
repositorySummaryInfo.setLastIndexedVersion(aggregationDetails.getDate());
break;
}
//TODO getRepositoryAggregations returns only the 20 more recent items. Is it positive that we will find an indexed version there?
long start = System.currentTimeMillis();
List<AggregationDetails> aggregationDetailsList = repositoryService.getRepositoryAggregations(repository.getId(), 0, 20);
for (AggregationDetails aggregationDetails : aggregationDetailsList) {
if (aggregationDetails.getIndexedVersion()) {
repositorySummaryInfo.setRecordsCollected(aggregationDetails.getNumberOfRecords());
repositorySummaryInfo.setLastIndexedVersion(aggregationDetails.getDate());
break;
}
long end = System.currentTimeMillis();
System.out.println("Got repo aggregations in "+ (end-start)+"ms");
try {
MetricsInfo metricsInfo = repositoryService.getMetricsInfoForRepository(repository.getId());
repositorySummaryInfo.setTotalDownloads(metricsInfo.getMetricsNumbers().getTotalDownloads());
repositorySummaryInfo.setTotalViews(metricsInfo.getMetricsNumbers().getTotalViews());
} catch (RepositoryServiceException e) {
logger.error("Exception getting metrics info for repository: " + repository.getId(), e);
}
try {
List<BrowseEntry> events = brokerService.getTopicsForDatasource(repository.getOfficialName());
Long totalEvents = 0L;
for(BrowseEntry browseEntry: events)
totalEvents += browseEntry.getSize();
repositorySummaryInfo.setEnrichmentEvents(totalEvents);
} catch (BrokerException e) {
logger.error("Exception getting broker events for repository: " + repository.getId(), e);
}
repositorySummaryInfoList.add(repositorySummaryInfo);
}
long end = System.currentTimeMillis();
System.out.println("Got repo aggregations in " + (end - start) + "ms");
try {
MetricsInfo metricsInfo = repositoryService.getMetricsInfoForRepository(repository.getId());
repositorySummaryInfo.setTotalDownloads(metricsInfo.getMetricsNumbers().getTotalDownloads());
repositorySummaryInfo.setTotalViews(metricsInfo.getMetricsNumbers().getTotalViews());
} catch (RepositoryServiceException e) {
logger.error("Exception getting metrics info for repository: " + repository.getId(), e);
}
try {
List<BrowseEntry> events = brokerService.getTopicsForDatasource(repository.getOfficialName());
Long totalEvents = 0L;
for (BrowseEntry browseEntry : events)
totalEvents += browseEntry.getSize();
repositorySummaryInfo.setEnrichmentEvents(totalEvents);
} catch (BrokerException e) {
logger.error("Exception getting broker events for repository: " + repository.getId(), e);
}
repositorySummaryInfoList.add(repositorySummaryInfo);
}
} catch (Exception e) {
logger.error("Something baad happened!", e);

View File

@ -14,7 +14,8 @@ import java.util.Map;
public interface RepositoryService {
Country[] getCountries() ;
// TODO: move this elsewhere
Country[] getCountries();
List<Repository> getRepositories(List<String> ids) throws JSONException;
@ -26,8 +27,10 @@ public interface RepositoryService {
List<RepositorySnippet> getRepositoriesByCountry(String country, String mode, Boolean managed) throws JSONException, IOException;
// TODO: remove?
List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException, IOException;
// TODO: remove?
List<Repository> getRepositoriesOfUser(String userEmail,
String page,
String size) throws JSONException, IOException;
@ -37,19 +40,20 @@ public interface RepositoryService {
List<RepositorySnippet> getRepositoriesSnippetOfUser(String userEmail, String page, String size) throws Exception;
RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException;
Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException;
List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException;
Map<String,List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException;
Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException;
List<Repository> getRepositoriesByName(String name,
String page,
String size) throws JSONException;
List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
String officialName, String requestSortBy, String order,
int page, int pageSize) throws Exception;
String officialName, String requestSortBy, String order,
int page, int pageSize) throws Exception;
List<RepositoryInterface> getRepositoryInterface(String id) throws JSONException;
@ -72,7 +76,7 @@ public interface RepositoryService {
List<String> getUrlsOfUserRepos(String user_email,
String page,
String size) throws JSONException;
String size);
List<String> getDatasourceVocabularies(String mode);
@ -84,7 +88,7 @@ public interface RepositoryService {
MetricsInfo getMetricsInfoForRepository(String repoId) throws RepositoryServiceException;
Map<String, String> getListLatestUpdate(String mode) throws RepositoryServiceException, JSONException;
Map<String, String> getListLatestUpdate(String mode) throws JSONException;
RepositoryInterface updateRepositoryInterface(String repoId, String registeredBy, String comment, RepositoryInterface repositoryInterface) throws Exception;

View File

@ -156,19 +156,19 @@ public class Converter {
repositorySnippet.setId(repositorySnippetObject.get("id").toString());
repositorySnippet.setOfficialname(repositorySnippetObject.get("officialname").toString());
repositorySnippet.setOfficialName(repositorySnippetObject.get("officialname").toString());
repositorySnippet.setEnglishname(repositorySnippetObject.get("englishname").toString());
if (repositorySnippet.getEnglishname().equals("null"))
repositorySnippet.setEnglishname("");
repositorySnippet.setEnglishName(repositorySnippetObject.get("englishname").toString());
if (repositorySnippet.getEnglishName().equals("null"))
repositorySnippet.setEnglishName("");
repositorySnippet.setWebsiteurl(repositorySnippetObject.get("websiteurl").toString());
if (repositorySnippet.getWebsiteurl().equals("null"))
repositorySnippet.setWebsiteurl("");
repositorySnippet.setWebsiteUrl(repositorySnippetObject.get("websiteurl").toString());
if (repositorySnippet.getWebsiteUrl().equals("null"))
repositorySnippet.setWebsiteUrl("");
repositorySnippet.setRegisteredby(repositorySnippetObject.get("registeredby").toString());
if (repositorySnippet.getRegisteredby().equals("null"))
repositorySnippet.setRegisteredby("");
repositorySnippet.setRegisteredBy(repositorySnippetObject.get("registeredby").toString());
if (repositorySnippet.getRegisteredBy().equals("null"))
repositorySnippet.setRegisteredBy("");
return repositorySnippet;
}