Example usage for org.dom4j DocumentHelper createDocument

List of usage examples for org.dom4j DocumentHelper createDocument

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createDocument.

Prototype

public static Document createDocument() 

Source Link

Usage

From source file:itensil.web.MethodServlet.java

License:Open Source License

protected void errorXML(HttpServletRequest request, HttpServletResponse response, Throwable th)
        throws IOException {

    Document doc = DocumentHelper.createDocument();
    Element error = doc.addElement("error");
    error.addAttribute("exception", th.getClass().getName());
    Element message = error.addElement("message");
    String msg = th.getMessage();
    message.addText(msg == null ? th.toString() : msg);
    if (logger.isInfoEnabled()) {
        Element details = error.addElement("details");
        StringWriter sw = new StringWriter();
        th.printStackTrace(new PrintWriter(sw));
        details.addCDATA(sw.toString());
    }//  w w w.  ja v a  2  s  .  c  om
    if (th.getCause() != null) {
        error.addAttribute("cause", th.getCause().getClass().getName());
    }
    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.rules.XPathConditionEval.java

License:Open Source License

public static Document getDefaultData(Activity activity, FlowModel model) throws EvalException {
    Document ddoc = activity.getDataCache();
    if (ddoc == null) {
        Collection<AppElement> elems = model.matchAppElements(ITENSIL_WF_NS, "data");
        if (elems.isEmpty()) {
            // throw new EvalException("Missing default data source");
            // supply an empty doc
            ddoc = DocumentHelper.createDocument();
            ddoc.addElement("data");
        } else {/*from ww w  .ja va  2s  .  c o  m*/
            AppElement datElem = elems.iterator().next();
            try {
                String dataUri = UriHelper.absoluteUri(activity.getNode().getUri(),
                        datElem.getAttribute("src"));
                ddoc = XMLDocument.readStream(RepositoryHelper.loadContent(dataUri));
            } catch (Exception e) {
                throw new EvalException("Data source error", e);
            }
        }
        activity.setDataCache(ddoc);
    }
    return ddoc;
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /assignedList/*from   w ww. j  a  v a2  s .  c om*/
 *
 * List Assign Activities
 *
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webAssignedList(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String flow = request.getParameter("flow");
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    User user = (User) request.getUserPrincipal();

    String userId = request.getParameter("userId");
    boolean includeEnds = "1".equals(request.getParameter("ends"));

    if (!Check.isEmpty(userId) && UserUtil.isAdmin((AuthenticatedUser) user)) {
        user = user.getUserSpace().resolve(new DefaultUser(userId));
    }

    Session session = HibernateUtil.getSession();
    UserActivities uActivities = new UserActivities(user, session);
    uActivities.setPageSize(100);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");

    List<Activity> aActivities;
    if (Check.isEmpty(flow)) {
        aActivities = uActivities.getAssignActivities(0, includeEnds);
    } else {

        // resolve flow
        FlowState fState = (FlowState) session.get(FlowState.class,
                RepositoryHelper.getNode(flow, false).getParentNodeId());
        aActivities = fState == null ? null : uActivities.getFlowAssignActivities(fState, 0, includeEnds);
    }

    if (aActivities != null) {
        for (Activity act : aActivities) {
            root.add(ActivityXML.display(act));
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.setExpired(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /submittedList//w  w  w  . ja va  2  s .  c o  m
 *
 * List Submit Activities
 *
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webSubmittedList(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String flow = request.getParameter("flow");
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    User user = (User) request.getUserPrincipal();

    String userId = request.getParameter("userId");
    boolean includeEnds = "1".equals(request.getParameter("ends"));

    if (!Check.isEmpty(userId) && UserUtil.isAdmin((AuthenticatedUser) user)) {
        user = user.getUserSpace().resolve(new DefaultUser(userId));
    }

    Session session = HibernateUtil.getSession();
    UserActivities uActivities = new UserActivities(user, session);
    uActivities.setPageSize(100);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");

    List<Activity> aActivities;
    if (Check.isEmpty(flow)) {
        aActivities = uActivities.getSubmitActivities(0, includeEnds);
    } else {
        flow = RepositoryHelper.resolveUri(flow);

        // resolve flow
        FlowState fState = (FlowState) session.get(FlowState.class,
                RepositoryHelper.getNode(flow, false).getParentNodeId());
        aActivities = fState == null ? null : uActivities.getFlowSubmitActivities(fState, 0, includeEnds);
    }

    if (aActivities != null) {
        for (Activity act : aActivities) {
            root.add(ActivityXML.display(act));
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.setExpired(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /kidList/*  w  w  w.  ja va 2s .  c  om*/
 *
 * List Activity Kids
 *
 */
@ContentType("text/xml")
public void webKidList(HttpServletRequest request, HttpServletResponse response) throws Exception {

    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    User user = (User) request.getUserPrincipal();
    UserActivities uActivities = new UserActivities(user, HibernateUtil.getSession());

    String id = request.getParameter("id");
    Activity activity = uActivities.getActivity(id);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");
    for (Activity kid : activity.getActiveChildren()) {
        root.add(ActivityXML.display(kid));
    }
    HibernateUtil.commitTransaction();

    ServletUtil.setExpired(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /setList//from w ww  .  java 2  s  .c o m
 *
 * Get actvitiy info
 *
 * Parameters:
 *  activity - id[]
 *
 */
@ContentType("text/xml")
public void webSetList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    User user = (User) request.getUserPrincipal();
    UserActivities uActivities = new UserActivities(user, HibernateUtil.getSession());
    String ids[] = request.getParameterValues("id");

    Document resDoc = DocumentHelper.createDocument();
    Element resRoot = resDoc.addElement("activities");

    for (String id : ids) {
        Activity activity = uActivities.getActivity(id);
        if (activity != null) {
            resRoot.add(ActivityXML.display(activity));
        }
    }

    HibernateUtil.commitTransaction();
    ServletUtil.setExpired(response);
    resDoc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /getPlan/* w  w  w  . jav  a2 s . co  m*/
 *
 * Get activity plan
 *
 * Parameters:
 *  activity - id
 *  step - step
 *
 */
@ContentType("text/xml")
public void webGetPlan(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    User user = (User) request.getUserPrincipal();
    UserActivities uActivities = new UserActivities(user, HibernateUtil.getSession());
    String id = request.getParameter("id");
    String step = request.getParameter("step");
    if (Check.isEmpty(step))
        throw new NotFoundException("[blank step]");

    Activity activity = uActivities.getActivity(id);

    ActivityPlan plan = activity.getPlans().get(step);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("plan");
    root.addAttribute("stepId", step);
    root.addAttribute("activity", activity.getId());
    if (plan != null) {
        root.addAttribute("skip", plan.isSkip() ? "1" : "0");
        if (plan.getDueDate() != null)
            root.addAttribute("dueDate", ActivityXML.dateFmtZ.format(plan.getDueDate()));
        if (plan.getAssignId() != null)
            root.addAttribute("assignId", plan.getAssignId());
    }
    HibernateUtil.commitTransaction();

    ServletUtil.setExpired(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /flowInfo//from   w  w  w .  j  ava2 s .c  o  m
 *
 * Get flow info
 *
 * Parameters:
 *  flow - uri
 *  masterFlow - optional uri
 *
 */
@ContentType("text/xml")
public void webFlowInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {

    // resolve the flow
    String flowUri = request.getParameter("flow");
    if (Check.isEmpty(flowUri))
        throw new NotFoundException("[blank]");
    MutableRepositoryNode flowNode;
    FlowState flowState = null;
    FlowModel flowMod = new FlowModel();
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    flowUri = RepositoryHelper.resolveUri(flowUri);
    String icon = null;
    try {
        flowNode = RepositoryHelper.getNode(flowUri, false);
        flowState = (FlowState) HibernateUtil.getSession().get(FlowState.class, flowNode.getNodeId());

        String style = flowNode.getPropertyValue(PropertyHelper.itensilQName("style"));
        if (Check.isEmpty(style)) {
            FlowSAXHandler hand = new FlowSAXHandler(flowMod);
            hand.parse(RepositoryHelper.loadContent(UriHelper.absoluteUri(flowUri, "chart.flow")));
        } else {
            if (style.startsWith("icon:"))
                icon = style.substring(5);
        }

    } catch (NotFoundException nfe) {

        // not-found, check for master-flow
        String mastUri = request.getParameter("masterFlow");
        if (Check.isEmpty(mastUri))
            throw nfe;
        MutableRepositoryNode mastNode = RepositoryHelper.getNode(mastUri, false);
        String style = mastNode.getPropertyValue(PropertyHelper.itensilQName("style"));
        if (Check.isEmpty(style)) {
            FlowSAXHandler hand = new FlowSAXHandler(flowMod);
            hand.parse(RepositoryHelper.loadContent(UriHelper.absoluteUri(mastUri, "chart.flow")));
        } else {
            if (style.startsWith("icon:"))
                icon = style.substring(5);
        }

    }
    if (icon == null) {
        AppElement activityType = flowMod.matchAppElements("http://itensil.com/workflow", "type").iterator()
                .next();
        if (activityType != null) {
            icon = activityType.getAttribute("icon");
        }
    }

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("flow-info");

    // get icon
    root.addAttribute("icon", Check.isEmpty(icon) ? "def" : icon);
    root.addAttribute("active", (flowState != null && flowState.isActive()) ? "1" : "0");

    HibernateUtil.commitTransaction();

    ServletUtil.cacheTimeout(response, 37);
    // send info
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /roleList//from   w w  w.j  a  v  a 2 s  .  c om
 *
 * Get roles
 *
 * Parameters:
 *  flowUri
 *
 */
@ContentType("text/xml")
public void webRoleList(HttpServletRequest request, HttpServletResponse response) throws Exception {

    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    String activityId = request.getParameter("activity");
    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("role-list");
    if (Check.isEmpty(activityId)) {
        FlowState fState = getFlowState(request.getParameter("flowUri"));
        for (FlowRole role : fState.getRoles().values()) {
            Element elem = root.addElement("role");
            elem.addAttribute("role", role.getRole());
            elem.addAttribute("assignId", role.getAssignId());
        }
    } else {
        User user = (User) request.getUserPrincipal();
        UserActivities uActivities = new UserActivities(user, HibernateUtil.getSession());
        Activity activity = uActivities.getActivity(activityId);
        for (ActivityRole role : activity.getRoles().values()) {
            Element elem = root.addElement("role");
            elem.addAttribute("role", role.getRole());
            elem.addAttribute("assignId", role.getAssignId());
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.setExpired(response);
    doc.write(response.getWriter());
}

From source file:itensil.workflow.activities.web.ActivityServlet.java

License:Open Source License

/**
 *  /getTimer//  w  w w .j  a  v  a 2s. c  o m
 *
 * Get an Activity Timer
 *
 * Parameters:
 *  activity
 *  tiemrId
 *
 */
@ContentType("text/xml")
public void webGetTimer(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String activityId = request.getParameter("activity");
    String timerId = request.getParameter("timer");
    if (!Check.isEmpty(activityId) && !Check.isEmpty(timerId)) {

        HibernateUtil.beginTransaction();
        HibernateUtil.readOnlySession();

        User user = (User) request.getUserPrincipal();
        UserActivities uActivities = new UserActivities(user, HibernateUtil.getSession());
        Activity activity = uActivities.getActivity(activityId);
        ActivityTimer att = uActivities.getActivityTimer(activity, timerId);
        Document doc = DocumentHelper.createDocument();
        Element elem = doc.addElement("activity-timer");
        elem.addAttribute("activity", activityId);
        elem.addAttribute("timerId", timerId);
        if (att != null) {
            elem.addAttribute("conditional", att.isConditional() ? "1" : "0");
            if (att.getAtTime() != null)
                elem.addAttribute("atTime", ActivityXML.dateFmtZ.format(att.getAtTime()));
        } else {
            elem.addAttribute("conditional", "0");
            elem.addAttribute("atTime", "");
        }
        HibernateUtil.commitTransaction();

        ServletUtil.setExpired(response);
        doc.write(response.getWriter());

    } else {
        throw new NotFoundException("[blank activity or timer]");
    }
}