entityClass = req.getDatabase().getClassForName(simpleEntityName);
String action = req.getString(INPUT_ACTION);
// create a database
Database db = req.getDatabase();
CsvReader csvReader;
if (req.get(INPUT_DATA) != null)
{
csvReader = new CsvReader(new StringReader(req.getString(INPUT_DATA)));
}
else if (req.get(INPUT_FILE) != null)
{
csvReader = new CsvReader(req.getFile(INPUT_FILE));
}
else
csvReader = null;
try
{
int nRowsChanged = 0;
if (action.equals("ADD"))
{
File temp = File.createTempFile("molgenis", "tab");
CsvWriter csvWriter = new CsvWriter(temp);
try
{
if (req.get(INPUT_SILENT) != null && req.getBoolean(INPUT_SILENT) == true)
{
csvWriter.close();
csvWriter = null;
}
if (req.get(INPUT_DATA) != null)
{
logger.info("processing textarea upload...");
nRowsChanged = db.add(entityClass, csvReader, csvWriter);
}
else if (req.get(INPUT_FILE) != null)
{
logger.info("processing file upload...");
nRowsChanged = db.add(entityClass, csvReader, csvWriter);
}
else
{
logger.error("no input data or input file provided.");
out.print("ERROR: no input data or input file provided.");
}
out.print("Uploaded " + formatter.format(nRowsChanged) + " rows of "
+ entityClass.getCanonicalName() + "\n");
}
finally
{
if (csvWriter != null) csvWriter.close();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(temp),
Charset.forName("UTF-8")));
try
{
String line = null;
while ((line = reader.readLine()) != null)
{
out.println(line);
}
}
finally
{
IOUtils.closeQuietly(reader);
}
boolean ok = temp.delete();
if (!ok) logger.warn("failed to delete file: " + temp);
}
else if (action.equals("UPDATE"))
{
if (req.get(INPUT_DATA) != null)
{
nRowsChanged = db.update(entityClass, csvReader);
out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
+ entityClass.getCanonicalName() + "\n");
}
else if (req.get(INPUT_FILE) != null)
{
nRowsChanged = db.update(entityClass, csvReader);
out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
+ entityClass.getCanonicalName() + "\n");
}
}
else if (action.equals("REMOVE"))
{
if (req.get(INPUT_DATA) != null)
{
nRowsChanged = db.remove(entityClass, csvReader);
out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
+ entityClass.getCanonicalName() + "\n");
}
else if (req.get(INPUT_FILE) != null)
{
nRowsChanged = db.remove(entityClass, csvReader);
out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
+ entityClass.getCanonicalName() + "\n");
}
}
else