Migrating code

This commit is contained in:
Luca Frosini 2023-05-12 16:06:48 +02:00
parent 50ff2fa19a
commit 972b8469e1
8 changed files with 109 additions and 127 deletions

View File

@ -1091,7 +1091,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
JsonNode elementOfArray = arrayElement.next();
Object object = null;
if(property.getLinkedType()!=null) {
if(property.getType()!=null) {
object = getObjectFromJsonNode(elementOfArray);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfArray);
@ -1101,30 +1101,13 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
((MutableDocument) element).set(key, list, com.arcadedb.schema.Type.LIST);
break;
case SET:
Set<Object> set = new HashSet<Object>();
Iterator<JsonNode> setElement = value.elements();
while(setElement.hasNext()) {
JsonNode elementOfSet = setElement.next();
Object object = null;
if(property.getLinkedType()!=null) {
object = getObjectFromJsonNode(elementOfSet);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfSet);
}
set.add(object);
}
element.setProperty(key, set, OType.EMBEDDEDSET);
break;
case MAP:
Map<String, Object> map = new HashMap<>();
Iterator<String> fieldNames = value.fieldNames();
while(fieldNames.hasNext()) {
String fieldKey = fieldNames.next();
Object object = null;
if(property.getLinkedType()!=null) {
if(property.getType()!=null) {
object = getObjectFromJsonNode(value.get(fieldKey));
}else {
object = PropertyElementManagement.getPropertyDocument(value.get(fieldKey));
@ -1146,7 +1129,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
default:
Object obj = getObjectFromJsonNode(value);
if(obj != null) {
element.set(key, obj);
((MutableDocument) element).set(key, obj);
}
break;
}
@ -1172,9 +1155,9 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
try {
JsonNode value = properties.get(key);
OProperty oProperty = type.getProperty(key);
Property property = type.getProperty(key);
if(oProperty==null) {
if(property==null) {
Object object = getObjectFromJsonNode(value);
if(object != null) {
if(object instanceof Document) {
@ -1190,7 +1173,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
}
}
}else {
setProperty(oProperty, key, value);
setProperty(property, key, value);
}
} catch(Exception e) {
@ -1214,7 +1197,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
}
}
element.removeProperty(key);
((MutableDocument) element).remove(key);
}
return element;
@ -1237,7 +1220,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
if(key.compareTo(IdentifiableElement.METADATA_PROPERTY) == 0) {
// Keeping the metadata
MetadataOrient metadataOrient = MetadataUtility.getMetadataOrient((ODocument) object);
MetadataOrient metadataOrient = MetadataUtility.getMetadataOrient((Document) object);
ObjectNode metadataJson = (ObjectNode) DBUtility.toJsonNode(metadataOrient);
if(!isUserAllowedToGetPrivacyMeta()) {
@ -1248,7 +1231,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
// TODO check a solution for supertypes
TypesCache typesCache = TypesCache.getInstance();
@SuppressWarnings("unchecked")
CachedType<PropertyType<Property>> metadataType = (CachedType<PropertyType<Property>>) typesCache.getCachedType(Metadata.NAME);
CachedType<PropertyType<org.gcube.informationsystem.model.reference.properties.Property>> metadataType = (CachedType<PropertyType<org.gcube.informationsystem.model.reference.properties.Property>>) typesCache.getCachedType(Metadata.NAME);
ObjectMapper objectMapper = new ObjectMapper();
Collection<String> superClasses = metadataType.getSuperTypes();
ArrayNode arrayNode = objectMapper.valueToTree(superClasses);
@ -1277,10 +1260,10 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
}
if(object instanceof Date) {
OProperty oProperty = getDocumentType().getProperty(key);
OType oType = oProperty.getType();
Property property = getDocumentType().getProperty(key);
com.arcadedb.schema.Type type = property.getType();
DateFormat dateFormat = ODateHelper.getDateTimeFormatInstance();
switch(oType) {
switch(type) {
case DATE:
dateFormat = ODateHelper.getDateFormatInstance();
break;
@ -1425,7 +1408,7 @@ public abstract class ElementManagement<El extends Document, T extends Type> {
if(propertyDefinition.isNotnull()) {
// If the field is mandatory but null value is accepted I add the
// field as null value
element.set(fieldName, null);
((MutableDocument) element).set(fieldName, null);
} else {
throw new SchemaViolationException(getMandatoryErrorMessage(fieldName));
}

View File

@ -1,7 +1,5 @@
package org.gcube.informationsystem.resourceregistry.queries.templates;
import java.util.HashMap;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
@ -25,11 +23,11 @@ import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import com.arcadedb.database.Document;
import com.arcadedb.graph.Vertex;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.remote.RemoteDatabase;
/**
* @author Luca Frosini (ISTI - CNR)
@ -46,9 +44,9 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
this.typeName = QueryTemplate.NAME;
}
public QueryTemplateManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
public QueryTemplateManagement(RemoteDatabase database) throws ResourceRegistryException {
this();
this.oDatabaseDocument = oDatabaseDocument;
this.database = database;
getWorkingContext();
}
@ -85,7 +83,7 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
name = jsonNode.get(QueryTemplate.NAME_PROPERTY).asText();
}
} else {
name = element.getProperty(QueryTemplate.NAME_PROPERTY);
name = element.getString(QueryTemplate.NAME_PROPERTY);
}
}
return name;
@ -104,7 +102,7 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
try {
JsonNode queryTemplate = serializeSelfAsJsonNode();
ObjectMapper objectMapper = new ObjectMapper();
String templateString = element.getProperty(QueryTemplate.TEMPLATE_PROPERTY);
String templateString = element.getString(QueryTemplate.TEMPLATE_PROPERTY);
JsonNode templateJsonNode = objectMapper.readTree(templateString);
((ObjectNode) queryTemplate).replace(QueryTemplate.TEMPLATE_PROPERTY, templateJsonNode);
return queryTemplate;
@ -141,7 +139,7 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
logger.trace("Checking if {} -> {}", errorMessage, select);
OResultSet resultSet = oDatabaseDocument.command(select.toString(), new HashMap<>());
ResultSet resultSet = database.command("sql", select.toString());
if (resultSet != null) {
try {
@ -170,10 +168,10 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
}
@Override
public OVertex retrieveElement() throws NotFoundException, ResourceRegistryException {
public Vertex retrieveElement() throws NotFoundException, ResourceRegistryException {
try {
StringBuffer select = getSelectQuery();
OResultSet resultSet = oDatabaseDocument.query(select.toString(), new HashMap<>());
ResultSet resultSet = database.query("sql", select.toString());
if(resultSet == null || !resultSet.hasNext()) {
if(resultSet!=null) {
@ -182,8 +180,8 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
throw new NotFoundException("Error retrieving " + QueryTemplate.NAME + " with name " + getName());
}
OResult oResult = resultSet.next();
OVertex queryTemplate = ElementManagementUtility.getElementFromOptional(oResult.getVertex());
Result result = resultSet.next();
Vertex queryTemplate = ElementManagementUtility.getElementFromOptional(result.getVertex());
logger.trace("{} representing vertex is {}", QueryTemplate.NAME, DBUtility.getAsStringForLogging(queryTemplate));
@ -204,7 +202,7 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
}
@Override
protected OVertex reallyCreate() throws AlreadyPresentException, InvalidQueryException, ResourceRegistryException {
protected Vertex reallyCreate() throws AlreadyPresentException, InvalidQueryException, ResourceRegistryException {
try {
checkIfNameAlreadyExists();
tryTemplate();
@ -213,23 +211,23 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}", OVertex.class.getSimpleName(),
logger.trace("Error while creating {} for {} ({}) using {}", Vertex.class.getSimpleName(),
accessType.getName(), typeName, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
}
}
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
protected Vertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
try {
tryTemplate();
OVertex queryTemplate = getElement();
queryTemplate = (OVertex) updateProperties(oClass, queryTemplate, jsonNode, ignoreKeys, ignoreStartWithKeys);
Vertex queryTemplate = getElement();
queryTemplate = (Vertex) updateProperties(documentType, queryTemplate, jsonNode, ignoreKeys, ignoreStartWithKeys);
return getElement();
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}", OVertex.class.getSimpleName(),
logger.trace("Error while creating {} for {} ({}) using {}", Vertex.class.getSimpleName(),
accessType.getName(), typeName, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
}
@ -255,10 +253,10 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode();
Iterable<ODocument> iterable = oDatabaseDocument.browseClass(typeName, polymorphic);
for (ODocument vertex : iterable) {
Iterable<Document> iterable = database.browseClass(typeName, polymorphic);
for (Document vertex : iterable) {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setElement((OVertex) vertex);
queryTemplateManagement.setElement((Vertex) vertex);
try {
JsonNode jsonObject = queryTemplateManagement.serializeAsJsonNode();
arrayNode.add(jsonObject);

View File

@ -45,7 +45,7 @@ import org.gcube.informationsystem.resourceregistry.types.TypeManagement;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import com.orientechnologies.orient.core.record.ODirection;
import com.arcadedb.graph.Vertex.DIRECTION;
/**
* @author Luca Frosini (ISTI - CNR)
@ -372,7 +372,7 @@ public class Access extends BaseRest {
if(erManagement instanceof ResourceManagement) {
UUID refereceUUID = null;
ODirection directionEnum = ODirection.OUT;
DIRECTION directionEnum = DIRECTION.OUT;
Map<String,String> constraint = new HashMap<>();
@ -414,9 +414,9 @@ public class Access extends BaseRest {
}
}
try {
directionEnum = ODirection.valueOf(direction.toUpperCase());
directionEnum = DIRECTION.valueOf(direction.toUpperCase());
} catch(Exception e) {
String error = String.format("%s is not a valid. Allowed values are %s", direction, ODirection.values());
String error = String.format("%s is not a valid. Allowed values are %s", direction, DIRECTION.values());
throw new InvalidQueryException(error);
}

View File

@ -0,0 +1,57 @@
package org.gcube.informationsystem.resourceregistry.types;
import java.util.HashMap;
import java.util.Map;
import org.gcube.informationsystem.types.PropertyTypeName.BaseType;
import com.arcadedb.schema.Type;
/**
* @author Luca Frosini (ISTI - CNR)
*
* Create a mapping between OrientDB {@link Type}
* https://orientdb.gitbooks.io/orientdb-manual/content/orientdb.wiki/Types.html
* and {@link BaseType}
*/
public class DBTypeMapping {
protected static final Map<BaseType,Type> IS_BASE_TYPE_TO_DB_TYPE;
protected static final Map<Type,BaseType> DB_TYPE_TO_IS_BASE_TYPE;
static {
IS_BASE_TYPE_TO_DB_TYPE = new HashMap<>();
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.BOOLEAN, Type.BOOLEAN);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.INTEGER, Type.INTEGER);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.SHORT, Type.SHORT);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.LONG, Type.LONG);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.FLOAT, Type.FLOAT);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.DOUBLE, Type.DOUBLE);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.DATE, Type.DATETIME);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.STRING, Type.STRING);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.BINARY, Type.BINARY);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.BYTE, Type.BYTE);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.PROPERTY, Type.EMBEDDED);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.LIST, Type.LIST);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.SET, Type.LIST);
IS_BASE_TYPE_TO_DB_TYPE.put(BaseType.MAP, Type.MAP);
DB_TYPE_TO_IS_BASE_TYPE = new HashMap<>();
for(BaseType baseType : IS_BASE_TYPE_TO_DB_TYPE.keySet()) {
DB_TYPE_TO_IS_BASE_TYPE.put(IS_BASE_TYPE_TO_DB_TYPE.get(baseType), baseType);
}
}
public static Type getDBType(final BaseType baseType) {
return IS_BASE_TYPE_TO_DB_TYPE.get(baseType);
}
public static BaseType getISBaseType(final Type oType) {
return DB_TYPE_TO_IS_BASE_TYPE.get(oType);
}
}

