|
package org.structr.importer; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.File; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileReader; |
|
import java.io.IOException; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import java.util.logging.Level; |
|
import java.util.logging.Logger; |
|
import org.structr.common.error.FrameworkException; |
|
import org.structr.core.app.App; |
|
import org.structr.core.app.StructrApp; |
|
import org.structr.core.graph.MaintenanceCommand; |
|
import org.structr.core.graph.NodeInterface; |
|
import org.structr.core.graph.NodeServiceCommand; |
|
import org.structr.core.graph.Tx; |
|
import org.structr.core.property.DoubleProperty; |
|
import org.structr.core.property.IntProperty; |
|
import org.structr.core.property.PropertyMap; |
|
import org.structr.core.property.StringProperty; |
|
import org.structr.rest.resource.MaintenanceParameterResource; |
|
import org.structr.schema.SchemaHelper; |
|
|
|
/** |
|
* |
|
* @author Alexander Thiele |
|
*/ |
|
|
|
public class SchemaDataImport extends NodeServiceCommand implements MaintenanceCommand { |
|
|
|
private static final Logger logger = Logger.getLogger(SchemaDataImport.class.getName()); |
|
|
|
static { |
|
|
|
MaintenanceParameterResource.registerMaintenanceCommand("schemaDataImport", SchemaDataImport.class); |
|
} |
|
|
|
@Override |
|
public void execute(Map<String, Object> attributes) throws FrameworkException { |
|
|
|
//CSVformat className,id,val:name:type,...,... |
|
//CSVformat RELATIONSHIP,idFrom,idTo,relName |
|
|
|
final int logAtLine = 1000; |
|
|
|
Map<String, NodeInterface> relMapping = new HashMap<>(); |
|
|
|
final App app = StructrApp.getInstance(securityContext); |
|
|
|
String filePath = (String)attributes.get("file"); |
|
String mainSeperator = (String)attributes.get("seperator"); |
|
|
|
if(mainSeperator == null) |
|
mainSeperator = ","; |
|
|
|
if(filePath != null && filePath.length() > 0){ |
|
File f = new File(filePath); |
|
try { |
|
BufferedReader br = new BufferedReader(new FileReader(f)); |
|
|
|
String line; |
|
|
|
try (final Tx tx = app.tx()) { |
|
|
|
int counter = 0; |
|
while ((line = br.readLine()) != null) { |
|
|
|
counter++; |
|
if(counter%logAtLine == 0){ |
|
logger.log(Level.INFO, "Current import line: " + counter); |
|
} |
|
|
|
String[] splitted = line.split(mainSeperator); |
|
if(splitted != null && splitted.length > 2){ |
|
String classType = splitted[0]; |
|
|
|
if(classType.equals("Relationship")){ |
|
|
|
NodeInterface from = relMapping.get(splitted[1]); |
|
NodeInterface to = relMapping.get(splitted[2]); |
|
|
|
if(from != null && to != null){ |
|
String relName = from.getType() + splitted[3] + to.getType(); |
|
Class relType = SchemaHelper.getEntityClassForRawType(relName); |
|
|
|
if(relType != null){ |
|
app.create(from, to, relType); |
|
}else{ |
|
logger.log(Level.INFO, "No relationship class found: " + relName); |
|
} |
|
}else{ |
|
logger.log(Level.INFO, "From (" + splitted[1] + ") or To (" + splitted[2] + ") not found"); |
|
} |
|
|
|
}else{ |
|
Class entityClass = SchemaHelper.getEntityClassForRawType(classType); |
|
if(entityClass != null){ |
|
|
|
PropertyMap propertyMap = new PropertyMap(); |
|
|
|
String id = splitted[1]; |
|
for(int i=2; i<splitted.length;i++){ |
|
String[] valSplit = splitted[i].split(":"); |
|
if(valSplit != null && valSplit.length > 2){ |
|
|
|
String val = valSplit[0]; |
|
String name = valSplit[1]; |
|
String type = valSplit[2]; |
|
|
|
if(type.equals("String")){ |
|
StringProperty sProperty = new StringProperty(name); |
|
propertyMap.put(sProperty, val); |
|
|
|
}else if(type.equals("Integer")){ |
|
IntProperty iProperty = new IntProperty(name); |
|
propertyMap.put(iProperty, Integer.parseInt(val)); |
|
|
|
}else if(type.equals("Double")){ |
|
DoubleProperty dProperty = new DoubleProperty(name); |
|
propertyMap.put(dProperty, Double.parseDouble(val)); |
|
}else{ |
|
logger.log(Level.INFO, "No such type yet implemented: " + type); |
|
} |
|
} |
|
} |
|
|
|
relMapping.put(id, app.create(entityClass, propertyMap)); |
|
}else{ |
|
logger.log(Level.INFO, "Entityclass not found: " + classType); |
|
} |
|
} |
|
} |
|
} |
|
relMapping = null; |
|
|
|
logger.log(Level.INFO, "Transaction will now commit"); |
|
tx.success(); |
|
|
|
} catch (FrameworkException e) { |
|
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, e); |
|
} |
|
|
|
br.close(); |
|
|
|
} catch (FileNotFoundException ex) { |
|
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, ex); |
|
} catch (IOException ex) { |
|
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, ex); |
|
} |
|
} |
|
} |
|
|
|
@Override |
|
public boolean requiresEnclosingTransaction() { |
|
return true; |
|
} |
|
|
|
} |