Fixing the feature

This commit is contained in:
Luca Frosini 2019-09-12 16:05:45 +02:00
parent 4a7c0f2a70
commit 0ad1df026e
10 changed files with 157 additions and 58 deletions

View File

@ -27,7 +27,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -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

14
pom.xml
View File

@ -66,20 +66,6 @@
<version>1.20</version>
</dependency>
<!-- ehCAChe -->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.5.2</version>
<scope>runtime</scope>
</dependency>
<!-- END ehCAChe -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -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<String> groupToCreate) throws Exception {
private void checkAsGroup(CustomField fieldToValidate, MetadataField metadataField, ArrayNode groupsArrayOriginal,
List<String> 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<String> parents = Validator
.getGroupHierarchyNames(CatalogueUtilMethods.fromGroupTitleToName(title));
List<String> 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!");
}
}

View File

@ -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", "");

View File

@ -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");

View File

@ -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<String> 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;
}
}

View File

@ -100,7 +100,6 @@ public class SocialServiceDiscovery {
}
}
/**
* @return the base path of the service
*/

View File

@ -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);
}
}

2
src/test/resources/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/*.gcubekey
/*.properties