Search in sources :

Example 1 with ActionInfo

use of com.manydesigns.portofino.resourceactions.registry.ActionInfo 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);
}
Also used : ActionDescriptor(com.manydesigns.portofino.actions.ActionDescriptor) ConfigurationWithDefaults(com.manydesigns.portofino.resourceactions.ConfigurationWithDefaults) ActionInfo(com.manydesigns.portofino.resourceactions.registry.ActionInfo) OgnlContext(ognl.OgnlContext) ActionInstance(com.manydesigns.portofino.resourceactions.ActionInstance) FileObject(org.apache.commons.vfs2.FileObject) OutputStreamWriter(java.io.OutputStreamWriter) FileObject(org.apache.commons.vfs2.FileObject) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) AbstractResourceAction(com.manydesigns.portofino.resourceactions.AbstractResourceAction) ResourceAction(com.manydesigns.portofino.resourceactions.ResourceAction)

Aggregations

ActionDescriptor (com.manydesigns.portofino.actions.ActionDescriptor)1 AbstractResourceAction (com.manydesigns.portofino.resourceactions.AbstractResourceAction)1 ActionInstance (com.manydesigns.portofino.resourceactions.ActionInstance)1 ConfigurationWithDefaults (com.manydesigns.portofino.resourceactions.ConfigurationWithDefaults)1 ResourceAction (com.manydesigns.portofino.resourceactions.ResourceAction)1 ActionInfo (com.manydesigns.portofino.resourceactions.registry.ActionInfo)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 OgnlContext (ognl.OgnlContext)1 FileObject (org.apache.commons.vfs2.FileObject)1