Search in sources :

Example 1 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class EditedPageDataParser method parseSnippet.

private SnippetInstanceTO parseSnippet(String ns) {
    if (StringUtils.hasValue(getParameter(SNIPPET_DISCARDED_ + ns)))
        return null;
    SnippetInstanceTO snip = new SnippetInstanceTO();
    snip.setSnippetID(getParameter(SNIPPET_ID_ + ns));
    snip.setSnippetVersion(getParameter(SNIPPET_VERSION_ + ns));
    snip.setPageRegion(getIntParameter(SNIPPET_PAGE_REGION_ + ns));
    String instId = getParameter(SNIPPET_INSTANCE_ID_ + ns);
    if (AbstractPageAssembler.AUTO_HEADER_INSTANCE_ID.equals(instId))
        return null;
    else if (XMLUtils.hasValue(instId))
        try {
            nextInstanceID = Math.max(nextInstanceID, Integer.parseInt(instId) + 1);
        } catch (NumberFormatException nfe) {
        }
    snip.setInstanceID(instId);
    if (parameters.containsKey(SNIPPET_VERBATIM_TEXT_ + ns)) {
        snip.setPersistedText(getParameter(SNIPPET_VERBATIM_TEXT_ + ns));
        snip.setPersisterID(getParameter(SNIPPET_VERBATIM_PERSISTER_ + ns));
    } else {
        SnippetDefinition defn = SnippetDefinitionManager.getSnippet(snip.getSnippetID());
        if (defn == null)
            return snip;
        snip.setDefinition(defn);
        if (defn.shouldParsePersistedText())
            parseParameters(snip, ns);
        else
            invokeSnippet(snip, ns);
    }
    return snip;
}
Also used : SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition)

Example 2 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class AddNewSnippet method getSortedSnippets.

private TreeSet getSortedSnippets() {
    DataContext ctx = getDataContext();
    String[] deny = (String[]) parameters.get("deny_ALL");
    Set allSnippets = SnippetDefinitionManager.getAllSnippets();
    TreeSet snippets = new TreeSet();
    for (Iterator i = allSnippets.iterator(); i.hasNext(); ) {
        SnippetDefinition d = (SnippetDefinition) i.next();
        if (!d.shouldHide() && !denied(d, deny) && d.matchesContext(ctx))
            snippets.add(new SnipData(d));
    }
    return snippets;
}
Also used : DataContext(net.sourceforge.processdash.data.DataContext) Set(java.util.Set) TreeSet(java.util.TreeSet) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition)

Example 3 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class SnippetInvoker method test.

/** Check to see if the given snippet is valid for the current context and
     * the current user.
     * 
     * @param snippet a snippet to test.
     * @return  true if the definition for this snippet matches the context
     *    of this invoker, and if the user has permission.  If false is
     *    returned, the snippet's status will be set to indicate the problem.
     */
public boolean test(SnippetInstanceTO snippet) {
    SnippetDefinition defn = snippet.getDefinition();
    if (defn == null) {
        snippet.setStatus(SnippetInvoker.STATUS_NO_DEFINITION);
        return false;
    } else if (!defn.matchesContext(dataContext)) {
        snippet.setStatus(SnippetInvoker.STATUS_CONTEXT_MISMATCH);
        return false;
    }
    String permID = defn.getPermission();
    if (permID != null && (!XMLUtils.hasValue(mode) || "view".equalsIgnoreCase(mode) || "toc".equalsIgnoreCase(mode)) && !PermissionsManager.getInstance().hasPermission(permID)) {
        snippet.setStatus(SnippetInvoker.NO_PERMISSION);
        return false;
    }
    return true;
}
Also used : SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition)

Example 4 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class ProcessAdvisor method getAdvisorSnippets.

/**
     * @param pageRegion
     * @return a list of all snippets in the "advice" category
     */
