96 lines
3.1 KiB
Java
96 lines
3.1 KiB
Java
package org.gcube.informationsystem.resourceregistry;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.gcube.informationsystem.impl.utils.discovery.SchemaAction;
|
|
import org.gcube.informationsystem.model.ISManageable;
|
|
import org.gcube.informationsystem.model.discovery.ISMDiscovery;
|
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
|
import org.gcube.informationsystem.model.embedded.ValueSchema;
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
|
import org.gcube.informationsystem.model.relation.Relation;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.SchemaActionImpl;
|
|
import org.jgrapht.graph.DefaultEdge;
|
|
import org.jgrapht.traverse.BreadthFirstIterator;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class DataBaseCreator {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
|
|
|
|
@SuppressWarnings({ "unchecked" })
|
|
@Test
|
|
public <ISM extends ISManageable> void createDatabase() throws Exception{
|
|
String db = DatabaseEnvironment.DB_URI;
|
|
logger.debug("Created DB {}", db);
|
|
|
|
List<Class<ISM>> isManageables= new ArrayList<>();
|
|
isManageables.add((Class<ISM>) Embedded.class);
|
|
isManageables.add((Class<ISM>) Entity.class);
|
|
isManageables.add((Class<ISM>) Relation.class);
|
|
|
|
SchemaAction schemaAction = new SchemaActionImpl();
|
|
|
|
for(Class<ISM> ism : isManageables){
|
|
ISMDiscovery<ISM> ismDiscovery = new ISMDiscovery<>(ism);
|
|
ismDiscovery.discover();
|
|
BreadthFirstIterator<Class<ISM>, DefaultEdge> breadthFirstIterator = new BreadthFirstIterator<>(ismDiscovery.getGraph());
|
|
|
|
List<Class<ISM>> toRetry = new ArrayList<>();
|
|
while(breadthFirstIterator.hasNext()){
|
|
Class<ISM> clz = breadthFirstIterator.next();
|
|
manageClass(schemaAction, ism, clz, toRetry);
|
|
|
|
List<Class<ISM>> nested = new ArrayList<>();
|
|
for(Class<ISM> c : toRetry){
|
|
manageClass(schemaAction, ism, c, nested);
|
|
}
|
|
toRetry = nested;
|
|
|
|
if(clz==Embedded.class){
|
|
schemaAction.manageEmbeddedClass(ValueSchema.class);
|
|
}
|
|
}
|
|
|
|
for(Class<ISM> c : toRetry){
|
|
manageClass(schemaAction, ism, c, null);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
protected <ISM extends ISManageable> void manageClass(SchemaAction schemaAction, Class<ISM> ism, Class<ISM> clz, List<Class<ISM>> toRetry) throws Exception{
|
|
|
|
try {
|
|
|
|
if(ism==Embedded.class){
|
|
schemaAction.manageEmbeddedClass((Class<Embedded>) clz);
|
|
}
|
|
|
|
if(ism==Entity.class){
|
|
schemaAction.manageEntityClass((Class<Entity>) clz);
|
|
}
|
|
|
|
if(ism==Relation.class){
|
|
schemaAction.manageRelationClass((Class<Relation>) clz);
|
|
}
|
|
} catch (SchemaNotFoundException e) {
|
|
if(toRetry!=null){
|
|
toRetry.add(clz);
|
|
}
|
|
} catch (SchemaAlreadyPresentException e) {
|
|
logger.error("{}", e.getMessage());
|
|
}catch (Exception e) {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
}
|