Skip to content

Commit

Permalink
Merge pull request #78 from daisy/v2.1-dev
Browse files Browse the repository at this point in the history
v2.1.3
  • Loading branch information
josteinaj committed Apr 6, 2016
2 parents 29889b9 + a26ae5d commit 72bb9ec
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 123 deletions.
6 changes: 3 additions & 3 deletions app/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void run() {
for (Job job : jobs) {
if (job.getFinished() != null && job.getFinished().before(timeoutDate)) {
Logger.info("Deleting old job: "+job.getId()+" ("+job.getNicename()+")");
job.delete();
job.deleteFromEngineAndWebUi();
}
}
} catch (javax.persistence.PersistenceException e) {
Expand Down Expand Up @@ -175,7 +175,7 @@ public void run() {
Date lastAccessed = Job.lastAccessed.get(webUiJob.getId());
final int deleteNewJobsAfterSeconds = 600;
if (lastAccessed == null || (new Date().getTime() - lastAccessed.getTime())/1000 > deleteNewJobsAfterSeconds) {
webUiJob.delete();
webUiJob.deleteFromEngineAndWebUi();
}

continue;
Expand All @@ -197,7 +197,7 @@ public void run() {
*/
//if ( should delete job ) {
//Logger.info("Deleting job that no longer exists in the Pipeline engine: "+webUiJob.getId()+" ("+webUiJob.getEngineId()+" - "+webUiJob.getNicename()+")");
//webUiJob.delete();
//webUiJob.deleteFromEngineAndWebUi();
//}
}
}
Expand Down
21 changes: 21 additions & 0 deletions app/assets/stylesheets/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,25 @@ body { font-size: 62.5%; font-family:Arial, Helvetica, sans-serif }

.control-group .help-block.no-validation-color, .control-group .help-inline.no-validation-color {
color: #595959;
}

/* #### enum datatype ####*/

dl.enum-terms dt em {
display: none;
}

dl.enum-terms dt.enum-term-selected em {
display: inline;
}

dl.enum-terms dt.enum-term-selected {
font-size: 120%;
}

/* #### highlight inputs with errors #### */

input:invalid {
border-color: #b94a48;
color: #b94a48;
}
4 changes: 2 additions & 2 deletions app/controllers/Administrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ public static void save(Form<SetStorageDirsForm> filledForm) {
templatesPath += System.getProperty("file.separator");

Setting.set("uploads", uploadPath);
Setting.set("jobs", uploadPath);
Setting.set("templates", uploadPath);
Setting.set("jobs", jobsPath);
Setting.set("templates", templatesPath);
}

public String getUploaddir() {
Expand Down
90 changes: 47 additions & 43 deletions app/controllers/Jobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -219,21 +221,23 @@ public static Result restart(Long jobId) {
webuiJob.cancelPushNotifications();

if (webuiJob.getEngineId() != null) {
Logger.info("deleting old job: "+webuiJob.getEngineId());
Application.ws.deleteJob(webuiJob.getEngineId());
Logger.debug("Deleting old job before restarting job: "+webuiJob.getEngineId());
org.daisy.pipeline.client.models.Job engineJob = Application.ws.getJob(webuiJob.getEngineId(), 0);
if (engineJob != null) {
boolean deleted = Application.ws.deleteJob(webuiJob.getEngineId());
if (!deleted) {
Logger.info("unable to delete old job: "+webuiJob.getEngineId());
flash("error", "An error occured while trying to delete the previous job. Please try creating a new job instead.");
return redirect(routes.Jobs.getJob(jobId));
}
}
webuiJob.setEngineId(null);
}

webuiJob.setStatus("NEW");
webuiJob.setNotifiedComplete(false);
org.daisy.pipeline.client.models.Job clientlibJob = webuiJob.asJob();
clientlibJob.setStatus(org.daisy.pipeline.client.models.Job.Status.IDLE);
webuiJob.setStatus("IDLE");
webuiJob.setStarted(null);
webuiJob.setFinished(null);
webuiJob.save();
webuiJob.reset();

Logger.info("------------------------------ Posting job... ------------------------------");
Logger.debug("------------------------------ Posting job... ------------------------------");
org.daisy.pipeline.client.models.Job clientlibJob = webuiJob.asJob();
Logger.debug(XML.toString(clientlibJob.toJobRequestXml(true)));
clientlibJob = Application.ws.postJob(clientlibJob);
if (clientlibJob == null) {
Expand Down Expand Up @@ -444,13 +448,16 @@ public static Result getJobJson(Long id) {

Map<String,Object> output = new HashMap<String,Object>();
output.put("webuiJob", webuiJob);

org.daisy.pipeline.client.models.Job clientlibJob;
org.daisy.pipeline.client.models.Job clientlibJob = null;
boolean jobAvailableInEngine = false;
if (webuiJob.getEngineId() != null) {
clientlibJob = webuiJob.getJobFromEngine(0);
} else {
jobAvailableInEngine = clientlibJob != null;
}
if (clientlibJob == null) {
clientlibJob = webuiJob.asJob();
}
output.put("jobAvailableInEngine", jobAvailableInEngine);

if (clientlibJob == null) {
Logger.error("An error occured while retrieving the job");
Expand Down Expand Up @@ -518,7 +525,7 @@ public static Result getResult(Long id, String href) {
filename = id+"";
}
}
response().setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");
response().setHeader("Content-Disposition", "inline; filename=\""+filename+"\"");

File resultFile = result.asFile();
if (resultFile == null || !resultFile.exists()) {
Expand Down Expand Up @@ -637,7 +644,7 @@ public static Result postJob(Long jobId) {
return internalServerError("Could not read form data");
}

String scriptId = params.get("id")[0];
String scriptId = params.get("_id")[0];
if ("false".equals(UserSetting.get(user.id, "scriptEnabled-"+scriptId))) {
return forbidden();
}
Expand All @@ -657,35 +664,15 @@ public static Result postJob(Long jobId) {

org.daisy.pipeline.client.models.Job clientlibJob = job.asJob();
clientlibJob.setScript(script);

for (String paramName : params.keySet()) {
String[] values = params.get(paramName);
Argument arg = script.getArgument(paramName);
if (arg != null) {
arg.clear();
if (values.length == 1 && "".equals(values[0])) {
// don't add value; treat empty strings as unset values
} else {
for (String value : values) {
arg.add(value);
}
}
} else {
Logger.warn(paramName+" is not a valid argument for the script "+script.getNicename());
}
}

// Parse and validate the submitted form (also create any necessary output directories in case of local mode)
// TODO: see if clientlib can be used for validation instead
// Parse the submitted form
Scripts.ScriptForm scriptForm = new Scripts.ScriptForm(user.id, script, params);
scriptForm.validate();

// If we're posting a template; delegate further processing to Templates.postTemplate
for (String paramName : params.keySet()) {
if (paramName.startsWith("submit_template")) {
Logger.debug("posted job is a template");
return Templates.postTemplate(user, job, clientlibJob);
}
if (params.containsKey("_submit_template")) {
Logger.debug("posted job is a template");
return Templates.postTemplate(user, job, clientlibJob);
}
Logger.debug("posted job is not a template");

Expand Down Expand Up @@ -744,8 +731,13 @@ public static Result delete(Long jobId) {
}

Logger.debug("deleting "+jobId);
webuiJob.delete();
return ok();
boolean deletedSuccessfully = webuiJob.deleteFromEngineAndWebUi();
if (deletedSuccessfully) {
return ok();
} else {
flash("error", "An error occured while trying to delete the job. Please try creating a new job instead.");
return internalServerError();
}
}

public static Result postUpload(Long jobId) {
Expand All @@ -771,7 +763,19 @@ public static Result postUpload(Long jobId) {
Logger.info("uploaded file: "+file.getFile());
// rename the uploaded file so that it is not automatically deleted by Play!
File renamedFile = new File(file.getFile().getParentFile(), file.getFile().getName()+"_");
file.getFile().renameTo(renamedFile);
try {
java.nio.file.Files.move(file.getFile().toPath(), renamedFile.toPath());

} catch (IOException e) {
Logger.error("Could not rename uploaded file. Might be a problem with permissions. Trying copying instead...", e);
try {
Files.copy(file.getFile(), renamedFile);

} catch (IOException ex) {
Logger.error("Could not copy uploaded file.", ex);
return internalServerError("Could not rename or make a copy of uploaded file.");
}
}

Logger.debug(request().method()+" | "+file.getContentType()+" | "+file.getFilename()+" | "+renamedFile.getAbsolutePath());

Expand Down
9 changes: 8 additions & 1 deletion app/controllers/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public static String logText(String title, List<Map<String,List<String>>> additi
{
List<String> daisyPipelineLog = new ArrayList<String>();
File daisyPipelineLogFile = new File(new File(new File(controllers.Application.DP2DATA).getParentFile(), "daisy-pipeline2"), "daisy-pipeline.log");
if (!daisyPipelineLogFile.exists()) {
// try alternative location
daisyPipelineLogFile = new File("/var/log/daisy-pipeline2/daisy-pipeline.log");
}
try {
FileInputStream stream = new FileInputStream(daisyPipelineLogFile);
try {
Expand All @@ -131,6 +135,10 @@ public static String logText(String title, List<Map<String,List<String>>> additi
{
List<String> derbyLog = new ArrayList<String>();
File derbyLogFile = new File(new File(new File(controllers.Application.DP2DATA).getParentFile(), "daisy-pipeline2"), "derby.log");
if (!derbyLogFile.exists()) {
// try alternative location
derbyLogFile = new File("/var/log/daisy-pipeline2/derby.log");
}
if (derbyLogFile.exists()) {
try {
FileInputStream stream = new FileInputStream(derbyLogFile);
Expand All @@ -152,7 +160,6 @@ public static String logText(String title, List<Map<String,List<String>>> additi
}
} else {
derbyLog.add("There is no Derby log file at: "+derbyLogFile.getAbsolutePath());
derbyLog.add("This probably means that you use a MySQL database instead.");
}
Map<String,List<String>> log = new HashMap<String,List<String>>();
log.put("Pipeline 2 Engine - derby.log", derbyLog);
Expand Down
40 changes: 16 additions & 24 deletions app/controllers/Scripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,51 +83,43 @@ public static String chooseWidget(Argument arg) {

public static class ScriptForm {

//public Script script;
public Map<String,List<String>> errors;

public String guestEmail;

// kind position part name
private static final Pattern PARAM_NAME = Pattern.compile("^([A-Za-z]+)(\\d*)([A-Za-z]*?)-(.*)$");

public ScriptForm(Long userId, Script script, Map<String, String[]> params) {
//this.script = script;

// Parse all arguments
for (String param : params.keySet()) {
Matcher matcher = PARAM_NAME.matcher(param);
if (!matcher.find()) {
Logger.debug("Unable to parse argument parameter: "+param);
} else {
String kind = matcher.group(1);
String name = matcher.group(4);
Logger.debug("script form: "+kind+": "+name);

Argument argument = script.getArgument(name);
if (argument == null) {
Logger.debug("'"+name+"' is not an argument for the script '"+script.getId()+"'; ignoring it");
if (param == null || param.startsWith("_")) continue; // skip arguments starting with an underscore
Argument argument = script.getArgument(param);
if (argument == null) {
Logger.debug("'"+param+"' is not an argument for the script '"+script.getId()+"'; ignoring it");
continue;
}

argument.clear();
for (String value : params.get(param)) {
String type = argument.getType();
if ("".equals(value) && ("anyDirURI".equals(type) || "anyFileURI".equals(type) || "anyURI".equals(type))) {
continue;
}

for (int i = 0; i < params.get(param).length; i++) {
argument.add(params.get(param)[i]);
}
argument.add(value);
}
}

if (userId < 0 && params.containsKey("guest-email"))
this.guestEmail = params.get("guest-email")[0];
if (userId < 0 && params.containsKey("_guest-email"))
this.guestEmail = params.get("_guest-email")[0];

this.errors = new HashMap<String, List<String>>();
}

public void validate() {
if (guestEmail != null && !"".equals(guestEmail) && !guestEmail.matches("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$")) {
addError("guest-email", "Please enter a valid e-mail address.");
addError("_guest-email", "Please enter a valid e-mail address.");
}

// TODO: validate arguments
// TODO: validate arguments (consider implementing validation in pipeline-clientlib-java)
}

public boolean hasErrors() {
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static Result postTemplate(User user, Job job, org.daisy.pipeline.client.
Template template = Template.create(clientlibJob, user);

if ("NEW".equals(job.getStatus())) {
job.delete();
job.deleteFromEngineAndWebUi();
}

String highlightTemplateName = template.name == null ? "" : ""+template.name;
Expand All @@ -110,6 +110,7 @@ public static Result postTemplate(User user, Job job, org.daisy.pipeline.client.
Logger.debug("return redirect(controllers.routes.Templates.getTemplates());");
Logger.debug("highlightTemplate:"+template.name);
Logger.debug("highlightTemplateOwner:"+template.ownerId);
flash("highlightNewTemplate", "true");
flash("highlightTemplateName", highlightTemplateName);
flash("highlightTemplateOwner", ""+highlightTemplateOwner);
return redirect(controllers.routes.Templates.getTemplates());
Expand Down
Loading

0 comments on commit 72bb9ec

Please sign in to comment.