diff --git a/.classpath b/.classpath index 9d886d5..ace8266 100644 --- a/.classpath +++ b/.classpath @@ -27,7 +27,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 4ede96d..714351a 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,2 +1,5 @@ eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/pom.xml b/pom.xml index e30e538..f2eb65e 100644 --- a/pom.xml +++ b/pom.xml @@ -66,20 +66,6 @@ 1.20 - - - javax.cache - cache-api - 1.0.0 - - - org.ehcache - ehcache - 3.5.2 - runtime - - - org.slf4j slf4j-api diff --git a/src/main/java/org/gcube/gcat/oldutils/Validator.java b/src/main/java/org/gcube/gcat/oldutils/Validator.java index 567f679..abe6728 100644 --- a/src/main/java/org/gcube/gcat/oldutils/Validator.java +++ b/src/main/java/org/gcube/gcat/oldutils/Validator.java @@ -70,8 +70,9 @@ public class Validator { public ObjectNode validateAgainstProfile(ObjectNode objectNode, MetadataUtility metadataUtility) throws Exception { ArrayNode extrasArrayOriginal = (ArrayNode) objectNode.get(CKANPackage.EXTRAS_KEY); - if(extrasArrayOriginal == null || extrasArrayOriginal.size()==0) { - throw new BadRequestException("'extras' field is missing in context where metadata profile(s) are defined!"); + if(extrasArrayOriginal == null || extrasArrayOriginal.size() == 0) { + throw new BadRequestException( + "'extras' field is missing in context where metadata profile(s) are defined!"); } ArrayNode groupsArrayOriginal = (ArrayNode) objectNode.get(CKANPackage.GROUPS_KEY); @@ -184,7 +185,7 @@ public class Validator { } // if there are no tags, throw an exception - if(tagsArrayOriginal.size()==0) { + if(tagsArrayOriginal.size() == 0) { throw new BadRequestException("Please define at least one tag for this item!"); } @@ -240,23 +241,23 @@ public class Validator { String sysAdminAPI = CKANUtility.getSysAdminAPI(); CKANGroup ckanGroup = new CKANGroup(); ckanGroup.setApiKey(sysAdminAPI); - ckanGroup.setName(CKANGroup.getGroupName(title)); + ckanGroup.setName(CKANGroup.getCKANGroupName(title)); try { ckanGroup.read(); - } catch (WebApplicationException e) { - if(e.getResponse().getStatus()==Status.NOT_FOUND.getStatusCode()) { + } catch(WebApplicationException e) { + if(e.getResponse().getStatus() == Status.NOT_FOUND.getStatusCode()) { ckanGroup.create(); - }else { + } else { throw e; } - }catch (Exception e) { + } catch(Exception e) { throw new InternalServerErrorException(e); - }finally { + } finally { try { addUserToGroupAsSysAdmin(title); - }catch (WebApplicationException e) { + } catch(WebApplicationException e) { throw e; - }catch (Exception e) { + } catch(Exception e) { throw new InternalServerErrorException(e); } } @@ -267,16 +268,14 @@ public class Validator { addUserToGroupAsSysAdmin(groupName, username); } - public void addUserToGroupAsSysAdmin(String groupName, String username) throws Exception { String sysAdminAPI = CKANUtility.getSysAdminAPI(); CKANUser ckanUser = new CKANUser(); ckanUser.setApiKey(sysAdminAPI); ckanUser.setName(username); - ckanUser.addToGroup(CKANGroup.getGroupName(groupName)); + ckanUser.addToGroup(CKANGroup.getCKANGroupName(groupName)); } - /** * Validate this field and generate a new value (or returns the same if there is nothing to update) * @param metadataIndex @@ -353,16 +352,13 @@ public class Validator { } - - /** * Check if a tag must be generated * @param fieldToValidate * @param metadataField * @param tagsArrayOriginal */ - private void checkAsTag(CustomField fieldToValidate, MetadataField metadataField, - ArrayNode tagsArrayOriginal) { + private void checkAsTag(CustomField fieldToValidate, MetadataField metadataField, ArrayNode tagsArrayOriginal) { MetadataTagging tagging = metadataField.getTagging(); if(tagging != null) { @@ -405,8 +401,8 @@ public class Validator { * @param isApplication * @throws Exception */ - private void checkAsGroup(CustomField fieldToValidate, MetadataField metadataField, - ArrayNode groupsArrayOriginal, List groupToCreate) throws Exception { + private void checkAsGroup(CustomField fieldToValidate, MetadataField metadataField, ArrayNode groupsArrayOriginal, + List groupToCreate) throws Exception { logger.debug("Custom field is " + fieldToValidate); logger.debug("MetadataField field is " + metadataField); @@ -437,12 +433,12 @@ public class Validator { } for(String title : groupNames) { - logger.debug("Adding group to which add this item " + CatalogueUtilMethods.fromGroupTitleToName(title)); + String groupName = CKANGroup.getCKANGroupName(title); + logger.debug("Adding group to which add this item {}", groupName); ObjectNode group = mapper.createObjectNode(); - group.put("name", CatalogueUtilMethods.fromGroupTitleToName(title)); + group.put("name", groupName); if(propagateUp) { - List parents = Validator - .getGroupHierarchyNames(CatalogueUtilMethods.fromGroupTitleToName(title)); + List parents = Validator.getGroupHierarchyNames(groupName); for(String parent : parents) { ObjectNode groupP = mapper.createObjectNode(); groupP.put("name", parent); @@ -500,8 +496,8 @@ public class Validator { case Text: if(regex != null && !value.matches(regex)) - throw new BadRequestException("Field with key '" + key + "' doesn't match the provided regular expression (" - + regex + ")!"); + throw new BadRequestException("Field with key '" + key + + "' doesn't match the provided regular expression (" + regex + ")!"); if(hasControlledVocabulary) { @@ -536,7 +532,8 @@ public class Validator { for(int i = 0; i < timeValues.length; i++) { String time = timeValues[i]; if(!isValidDate(time)) - throw new BadRequestException("Field with key '" + key + "' doesn't seem a valid time interval!"); + throw new BadRequestException( + "Field with key '" + key + "' doesn't seem a valid time interval!"); } break; @@ -546,11 +543,13 @@ public class Validator { for(int i = 0; i < timeIntervals.length; i++) { String[] timeIntervalValues = timeIntervals[i].split("/"); if(timeIntervalValues.length > 2) - throw new BadRequestException("Field with key '" + key + "' doesn't seem a valid list of times!"); + throw new BadRequestException( + "Field with key '" + key + "' doesn't seem a valid list of times!"); for(i = 0; i < timeIntervalValues.length; i++) { String time = timeIntervalValues[i]; if(!isValidDate(time)) - throw new BadRequestException("Field with key '" + key + "' doesn't seem a valid list of times!"); + throw new BadRequestException( + "Field with key '" + key + "' doesn't seem a valid list of times!"); } } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java index cbf8620..e5d9c36 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java @@ -3,8 +3,6 @@ package org.gcube.gcat.persistence.ckan; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; -import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods; - import com.fasterxml.jackson.databind.node.ObjectNode; /** @@ -38,15 +36,30 @@ public class CKANGroup extends CKAN { PURGE = GROUP_PURGE; } - public static String getGroupName(String name) { - return CatalogueUtilMethods.fromGroupTitleToName(name); - // return name.trim().toLowerCase().replaceAll(" ", "_"); + private static String fromGroupTitleToName(String groupName){ + if(groupName == null) + return null; + + String regexGroupNameTransform = "[^A-Za-z0-9-]"; + String modified = groupName.trim().replaceAll(regexGroupNameTransform, "-").replaceAll("-+", "-").toLowerCase(); + + if(modified.startsWith("-")) + modified = modified.substring(1); + if(modified.endsWith("-")) + modified = modified.substring(0, modified.length() -1); + + return modified; + + } + + public static String getCKANGroupName(String name) { + return CKANGroup.fromGroupTitleToName(name); } public String create() throws WebApplicationException { try { ObjectNode objectNode = mapper.createObjectNode(); - objectNode.put(NAME_KEY, CKANGroup.getGroupName(name)); + objectNode.put(NAME_KEY, CKANGroup.getCKANGroupName(name)); objectNode.put("title", name); objectNode.put("display_name", name); objectNode.put("description", ""); diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java index 9fd755b..5cfa761 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java @@ -38,9 +38,13 @@ public class CKANUser extends CKAN { public static final String ADD_USER_TO_GROUP = CKAN.CKAN_API_PATH + "member_create"; public static final String NAME = "name"; + public static final String DISPLAY_NAME = "display_name"; + public static final String FULL_NAME = "fullname"; + public static final String ABOUT = "about"; public static final String EMAIL = "email"; public static final String PASSWORD = "password"; + private static final String API_KEY = "apikey"; public enum Role { @@ -134,8 +138,10 @@ public class CKANUser extends CKAN { RandomString randomString = new RandomString(12); ObjectNode objectNode = mapper.createObjectNode(); objectNode.put(NAME, name); - objectNode.put(EMAIL, getPortalUser().getEMail()); objectNode.put(PASSWORD, randomString.nextString()); + checkAndSetEMail(objectNode); + checkAndSetFullName(objectNode); + checkAndSetJobTitle(objectNode); return create(getAsString(objectNode)); } @@ -144,6 +150,84 @@ public class CKANUser extends CKAN { this.delete(); } + /** + * + * @param objectNode + * @return true if the display name and the full name has been updated in objectNode + */ + private boolean checkAndSetJobTitle(ObjectNode objectNode) { + String portalJobTitle = getPortalUser().getJobTitle(); + + String ckanJobTitle = ""; + if(objectNode.has(ABOUT)) { + ckanJobTitle = objectNode.get(ABOUT).asText(); + } + if(portalJobTitle.compareTo(ckanJobTitle)!=0){ + objectNode = (ObjectNode) result; + objectNode.put(ABOUT, portalJobTitle); + return true; + } + return false; + } + + /** + * + * @param objectNode + * @return true if the display name and the full name has been updated in objectNode + */ + private boolean checkAndSetFullName(ObjectNode objectNode) { + String portalFullname = getPortalUser().getFullName(); + + String ckanFullname = ""; + if(objectNode.has(FULL_NAME)) { + ckanFullname = objectNode.get(FULL_NAME).asText(); + } + if(portalFullname.compareTo(ckanFullname)!=0){ + objectNode = (ObjectNode) result; + objectNode.put(FULL_NAME, portalFullname); + objectNode.put(DISPLAY_NAME, portalFullname); + return true; + } + return false; + } + + /** + * + * @param objectNode + * @return true if the display name and the full name has been updated + */ + private boolean checkAndSetEMail(ObjectNode objectNode) { + String portalEmail = getPortalUser().getEMail(); + + String ckanEmail = ""; + if(objectNode.has(EMAIL)) { + ckanEmail = objectNode.get(EMAIL).asText(); + } + if(portalEmail.compareTo(ckanEmail)!=0){ + objectNode = (ObjectNode) result; + objectNode.put(EMAIL, portalEmail); + return true; + } + return false; + } + + /** + * Update the user profile on CKAN if the got got informations differs from the portal information + * @return true if the profile information has been updated + */ + protected boolean updateProfileIfNeeded() { + ObjectNode objectNode = (ObjectNode) result; + boolean toBeUpdated = false; + + toBeUpdated = checkAndSetEMail(objectNode) || toBeUpdated; + toBeUpdated = checkAndSetFullName(objectNode) || toBeUpdated; + toBeUpdated = checkAndSetJobTitle(objectNode) || toBeUpdated; + + if(toBeUpdated) { + update(getAsString(objectNode)); + } + return toBeUpdated; + } private void retrieve() { setApiKey(CKANUtility.getSysAdminAPI()); @@ -152,6 +236,7 @@ public class CKANUser extends CKAN { setName(getCKANUsername()); } read(); + updateProfileIfNeeded(); } catch(WebApplicationException e) { if(e.getResponse().getStatusInfo() == Status.NOT_FOUND) { create(); @@ -217,7 +302,7 @@ public class CKANUser extends CKAN { public void addToGroup(String groupName) throws WebApplicationException { try { ObjectNode objectNode = mapper.createObjectNode(); - objectNode.put(ID_KEY, CKANGroup.getGroupName(groupName)); + objectNode.put(ID_KEY, CKANGroup.getCKANGroupName(groupName)); objectNode.put("object", name); objectNode.put("object_type", "user"); objectNode.put("capacity", "member"); diff --git a/src/main/java/org/gcube/gcat/social/PortalUser.java b/src/main/java/org/gcube/gcat/social/PortalUser.java index b1ee059..f35a521 100644 --- a/src/main/java/org/gcube/gcat/social/PortalUser.java +++ b/src/main/java/org/gcube/gcat/social/PortalUser.java @@ -25,11 +25,10 @@ public class PortalUser { protected static final String SOCIAL_SERVICE_GET_OAUTH_USER_PROFILE_PATH = "2/users/get-oauth-profile"; - // This key cotnains the fullname + // This key contains the fullname protected static final String OAUTH_USER_PROFILE_NAME_KEY = "name"; - protected static final String OAUTH_USER_PROFILE_EMAIL_KEY = "email"; - + protected static final String OAUTH_USER_PROFILE_JOB_TITLE_KEY = "job_title"; protected static final String OAUTH_USER_PROFILE_ROLES_KEY = "roles"; protected final ObjectMapper objectMapper; @@ -39,6 +38,7 @@ public class PortalUser { protected String fullName; protected String eMail; + protected String jobTitle; protected List roles; @@ -120,6 +120,12 @@ public class PortalUser { } return roles; } - + + public String getJobTitle() { + if(jobTitle == null) { + jobTitle = getOAuthUserProfile().get(OAUTH_USER_PROFILE_JOB_TITLE_KEY).asText(); + } + return jobTitle; + } } diff --git a/src/main/java/org/gcube/gcat/social/SocialServiceDiscovery.java b/src/main/java/org/gcube/gcat/social/SocialServiceDiscovery.java index 6f8840a..2ae02a2 100644 --- a/src/main/java/org/gcube/gcat/social/SocialServiceDiscovery.java +++ b/src/main/java/org/gcube/gcat/social/SocialServiceDiscovery.java @@ -100,7 +100,6 @@ public class SocialServiceDiscovery { } } - /** * @return the base path of the service */ diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java index b0ef9ac..ff5f6f7 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java @@ -15,7 +15,7 @@ public class CKANUserTest extends ContextTest { private static Logger logger = LoggerFactory.getLogger(CKANUserTest.class); - private static final String USERNAME = "pippo"; + private static final String USERNAME = "lucio_lelii"; private CKANUser getCKANUser() { CKANUser user = new CKANUser(); @@ -72,4 +72,10 @@ public class CKANUserTest extends ContextTest { ckanUser.addUserToOrganization(); } + + @Test + public void checkAndUpdateInformation() throws Exception { + CKANUser ckanUser = CKANUser.getCurrentCKANUser(); + logger.debug("{}", ckanUser.result); + } } diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..7a97cb1 --- /dev/null +++ b/src/test/resources/.gitignore @@ -0,0 +1,2 @@ +/*.gcubekey +/*.properties