123 lines
3.7 KiB
Java
123 lines
3.7 KiB
Java
package org.gcube.informationsystem.resourceregistry.types;
|
|
|
|
import java.util.Set;
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
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;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class CachedType {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(CachedType.class);
|
|
|
|
protected final String typeName;
|
|
|
|
protected OClass oClass;
|
|
|
|
protected AccessType accessType;
|
|
|
|
protected Type type;
|
|
protected Set<String> superTypes;
|
|
protected Set<String> specilisationTypes;
|
|
|
|
|
|
public CachedType(String typeName) {
|
|
this.typeName = typeName;
|
|
}
|
|
|
|
private OClass retrieveOClass() throws SchemaException, SchemaNotFoundException, ResourceRegistryException {
|
|
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
|
ODatabaseDocument oDatabaseDocument = null;
|
|
try {
|
|
logger.debug("GettingType {} schema", type);
|
|
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
|
|
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
|
|
OMetadata oMetadata = oDatabaseDocument.getMetadata();
|
|
OSchema oSchema = oMetadata.getSchema();
|
|
try {
|
|
OClass oClass = oSchema.getClass(typeName);
|
|
if(oClass == null) {
|
|
throw new SchemaNotFoundException(typeName + " was not registered");
|
|
}
|
|
return oClass;
|
|
} catch(SchemaNotFoundException snfe) {
|
|
throw snfe;
|
|
} catch(Exception e) {
|
|
throw new SchemaException(e.getMessage());
|
|
}
|
|
} catch(ResourceRegistryException e) {
|
|
throw e;
|
|
} catch(Exception e) {
|
|
throw new ResourceRegistryException(e);
|
|
} finally {
|
|
if(oDatabaseDocument != null) {
|
|
oDatabaseDocument.close();
|
|
}
|
|
|
|
if(current!=null) {
|
|
current.activateOnCurrentThread();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public synchronized OClass getOClass() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
|
|
if(oClass==null) {
|
|
oClass = retrieveOClass();
|
|
}
|
|
return oClass;
|
|
}
|
|
|
|
private AccessType getAccessTypeFromOClass(OClass oClass) throws ResourceRegistryException {
|
|
AccessType[] accessTypes = AccessType.values();
|
|
for(int i=accessTypes.length-1; i>=0; i--) {
|
|
AccessType accessType = accessTypes[i];
|
|
if(oClass.isSubClassOf(accessType.getName())) {
|
|
return accessType;
|
|
}
|
|
}
|
|
throw new ResourceRegistryException(typeName + " is not a base type");
|
|
}
|
|
|
|
public synchronized AccessType getAccessType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
|
|
if(accessType==null) {
|
|
if(type!=null) {
|
|
accessType = type.getAccessType();
|
|
}else {
|
|
accessType = getAccessTypeFromOClass(getOClass());
|
|
}
|
|
}
|
|
return accessType;
|
|
}
|
|
|
|
public Type getType() {
|
|
return type;
|
|
}
|
|
|
|
public synchronized Set<String> getSuperTypes() {
|
|
return superTypes;
|
|
}
|
|
|
|
public synchronized Set<String> getSpecilisationTypes() {
|
|
return specilisationTypes;
|
|
}
|
|
|
|
|
|
}
|