use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class AddModifyApplicationVersionBacking method process.
/**
* With the first of the bean name matching "addModifyApp", there are
* three ways to access this:
* - request has applicationId and processTarget - modifying an application
* - request has applicationId only - pulling up an application to modify
* - request has processTarget only - submitting a brand new application
*
* See the WEB-INF/ftl/form-application-addmodify.ftl for input/output parameters.
*
* @param context Not referenced at all, may be null
* @param templateVariables Variables output to for the view
* @param parameterMap Parameters passed in to drive processing
* @return on errors, returns an array of error processingevents
* @see TemplatedSectionBacking::process()
*/
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
Application app = null;
ApplicationVersion version = null;
// make sure we're configured to accept uploads, warn otherwise
validateStorageConfiguration(templateVariables, events);
// we must have an application in order to add a version
if (!notEmpty(FormConstants.APP_ID, parameterMap)) {
return ProcessingUtils.newList(new GenericProcessingEvent<String>(ProcessingTargets.MESSAGES, "An application must be specified in order to add a version"));
}
Long appId = Long.valueOf(firstValue(FormConstants.APP_ID, parameterMap));
app = modelManager.getModelService().findByPrimaryKey(Application.class, appId);
if (app == null) {
return ProcessingUtils.newList(new GenericProcessingEvent<String>(ProcessingTargets.MESSAGES, "The application with id " + appId + " could not be found."));
}
events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppPage&applicationId=" + app.getId(), "View/Modify Application", "View/Modify Application")));
events.add(new AddSubNavAnchorEvent(new Anchor("?bean=appVersionListingsPage&applicationId=" + app.getId(), "Version Listings", "Version Listings")));
events.add(new AddSubNavAnchorEvent(new Anchor("?bean=deploymentListingsPage&applicationId=" + app.getId(), "Deployment History", "Deployment History")));
// at this point, we're committed to form setup at least
templateVariables.put(FormConstants.PROCESS_TARGET, PROCESS_TARGET);
version = obtainExistingApplicationVersionFromParameters(app, appId, events, parameterMap);
if (version == null) {
version = new ApplicationVersion();
}
// determine if the user is allowed to modify application versions
Boolean willProcess = canUserModifyOrCreate(app, version);
if (!willProcess) {
events.add(new MessagesEvent("Current user does not have permissions to make changes here."));
}
if (!version.getActiveFlag()) {
events.add(new MessagesEvent("This version is not currently active."));
willProcess = false;
}
templateVariables.put("willProcess", willProcess);
if (notEmpty(FormConstants.PROCESS_TARGET, parameterMap) && PROCESS_TARGET.compareTo(firstValue(FormConstants.PROCESS_TARGET, parameterMap)) == 0 && willProcess) {
// TODO: check to see if the user can delete versions
if (ParameterMapUtils.notEmpty(FormConstants.DELETE, parameterMap) && ParameterMapUtils.notEmpty("deleteConfirm", parameterMap)) {
if (ParameterMapUtils.firstValue("deleteConfirm", parameterMap).equals(FormConstants.APPVER_DELETE_CONFIRM_TEXT)) {
try {
modelManager.begin();
modelManager.delete(version, events);
modelManager.commit(events);
} catch (Exception e) {
modelManager.rollback();
String msg = String.format("Unable to delete the version - %s", ExceptionUtils.getRootCauseMessage(e));
logger.error(msg, e);
events.add(new MessagesEvent(msg));
}
} else {
events.add(new MessagesEvent("You must confirm your desire to delete by typing in the delete confirmation message."));
}
} else {
processApplicationVersionFromParameters(app, version, events, parameterMap);
}
}
if (version != null) {
templateVariables.put("version", version);
}
templateVariables.put("application", app);
createHashTypes(templateVariables, version != null ? version.getArchive() : null);
return events;
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class ApplicationVersionListingsBacking method process.
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
if (ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap)) {
Application app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap)));
if (app != null) {
setupMayCreateDeployments(templateVariables, app, events);
Deployment lastDeployment = modelManager.getModelService().getLastDeployment(app);
String currentVersionId = null;
if (lastDeployment != null && lastDeployment.getVersionIdentifier() != null) {
currentVersionId = lastDeployment.getVersionIdentifier();
}
currentVersionId = currentVersionId != null ? currentVersionId : "";
templateVariables.put("application", app);
templateVariables.put(FormConstants.PROCESS_TARGET, ProcessingTargets.DEPLOYMENTS);
templateVariables.put("currentVersionId", currentVersionId);
if (app.getVersions() != null && app.getVersions().size() > 0) {
createVersionsDisplayLists(app, templateVariables);
} else {
events.add(new MessagesEvent("Application with id " + ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap) + " has no versions associated to it"));
}
if (modelManager.getAuthorizer().may(Authorizer.Action.CREATE, new ApplicationVersion())) {
events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppVersionPage&applicationId=" + app.getId(), "Create new version", "Create new version")));
}
events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppPage&applicationId=" + app.getId(), "View/Modify Application", "View/Modify Application")));
Anchor deploymentHistoryAnchor = new Anchor("?bean=deploymentListingsPage&applicationId=" + app.getId(), "Deployment History", "Deployment History");
templateVariables.put("deploymentsAnchor", deploymentHistoryAnchor);
events.add(new AddSubNavAnchorEvent(deploymentHistoryAnchor));
} else {
events.add(new MessagesEvent("Application with id " + ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap) + " not found"));
}
} else {
events.add(new MessagesEvent("An application must be selected"));
}
return events;
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class AddModifyApplicationBackingTest method testDelete.
@Test
public void testDelete() {
ModelTestUtils.resetTestDb();
ModelTestUtils.createModel(null);
ModelManager mm = ModelTestUtils.createModelManager();
Map<Object, Object> vars = new HashMap<Object, Object>();
Map<Object, Object> parms = new HashMap<Object, Object>();
AddModifyApplicationBacking amab = null;
//////////////////////////
// Verify that trying to delete an application
// without submitting the correct deleteConfirm
// results in no action, except message
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
parms.put(FormConstants.DELETE, new String[] { "Delete!" });
parms.put("deleteConfirm", new String[] { "this is squirreled up" });
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("name", new String[] { "Application.name.1" });
parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.1_modified" });
parms.put("deviceTypes", new String[] { "1", "2" });
amab = new AddModifyApplicationBacking();
amab.setModelManager(mm);
Collection<ProcessingEvent> events = amab.process(null, vars, parms);
Assert.assertTrue(ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(mm.getModelService().findByPrimaryKey(Application.class, 1L) != null);
//////////////////////////
// Verify that we can use the backing to delete an application
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
parms.put(FormConstants.DELETE, new String[] { "Delete!" });
parms.put("deleteConfirm", new String[] { "delete the application" });
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("name", new String[] { "Application.name.1" });
parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.1_modified" });
parms.put("deviceTypes", new String[] { "1", "2" });
amab = new AddModifyApplicationBacking();
amab.setModelManager(mm);
amab.process(null, vars, parms);
Assert.assertTrue(mm.getModelService().findByPrimaryKey(Application.class, 1L) == null);
Assert.assertTrue(parms.get(FormConstants.APP_ID) == null);
Assert.assertTrue(ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class AddModifyApplicationVersionsBackingTest method testFormPost.
@Test
public void testFormPost() {
ApplicationVersion version = null;
Map<Object, Object> vars = null;
Map<Object, Object> parms = null;
////////////
// Verify that we can create a new ApplicationVersion
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APPVER });
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("versionId", new String[] { "" });
parms.put("identifier", new String[] { "ApplicationVersion.identifier.1" });
parms.put("url", new String[] { "ANewDownloadUrl" });
parms.put("hashType", new String[] { "MD5" });
parms.put("hash", new String[] { "ANewHashValue" });
parms.put("notes", new String[] { "These are them application version notes" });
parms.put("deviceTypes", new String[] { "1", "2" });
parms.put("bytesLength", new String[] { "13456342" });
parms.put("bytesLengthUncompressed", new String[] { "13456342" });
AddModifyApplicationVersionBacking amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(modelManager);
Collection<ProcessingEvent> events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() == 4 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(vars.get("hashTypes") != null);
Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APPVER) == 0);
Assert.assertTrue(vars.get("version") != null);
version = ((ApplicationVersion) vars.get("version"));
Assert.assertTrue(version.getIdentifier().compareTo("ApplicationVersion.identifier.1") == 0);
Assert.assertTrue(version.getArchive() != null);
/////////////
// Verify that we can change pretty much everything
Long ourVersionId = ((ApplicationVersion) vars.get("version")).getId();
parms.put("versionId", new String[] { ourVersionId.toString() });
parms.put("identifier", new String[] { "ApplicationVersion.new_version.identifier" });
parms.put("url", new String[] { "AnotherNewDownloadUrl" });
parms.put("hashType", new String[] { "SHA1" });
parms.put("hash", new String[] { "AnotherNewHashValue" });
parms.put("notes", new String[] { "New notes" });
parms.put("deviceTypes", new String[] { "1" });
parms.put("bytesLength", new String[] { "10000" });
parms.put("bytesLengthUncompressed", new String[] { "10000" });
amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(modelManager);
events = amab.process(null, vars, parms);
version = modelManager.getModelService().findByPrimaryKey(ApplicationVersion.class, ourVersionId);
Assert.assertTrue(version.getIdentifier().compareTo("ApplicationVersion.new_version.identifier") == 0);
Assert.assertTrue(version.getArchive().getUrl().compareTo("AnotherNewDownloadUrl") == 0);
Assert.assertTrue(version.getArchive().getHashAlgorithm().compareTo("SHA1") == 0);
Assert.assertTrue(version.getArchive().getHash().compareTo("AnotherNewHashValue") == 0);
Assert.assertTrue(version.getNotes().compareTo("New notes") == 0);
Assert.assertTrue(version.getArchive().getBytesLength() == 10000);
Assert.assertTrue(version.getArchive().getBytesLengthUncompressed() == 10000);
/////////////
// Verify that we cannot change an inactive version
ourVersionId = ((ApplicationVersion) vars.get("version")).getId();
((ApplicationVersion) vars.get("version")).setActiveFlag(false);
parms.put("versionId", new String[] { ourVersionId.toString() });
parms.put("identifier", new String[] { "ApplicationVersion.new_version_2.identifier" });
parms.put("url", new String[] { "AnotherNewDownloadUrl2" });
parms.put("hashType", new String[] { "MD5" });
parms.put("hash", new String[] { "AnotherNewHashValue2" });
parms.put("notes", new String[] { "New notes2" });
parms.put("deviceTypes", new String[] { "12" });
parms.put("bytesLength", new String[] { "100002" });
parms.put("bytesLengthUncompressed", new String[] { "100002" });
amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(modelManager);
events = amab.process(null, vars, parms);
version = modelManager.getModelService().findByPrimaryKey(ApplicationVersion.class, ourVersionId);
Assert.assertTrue(version.getIdentifier().compareTo("ApplicationVersion.new_version.identifier") == 0);
Assert.assertTrue(version.getArchive().getUrl().compareTo("AnotherNewDownloadUrl") == 0);
Assert.assertTrue(version.getArchive().getHashAlgorithm().compareTo("SHA1") == 0);
Assert.assertTrue(version.getArchive().getHash().compareTo("AnotherNewHashValue") == 0);
Assert.assertTrue(version.getNotes().compareTo("New notes") == 0);
Assert.assertTrue(version.getArchive().getBytesLength() == 10000);
Assert.assertTrue(version.getArchive().getBytesLengthUncompressed() == 10000);
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class ApplicationVersionListingsBackingTest method testFormSetup.
@Test
public void testFormSetup() {
Map<Object, Object> vars = new HashMap<Object, Object>();
Map<Object, Object> parms = new HashMap<Object, Object>();
////////////////
// If no application id is specified, then we should get only a MessageBacking targeting event back
ApplicationVersionListingsBacking avlb = new ApplicationVersionListingsBacking();
avlb.setModelManager(modelManager);
Collection<ProcessingEvent> events = avlb.process(null, vars, parms);
Assert.assertTrue(events != null && events.size() == 1 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(vars.size() == 0);
////////////////
// Verify that a valid application id returns
// - a "Create Application Version" anchor
// - a list of versions
parms.put(FormConstants.APP_ID, new String[] { "1" });
events = avlb.process(null, vars, parms);
Assert.assertTrue(events != null && events.size() == 3 && ProcessingUtils.containsTarget(events, ProcessingTargets.NAV_SUB));
for (ProcessingEvent event : events) {
Assert.assertTrue(event.getPayload() instanceof Anchor);
}
Assert.assertTrue(vars.get("versions") != null && ((Map<String, ApplicationVersion>) vars.get("versions")).size() == 2);
}
Aggregations