Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

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

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.pentaho.platform.repository.subscription.SubscriptionPublisher.java

License:Open Source License

@Override
public String publish(IPentahoSession session) {
    String publishSrcPath = PentahoSystem.getApplicationContext().getSolutionPath("") //$NON-NLS-1$
            + "system/ScheduleAndContentImport.xml"; //$NON-NLS-1$
    Document document = DocumentHelper.createDocument();
    Element root = DocumentHelper.createElement("importContentResults"); //$NON-NLS-1$
    document.add(root);//from ww w  .j a va 2 s  . com
    try {
        ISubscriptionRepository subscriptionRepository = PentahoSystem.get(ISubscriptionRepository.class,
                session);
        File file = new File(publishSrcPath);
        if (!file.canRead()) {
            throw new FileNotFoundException("SubscriptionPublisher.publish() requires the file \""
                    + publishSrcPath + "\" to exist. The file does not exist.");
        }
        Document importDoc = XmlDom4JHelper.getDocFromFile(file, null);

        root.add(subscriptionRepository.importSchedules(importDoc));
        root.add(subscriptionRepository.importContent(importDoc));
    } catch (FileNotFoundException e) {
        getLogger().error(Messages.getString("SubscriptionPublisher.ERROR_0001", publishSrcPath), e); //$NON-NLS-1$
        return Messages.getString("SubscriptionPublisher.ERROR_0002", publishSrcPath); //$NON-NLS-1$
    } catch (DocumentException e) {
        getLogger().error(Messages.getString("SubscriptionPublisher.ERROR_0003", publishSrcPath), e); //$NON-NLS-1$
        return Messages.getString("SubscriptionPublisher.ERROR_0004") + publishSrcPath; //$NON-NLS-1$
    } catch (IOException e) {
        getLogger().error(Messages.getString("SubscriptionPublisher.ERROR_0005", publishSrcPath), e); //$NON-NLS-1$
        return Messages.getString("SubscriptionPublisher.ERROR_0006", publishSrcPath); //$NON-NLS-1$
    }

    List resultNodes = document.selectNodes("//@result"); //$NON-NLS-1$
    for (Iterator iter = resultNodes.iterator(); iter.hasNext();) {
        Attribute attribute = (Attribute) iter.next();
        if ("ERROR".equalsIgnoreCase(attribute.getValue())) { //$NON-NLS-1$
            return Messages.getString("SubscriptionPublisher.ERROR_0007"); //$NON-NLS-1$
        }
    }

    return Messages.getString("SubscriptionPublisher.INFO_0001"); //$NON-NLS-1$
}

From source file:org.pentaho.platform.repository.subscription.SubscriptionRepository.java

License:Open Source License