View File

@ -1,57 +0,0 @@
package org.gcube.informationsystem.resourceregistry.types;
import java.util.HashMap;
import java.util.Map;
import org.gcube.informationsystem.types.PropertyTypeName.BaseType;
import com.orientechnologies.orient.core.metadata.schema.OType;
/**
* @author Luca Frosini (ISTI - CNR)
*
* Create a mapping between OrientDB {@link OType}
* https://orientdb.gitbooks.io/orientdb-manual/content/orientdb.wiki/Types.html
* and {@link BaseType}
*/
public class OrientDBTypeMapping {
protected static final Map<BaseType,OType> BASE_TYPE_TO_OTYPE;
protected static final Map<OType,BaseType> OTYPE_TO_BASE_TYPE;
static {
BASE_TYPE_TO_OTYPE = new HashMap<>();
BASE_TYPE_TO_OTYPE.put(BaseType.BOOLEAN, OType.BOOLEAN);
BASE_TYPE_TO_OTYPE.put(BaseType.INTEGER, OType.INTEGER);
BASE_TYPE_TO_OTYPE.put(BaseType.SHORT, OType.SHORT);
BASE_TYPE_TO_OTYPE.put(BaseType.LONG, OType.LONG);
BASE_TYPE_TO_OTYPE.put(BaseType.FLOAT, OType.FLOAT);
BASE_TYPE_TO_OTYPE.put(BaseType.DOUBLE, OType.DOUBLE);
BASE_TYPE_TO_OTYPE.put(BaseType.DATE, OType.DATETIME);
BASE_TYPE_TO_OTYPE.put(BaseType.STRING, OType.STRING);
BASE_TYPE_TO_OTYPE.put(BaseType.BINARY, OType.BINARY);
BASE_TYPE_TO_OTYPE.put(BaseType.BYTE, OType.BYTE);
BASE_TYPE_TO_OTYPE.put(BaseType.PROPERTY, OType.EMBEDDED);
BASE_TYPE_TO_OTYPE.put(BaseType.LIST, OType.EMBEDDEDLIST);
BASE_TYPE_TO_OTYPE.put(BaseType.SET, OType.EMBEDDEDSET);
BASE_TYPE_TO_OTYPE.put(BaseType.MAP, OType.EMBEDDEDMAP);
OTYPE_TO_BASE_TYPE = new HashMap<>();
for(BaseType baseType : BASE_TYPE_TO_OTYPE.keySet()) {
OTYPE_TO_BASE_TYPE.put(BASE_TYPE_TO_OTYPE.get(baseType), baseType);
}
}
public static OType getOTypeByBaseType(final BaseType baseType) {
return BASE_TYPE_TO_OTYPE.get(baseType);
}
public static BaseType getBaseTypeByOType(final OType oType) {
return OTYPE_TO_BASE_TYPE.get(oType);
}
}

