testfile.ftl: update to conform with controller
TestFileController.java: update while we're at it
This commit is contained in:
parent
95a8c99bc5
commit
2b2519ab34
2 changed files with 92 additions and 64 deletions
|
@ -64,7 +64,7 @@
|
||||||
newLi.innerHTML = json.fileName + " <span style=\"color:red\">upload failed: " + json.errorMessage + "</span>";
|
newLi.innerHTML = json.fileName + " <span style=\"color:red\">upload failed: " + json.errorMessage + "</span>";
|
||||||
fileListing.appendChild(newLi);
|
fileListing.appendChild(newLi);
|
||||||
|
|
||||||
document.getElementById("firstUpload").value = "false";
|
document.getElementById("${paramFirstUpload}").value = "false";
|
||||||
|
|
||||||
//document.getElementById("responseArea").innerHTML = response;
|
//document.getElementById("responseArea").innerHTML = response;
|
||||||
}
|
}
|
||||||
|
@ -134,10 +134,10 @@
|
||||||
<h4 class="testfile-step-subheader">Upload file(s)</h4>
|
<h4 class="testfile-step-subheader">Upload file(s)</h4>
|
||||||
<p>Upload your filled-in template(s).</p>
|
<p>Upload your filled-in template(s).</p>
|
||||||
<form id="fileUploadForm" method="post" enctype="multipart/form-data" action="/vivo/harvester/testfile">
|
<form id="fileUploadForm" method="post" enctype="multipart/form-data" action="/vivo/harvester/testfile">
|
||||||
<input type="hidden" id="firstUpload" name="firstUpload" value="true" />
|
<input type="hidden" id="${paramFirstUpload}" name="${paramFirstUpload}" value="true" />
|
||||||
<input type="file" name="csvFile" />
|
<input type="file" name="${paramUploadedFile}" />
|
||||||
<input type="submit" name="submit" value="Upload" />
|
<input type="submit" name="submit" value="Upload" />
|
||||||
<iframe id="uploadTarget" name="uploadTarget" src="" style="width:0;height:0;border:0px solid #fff;">${testvalue!}</iframe>
|
<iframe id="uploadTarget" name="uploadTarget" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
|
||||||
</form>
|
</form>
|
||||||
<h5>Uploaded files</h5>
|
<h5>Uploaded files</h5>
|
||||||
<ul id="fileListing">
|
<ul id="fileListing">
|
||||||
|
|
|
@ -33,12 +33,16 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
private static final Log log = LogFactory.getLog(TestFileController.class);
|
private static final Log log = LogFactory.getLog(TestFileController.class);
|
||||||
private static final String TEMPLATE_DEFAULT = "testfile.ftl";
|
private static final String TEMPLATE_DEFAULT = "testfile.ftl";
|
||||||
|
|
||||||
|
private static final String PARAMETER_FIRST_UPLOAD = "firstUpload";
|
||||||
|
private static final String PARAMETER_UPLOADED_FILE = "uploadedFile";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ResponseValues processRequest(VitroRequest vreq) {
|
protected ResponseValues processRequest(VitroRequest vreq) {
|
||||||
try {
|
try {
|
||||||
Map<String, Object> body = new HashMap<String, Object>();
|
Map<String, Object> body = new HashMap<String, Object>();
|
||||||
//body.put("uploadPostback", "false");
|
//body.put("uploadPostback", "false");
|
||||||
body.put("processRequestTest", "hi.");
|
body.put("paramFirstUpload", PARAMETER_FIRST_UPLOAD);
|
||||||
|
body.put("paramUploadedFile", PARAMETER_UPLOADED_FILE);
|
||||||
return new TemplateResponseValues(TEMPLATE_DEFAULT, body);
|
return new TemplateResponseValues(TEMPLATE_DEFAULT, body);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
log.error(e, e);
|
log.error(e, e);
|
||||||
|
@ -68,6 +72,12 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
return pathBase;
|
return pathBase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private FileHarvestJob getJob(HttpServletRequest request)
|
||||||
|
{
|
||||||
|
//todo: complete
|
||||||
|
return new CsvHarvestJob("persontemplate.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
@ -87,7 +97,7 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
new ExceptionVisibleToUser(e);
|
new ExceptionVisibleToUser(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
String firstUpload = req.getParameter("firstUpload"); //clear directory on first upload
|
String firstUpload = req.getParameter(PARAMETER_FIRST_UPLOAD); //clear directory on first upload
|
||||||
if(firstUpload.toLowerCase().equals("true")) {
|
if(firstUpload.toLowerCase().equals("true")) {
|
||||||
if(directory.exists()) {
|
if(directory.exists()) {
|
||||||
File[] children = directory.listFiles();
|
File[] children = directory.listFiles();
|
||||||
|
@ -100,11 +110,11 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
if(!directory.exists())
|
if(!directory.exists())
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
|
|
||||||
File csvTemplateFile = getCsvTemplateFile();
|
FileHarvestJob job = getJob(req);
|
||||||
|
|
||||||
Map<String, List<FileItem>> fileStreams = req.getFiles();
|
Map<String, List<FileItem>> fileStreams = req.getFiles();
|
||||||
if(fileStreams.get("csvFile") != null && fileStreams.get("csvFile").size() > 0) {
|
if(fileStreams.get(PARAMETER_UPLOADED_FILE) != null && fileStreams.get(PARAMETER_UPLOADED_FILE).size() > 0) {
|
||||||
FileItem csvStream = fileStreams.get("csvFile").get(0);
|
FileItem csvStream = fileStreams.get(PARAMETER_UPLOADED_FILE).get(0);
|
||||||
String name = csvStream.getName();
|
String name = csvStream.getName();
|
||||||
name = handleNameCollision(path, name, directory);
|
name = handleNameCollision(path, name, directory);
|
||||||
File file = new File(path + name);
|
File file = new File(path + name);
|
||||||
|
@ -114,7 +124,7 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
csvStream.delete();
|
csvStream.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
String errorMessage = validateCsvFile(csvTemplateFile, file);
|
String errorMessage = job.validateUpload(file);
|
||||||
boolean success;
|
boolean success;
|
||||||
if(errorMessage != null) {
|
if(errorMessage != null) {
|
||||||
success = false;
|
success = false;
|
||||||
|
@ -164,13 +174,6 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
response.getWriter().write(json.toString());
|
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) {
|
private String handleNameCollision(String path, String filename, File directory) {
|
||||||
String base = filename;
|
String base = filename;
|
||||||
|
@ -195,12 +198,70 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class CsvHarvestJob implements FileHarvestJob {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(CsvHarvestJob.class);
|
||||||
|
|
||||||
|
private File templateFile;
|
||||||
|
|
||||||
|
public CsvHarvestJob(String templateFileName) {
|
||||||
|
templateFile = new File(getTemplateFileDirectory() + templateFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getTemplateFileDirectory() {
|
||||||
|
String harvesterPath = "/usr/share/vivo/harvester/"; //todo: hack
|
||||||
|
String pathToTemplateFiles = harvesterPath + "files/";
|
||||||
|
return pathToTemplateFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
private String validateCsvFile(File templateFile, File file) {
|
public String validateUpload(File file) {
|
||||||
try {
|
try {
|
||||||
SimpleReader reader = new SimpleReader();
|
SimpleReader reader = new SimpleReader();
|
||||||
|
|
||||||
List templateCsv = reader.parse(templateFile);
|
List templateCsv = reader.parse(this.templateFile);
|
||||||
String[] templateFirstLine = (String[])templateCsv.get(0);
|
String[] templateFirstLine = (String[])templateCsv.get(0);
|
||||||
|
|
||||||
List csv = reader.parse(file);
|
List csv = reader.parse(file);
|
||||||
|
@ -242,39 +303,6 @@ public class TestFileController extends FreemarkerHttpServlet {
|
||||||
return null;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue