resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/dbinitialization/SchemaActionImpl.java

69 lines
2.7 KiB
Java

package org.gcube.informationsystem.resourceregistry.dbinitialization;
import org.gcube.informationsystem.base.reference.entities.EntityElement;
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.base.reference.relations.RelationElement;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.types.TypeManagement;
import org.gcube.informationsystem.types.TypeMapper;
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);
@Override
public <R extends RelationElement<? extends EntityElement,? extends EntityElement>> void manageRelationClass(Class<R> r)
throws Exception {
try {
TypeManagement typeManagement = new TypeManagement();
typeManagement.setTypeAndTypeName(r);
typeManagement.create();
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeMapper.getType(r));
} catch(Exception ex) {
logger.error("Error creating schema for {} {}: {}", RelationElement.NAME, r.getSimpleName(), ex.getMessage());
throw ex;
}
}
@Override
public <E extends EntityElement> void manageEntityClass(Class<E> e) throws Exception {
try {
TypeManagement typeManagement = new TypeManagement();
typeManagement.setTypeAndTypeName(e);
typeManagement.create();
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeMapper.getType(e));
} catch(Exception ex) {
logger.error("Error creating schema for {} {}: {}", EntityElement.NAME, e.getSimpleName(), ex.getMessage());
throw ex;
}
}
@Override
public <P extends PropertyElement> void managePropertyClass(Class<P> p) throws Exception {
try {
TypeManagement typeManagement = new TypeManagement();
typeManagement.setTypeAndTypeName(p);
if(p.equals(Property.class) || p.equals(Header.class) ) {
((TypeManagement) typeManagement).setSkipTypeDefinitionCreation(true);
}
typeManagement.create();
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeMapper.getType(p));
} catch(Exception ex) {
logger.error("Error creating schema for {} {}: {}", PropertyElement.NAME, p.getSimpleName(), ex.getMessage());
throw ex;
}
}
}