Start adding support for Metadata formats

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@129019 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-08 16:31:25 +00:00
parent bd1ac06fe9
commit 0eafcafdc1
6 changed files with 375 additions and 15 deletions

10
pom.xml
View File

@ -42,12 +42,11 @@
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
@ -88,6 +87,11 @@
<scope>provided</scope>
<!-- put at provided for deploying -->
</dependency>
<dependency>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcubedatacatalogue-metadata-discovery</artifactId>
<version>[0.1.0-SNAPSHOT,1.0.0-SNAPSHOT)</version>
</dependency>
</dependencies>
<build>

View File

@ -10,9 +10,18 @@ import java.util.List;
import java.util.Map;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
import org.gcube.datacatalogue.ckanutillibrary.models.MetaDataBean;
import org.gcube.datacatalogue.ckanutillibrary.models.MetadataFieldWrapper;
import org.gcube.datacatalogue.ckanutillibrary.models.ROLES_IN_ORGANIZATION;
import org.gcube.datacatalogue.ckanutillibrary.models.STATE;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -276,7 +285,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
}
return orgsName;
}
@Override
@ -316,7 +325,58 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
logger.debug("License is " + ckanLicense.getTitle() + " and id " + ckanLicense.getId());
}
return result;
}
@Override
public List<MetaDataBean> getMetadataList() {
List<MetaDataBean> beans = new ArrayList<MetaDataBean>();
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE"); // TODO remove
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader();
for (MetadataType mt : reader.getListOfMetadataTypes()) {
MetadataFormat metadata = reader.getMetadataFormatForMetadataType(mt);
// we need to wrap the list of metadata
List<MetadataFieldWrapper> wrapperList = new ArrayList<MetadataFieldWrapper>();
List<MetadataField> toWrap = metadata.getMetadataFields();
for(MetadataField metadataField: toWrap){
MetadataFieldWrapper wrapperObj = new MetadataFieldWrapper();
wrapperObj.setDefaulValue(metadataField.getDefaulValue());
wrapperObj.setFieldName(metadataField.getFieldName());
wrapperObj.setIsBoolean(metadataField.getIsBoolean());
wrapperObj.setMandatory(metadataField.getMandatory());
wrapperObj.setNote(metadataField.getNote());
MetadataValidator validator = metadataField.getValidator();
if(validator != null)
wrapperObj.setValidator(validator.getRegularExpression());
MetadataVocabulary vocabulary = metadataField.getVocabulary();
if(vocabulary != null)
wrapperObj.setVocabulary(vocabulary.getVocabularyFields());
// add to the list
wrapperList.add(wrapperObj);
}
MetaDataBean bean = new MetaDataBean(mt, wrapperList);
beans.add(bean);
}
return beans;
} catch (Exception e) {
logger.error("Error while retrieving metadata beans ", e);
}
return null;
}
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map;
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
import org.gcube.datacatalogue.ckanutillibrary.models.MetaDataBean;
import org.gcube.datacatalogue.ckanutillibrary.models.ROLES_IN_ORGANIZATION;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
@ -53,21 +54,26 @@ public interface CKanUtilsInterface {
/**
* Return the ckan catalogue url in this scope.
* @return
* @return the catalogue url or exception if not found
*/
public String getCatalogueUrl();
/**
* Get the list of licenses' titles.
* @return
* @return the list of licenses' titles
*/
public List<String> getLicenseTitles();
/**
* Finds the id associated to the chosen license
* @param chosenLicense
* @return
* @return the id on success, null otherwise
*/
public String findLicenseIdByLicense(String chosenLicense);
/**
* Retrieve the list of metadata
*/
public List<MetaDataBean> getMetadataList();
}

View File

@ -0,0 +1,64 @@
package org.gcube.datacatalogue.ckanutillibrary.models;
import java.io.Serializable;
import java.util.List;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType;
/**
* A MetaDataType with its children (MetaDataFormat)
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class MetaDataBean implements Serializable{
private static final long serialVersionUID = -7377022025375553568L;
private MetadataType type; // the type
private List<MetadataFieldWrapper> metadataFields; // the fields of this type
public MetaDataBean(){
super();
}
/**
* @param type
* @param metadataFields
*/
public MetaDataBean(MetadataType type, List<MetadataFieldWrapper> metadataFields) {
super();
this.type = type;
this.metadataFields = metadataFields;
}
/**
* @return the type
*/
public MetadataType getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(MetadataType type) {
this.type = type;
}
/**
* @return the metadataFields
*/
public List<MetadataFieldWrapper> getMetadataFields() {
return metadataFields;
}
/**
* @param metadataFields the metadataFields to set
*/
public void setMetadataFields(List<MetadataFieldWrapper> metadataFields) {
this.metadataFields = metadataFields;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MetaDataBean [type=" + type + ", metadataFields="
+ metadataFields + "]";
}
}

