FileHarvestJob.java: created

TestFileController.java: saving current state
This commit is contained in:
mbarbier 2011-04-27 14:16:00 +00:00
parent 40fa231536
commit a175951431
2 changed files with 188 additions and 95 deletions

View file

@ -0,0 +1,22 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.harvester;
import java.io.File;
/**
* Handles specifics of a file harvest.
* @author mbarbieri
*
*/
interface FileHarvestJob {
/**
* Checks to make sure the uploaded file can be handled by this job (for instance, are we looking for a CSV file with specific columns?)
* @param file the uploaded file to check
* @return null if success, message to be returned to the user if failure
*/
String validateUpload(File file);
}

View file

@ -51,35 +51,42 @@ public class TestFileController extends FreemarkerHttpServlet {
return "VIVO Harvester Test";
}
private String getHarvesterPath()
{
//todo: complete
return "";
}
private String getUploadPathBase(HttpServletRequest request) throws Exception
{
String vitroHomeDirectoryName = ConfigurationProperties.getBean(request.getSession().getServletContext()).getProperty(FileStorageSetup.PROPERTY_VITRO_HOME_DIR);
if (vitroHomeDirectoryName == null) {
throw new Exception("Vitro home directory name could not be found.");
}
String pathBase = vitroHomeDirectoryName + "/" + FileStorageSetup.FILE_STORAGE_SUBDIRECTORY + "/harvester/";
return pathBase;
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
int maxFileSize = 1024 * 1024;
JSONObject json = new JSONObject();
try {
String path = getUploadPathBase(request) + getSessionId(request) + "/";
File directory = new File(path);
int maxFileSize = 1024 * 1024;
FileUploadServletRequest req = FileUploadServletRequest.parseRequest(request, maxFileSize);
if(req.hasFileUploadException()) {
Exception e = req.getFileUploadException();
log.error(e, e);
return;
new ExceptionVisibleToUser(e);
}
Map<String, List<FileItem>> fileStreams = req.getFiles();
JSONObject json = new JSONObject();
String vitroHomeDirectoryName = ConfigurationProperties.getBean(request.getSession().getServletContext()).getProperty(FileStorageSetup.PROPERTY_VITRO_HOME_DIR);
if (vitroHomeDirectoryName == null) {
log.error("Vitro home directory name could not be found.");
return;
}
String pathBase = vitroHomeDirectoryName + "/" + FileStorageSetup.FILE_STORAGE_SUBDIRECTORY + "/harvester/";
String path = pathBase + getSessionId(request) + "/";
File directory = new File(path);
String firstUpload = req.getParameter("firstUpload"); //clear directory on first upload
if(firstUpload.toLowerCase().equals("true")) {
if(directory.exists()) {
@ -93,6 +100,9 @@ public class TestFileController extends FreemarkerHttpServlet {
if(!directory.exists())
directory.mkdirs();
File csvTemplateFile = getCsvTemplateFile();
Map<String, List<FileItem>> fileStreams = req.getFiles();
if(fileStreams.get("csvFile") != null && fileStreams.get("csvFile").size() > 0) {
FileItem csvStream = fileStreams.get("csvFile").get(0);
String name = csvStream.getName();
@ -100,14 +110,11 @@ public class TestFileController extends FreemarkerHttpServlet {
File file = new File(path + name);
try {
csvStream.write(file);
} catch (Exception e) {
log.error(e, e);
return;
} finally {
csvStream.delete();
}
String errorMessage = validateCsvFile(file);
String errorMessage = validateCsvFile(csvTemplateFile, file);
boolean success;
if(errorMessage != null) {
success = false;
@ -139,10 +146,32 @@ public class TestFileController extends FreemarkerHttpServlet {
}
}
} catch(ExceptionVisibleToUser e) {
log.error(e, e);
try {
json.put("success", false);
json.put("filename", "(none)");
json.put("errorMessage", e.getMessage());
} catch(JSONException f) {
log.error(f, f);
return;
}
} catch(Exception e) {
log.error(e, e);
return;
}
response.getWriter().write(json.toString());
}
private File getCsvTemplateFile()
{
//todo: complete
String templateBasePath = getHarvesterPath() + "files/";
String templateFileName = "";
return new File(templateBasePath + templateFileName);
}
private String handleNameCollision(String path, String filename, File directory) {
String base = filename;
String extension = "";
@ -166,10 +195,16 @@ public class TestFileController extends FreemarkerHttpServlet {
}
private String validateCsvFile(File file) {
@SuppressWarnings("rawtypes")
private String validateCsvFile(File templateFile, File file) {
try {
SimpleReader reader = new SimpleReader();
List templateCsv = reader.parse(templateFile);
String[] templateFirstLine = (String[])templateCsv.get(0);
List csv = reader.parse(file);
int length = csv.size();
if(length == 0)
@ -178,12 +213,12 @@ public class TestFileController extends FreemarkerHttpServlet {
for(int i = 0; i < length; i++) {
String[] line = (String[])csv.get(i);
if(i == 0) {
String errorMessage = validateCsvFirstLine(line);
String errorMessage = validateCsvFirstLine(templateFirstLine, line);
if(errorMessage != null)
return errorMessage;
}
else if(line.length != 0) {
if(line.length != 2)
if(line.length != templateFirstLine.length)
return "Mismatch in number of entries in row " + i;
}
}
@ -195,15 +230,51 @@ public class TestFileController extends FreemarkerHttpServlet {
return null;
}
private String validateCsvFirstLine(String[] line) {
private String validateCsvFirstLine(String[] templateFirstLine, String[] line) {
String errorMessage = "File header does not match specification";
if(line.length != 2)
if(line.length != templateFirstLine.length)
return errorMessage;
if(!line[0].equals("firstName"))
return errorMessage;
if(!line[1].equals("lastName"))
for(int i = 0; i < line.length; i++)
{
if(!line[i].equals(templateFirstLine[i]))
return errorMessage;
}
return null;
}
@SuppressWarnings("unused")
private void doHarvest()
{
/*
Harvest will entail:
D2RMapFetch
Transfer to local temp model
Diffs
Transfers
If this is being done with a script, then we should probably use a templating system.
run-csv.sh
*/
}
private class ExceptionVisibleToUser extends Exception {
private static final long serialVersionUID = 1L;
public ExceptionVisibleToUser(Throwable cause) {
super(cause);
}
}
}