2021-02-08 16:26:58 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.types;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
|
|
|
import org.gcube.informationsystem.model.reference.properties.Property;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
|
|
|
import com.orientechnologies.orient.core.metadata.OMetadata;
|
|
|
|
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
|
|
|
import com.orientechnologies.orient.core.metadata.schema.OSchema;
|
|
|
|
|
|
|
|
public class TypesCache {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(TypesCache.class);
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
private static TypesCache typesCache;
|
2021-02-08 16:26:58 +01:00
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
public synchronized static TypesCache getInstance() {
|
|
|
|
if(typesCache == null) {
|
|
|
|
typesCache = new TypesCache();
|
|
|
|
}
|
|
|
|
return typesCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final Map<String, OClass> oClasses;
|
|
|
|
protected final Map<String, AccessType> accessTypes;
|
|
|
|
protected final Map<String, Type> types;
|
|
|
|
protected final Map<String, List<String>> superTypes;
|
|
|
|
protected final Map<String, List<String>> specilisationTypes;
|
2021-02-08 16:26:58 +01:00
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
|
|
|
|
private TypesCache() {
|
2021-02-08 16:26:58 +01:00
|
|
|
oClasses = new HashMap<>();
|
2021-02-10 15:45:48 +01:00
|
|
|
accessTypes = new HashMap<>();
|
2021-02-08 16:26:58 +01:00
|
|
|
types = new HashMap<>();
|
|
|
|
superTypes = new HashMap<>();
|
|
|
|
specilisationTypes = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
public synchronized AccessType getBaseAccessType(String type) throws ResourceRegistryException {
|
|
|
|
AccessType accessType = accessTypes.get(type);
|
|
|
|
if(accessType==null) {
|
2021-02-08 16:26:58 +01:00
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
OClass oClass = getTypeOClass(type);
|
|
|
|
|
|
|
|
if(oClass.isSubClassOf(Resource.NAME)) {
|
|
|
|
accessType = AccessType.RESOURCE;
|
|
|
|
} else if(oClass.isSubClassOf(Facet.NAME)) {
|
|
|
|
accessType = AccessType.FACET;
|
|
|
|
} else if(oClass.isSubClassOf(ConsistsOf.NAME)) {
|
|
|
|
accessType = AccessType.CONSISTS_OF;
|
|
|
|
} else if(oClass.isSubClassOf(IsRelatedTo.NAME)) {
|
|
|
|
accessType = AccessType.IS_RELATED_TO;
|
|
|
|
} else if(oClass.isSubClassOf(Property.NAME)) {
|
|
|
|
accessType = AccessType.PROPERTY;
|
|
|
|
}else {
|
|
|
|
throw new ResourceRegistryException(type + "is not a base type");
|
|
|
|
}
|
|
|
|
|
|
|
|
accessTypes.put(type, accessType);
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
2021-02-10 15:45:48 +01:00
|
|
|
return accessType;
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
/*
|
|
|
|
public void checkAccessType(OClass oClass, String type, AccessType accessType) throws SchemaException {
|
2021-02-08 16:26:58 +01:00
|
|
|
if(accessType != null && type.compareTo(accessType.getName()) != 0) {
|
|
|
|
if(!oClass.isSubClassOf(accessType.getName())) {
|
|
|
|
throw new SchemaException(type + " is not a " + accessType.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 15:45:48 +01:00
|
|
|
*/
|
2021-02-08 16:26:58 +01:00
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
private OClass getTypeOClass(OSchema oSchema, String type)
|
2021-02-08 16:26:58 +01:00
|
|
|
throws SchemaException, SchemaNotFoundException {
|
|
|
|
try {
|
2021-02-10 15:45:48 +01:00
|
|
|
OClass oClass= oClasses.get(type);
|
|
|
|
if(oClass==null) {
|
|
|
|
oClass = oSchema.getClass(type);
|
|
|
|
if(oClass == null) {
|
|
|
|
throw new SchemaNotFoundException(type + " was not registered");
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
2021-02-10 15:45:48 +01:00
|
|
|
oClasses.put(type, oClass);
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
|
|
|
return oClass;
|
|
|
|
} catch(SchemaNotFoundException snfe) {
|
|
|
|
throw snfe;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new SchemaException(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
public synchronized OClass getTypeOClass(ODatabaseDocument oDatabaseDocument, String type)
|
2021-02-08 16:26:58 +01:00
|
|
|
throws SchemaException, SchemaNotFoundException {
|
|
|
|
|
|
|
|
synchronized (oClasses) {
|
|
|
|
OClass oClass = oClasses.get(type);
|
|
|
|
if(oClass!=null) {
|
|
|
|
return oClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OMetadata oMetadata = oDatabaseDocument.getMetadata();
|
|
|
|
OSchema oSchema = oMetadata.getSchema();
|
2021-02-10 15:45:48 +01:00
|
|
|
return getTypeOClass(oSchema, type);
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
public synchronized OClass getTypeOClass(String type)
|
2021-02-08 16:26:58 +01:00
|
|
|
throws SchemaException, ResourceRegistryException {
|
|
|
|
|
2021-02-10 15:45:48 +01:00
|
|
|
OClass oClass = oClasses.get(type);
|
|
|
|
if(oClass!=null) {
|
|
|
|
return oClass;
|
2021-02-08 16:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
|
|
|
ODatabaseDocument oDatabaseDocument = null;
|
|
|
|
try {
|
|
|
|
logger.debug("GettingType {} schema", type);
|
|
|
|
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
|
|
|
|
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
|
2021-02-10 15:45:48 +01:00
|
|
|
return getTypeOClass(oDatabaseDocument, type);
|
2021-02-08 16:26:58 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
|
|
|
if(oDatabaseDocument != null) {
|
|
|
|
oDatabaseDocument.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(current!=null) {
|
|
|
|
current.activateOnCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|