Basic JSON Object
This commit is contained in:
parent
d01d5b598b
commit
c21ee9a296
|
@ -0,0 +1 @@
|
|||
/org.eclipse.m2e.core.prefs
|
26
pom.xml
26
pom.xml
|
@ -12,14 +12,14 @@
|
|||
<artifactId>maven-parent</artifactId>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<version>1.1.0</version>
|
||||
|
||||
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<gitBaseUrl>https://code-repo.d4science.org/gCubeSystem</gitBaseUrl>
|
||||
<sis.version>1.0</sis.version>
|
||||
|
||||
</properties>
|
||||
<properties>
|
||||
<gitBaseUrl>https://code-repo.d4science.org/gCubeSystem</gitBaseUrl>
|
||||
<sis.version>1.0</sis.version>
|
||||
|
||||
</properties>
|
||||
|
||||
|
||||
|
||||
|
@ -51,8 +51,12 @@
|
|||
<version>1.14.8</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.json</groupId>
|
||||
<artifactId>javax.json-api</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!-- TEST -->
|
||||
<dependency>
|
||||
|
@ -62,6 +66,12 @@
|
|||
<version>4.11</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Provider JSON -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
|
@ -69,7 +79,7 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,37 @@
|
|||
package org.gcube.application.geoportal.common.faults;
|
||||
|
||||
public class JsonParseException extends Exception{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JsonParseException() {
|
||||
super();
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message) {
|
||||
super(message);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(Throwable cause) {
|
||||
super(cause);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
package org.gcube.application.geoportal.common.model;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonNumber;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonValue;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BasicJSONObject implements JsonObject {
|
||||
|
||||
@Getter
|
||||
private JsonObject theObject;
|
||||
|
||||
public String toJson() {
|
||||
return getTheObject().toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return getTheObject().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return getTheObject().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return getTheObject().containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return getTheObject().containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue get(Object key) {
|
||||
return getTheObject().get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue put(String key, JsonValue value) {
|
||||
return getTheObject().put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue remove(Object key) {
|
||||
return getTheObject().remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends JsonValue> m) {
|
||||
getTheObject().putAll(m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
getTheObject().clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return getTheObject().keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<JsonValue> values() {
|
||||
return getTheObject().values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, JsonValue>> entrySet() {
|
||||
return getTheObject().entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonArray getJsonArray(String name) {
|
||||
return getTheObject().getJsonArray(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject getJsonObject(String name) {
|
||||
return getTheObject().getJsonObject(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNumber getJsonNumber(String name) {
|
||||
return getTheObject().getJsonNumber(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonString getJsonString(String name) {
|
||||
return getTheObject().getJsonString(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name) {
|
||||
return getTheObject().getString(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue) {
|
||||
return getTheObject().getString(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String name) {
|
||||
return getTheObject().getInt(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String name, int defaultValue) {
|
||||
return getTheObject().getInt(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
return getTheObject().getBoolean(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue) {
|
||||
return getTheObject().getBoolean(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(String name) {
|
||||
return getTheObject().isNull(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType() {
|
||||
return getTheObject().getValueType();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,14 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class DefaultCompiler {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class DefaultCompiler extends BasicJSONObject{
|
||||
|
||||
public DefaultCompiler(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class Field {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Field extends BasicJSONObject{
|
||||
|
||||
public Field(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class FieldMapping {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class FieldMapping extends BasicJSONObject {
|
||||
|
||||
public FieldMapping(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class IndexDefinition {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class IndexDefinition extends BasicJSONObject {
|
||||
|
||||
public IndexDefinition(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class IsoMapper {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class IsoMapper extends BasicJSONObject{
|
||||
|
||||
public IsoMapper(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,38 @@ package org.gcube.application.geoportal.common.model.profile;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
|
||||
public class Profile {
|
||||
import org.gcube.application.geoportal.common.faults.JsonParseException;
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Profile extends BasicJSONObject{
|
||||
|
||||
|
||||
|
||||
public Profile(JsonObject theObject) throws JsonParseException {
|
||||
super(theObject);
|
||||
name=theObject.getString("name");
|
||||
id=theObject.getString("_id");
|
||||
// JsonArray jsonFields=theObject.getJsonArray("fields");
|
||||
// if(jsonFields==null)
|
||||
// fields=null;
|
||||
// else {
|
||||
// fields=new ArrayList<Field>();
|
||||
// for(int i= 0; i<jsonFields.size();i++)
|
||||
// fields.add(new Field(jsonFields.getJsonObject(i)));
|
||||
// }
|
||||
fields=Serialization.asArray(theObject.getJsonArray("fields"), Field.class);
|
||||
defaultCompilers=Serialization.asArray(theObject.getJsonArray("default_compilers"), DefaultCompiler.class);
|
||||
validators=Serialization.asArray(theObject.getJsonArray("validators"), Validator.class);
|
||||
isoMapper=new IsoMapper(theObject.getJsonObject("iso_mapper"));
|
||||
centroidsMapping=Serialization.asArray(theObject.getJsonArray("centroid_mapping"), FieldMapping.class);
|
||||
indexes=Serialization.asArray(theObject.getJsonArray("findexes"), IndexDefinition.class);
|
||||
|
||||
}
|
||||
private String name;
|
||||
private String id;
|
||||
private List<Field> fields;
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.faults.JsonParseException;
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class Serialization {
|
||||
|
||||
|
||||
public static <T extends BasicJSONObject> ArrayList<T> asArray(JsonArray array,Class<T> clazz) throws JsonParseException{
|
||||
try {
|
||||
ArrayList<T> toReturn=new ArrayList<T>();
|
||||
if(array!=null)
|
||||
for(int i=0;i<array.size();i++)
|
||||
toReturn.add(clazz.getConstructor(JsonObject.class).newInstance(array.getJsonObject(i)));
|
||||
return toReturn;
|
||||
}catch(Exception e) {
|
||||
throw new JsonParseException("Unable to create list of "+clazz,e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,14 @@
|
|||
package org.gcube.application.geoportal.common.model.profile;
|
||||
|
||||
public class Validator {
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
public class Validator extends BasicJSONObject {
|
||||
|
||||
public Validator(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package org.gcube.application.geoportal.common.model.project;
|
||||
|
||||
public interface Document {
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,8 +1,16 @@
|
|||
package org.gcube.application.geoportal.common.model.project;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
|
||||
public class Project {
|
||||
import org.gcube.application.geoportal.common.model.BasicJSONObject;
|
||||
|
||||
|
||||
public class Project extends BasicJSONObject{
|
||||
|
||||
public Project(JsonObject theObject) {
|
||||
super(theObject);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
/**
|
||||
* Project{
|
||||
_id:
|
||||
|
@ -35,7 +43,8 @@ public class Project {
|
|||
private String profile_id;
|
||||
private PublicationDetails publication;
|
||||
private Status status;
|
||||
private Document document;
|
||||
private BasicJSONObject document;
|
||||
private Centroid centroid;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package org.gcube.application.geoportal.common.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class Files {
|
||||
|
||||
public static File getFileFromResources(String fileName) {
|
||||
|
||||
ClassLoader classLoader =Files.class.getClassLoader();
|
||||
|
||||
URL resource = classLoader.getResource(fileName);
|
||||
if (resource == null) {
|
||||
throw new IllegalArgumentException("file is not found!");
|
||||
} else {
|
||||
return new File(resource.getFile());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String readFileAsString(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = java.nio.file.Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
|
||||
|
||||
public static String getName(String path) {
|
||||
|
||||
return path.substring((path.contains(File.separator)?path.lastIndexOf(File.separator)+1:0)
|
||||
,(path.contains(".")?path.lastIndexOf("."):path.length()));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.gcube.application.geoportal.common.model;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
|
||||
import org.gcube.application.geoportal.common.faults.JsonParseException;
|
||||
import org.gcube.application.geoportal.common.model.profile.Profile;
|
||||
import org.gcube.application.geoportal.common.utils.Files;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SerializationTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void readProfile() throws FileNotFoundException, JsonParseException {
|
||||
JsonObject object=Json.createReader(new FileInputStream(Files.getFileFromResources("ProfileConcessioni.json"))).readObject();
|
||||
|
||||
System.out.println("object is "+object);
|
||||
Assert.assertNotNull(object);
|
||||
Profile concessione=new Profile(object);
|
||||
System.out.println("Profile is "+concessione.toJson());
|
||||
|
||||
Assert.assertTrue(concessione.getFields().size()>0);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"name" : "Concessioni Profile",
|
||||
"_id" : "",
|
||||
"fields" :[
|
||||
{ "name" : "introduzione", "label" : "Introduzione", "type" : "STRING"},
|
||||
{ "label" : "Descrizione Contenuto", "type" : "STRING"},
|
||||
{ "label" : "Autori", "type" : "STRING", "cardinality" : "MULTIPLE"},
|
||||
{ "label" : "Contributore", "type" : "STRING"},
|
||||
{ "label" : "Titolari", "type" : "STRING", "cardinality" : "MULTIPLE"},
|
||||
|
||||
{ "name":"relazioneScavo", "label" : "Relazione di Scavo", "type" : "DOCUMENT"},
|
||||
{ "name":"posizionameno",
|
||||
"label" : "Posizionamento Scavo",
|
||||
"type" : "DOCUMENT",
|
||||
"fields":[
|
||||
{ "label" : "Descrizione Contenuto", "type" : "STRING"},
|
||||
{ "label" : "Autori", "type" : "STRING", "cardinality" : "MULTIPLE"},
|
||||
{ "label" : "Contributore", "type" : "STRING"}
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
"validators" : [
|
||||
{"name":"Validator Concessioni",
|
||||
"type":"JAVA",
|
||||
"qName":"org.gcube.application.concessioni.Validator"}
|
||||
]
|
||||
|
||||
}
|
Reference in New Issue