View File

@ -0,0 +1,205 @@
package org.gcube.datacatalogue.ckanutillibrary.models;
import java.io.Serializable;
import java.util.List;
/**
* The Class MetadataFieldWrapper.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class MetadataFieldWrapper implements Serializable{
private static final long serialVersionUID = -8476731365884466698L;
private String fieldName;
private Boolean mandatory = false;
private Boolean isBoolean = false;
private String defaulValue;
private String note;
private List<String> vocabulary;
private String validator;
/**
* Instantiates a new metadata field.
*/
public MetadataFieldWrapper() {
super();
}
/**
* Instantiates a new metadata field.
*
* @param fieldName the field name
* @param mandatory the mandatory
* @param isBoolean the is boolean
* @param defaulValue the defaul value
* @param note the note
* @param vocabulary the vocabulary
* @param validator the validator
*/
public MetadataFieldWrapper(
String fieldName, Boolean mandatory, Boolean isBoolean,
String defaulValue, String note, List<String> vocabulary,
String validator) {
super();
this.fieldName = fieldName;
this.mandatory = mandatory;
this.isBoolean = isBoolean;
this.defaulValue = defaulValue;
this.note = note;
this.vocabulary = vocabulary;
this.validator = validator;
}
/**
* Gets the field name.
*
* @return the fieldName
*/
public String getFieldName() {
return fieldName;
}
/**
* Gets the mandatory.
*
* @return the mandatory
*/
public Boolean getMandatory() {
return mandatory;
}
/**
* Gets the checks if is boolean.
*
* @return the isBoolean
*/
public Boolean getIsBoolean() {
return isBoolean;
}
/**
* Gets the defaul value.
*
* @return the defaulValue
*/
public String getDefaulValue() {
return defaulValue;
}
/**
* Gets the note.
*
* @return the note
*/
public String getNote() {
return note;
}
/**
* Gets the vocabulary.
*
* @return the vocabulary
*/
public List<String> getVocabulary() {
return vocabulary;
}
/**
* Gets the validator.
*
* @return the validator
*/
public String getValidator() {
return validator;
}
/**
* Sets the field name.
*
* @param fieldName the fieldName to set
*/
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
/**
* Sets the mandatory.
*
* @param mandatory the mandatory to set
*/
public void setMandatory(Boolean mandatory) {
this.mandatory = mandatory;
}
/**
* Sets the checks if is boolean.
*
* @param isBoolean the isBoolean to set
*/
public void setIsBoolean(Boolean isBoolean) {
this.isBoolean = isBoolean;
}
/**
* Sets the defaul value.
*
* @param defaulValue the defaulValue to set
*/
public void setDefaulValue(String defaulValue) {
this.defaulValue = defaulValue;
}
/**
* Sets the note.
*
* @param note the note to set
*/
public void setNote(String note) {
this.note = note;
}
/**
* Sets the vocabulary.
*
* @param vocabulary the vocabulary to set
*/
public void setVocabulary(List<String> vocabulary) {
this.vocabulary = vocabulary;
}
/**
* Sets the validator.
*
* @param validator the validator to set
*/
public void setValidator(String validator) {
this.validator = validator;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MetadataFieldWrapper [fieldName=" + fieldName + ", mandatory="
+ mandatory + ", isBoolean=" + isBoolean + ", defaulValue="
+ defaulValue + ", note=" + note + ", vocabulary=" + vocabulary
+ ", validator=" + validator + "]";
}
}

View File

@ -1,22 +1,23 @@
package org.gcube.datacatalogue.ckanutillibrary;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsFactory;
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsImpl;
import javax.net.ssl.HttpsURLConnection;
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
import org.gcube.datacatalogue.ckanutillibrary.models.MetaDataBean;
import org.gcube.datacatalogue.ckanutillibrary.models.ROLES_IN_ORGANIZATION;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
public class TestCKanLib {
private static final Logger logger = LoggerFactory.getLogger(TestCKanLib.class);
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCKanLib.class);
CKanUtilsImpl instance;
@ -78,15 +79,35 @@ public class TestCKanLib {
System.out.println("organizations for user " + username + " are " + map);
}
//@Test
public void testFactory() throws Exception{
System.out.println("Creating factory object");
CKanUtilsImpl obj = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube");
System.out.println("Object created " + obj.getCatalogueUrl());
}
//@Test
public void getMetadataTest() throws Exception{
List<MetaDataBean> beans = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube").getMetadataList();
System.out.println("List is " + beans);
}
//@Test
public void createOrganization() throws Exception{
String callUrl = "https://ckan-d-d4s.d4science.org/ckan-connector/organization/CreationTest?gcube-token=34c34146-ab38-42d5-9332-f325e8b2b930";
URL url = new URL(callUrl);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("PUT");
System.out.println(connection.getResponseCode() + " " + connection.getResponseMessage());
}
}