use of com.manydesigns.portofino.resourceactions.ConfigurationWithDefaults in project Portofino by ManyDesigns.
the class ActionsAction method create.
@POST
public void create(String actionClassName) throws Exception {
String actionPath = StringUtils.join(parameters.subList(0, parameters.size() - 1), "/");
String segment = parameters.get(parameters.size() - 1);
ActionInstance parentActionInstance = getPageInstance(actionPath);
ResourceAction parent = parentActionInstance.getActionBean();
Class actionClass = codeBase.loadClass(actionClassName);
ActionInfo info = actionRegistry.getInfo(actionClass);
String scriptTemplate = info.scriptTemplate;
Class<?> configurationClass = info.configurationClass;
boolean supportsDetail = info.supportsDetail;
String className = actionClass.getSimpleName() + "_" + RandomUtil.createRandomId();
OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
ognlContext.put("generatedClassName", className);
ognlContext.put("actionClassName", actionClassName);
String script = OgnlTextFormat.format(scriptTemplate, parent);
ActionDescriptor action = new ActionDescriptor();
Object configuration = null;
if (configurationClass != null) {
configuration = ReflectionUtil.newInstance(configurationClass);
if (configuration instanceof ConfigurationWithDefaults) {
((ConfigurationWithDefaults) configuration).setupDefaults();
}
}
action.init();
FileObject directory = parentActionInstance.getChildPageDirectory(segment);
if (directory.exists()) {
logger.error("Can't create actionDescriptor - directory {} exists", directory.getName().getPath());
throw new WebApplicationException(Response.serverError().entity(ElementsThreadLocals.getText("error.creating.page.the.directory.already.exists")).build());
}
directory.createFolder();
logger.debug("Creating the new child actionDescriptor in directory: {}", directory);
ActionLogic.saveActionDescriptor(directory, action);
if (configuration != null) {
ActionLogic.saveConfiguration(directory, configuration);
}
FileObject groovyScriptFile = directory.resolveFile("action.groovy");
groovyScriptFile.createFile();
try (Writer w = new OutputStreamWriter(groovyScriptFile.getContent().getOutputStream())) {
w.write(script);
}
if (supportsDetail) {
FileObject detailDir = directory.resolveFile(ActionInstance.DETAIL);
logger.debug("Creating _detail directory: {}", detailDir);
detailDir.createFolder();
}
logger.info("Created action of type " + actionClassName + " in directory " + directory);
}
Aggregations