adding feature #26142
This commit is contained in:
parent
c17bf6de9a
commit
51731bbd04
|
@ -1,16 +1,31 @@
|
|||
package org.gcube.gcat.profile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import javax.ws.rs.BadRequestException;
|
||||
import javax.ws.rs.InternalServerErrorException;
|
||||
import javax.ws.rs.NotAuthorizedException;
|
||||
import javax.ws.rs.NotFoundException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import org.gcube.common.resources.gcore.GenericResource;
|
||||
import org.gcube.common.resources.gcore.Resources;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil;
|
||||
import org.gcube.gcat.api.roles.Role;
|
||||
|
@ -29,9 +44,6 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
|
@ -169,7 +181,61 @@ public class ISProfile {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean createOrUpdate(String name, String xml) throws SAXException {
|
||||
public static MetadataFormat getMetadataFormat(String xml) throws JAXBException {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class);
|
||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
InputStream stream = new ByteArrayInputStream(xml.getBytes());
|
||||
MetadataFormat metadataFormat = (MetadataFormat) unmarshaller.unmarshal(stream);
|
||||
return metadataFormat;
|
||||
}
|
||||
|
||||
public static void checkRegex(String regex, String text) {
|
||||
try {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
if(text!=null) {
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
if(!matcher.find()) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
} catch (PatternSyntaxException e) {
|
||||
throw e;
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void validateMetadataFormat(MetadataFormat metadataFormat) throws BadRequestException {
|
||||
List<MetadataField> metadataFields = metadataFormat.getMetadataFields();
|
||||
for(MetadataField metadataField : metadataFields) {
|
||||
String defaultValue = metadataField.getDefaultValue();
|
||||
|
||||
MetadataValidator metadataValidator = metadataField.getValidator();
|
||||
if(metadataValidator!=null) {
|
||||
String regularExpression = metadataValidator.getRegularExpression();
|
||||
if(regularExpression!=null && regularExpression.length()>0) {
|
||||
try {
|
||||
checkRegex(regularExpression, defaultValue);
|
||||
} catch (PatternSyntaxException e) {
|
||||
throw new BadRequestException("The regular expression provided for metadata field '" + metadataField.getFieldName() + "' (i.e. '" + regularExpression + "') is not valid." );
|
||||
} catch (RuntimeException e) {
|
||||
throw new BadRequestException("The default value provided for metadata field '" + metadataField.getFieldName() + "' (i.e. '" + defaultValue + "') does not match the regular expression defined (i.e. '" + regularExpression + "')");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MetadataVocabulary metadataVocabulary = metadataField.getVocabulary();
|
||||
if(metadataVocabulary!=null && defaultValue!=null) {
|
||||
List<String> vocabularyFields = metadataVocabulary.getVocabularyFields();
|
||||
if(!vocabularyFields.contains(defaultValue)) {
|
||||
throw new BadRequestException("The default value provided for metadata field '" + metadataField.getFieldName() + "' (i.e. '" + defaultValue + "') does not match the vocabulary (i.e. '" + vocabularyFields.toString() + "')");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean createOrUpdate(String name, String xml) throws Exception {
|
||||
try {
|
||||
CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
|
||||
if(ckanUser.getRole().ordinal()<Role.EDITOR.ordinal()) {
|
||||
|
@ -177,7 +243,12 @@ public class ISProfile {
|
|||
}
|
||||
MetadataUtility metadataUtility = new MetadataUtility();
|
||||
metadataUtility.validateProfile(xml);
|
||||
if(metadataUtility.getMetadataFormat(name) == null) {
|
||||
|
||||
MetadataFormat newMetadataFormat = getMetadataFormat(xml);
|
||||
validateMetadataFormat(newMetadataFormat);
|
||||
|
||||
MetadataFormat oldMetadataFormat = metadataUtility.getMetadataFormat(name);
|
||||
if(oldMetadataFormat == null) {
|
||||
createGenericResource(name, xml);
|
||||
return true;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue