diff --git a/src/main/java/eu/dnetlib/repo/manager/service/config/FrontEndLinkURIAuthenticationSuccessHandler.java b/src/main/java/eu/dnetlib/repo/manager/service/config/FrontEndLinkURIAuthenticationSuccessHandler.java index abff183..d0f5c39 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/config/FrontEndLinkURIAuthenticationSuccessHandler.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/config/FrontEndLinkURIAuthenticationSuccessHandler.java @@ -27,9 +27,6 @@ public class FrontEndLinkURIAuthenticationSuccessHandler implements Authenticati @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { - LOGGER.info(request); - LOGGER.info(response); - OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication; Cookie sessionCookie = new Cookie("currentUser", authOIDC.getSub()); int expireSec = -1; diff --git a/src/main/java/eu/dnetlib/repo/manager/service/utils/OMTDAuthoritiesMapper.java b/src/main/java/eu/dnetlib/repo/manager/service/config/OpenAireProviderAuthoritiesMapper.java similarity index 73% rename from src/main/java/eu/dnetlib/repo/manager/service/utils/OMTDAuthoritiesMapper.java rename to src/main/java/eu/dnetlib/repo/manager/service/config/OpenAireProviderAuthoritiesMapper.java index 56bb73d..6218cdf 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/utils/OMTDAuthoritiesMapper.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/config/OpenAireProviderAuthoritiesMapper.java @@ -1,4 +1,4 @@ -package eu.dnetlib.repo.manager.service.utils; +package eu.dnetlib.repo.manager.service.config; import com.nimbusds.jwt.JWT; import org.mitre.openid.connect.client.OIDCAuthoritiesMapper; @@ -10,23 +10,24 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import java.util.*; -public class OMTDAuthoritiesMapper implements OIDCAuthoritiesMapper { +public class OpenAireProviderAuthoritiesMapper implements OIDCAuthoritiesMapper { - private static Logger logger = LoggerFactory.getLogger(OMTDAuthoritiesMapper.class); + private static Logger logger = LoggerFactory.getLogger(OpenAireProviderAuthoritiesMapper.class); final private static String ROLE_CLAIMS = "edu_person_entitlements"; private Map userRolesMap; - OMTDAuthoritiesMapper(Map userRoles) { + OpenAireProviderAuthoritiesMapper(Map userRoles) { userRolesMap = new HashMap<>(); - userRoles.forEach((omtdRole, appRole) -> userRolesMap.put(omtdRole, new SimpleGrantedAuthority(appRole))); + userRoles.forEach((openaireRole, appRole) -> userRolesMap.put(openaireRole, new SimpleGrantedAuthority(appRole))); } @Override public Collection mapAuthorities(JWT idToken, UserInfo userInfo) { Set out = new HashSet<>(); out.add(new SimpleGrantedAuthority("ROLE_USER")); + if(userInfo.getSource().getAsJsonArray(ROLE_CLAIMS) != null) { userInfo.getSource().getAsJsonArray(ROLE_CLAIMS).forEach(role -> { SimpleGrantedAuthority authority = userRolesMap.get(role.getAsString()); @@ -38,4 +39,4 @@ public class OMTDAuthoritiesMapper implements OIDCAuthoritiesMapper { } return out; } -} \ No newline at end of file +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtils.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtils.java similarity index 81% rename from src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtils.java rename to src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtils.java index df4db95..bb01264 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtils.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtils.java @@ -1,7 +1,6 @@ -package eu.dnetlib.repo.manager.service.utils; +package eu.dnetlib.repo.manager.service.controllers; import eu.dnetlib.domain.data.PiwikInfo; -import eu.dnetlib.domain.functionality.UserProfile; public interface EmailUtils { diff --git a/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtilsImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtilsImpl.java similarity index 99% rename from src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtilsImpl.java rename to src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtilsImpl.java index f82b782..7dbc189 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtilsImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtilsImpl.java @@ -1,4 +1,4 @@ -package eu.dnetlib.repo.manager.service.utils; +package eu.dnetlib.repo.manager.service.controllers; import eu.dnetlib.domain.data.PiwikInfo; import eu.dnetlib.repo.manager.service.config.CascadingPropertyLoader; @@ -15,7 +15,7 @@ import java.util.ArrayList; import java.util.List; -@Component +@Component("emailUtils") public class EmailUtilsImpl implements EmailUtils { private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class); @@ -39,7 +39,6 @@ public class EmailUtilsImpl implements EmailUtils { @Value("${services.repomanager.usagestats.adminEmail}") private String usageStatsAdminEmail; - @Override public void reportException(Exception exception) { Writer writer = new StringWriter(); @@ -187,7 +186,7 @@ public class EmailUtilsImpl implements EmailUtils { private void sendMail(List recipients, String subject, String message, boolean sendToSpecial, List repoAdminMails) throws Exception { - /* try { + try { if (sendToSpecial) { recipients.addAll(this.specialRecipients); } @@ -205,7 +204,7 @@ public class EmailUtilsImpl implements EmailUtils { } catch (Exception e) { LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e); throw new Exception(e); - }*/ + } } private String getEmailProperty(String key) { diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApiImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApiImpl.java index 2337722..693b4fc 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApiImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApiImpl.java @@ -50,8 +50,6 @@ public class MonitorApiImpl implements MonitorApi { LOGGER.debug("Getting jobs of user : " + user); LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal); JobsOfUser retJobs = new JobsOfUser(); - LOGGER.debug("Size of jobs list -> " + getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), - Integer.parseInt(limit), dateFrom, dateTo, validationStatus).size()); retJobs.setJobs(getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset), Integer.parseInt(limit), dateFrom, dateTo, validationStatus)); if (Boolean.parseBoolean(includeJobsTotal)) { @@ -62,21 +60,24 @@ public class MonitorApiImpl implements MonitorApi { } //TODO fix status with new validator version - for(StoredJob job :retJobs.getJobs()){ - if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) { - job.setValidationStatus("ongoing"); - } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50) - || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50) - || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) { - job.setValidationStatus("successful"); - } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50)) - || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50) - || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) { - job.setValidationStatus("failed"); - } + if(retJobs.getJobs() != null){ + for(StoredJob job :retJobs.getJobs()){ + if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) { + job.setValidationStatus("ongoing"); + } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50) + || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50) + || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) { + job.setValidationStatus("successful"); + } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50)) + || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50) + || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) { + job.setValidationStatus("failed"); + } + } } + return retJobs; } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApi.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApi.java index cf8866b..cc51d22 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApi.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApi.java @@ -2,6 +2,9 @@ package eu.dnetlib.repo.manager.service.controllers; import eu.dnetlib.domain.data.PiwikInfo; +import eu.dnetlib.domain.data.Repository; +import eu.dnetlib.domain.functionality.UserProfile; +import eu.dnetlib.repo.manager.shared.RepositoryServiceException; import io.swagger.annotations.Api; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; @@ -31,4 +34,14 @@ public interface PiWikApi { @RequestMapping(value = "/getOpenaireId/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody String getOpenaireId(String repositoryid); + + + @RequestMapping(value = "/markPiwikSiteAsValidated/{repositoryId}" , method = RequestMethod.POST, + consumes = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + void markPiwikSiteAsValidated(String repositoryId) throws RepositoryServiceException; + + @RequestMapping(value = "/enableMetricsForRepository", method = RequestMethod.POST, + consumes = MediaType.APPLICATION_JSON_VALUE) + void enableMetricsForRepository(PiwikInfo piwikInfo) throws RepositoryServiceException; } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java index a923820..acae70e 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java @@ -1,10 +1,12 @@ package eu.dnetlib.repo.manager.service.controllers; +import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.domain.data.PiwikInfo; -import eu.dnetlib.utils.md5.MD5; +import eu.dnetlib.repo.manager.shared.RepositoryServiceException; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; @@ -12,13 +14,14 @@ import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; -import javax.annotation.PostConstruct; import javax.sql.DataSource; -import java.security.NoSuchAlgorithmException; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLEncoder; import java.sql.Types; import java.util.List; +import java.util.Map; @Component public class PiWikApiImpl implements PiWikApi{ @@ -27,6 +30,15 @@ public class PiWikApiImpl implements PiWikApi{ @Qualifier("repomanager.dataSource") private DataSource dataSource; + + @Value("${services.repomanager.analyticsURL}") + private String analyticsURL; + + + @Autowired + @Qualifier("emailUtils") + EmailUtils emailUtils; + private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger .getLogger(PiWikApiImpl.class); @@ -39,6 +51,7 @@ public class PiWikApiImpl implements PiWikApi{ private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;"; + private RowMapper piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"), rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"), rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment")); @@ -85,5 +98,68 @@ public class PiWikApiImpl implements PiWikApi{ return null; } + @Override + public void markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException { + try { + approvePiwikSite(repositoryId); + + PiwikInfo piwikInfo = getPiwikSiteForRepo(repositoryId); + emailUtils.sendAdministratorMetricsEnabled(piwikInfo); + emailUtils.sendUserMetricsEnabled(piwikInfo); + + } catch (EmptyResultDataAccessException e) { + LOGGER.error("Error while approving piwik site: ", e); + emailUtils.reportException(e); + throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR); + } catch (Exception e) { + LOGGER.error("Error while sending email to administrator or user about the enabling of metrics", e); + emailUtils.reportException(e); + } + } + + @Override + public void enableMetricsForRepository(@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException { + + try { + String URL = analyticsURL + "siteName=" + URLEncoder.encode(piwikInfo.getRepositoryName(), "UTF-8") + + "&url=" + piwikInfo.getSiteId(); + Map map = new ObjectMapper().readValue(new URL(URL), Map.class); + + /* String siteId = null; + if(map.get("value")!=null) { + siteId = map.get("value").toString(); + } + + String authenticationToken = "32846584f571be9b57488bf4088f30ea"; + + PiwikInfo piwikInfo = new PiwikInfo(); + piwikInfo.setRepositoryId(repository.getId()); + piwikInfo.setRepositoryName(repository.getOfficialName()); + piwikInfo.setCountry(repository.getCountryName()); + piwikInfo.setSiteId(siteId); + piwikInfo.setAuthenticationToken(authenticationToken); + piwikInfo.setRequestorEmail(userProfile.getEmail()); + piwikInfo.setRequestorName(userProfile.getFirstname() + " " + userProfile.getLastname()); + piwikInfo.setValidated(false); +*/ + savePiwikInfo(piwikInfo); + + emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo); + emailUtils.sendUserRequestToEnableMetrics(piwikInfo); + + } catch (UnsupportedEncodingException uee) { + LOGGER.error("Error while creating piwikScript URL", uee); + emailUtils.reportException(uee); + throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR); + } catch (IOException ioe) { + LOGGER.error("Error while creating piwik site", ioe); + emailUtils.reportException(ioe); + throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR); + } catch (Exception e) { + LOGGER.error("Error while sending email to administrator or user about the request to enable metrics", e); + emailUtils.reportException(e); + } + } + } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApi.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApi.java index c0ed3f3..460edec 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApi.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApi.java @@ -2,6 +2,7 @@ package eu.dnetlib.repo.manager.service.controllers; import eu.dnetlib.domain.data.Repository; import eu.dnetlib.domain.data.RepositoryInterface; +import eu.dnetlib.domain.functionality.UserProfile; import eu.dnetlib.repo.manager.shared.*; import io.swagger.annotations.Api; import org.json.JSONException; @@ -68,8 +69,7 @@ public interface RepositoryApi { Repository addRepository(String datatype, Repository repository) throws Exception; - @RequestMapping(value = "/deleteInterface", method = RequestMethod.DELETE) - @ResponseBody + @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE) void deleteRepositoryInterface(String id); @RequestMapping(value = "/addInterface", method = RequestMethod.POST, @@ -134,6 +134,11 @@ public interface RepositoryApi { @ResponseBody Map getListLatestUpdate(String mode) throws RepositoryServiceException, JSONException; + @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST, + consumes = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + RepositoryInterface updateRepositoryInterface(String repositoryId,RepositoryInterface repositoryInterface) throws JSONException; + } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApiImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApiImpl.java index ad5b88c..ace3af3 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApiImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApiImpl.java @@ -151,16 +151,15 @@ public class RepositoryApiImpl implements RepositoryApi { String countryCode = countriesMap.get(country); String filterKey = "UNKNOWN"; - if (mode.equalsIgnoreCase("opendoar")) { + if (mode.equalsIgnoreCase("opendoar")) filterKey = "openaire____::opendoar"; - } else if (mode.equalsIgnoreCase("re3data")) { + else if (mode.equalsIgnoreCase("re3data")) filterKey = "openaire____::re3data"; - } else if (mode.equalsIgnoreCase("jour_aggr")) { - filterKey = "infrastruct_::openaire"; - } + LOGGER.debug("Country code equals : " + countryCode); LOGGER.debug("Filter mode equals : " + filterKey); + UriComponents uriComponents = searchDatasource(String.valueOf(page),String.valueOf(size)); RequestFilter requestFilter = new RequestFilter(); requestFilter.setCountry(countryCode); @@ -168,14 +167,9 @@ public class RepositoryApiImpl implements RepositoryApi { String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); while (jsonArray.length() > 0 ) { - List rep = Converter.jsonToRepositoryList(new JSONObject(rs)); - - Collection repos = this.getRepositoriesByMode(filterKey, rep); - resultSet.addAll(repos); - + resultSet.addAll(this.getRepositoriesByMode(filterKey, Converter.jsonToRepositoryList(new JSONObject(rs)))); page += 1; uriComponents = searchDatasource(String.valueOf(page),String.valueOf(size)); - rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); } @@ -198,6 +192,7 @@ public class RepositoryApiImpl implements RepositoryApi { for (Repository r : rs) { if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode)) reps.add(r); + } return reps; } @@ -298,38 +293,7 @@ public class RepositoryApiImpl implements RepositoryApi { repository = this.setRepositoryFeatures(datatype,repository); LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId()); - if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) { - if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) { - LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources"); - if (getRepositoryById(repository.getId()) != null) { - String retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources."; - repository.getInterfaces().clear(); - LOGGER.debug(retMessage); - } else { - LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting.."); - this.storeRepository(repository); - } - } - } else { - this.updateRepository(repository); - } - - LOGGER.debug("Inserting Interfaces"); - Iterator var11 = repository.getInterfaces().iterator(); - - while (var11.hasNext()) { - RepositoryInterface iFace = (RepositoryInterface) var11.next(); - if (!iFace.getBaseUrl().isEmpty() && !iFace.getDesiredCompatibilityLevel().isEmpty()) { - if (iFace.getId() != null && !iFace.getId().isEmpty()) { - LOGGER.debug("updating iface.."); - this.updateInterface(datatype,iFace); - LOGGER.debug("updated successfully"); - } else { - LOGGER.debug("adding new iface.."); - this.registerRepositoryInterface(repository.getId(),iFace,datatype); - } - } - } + this.storeRepository(repository); return repository; } @@ -345,19 +309,6 @@ public class RepositoryApiImpl implements RepositoryApi { return repository; } - private void updateRegisteredByValue(String id, String registeredBy) { - - LOGGER.debug("Updating registered by value with : " + registeredBy ); - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(baseAddress + "/ds/registeredby/") - .queryParam("dsId",id) - .queryParam("registeredBy", registeredBy) - .build() - .encode(); - - restTemplate.postForObject(uriComponents.toUri(), null,String.class); - } - private Repository setRepositoryFeatures(String datatype, Repository repository) { //TODO update map @@ -415,45 +366,13 @@ public class RepositoryApiImpl implements RepositoryApi { } - private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) { - - iFace.setContentDescription("metadata"); - iFace.setCompliance("UNKNOWN"); - if (datatype.equals("re3data")) { - iFace.setAccessFormat("oai_datacite"); - } else { - iFace.setAccessFormat("oai_dc"); - } - - if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) { - iFace.setTypology(repo.getDatasourceClass()); - } else if (datatype.equalsIgnoreCase("journal")) { - iFace.setTypology("pubsrepository::journal"); - } else if (datatype.equalsIgnoreCase("aggregator")) { - iFace.setTypology("aggregator::pubsrepository::unknown"); - } else if (datatype.equalsIgnoreCase("opendoar")) { - iFace.setTypology("pubsrepository::unknown"); - } else if (datatype.equalsIgnoreCase("re3data")) { - iFace.setTypology("datarepository::unknown"); - } - - iFace.setRemovable(true); - iFace.setAccessProtocol("oai"); - iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); - iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); - if (iFace.getAccessSet().isEmpty()) { - LOGGER.debug("set is empty: " + iFace.getAccessSet()); - iFace.removeAccessSet(); - } - return iFace; - } - private void storeRepository(Repository repository) throws JSONException { Date utilDate = new Date(); Timestamp date = new Timestamp(utilDate.getTime()); repository.setDateOfCollection(date); repository.setAggregator("OPENAIRE"); + repository.setCountryCode(countriesMap.get(repository.getCountryName())); UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/add/") @@ -465,11 +384,12 @@ public class RepositoryApiImpl implements RepositoryApi { } @Override - public void deleteRepositoryInterface(@PathVariable("id") String id){ + public void deleteRepositoryInterface(@RequestParam("id") String id){ UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/api/") - .path("/{id}/") + .path("/{id}") .build().expand(id).encode(); + LOGGER.debug(uriComponents.toUri()); restTemplate.delete(uriComponents.toUri()); } @@ -477,26 +397,19 @@ public class RepositoryApiImpl implements RepositoryApi { public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype, @RequestParam("repoId") String repoId, @RequestBody RepositoryInterface repositoryInterface) throws JSONException { - return registerRepositoryInterface(repoId,repositoryInterface,datatype); - } - - private RepositoryInterface registerRepositoryInterface(String repoId, RepositoryInterface iFace, String datatype) { - Repository e = null; try { - e = this.getRepositoryById(repoId); - iFace = createRepositoryInterface(e,iFace,datatype); + Repository e = this.getRepositoryById(repoId); + repositoryInterface = createRepositoryInterface(e,repositoryInterface,datatype); + String json_interface = Converter.repositoryInterfaceObjectToJson(e,repositoryInterface); - String json_interface = Converter.repositoryInterfaceObjectToJson(e,iFace); - LOGGER.debug("iFace equals -> " + json_interface); UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/api/add/") .build() .encode(); - HttpEntity httpEntity = new HttpEntity (json_interface,httpHeaders); restTemplate.postForObject(uriComponents.toUri(),httpEntity,String.class); - return iFace; + return repositoryInterface; } catch (JSONException e1) { LOGGER.debug("Error parsing json ",e1); @@ -504,6 +417,39 @@ public class RepositoryApiImpl implements RepositoryApi { return null; } + private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) { + + iFace.setContentDescription("metadata"); + iFace.setCompliance("UNKNOWN"); + + if (datatype.equals("re3data")) + iFace.setAccessFormat("oai_datacite"); + else + iFace.setAccessFormat("oai_dc"); + + + if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) + iFace.setTypology(repo.getDatasourceClass()); + else if (datatype.equalsIgnoreCase("journal")) + iFace.setTypology("pubsrepository::journal"); + else if (datatype.equalsIgnoreCase("aggregator")) + iFace.setTypology("aggregator::pubsrepository::unknown"); + else if (datatype.equalsIgnoreCase("opendoar")) + iFace.setTypology("pubsrepository::unknown"); + else if (datatype.equalsIgnoreCase("re3data")) + iFace.setTypology("datarepository::unknown"); + + iFace.setRemovable(true); + iFace.setAccessProtocol("oai"); + iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); + iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); + if (iFace.getAccessSet().isEmpty()) { + LOGGER.debug("set is empty: " + iFace.getAccessSet()); + iFace.removeAccessSet(); + } + return iFace; + } + @Override public List getDnetCountries() { LOGGER.debug("Getting dnet-countries!"); @@ -656,6 +602,36 @@ public class RepositoryApiImpl implements RepositoryApi { return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::"+mode).get(1).getLastCollectionDate()); } + @Override + public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId, + @RequestBody RepositoryInterface repositoryInterface) throws JSONException { + + this.updateBaseUrl(repoId,repositoryInterface.getId(),repositoryInterface.getBaseUrl()); + this.updateCompliance(repoId,repositoryInterface.getId(),repositoryInterface.getCompliance()); + return repositoryInterface; + } + + + private void updateBaseUrl(String repositoryId, String repositoryInterfaceId, String baseUrl) { + UriComponents uriComponents = UriComponentsBuilder + .fromHttpUrl(baseAddress + "/ds/api/baseurl") + .queryParam("dsId",repositoryId) + .queryParam("apiId",repositoryInterfaceId) + .queryParam("baseUrl",baseUrl) + .build().encode(); + restTemplate.postForObject(uriComponents.toUri(),null,String.class); + } + + private void updateCompliance(String repositoryId, String repositoryInterfaceId,String compliance) { + UriComponents uriComponents = UriComponentsBuilder + .fromHttpUrl(baseAddress + "/ds/api/compliance") + .queryParam("dsId",repositoryId) + .queryParam("apiId",repositoryInterfaceId) + .queryParam("compliance",compliance) + .build().encode(); + restTemplate.postForObject(uriComponents.toUri(),null,String.class); + } + private MetricsNumbers getMetricsNumbers(String openAIREID) throws BrokerException { //build the uri params diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/UserApiImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/UserApiImpl.java index 603162c..f846397 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/controllers/UserApiImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/UserApiImpl.java @@ -30,11 +30,14 @@ public class UserApiImpl implements UserApi { LOGGER.debug("User authentication : " + authentication); Map body = new HashMap<>(); body.put("sub",authentication.getSub()); - if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals("")) { + if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals("")) body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName()); - } else { + else body.put("name",authentication.getUserInfo().getName()); - } + + for (GrantedAuthority authority : authentication.getAuthorities()) + LOGGER.debug("Role: " + authority.getAuthority()); + body.put("email",authentication.getUserInfo().getEmail()); List roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); body.put("role",roles); diff --git a/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java b/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java index fed4bfb..edc771f 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java @@ -3,7 +3,6 @@ package eu.dnetlib.repo.manager.service.utils; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.domain.data.Repository; import eu.dnetlib.domain.data.RepositoryInterface; -import eu.dnetlib.repo.manager.service.controllers.PiWikApi; import eu.dnetlib.repo.manager.shared.AggregationDetails; import eu.dnetlib.repo.manager.shared.Timezone; import org.apache.commons.codec.digest.DigestUtils; @@ -11,8 +10,6 @@ import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; import java.io.BufferedReader; import java.io.IOException; @@ -34,83 +31,53 @@ public class Converter { if( datasource.equals(null)) return null; - - repository.setActivationId(datasource.get("activationId").toString()); - repository.setAggregator(datasource.get("aggregator").toString()); -// repository.setCertificates(datasource.get("certificates").toString()); -// repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString()); - repository.setCollectedFrom( datasource.get("collectedfrom").toString()); - repository.setContactEmail(datasource.get("contactemail").toString()); - if(repository.getContactEmail().equals("null")) - repository.setContactEmail(""); - -// repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString()); -// repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString()); -// repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString()); -// repository.setDataUploadType(datasource.get("datauploadtype").toString()); - repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString())); - repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString())); - - repository.setDescription(datasource.get("description").toString()); - if(repository.getDescription().equals("null")) - repository.setDescription(""); - - repository.setEissn(datasource.get("eissn").toString()); + repository.setId(datasource.get("id").toString()); + repository.setOfficialName(datasource.get("officialname").toString()); repository.setEnglishName( datasource.get("englishname").toString()); if(repository.getEnglishName().equals("null")) repository.setEnglishName(""); - - repository.setId(datasource.get("id").toString()); - repository.setIssn(datasource.get("issn").toString()); - repository.setOdLanguages(datasource.get("languages").toString()); - repository.setLatitude( toDouble(datasource.get("latitude").toString())); - repository.setLissn(datasource.get("lissn").toString()); - + repository.setWebsiteUrl(datasource.get("websiteurl").toString()); repository.setLogoUrl(datasource.get("logourl").toString()); if(repository.getLogoUrl().equals("null")) repository.setLogoUrl(""); + repository.setContactEmail(datasource.get("contactemail").toString()); + repository.setLatitude( toDouble(datasource.get("latitude").toString())); repository.setLongitude(toDouble(datasource.get("longitude").toString())); - //datasource.get("managed"); -// repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString()); - repository.setNamespacePrefix(datasource.get("namespaceprefix").toString()); -// repository.setOdContentTypes(datasource.get("od_contenttypes").toString()); - repository.setOfficialName(datasource.get("officialname").toString()); - if(repository.getOfficialName().equals("null")) - repository.setOfficialName(""); - -// repository.setPidSystems(datasource.get("pidsystems").toString()); - //datasource.get("platform"); -// repository.setProvenanceActionClass( datasource.get("provenanceaction").toString()); -// repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString()); - repository.setRegisteredBy(datasource.get("registeredby").toString()); - - if(Objects.equals(repository.getRegisteredBy(),"null")) - repository.setRegistered(true); - -// repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString())); -// repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString())); -// repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString())); - //datasource.get("subjects"); Double timezone = toDouble(datasource.get("timezone").toString()); repository.setTimezone(timezone!=null?timezone:0.0); + repository.setNamespacePrefix(datasource.get("namespaceprefix").toString()); + repository.setOdLanguages(datasource.get("languages").toString()); + repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString())); + + /* typology -> platform + * datasource class -> typology */ repository.setTypology(datasource.get("platform").toString()); -// repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString())); - repository.setWebsiteUrl(datasource.get("websiteurl").toString()); + if(repository.getTypology().equals("null")) + repository.setTypology(""); repository.setDatasourceClass(datasource.get("typology").toString()); - //TODO change organization to list - JSONArray organizations = ((JSONArray)datasource.get("organizations")); - if(organizations.length() != 0) { - repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString()); - String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString(); - repository.setCountryCode(countryCode); - } + repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString())); + repository.setActivationId(datasource.get("activationId").toString()); + + repository.setDescription(datasource.get("description").toString()); + if(repository.getDescription().equals("null")) + repository.setDescription(""); + + repository.setIssn(datasource.get("issn").toString()); + repository.setLissn(datasource.get("lissn").toString()); + repository.setEissn(datasource.get("eissn").toString()); + repository.setRegisteredBy(datasource.get("registeredby").toString()); + + /* managed field */ + repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString())); + + //subjects + repository.setAggregator(datasource.get("aggregator").toString()); - String collectedFrom = datasource.get("collectedfrom").toString(); //TODO check data consistency String type = "UNKNOWN"; @@ -121,7 +88,20 @@ public class Converter { } else if (collectedFrom.equalsIgnoreCase("infrastruct_::openaire")) { type = "journal"; } + /* collected from field */ repository.setDatasourceType(type); + repository.setCollectedFrom(collectedFrom); + + //TODO change organization to list + JSONArray organizations = ((JSONArray)datasource.get("organizations")); + if(organizations.length() != 0) { + repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString()); + String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString(); + repository.setCountryCode(countryCode); + } + + /* identities field */ + return repository; } @@ -177,18 +157,21 @@ public class Converter { RepositoryInterface repositoryInterface = new RepositoryInterface(); - repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString()); - repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString()); repositoryInterface.setId(repositoryInterfaceObject.get("id").toString()); - // repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString()); repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString()); + repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString()); repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString()); - repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString()); - //repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString())); - repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString())); repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString()); repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString()); + repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString()); + repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString())); + + + // repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString()); + repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString()); + //repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString())); + Map accessParams = new HashMap<>(); Map extraFields = new HashMap<>(); @@ -199,6 +182,8 @@ public class Converter { for(int i=0;i platform + * datasource class -> typology + * */ + jsonObject.put("typology",repository.getDatasourceClass()); + jsonObject.put("platform",repository.getTypology()); + jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection())); -// jsonObject.put("platform",repository.getPlatform()); jsonObject.put("activationId",repository.getActivationId()); jsonObject.put("description",repository.getDescription()); jsonObject.put("eissn",repository.getEissn()); jsonObject.put("issn",repository.getIssn()); jsonObject.put("lissn",repository.getLissn()); jsonObject.put("registeredby",repository.getRegisteredBy()); -// jsonObject.put("subjects",repository.getSubjects()); + jsonObject.put("aggregator",repository.getAggregator()); jsonObject.put("collectedfrom",repository.getCollectedFrom()); -// jsonObject.put("managed",repository.getIsManaged()); -// jsonObject.put("organizations",repository.getOrganization()); -// for(repository.getOrganization()) + jsonObject.put("managed",repository.isRegistered()); + + JSONObject organization = new JSONObject(); + organization.put("legalname",repository.getOrganization()); + organization.put("country",repository.getCountryCode()); + organization.put("legalshortname",""); + organization.put("websiteurl",""); + organization.put("logourl",""); + + JSONArray organizations = new JSONArray(); + organizations.put(organization); + jsonObject.put("organizations",organizations); + + + //TODO check fields /* jsonObject.put("certificates",repository.getCertificates()); @@ -262,20 +265,26 @@ public class Converter { JSONObject jsonObject = new JSONObject(); - jsonObject.put("baseurl",repositoryInterface.getBaseUrl()); - jsonObject.put("contentdescription",repositoryInterface.getContentDescription()); jsonObject.put("id",repositoryInterface.getId()); - jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath()); jsonObject.put("protocol",repositoryInterface.getAccessProtocol()); + jsonObject.put("datasource",repository.getId()); + jsonObject.put("contentdescription",repositoryInterface.getContentDescription()); jsonObject.put("typology",repositoryInterface.getTypology()); jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel()); - jsonObject.put("datasource",repository.getId()); - jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath()); - jsonObject.put("protocol",repositoryInterface.getAccessProtocol()); + jsonObject.put("compatibilityOverride",repositoryInterface.getDesiredCompatibilityLevel()); + + jsonObject.put("lastCollectionTotal",""); + + jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate()); + jsonObject.put("lastAggregationTotal",""); + jsonObject.put("lastAggregationDate",""); + jsonObject.put("lastDownloadTotal",""); + jsonObject.put("lastDownloadDate",""); + + jsonObject.put("baseurl",repositoryInterface.getBaseUrl()); jsonObject.put("removable",repositoryInterface.isRemovable()); - jsonObject.put("active",repositoryInterface.isActive()); - + JSONArray apiparams = new JSONArray(); for(String param: repositoryInterface.getAccessParams().keySet()){ JSONObject jo = new JSONObject(); @@ -283,19 +292,11 @@ public class Converter { jo.put("value",repositoryInterface.getAccessParams().get(param)); apiparams.put(jo); } - jsonObject.put("apiparam",apiparams); + jsonObject.put("apiParams",apiparams); - jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate()); - //jsonObject.put("lastCollectionMdid",repositoryInterface); - //jsonObject.put("lastCollectionTotal"); - //jsonObject.put("lastDownloadDate"); -// jsonObject.put("lastDownloadMdid"); -// jsonObject.put("lastDownloadTotal"); -// jsonObject.put("lastValidationJob"); - //jsonObject.put("lastAggregationDate"); - //jsonObject.put("lastAggregationMdid"); - //jsonObject.put("lastAggregationTotal"); +// jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath()); + return jsonObject.toString(); } diff --git a/src/main/resources/application-context.xml b/src/main/resources/application-context.xml index 0420009..1078192 100644 --- a/src/main/resources/application-context.xml +++ b/src/main/resources/application-context.xml @@ -1,8 +1,8 @@ - - + + - - - - - - - + + + diff --git a/src/main/webapp/WEB-INF/applicationContext.xml b/src/main/webapp/WEB-INF/applicationContext.xml index d105060..564d389 100644 --- a/src/main/webapp/WEB-INF/applicationContext.xml +++ b/src/main/webapp/WEB-INF/applicationContext.xml @@ -19,6 +19,18 @@ + + + + + + + + + + + + @@ -77,4 +89,5 @@ + \ No newline at end of file