96 lines
3.9 KiB
Java
96 lines
3.9 KiB
Java
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
|
|
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
|
|
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
|
|
import org.gcube.informationsystem.model.reference.entities.Entity;
|
|
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.model.reference.relations.Relation;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
|
|
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagement;
|
|
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl;
|
|
import org.gcube.informationsystem.types.TypeBinder;
|
|
import org.gcube.informationsystem.utils.discovery.SchemaAction;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class SchemaActionImpl implements SchemaAction {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(SchemaActionImpl.class);
|
|
|
|
protected SchemaManagement schemaManagement;
|
|
|
|
public SchemaActionImpl() {
|
|
this.schemaManagement = new SchemaManagementImpl();
|
|
}
|
|
|
|
@Override
|
|
public <R extends BaseRelation<? extends BaseEntity,? extends BaseEntity>> void manageRelationClass(Class<R> r)
|
|
throws Exception {
|
|
try {
|
|
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(r));
|
|
String json = TypeBinder.serializeType(r);
|
|
logger.trace(json);
|
|
if(ConsistsOf.class.isAssignableFrom(r)) {
|
|
schemaManagement.create(json, AccessType.CONSISTS_OF);
|
|
} else if(IsRelatedTo.class.isAssignableFrom(r)) {
|
|
schemaManagement.create(json, AccessType.IS_RELATED_TO);
|
|
} else {
|
|
schemaManagement.create(json, AccessType.BASE_RELATION);
|
|
}
|
|
} catch(SchemaAlreadyPresentException sape) {
|
|
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(r));
|
|
} catch(Exception ex) {
|
|
logger.error("Error creating schema for {} type {} : {}", Relation.NAME, r.getSimpleName(),
|
|
ex.getMessage());
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public <E extends BaseEntity> void manageEntityClass(Class<E> e) throws Exception {
|
|
try {
|
|
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(e));
|
|
String json = TypeBinder.serializeType(e);
|
|
logger.trace(json);
|
|
if(Facet.class.isAssignableFrom(e)) {
|
|
schemaManagement.create(json, AccessType.FACET);
|
|
} else if(Resource.class.isAssignableFrom(e)) {
|
|
schemaManagement.create(json, AccessType.RESOURCE);
|
|
} else {
|
|
schemaManagement.create(json, AccessType.BASE_ENTITY);
|
|
}
|
|
} catch(SchemaAlreadyPresentException sape) {
|
|
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e));
|
|
} catch(Exception ex) {
|
|
logger.error("Error creating schema for {} type {} : {}", Entity.NAME, e.getSimpleName(), ex.getMessage());
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public <P extends BaseProperty> void managePropertyClass(Class<P> e) throws Exception {
|
|
try {
|
|
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(e));
|
|
String json = TypeBinder.serializeType(e);
|
|
logger.trace(json);
|
|
schemaManagement.create(json, AccessType.BASE_PROPERTY);
|
|
} catch(SchemaAlreadyPresentException sape) {
|
|
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e));
|
|
} catch(Exception ex) {
|
|
logger.error("Error creating schema for {} type {} : {}", Property.NAME, e.getSimpleName(),
|
|
ex.getMessage());
|
|
throw ex;
|
|
}
|
|
|
|
}
|
|
}
|