public Element importSchedules(final Document doc) {
    Element resultElement = DocumentHelper.createElement("importSchedulesResults"); //$NON-NLS-1$

    if (doc == null) {
        Element ele = resultElement.addElement("message") //$NON-NLS-1$
                .addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$
        ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
        return (resultElement);
    }//from  w ww  .  ja  v  a 2s  .co m

    List scheduleNodes = doc.selectNodes("//schedules/*"); //$NON-NLS-1$
    if (scheduleNodes.size() == 0) {
        Element ele = resultElement.addElement("message") //$NON-NLS-1$
                .addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$
        ele.addAttribute("result", "WARNING"); //$NON-NLS-1$ //$NON-NLS-2$
        return (resultElement);
    }

    synchronized (SubscriptionRepository.SyncLock) {

        Node scheduleNode = null;
        String schedName, schedDesc, schedRef, cronString, schedGroup;
        ISchedule aSchedule;
        // List schedList;

        try {
            SubscriptionRepository.logger
                    .info(Messages.getString("PRO_SUBSCRIPTREP.USER_IMPORT_PROCESSING_SCHEDULES", //$NON-NLS-1$
                            Integer.toString(scheduleNodes.size())));
            for (int i = 0; i < scheduleNodes.size(); i++) {
                scheduleNode = (Node) scheduleNodes.get(i);
                schedRef = scheduleNode.selectSingleNode("@ref").getText(); //$NON-NLS-1$
                schedName = scheduleNode.selectSingleNode("@name").getText(); //$NON-NLS-1$
                schedDesc = scheduleNode.selectSingleNode("@description").getText(); //$NON-NLS-1$
                schedGroup = scheduleNode.selectSingleNode("@group").getText(); //$NON-NLS-1$
                cronString = scheduleNode.getText();

                try {
                    aSchedule = getScheduleByScheduleReference(schedRef);
                    if (aSchedule != null) {
                        aSchedule.setCronString(cronString);
                        aSchedule.setDescription(schedDesc);
                        aSchedule.setGroup(schedGroup);
                        aSchedule.setTitle(schedName);
                        resultElement.addElement("modified").addText(schedRef); //$NON-NLS-1$
                        SubscriptionRepository.logger.info(Messages
                                .getString("PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
                    } else {
                        aSchedule = addCronSchedule(schedName, schedRef, schedDesc, cronString, schedGroup,
                                null, null);
                        resultElement.addElement("added").addText(schedRef); //$NON-NLS-1$
                        SubscriptionRepository.logger.info(
                                Messages.getString("PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
                    }
                    SubscriptionRepository.subscriptionScheduler.getCronSummary(cronString); // Throws an exception if invalid                        
                } catch (Exception e) {
                    resultElement.addElement("message") //$NON-NLS-1$
                            .addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_OCCURRED_WITH_SCHEDULE", //$NON-NLS-1$
                                    schedRef, e.getLocalizedMessage()));
                    SubscriptionRepository.logger
                            .warn(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_WITH_SCHEDULE", schedRef), e); //$NON-NLS-1$
                }
            }

        } catch (Exception e) {
            Element ele = resultElement.addElement("message").addText( //$NON-NLS-1$
                    Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$
            ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
            SubscriptionRepository.logger
                    .error(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
            return (resultElement);
        }
    }
    return (resultElement);
}

From source file:org.pentaho.platform.repository.subscription.SubscriptionRepository.java

License:Open Source License

public Element importContent(final Document doc) {
    Element resultElement = DocumentHelper.createElement("importContentResults"); //$NON-NLS-1$

    if (doc == null) {
        Element ele = resultElement.addElement("message") //$NON-NLS-1$
                .addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$
        ele.addAttribute("result", "ERROR"); //$NON-NLS-1$//$NON-NLS-2$
        return (resultElement);
    }//  www  .j a va  2s. c  om

    List contentNodes = doc.selectNodes("//subscription-content/*"); //$NON-NLS-1$
    if (contentNodes.size() == 0) {
        Element ele = resultElement.addElement("message") //$NON-NLS-1$
                .addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$
        ele.addAttribute("result", "WARNING"); //$NON-NLS-1$//$NON-NLS-2$
        return (resultElement);
    }

    synchronized (SubscriptionRepository.SyncLock) {

        Node contentNode = null;
        Node tempNode = null;
        String actionRef, contentType, allowAllSchedules;
        ISubscribeContent subscribeContent = null;

        List schedList = getSchedules();
        Map scheduleMap = new HashMap();
        Map groupMap = new HashMap();

        for (int j = 0; j < schedList.size(); ++j) {
            Schedule aSchedule = (Schedule) schedList.get(j);
            scheduleMap.put(aSchedule.getScheduleReference(), aSchedule);
            List groupList = (List) groupMap.get(aSchedule.getGroup());
            if (groupList == null) {
                groupList = new ArrayList();
                groupMap.put(aSchedule.getGroup(), groupList);
            }
            groupList.add(aSchedule);
        }

        try {
            SubscriptionRepository.logger.info(Messages.getString(
                    "PRO_SUBSCRIPTREP.USER_PROCESSING_CONTENT_NODES", Integer.toString(contentNodes.size()))); //$NON-NLS-1$
            for (int i = 0; i < contentNodes.size(); i++) {
                contentNode = (Node) contentNodes.get(i);
                actionRef = contentNode.selectSingleNode("@action").getText(); //$NON-NLS-1$
                contentType = contentNode.selectSingleNode("@type").getText(); //$NON-NLS-1$
                tempNode = contentNode.selectSingleNode("@allowAllSchedules"); //$NON-NLS-1$
                if (tempNode != null) {
                    allowAllSchedules = tempNode.getText();
                } else {
                    allowAllSchedules = "false"; //$NON-NLS-1$
                }

                try {
                    subscribeContent = getContentByActionReference(actionRef);
                    if (subscribeContent != null) {
                        subscribeContent.setType(contentType);
                        resultElement.addElement("modified").addText(actionRef); //$NON-NLS-1$
                        SubscriptionRepository.logger.info(Messages
                                .getString("PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
                    } else {
                        subscribeContent = addContent(actionRef, contentType);
                        resultElement.addElement("added").addText(actionRef); //$NON-NLS-1$
                        SubscriptionRepository.logger.info(
                                Messages.getString("PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
                    }

                } catch (Exception e) {
                    resultElement.addElement("message").addText(Messages.getString( //$NON-NLS-1$
                            "PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT", actionRef, e.getLocalizedMessage())); //$NON-NLS-1$ 
                    SubscriptionRepository.logger
                            .warn(Messages.getString("PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT_LOG", actionRef), e); //$NON-NLS-1$ 
                }

                List contentSchedules = new ArrayList();
                if ("true".equalsIgnoreCase(allowAllSchedules)) { //$NON-NLS-1$
                    contentSchedules.addAll(schedList);
                } else {

                    List suppliedSchedules = contentNode.selectNodes("schedule"); //$NON-NLS-1$
                    if (suppliedSchedules != null) {
                        for (int j = 0; j < suppliedSchedules.size(); j++) {
                            tempNode = (Node) suppliedSchedules.get(j);
                            String schName = tempNode.getText();
                            if (schName != null) {
                                Object aSchedule = scheduleMap.get(schName);
                                if (aSchedule != null) {
                                    contentSchedules.add(aSchedule);
                                }
                            }
                        }
                    }

                    List suppliedGroups = contentNode.selectNodes("group"); //$NON-NLS-1$
                    if (suppliedGroups != null) {
                        for (int j = 0; j < suppliedGroups.size(); j++) {
                            tempNode = (Node) suppliedGroups.get(j);
                            String grpName = tempNode.getText();
                            if (grpName != null) {
                                List groupList = (List) groupMap.get(grpName);
                                if (groupList != null) {
                                    contentSchedules.addAll(groupList);
                                }
                            }
                        }
                    }
                }
                HibernateUtil.beginTransaction(); // Need to do this or the schedules don't get saved if the content item is new
                subscribeContent.setSchedules(contentSchedules);
                HibernateUtil.commitTransaction();
            }
        } catch (Exception e) {
            Element ele = resultElement.addElement("message").addText( //$NON-NLS-1$
                    Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$
            ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
            SubscriptionRepository.logger
                    .error(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
            return (resultElement);
        }

    }
    return (resultElement);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

private Element getSubscription(final ISubscription sub, final boolean schedules) {
    //@todo Protect the nulls!!!
    Element ele = DocumentHelper.createElement("subscription"); //$NON-NLS-1$
    ele.addAttribute("subscriptionId", sub.getId()); //$NON-NLS-1$
    ele.addElement("actionRef").addText(sub.getContent().getActionReference()); //$NON-NLS-1$
    ele.addElement("title").addText(sub.getTitle()); //$NON-NLS-1$
    ele.addElement("user").addText(sub.getUser()); //$NON-NLS-1$

    if (schedules) {
        ele.add(getSchedules(sub.getSchedules()));
    }/*from   w ww. ja  va2 s  . co m*/
    return (ele);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

private Element getSchedule(final Schedule sched) {
    Element ele = DocumentHelper.createElement("schedule"); //$NON-NLS-1$
    ele.addElement("schedId").addText(sched.getId()); //$NON-NLS-1$
    ele.addElement("schedRef").addText(sched.getScheduleReference()); //$NON-NLS-1$
    ele.addElement("title").addText(sched.getTitle()); //$NON-NLS-1$
    ele.addElement("desc").addText(sched.getDescription()); //$NON-NLS-1$
    ele.addElement("group").addText(sched.getGroup()); //$NON-NLS-1$
    return (ele);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

private Element getCollectionElement(final String name, final Collection c) {
    Element ele = DocumentHelper.createElement(name);
    if (c == null) {
        ele.addAttribute("count", "0"); //$NON-NLS-1$ //$NON-NLS-2$
    } else {/*from   w w w. j a  va 2 s  .c o m*/
        ele.addAttribute("count", String.valueOf(c.size())); //$NON-NLS-1$
    }
    return (ele);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

/**
 * Creates XML Document for the Add Content Page - Add a new content
 *///from   ww  w.  ja v a  2  s . c  o m
Document showAddContentUI(Element ele) {
    Document document = getDocument(SubscriptionAdminUIComponent.ACTION_CONTENT_SHOW_ADD, ele);
    document.getRootElement().add(getReturnURL());

    List contentList = subscriptionRepository.getAllContent();
    Set currentContent = new HashSet();
    for (int i = 0; i < contentList.size(); ++i) {
        currentContent.add(((SubscribeContent) contentList.get(i)).getActionReference());
    }

    ISolutionRepository repository = PentahoSystem.get(ISolutionRepository.class, getSession());
    String actionSequences[] = repository.getAllActionSequences(ISolutionRepository.ACTION_EXECUTE);
    Arrays.sort(actionSequences, new SolutionCompare()); // Put these babies in order
    ele = DocumentHelper.createElement("listContent"); //$NON-NLS-1$
    ele.addAttribute("count", String.valueOf(actionSequences.length)); //$NON-NLS-1$
    String lastFolder = null;
    Element folderEle = null;
    for (String s : actionSequences) {
        String currentFolder = getFolder(s);
        if (!currentFolder.equals(lastFolder)) {
            folderEle = DocumentHelper.createElement("folder");
            folderEle.addAttribute("name", currentFolder);
            ele.add(folderEle);
            lastFolder = currentFolder;
        }
        Element tmpEle = DocumentHelper.createElement("content").addText(getFile(s)); //$NON-NLS-1$

        tmpEle.addAttribute("selected", currentContent.contains(s) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        folderEle.add(tmpEle);
    }

    document.getRootElement().add(ele);
    System.out.println(document.asXML());
    return (document);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

Element getReturnParams() {
    Element ele = DocumentHelper.createElement(SubscriptionAdminUIComponent.NODE_RETURN_PARAM);
    IParameterProvider parameterProvider = (IParameterProvider) getParameterProviders()
            .get(HttpRequestParameterProvider.SCOPE_REQUEST);
    for (Iterator nameItr = parameterProvider.getParameterNames(); nameItr.hasNext();) {
        String name = (String) nameItr.next();
        String value = parameterProvider.getStringParameter(name, null);
        if (value != null) {
            ele.addElement(name).addText(value);
        }//from w w w. j  a  v a 2s.c om
    }
    return (ele);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

Element getReturnURL() {
    Element ele = DocumentHelper.createElement(SubscriptionAdminUIComponent.NODE_RETURN_URL);
    IParameterProvider parameterProvider = (IParameterProvider) getParameterProviders()
            .get(HttpRequestParameterProvider.SCOPE_REQUEST);
    String str = ""; //$NON-NLS-1$
    for (Iterator nameItr = parameterProvider.getParameterNames(); nameItr.hasNext();) {
        String name = (String) nameItr.next();
        String value = parameterProvider.getStringParameter(name, null);
        if (value != null) {
            str += "&" + name + "=" + value; //$NON-NLS-1$ //$NON-NLS-2$
        }/*w  w  w . j av a  2  s . co m*/
    }
    ele.addText(str);
    return (ele);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

Element getMessage(final String type, final String message) {
    return (DocumentHelper.createElement(SubscriptionAdminUIComponent.NODE_RESULT_MSG)
            .addAttribute("result", type).addText(message)); //$NON-NLS-1$
}