private static List getAdvisorSnippets(int pageRegion) {
    List result = new ArrayList();
    Set snippets = SnippetDefinitionManager.getAllSnippets();
    for (Iterator i = snippets.iterator(); i.hasNext(); ) {
        SnippetDefinition snipDef = (SnippetDefinition) i.next();
        if (snipDef.matchesCategory(ADVISOR_SNIPPET_CATEGORY)) {
            SnippetInstanceTO snip = new SnippetInstanceTO();
            snip.setSnippetID(snipDef.getId());
            snip.setDefinition(snipDef);
            snip.setPageRegion(pageRegion);
            result.add(snip);
        }
    }
    return result;
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) SnippetInstanceTO(net.sourceforge.processdash.net.cms.SnippetInstanceTO) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition)

Example 5 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class SnippetInvoker method invoke.

/** Invoke a single snippet.
     *
     * @param snippet the snippet to run
     * @return the content generated by the snippet
     * @throws IOException if an error was encountered
     */
public String invoke(SnippetInstanceTO snippet) throws IOException {
    if (!test(snippet))
        return null;
    SnippetDefinition defn = snippet.getDefinition();
    if (XMLUtils.hasValue(mode) && !"view".equalsIgnoreCase(mode) && !defn.getModes().contains(mode)) {
        snippet.setStatus(SnippetInvoker.UNSUPPORTED_MODE);
        return null;
    }
    String uri = defn.getUri(mode, action);
    if (uri == null) {
        snippet.setStatus(SnippetInvoker.UNSUPPORTED_MODE);
        return null;
    }
    String namespace = snippet.getNamespace();
    Map extraEnvironment = new HashMap();
    extraEnvironment.put(SNIPPET_ID, snippet.getSnippetID());
    extraEnvironment.put(SNIPPET_VERSION, snippet.getSnippetVersion());
    extraEnvironment.put(PERSISTED_TEXT, snippet.getPersistedText());
    extraEnvironment.put(RESOURCES, defn.getResources());
    extraEnvironment.put(HTMLPreprocessor.REPLACEMENTS_PARAM, Collections.singletonMap("$$$_", namespace));
    for (int i = 0; i < PROPAGATE_TO_ENV.length; i++) extraEnvironment.put(PROPAGATE_TO_ENV[i], parentParameters.get(PROPAGATE_TO_ENV[i]));
    StringBuffer queryString = new StringBuffer(this.queryString);
    addNamespacedParameters(parentParameters, namespace, queryString);
    if (defn.shouldParsePersistedText())
        addParsedParameters(snippet.getPersistedText(), queryString);
    StringBuffer fullUri = new StringBuffer();
    fullUri.append(WebServer.urlEncodePath(prefix)).append("/").append(uri);
    HTMLUtils.appendQuery(fullUri, queryString.toString());
    WebServer webServer = (WebServer) parentEnv.get(TinyCGI.TINY_WEB_SERVER);
    try {
        String results = webServer.getRequestAsString(fullUri.toString(), extraEnvironment);
        snippet.setStatus(SnippetInvoker.STATUS_OK);
        snippet.setUri(fullUri.toString());
        return results;
    } catch (IOException ioe) {
        snippet.setStatus(SnippetInvoker.STATUS_INTERNAL_ERROR);
        snippet.setInvocationException(ioe);
        throw ioe;
    }
}
Also used : HashMap(java.util.HashMap) WebServer(net.sourceforge.processdash.net.http.WebServer) IOException(java.io.IOException) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SnippetDefinition (net.sourceforge.processdash.ui.snippet.SnippetDefinition)8 Iterator (java.util.Iterator)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SnippetInstanceTO (net.sourceforge.processdash.net.cms.SnippetInstanceTO)2 LinkedHashMap (java.util.LinkedHashMap)1 TreeSet (java.util.TreeSet)1 DataContext (net.sourceforge.processdash.data.DataContext)1 SimpleDataContext (net.sourceforge.processdash.data.util.SimpleDataContext)1 EVTaskList (net.sourceforge.processdash.ev.EVTaskList)1 PersistenceException (net.sourceforge.processdash.ev.ui.TaskScheduleChartSettings.PersistenceException)1 TinyCGIException (net.sourceforge.processdash.net.http.TinyCGIException)1 WebServer (net.sourceforge.processdash.net.http.WebServer)1 SnippetPageFilter (net.sourceforge.processdash.ui.snippet.SnippetPageFilter)1