View File

@ -368,7 +368,7 @@ public class TypeManagement {
}
}
com.arcadedb.schema.Type oType = OrientDBTypeMapping.getOTypeByBaseType(propertyTypeName.getBaseType());
com.arcadedb.schema.Type oType = DBTypeMapping.getDBType(propertyTypeName.getBaseType());
com.arcadedb.schema.Property op = documentType.createProperty(propertyDefinition.getName(), oType);
op.setDescription(propertyDefinition.getDescription());
@ -404,8 +404,8 @@ public class TypeManagement {
op.setLinkedClass(linkedClass);
} else {
OType linkedOType = OrientDBTypeMapping
.getOTypeByBaseType(propertyTypeName.getGenericBaseType());
OType linkedOType = DBTypeMapping
.getDBType(propertyTypeName.getGenericBaseType());
op.setLinkedType(OType.getById(Integer.valueOf(linkedOType.ordinal()).byteValue()));
}
@ -542,7 +542,7 @@ public class TypeManagement {
}
PropertyTypeName newPropertyTypeName = ((PropertyDefinitionImpl) newPropertyDefinition).getPropertyTypeName();
com.arcadedb.schema.Type dbType = OrientDBTypeMapping.getOTypeByBaseType(newPropertyTypeName.getBaseType());
com.arcadedb.schema.Type dbType = DBTypeMapping.getDBType(newPropertyTypeName.getBaseType());
/*
* Excluding EMBEDDEDLIST and EMBEDDEDSET
@ -603,7 +603,7 @@ public class TypeManagement {
dbProperty.setLinkedClass(linkedClass);
} else {
com.arcadedb.schema.Type linkedOType = OrientDBTypeMapping.getOTypeByBaseType(newPropertyTypeName.getGenericBaseType());
com.arcadedb.schema.Type linkedOType = DBTypeMapping.getDBType(newPropertyTypeName.getGenericBaseType());
dbProperty.setLinkedType(Type.getById(Integer.valueOf(linkedOType.ordinal()).byteValue()));
}
}

View File

@ -5,7 +5,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.model.reference.properties.Encrypted;
@ -16,10 +15,12 @@ import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.types.reference.properties.PropertyType;
import org.gcube.informationsystem.utils.TypeUtility;
import com.arcadedb.database.MutableDocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class EncryptedOrient extends Document implements Encrypted {
public class EncryptedOrient extends MutableDocument implements Encrypted {
public static final String NAME = "Encrypted";
public static final String VALUE = "value";
@ -61,11 +62,11 @@ public class EncryptedOrient extends Document implements Encrypted {
}
public String getEncryptedValue() {
return this.field(EncryptedOrient.VALUE);
return this.getString(EncryptedOrient.VALUE);
}
public void setEncryptedValue(String encryptedValue) {
this.field(EncryptedOrient.VALUE, encryptedValue);
this.set(EncryptedOrient.VALUE, encryptedValue);
}
public String getDecryptedValue() {

View File

@ -12,12 +12,12 @@ import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.types.reference.properties.PropertyType;
import org.gcube.informationsystem.utils.TypeUtility;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.arcadedb.database.MutableDocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class PropagationConstraintOrient extends ODocument implements PropagationConstraint {
public class PropagationConstraintOrient extends MutableDocument implements PropagationConstraint {
public PropagationConstraintOrient() {
super(PropagationConstraint.NAME);