Example usage for org.dom4j Node valueOf

List of usage examples for org.dom4j Node valueOf

Introduction

In this page you can find the example usage for org.dom4j Node valueOf.

Prototype

String valueOf(String xpathExpression);

Source Link

Document

valueOf evaluates an XPath expression and returns the textual representation of the results the XPath string-value of this node.

Usage

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create child folders and files /*w  ww  .  j av  a2 s . c  o  m*/
 * 
 * @param parentClassFilePath
 * @param importFromFilePath
 * @param parentRef
 * @param childNodes
 * @param isFolder
 * @param dateFormat
 * @throws Exception
 */
@SuppressWarnings("unchecked")
protected void createChildren(String parentClassFilePath, boolean importFromFilePath, NodeRef parentRef,
        List<Node> childNodes, boolean isFolder, SimpleDateFormat dateFormat) {
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[IMPORTSERVICE] createChildren parentClassFilePath[" + parentClassFilePath + "]");
    }
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    if (parentRef != null && childNodes != null) {
        for (Node childNode : childNodes) {
            String childName = childNode.valueOf("@name");
            boolean importAll = (isFolder)
                    ? ContentFormatUtils.getBooleanValue(childNode.valueOf("@import-all"))
                    : false;
            if (importAll) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("[IMPORTSERVICE] Importing all folders and files from " + childName);
                }
                NodeRef childRef = getFileFolderRef(parentRef, childName, ContentModel.TYPE_FOLDER);
                List<Node> aspectNodes = childNode.selectNodes("common-aspects/aspect");
                Map<QName, Map<QName, Serializable>> commonAspects = new FastMap<QName, Map<QName, Serializable>>();
                if (aspectNodes != null && aspectNodes.size() > 0) {
                    for (Node aspectNode : aspectNodes) {
                        QName aspectName = persistenceManagerService.createQName(aspectNode.valueOf("@name"));
                        Map<QName, Serializable> properties = getProperties(
                                aspectNode.selectNodes("properties/property"), dateFormat);
                        commonAspects.put(aspectName, properties);
                    }
                }
                importFileList(childRef, parentClassFilePath + "/" + childName, importFromFilePath,
                        commonAspects);
                createRules(childName, childRef, childNode.selectNodes("rule"));
            } else {
                String childType = childNode.valueOf("@type");
                QName type = persistenceManagerService.createQName(childType);
                if (childName != null && type != null) {
                    createContent(childNode, parentClassFilePath, importFromFilePath, parentRef, childName,
                            type, isFolder, dateFormat);
                }
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create folder or file//w  w w.  j  av  a  2 s  .co m
 *
 * @param node
 * @param parentClassFilePath
 * @param parentRef
 * @param name
 * @param type
 * @param isFolder
 * @param dateFormat
 * @throws Exception
 */
@SuppressWarnings("unchecked")
protected void createContent(Node node, String parentClassFilePath, boolean importFromFilePath,
        NodeRef parentRef, String name, QName type, boolean isFolder, SimpleDateFormat dateFormat) {
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[IMPORTSERVICE] createContent [" + parentClassFilePath + "]");
    }
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    NodeRef nodeRef = getFileFolderRef(parentRef, name, type);
    addAspects(nodeRef, node, dateFormat);
    // set properties
    Map<QName, Serializable> properties = getProperties(node.selectNodes("properties/property"), dateFormat);
    if (properties != null) {
        persistenceManagerService.setProperties(nodeRef, properties);
    }
    String childClassFilePath = parentClassFilePath + "/" + name;
    if (isFolder) {
        // if the folder is identifiable, assign an ID unless the content already carries an id
        if (persistenceManagerService.hasAspect(nodeRef, CStudioContentModel.ASPECT_IDENTIFIABLE)) {
            String namespace = (String) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_NAMESPACE);
            Long order = (Long) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_ORDER);
            // if is-current flag is not found, it is current by default
            boolean current = (Boolean) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_CURRENT);
            assignIdentifier(name, type, nodeRef, namespace, order, current);
        }
        createChildren(childClassFilePath, importFromFilePath, nodeRef, node.selectNodes("folder"), true,
                dateFormat);
        createChildren(childClassFilePath, importFromFilePath, nodeRef, node.selectNodes("file"), false,
                dateFormat);
        createRules(name, nodeRef, node.selectNodes("rule"));
    } else {
        boolean overwrite = ContentFormatUtils.getBooleanValue(node.valueOf("@overwrite"));
        ;
        if (overwrite) {
            try {
                copyFile(childClassFilePath, importFromFilePath, nodeRef, name);
            } catch (Exception e) {
                LOGGER.error("[IMPORTSERVICE] error copying file [" + childClassFilePath + "]", e);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create rules for the space associated with the nodeRef
 *
 * @param nodeRef//from   w  w  w.ja va2s.com
 * @param nodes
 */
protected void createRules(String name, NodeRef nodeRef, List<Node> nodes) {
    if (nodes != null) {
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node node : nodes) {
            // clean up
            persistenceManagerService.removeAllRules(nodeRef);
            // create an action for the rule
            Action action = createAction(name, node.selectSingleNode("action"));
            if (action != null) {
                Rule rule = new Rule();
                rule.setRuleType(node.valueOf("@ruleType"));
                rule.setDescription(node.valueOf("@description"));
                rule.setTitle(node.valueOf("@title"));
                boolean executeAsynchronously = Boolean.parseBoolean(node.valueOf("@executeAsynchronously"));
                rule.setExecuteAsynchronously(executeAsynchronously);
                boolean applyToChildren = Boolean.parseBoolean(node.valueOf("@applyToChildren"));
                rule.applyToChildren(applyToChildren);
                rule.setAction(action);
                persistenceManagerService.saveRule(nodeRef, rule);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create an add-aspect action given action configuration
 *
 * @param name/*from   ww w .  j a v  a2 s.c o m*/
 * @param node
 * @return
 */
protected Action createAction(String name, Node node) {
    if (node != null) {
        String actionName = node.valueOf("@name");
        if (actionName != null && actionName.equals(AddFeaturesActionExecuter.NAME)) {
            return createAddAspectAction(name, node, actionName);
        } else if (!StringUtils.isEmpty(actionName)) {
            return createImportAction(name, node, actionName);
        } else {
            LOGGER.error("[IMPORTSERVICE] action name cannot be empty");
        }
    } else {
        LOGGER.error("[IMPORTSERVICE] An error occured while creating a rule in " + name
                + ". No action specified to create an action.");
    }
    return null;
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create add aspect action//from  w w  w .ja v  a 2 s  . c o m
 *
 * @param name
 * @param node
 * @param actionName
 * @return
 */
protected Action createAddAspectAction(String name, Node node, String actionName) {
    String paramName = node.valueOf("@paramName");
    String paramValue = node.valueOf("@paramValue");
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    QName aspectType = persistenceManagerService.createQName(paramValue);
    if (aspectType != null) {
        CompositeAction compositeAction = persistenceManagerService.createCompositeAction();
        // add action
        Action action = persistenceManagerService.createAction(actionName);
        action.setParameterValue(paramName, aspectType);
        compositeAction.addAction(action);
        // add action condition
        String actionConditionName = node.valueOf("condition/@name");
        ActionCondition actionCondition = persistenceManagerService.createActionCondition(actionConditionName);
        compositeAction.addActionCondition(actionCondition);
        return compositeAction;
    } else {
        LOGGER.error("An error occured while creating a rule in " + name
                + ". No aspect specified to create an action.");
    }
    return null;
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * add aspects to content/*from w w  w  . j a  va  2 s .  c o m*/
 *
 * @param nodeRef
 * @param node
 * @param dateFormat
 */
@SuppressWarnings("unchecked")
protected void addAspects(NodeRef nodeRef, Node node, SimpleDateFormat dateFormat) {
    List<Node> aspectNodes = node.selectNodes("aspects/aspect");
    if (aspectNodes != null && aspectNodes.size() > 0) {
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node aspectNode : aspectNodes) {
            String name = aspectNode.valueOf("@name");
            QName type = persistenceManagerService.createQName(name);
            if (type != null) {
                Map<QName, Serializable> properties = getProperties(
                        aspectNode.selectNodes("properties/property"), dateFormat);
                persistenceManagerService.addAspect(nodeRef, type, properties);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * get a map of properties from the given nodes
 *
 * @param nodes/*from ww  w  . j  a  va 2 s  .  c o  m*/
 * @param dateFormat
 * @return
 */
protected Map<QName, Serializable> getProperties(List<Node> nodes, SimpleDateFormat dateFormat) {
    Map<QName, Serializable> properties = null;
    if (nodes != null && nodes.size() > 0) {
        properties = new HashMap<QName, Serializable>();
        NamespaceService namespaceService = getService(NamespaceService.class);
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node propNode : nodes) {
            String propName = propNode.valueOf("@name");
            String value = propNode.getText();
            QName propType = namespaceService.createQName(propName);
            if (propType != null) {
                PropertyDefinition propDef = persistenceManagerService.getProperty(propType);
                if (propDef != null) {
                    String className = propDef.getDataType().getJavaClassName();
                    Serializable convertedValue = ContentFormatUtils.convertType(namespaceService, propType,
                            className, value, dateFormat);
                    properties.put(propType, convertedValue);
                }
            }
        }
    }
    return properties;
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create groups specified/*from w  w  w  .  j  a  v a  2 s .  c om*/
 *
 * @param nodes
 * @param parentName parent group name. if null, groups will be created at root
 */
@SuppressWarnings("unchecked")
protected List<String> createGroups(List<Node> nodes, String parentName) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[IMPORTSERVICE] createGroups parentName[" + parentName + "]");
    if (nodes != null && nodes.size() > 0) {
        List<String> groups = new ArrayList<String>(nodes.size());
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node node : nodes) {
            String parentGroup = null;
            if (parentName != null) {
                parentGroup = "GROUP_" + parentName;
            }
            String name = node.valueOf("@name");
            if (!StringUtils.isEmpty(name)) {
                if (!persistenceManagerService.authorityExists(name)) {
                    // passing null for authority zone since it is not applicable
                    persistenceManagerService.createAuthority(AuthorityType.GROUP, parentGroup, name, null);
                    groups.add(name);
                }
                createGroups(node.selectNodes("group"), name);
            }
            // add users to the group if the list is provided
            String userList = node.valueOf("users");
            if (!StringUtils.isEmpty(userList)) {
                String[] users = userList.split(",");
                for (String user : users) {
                    if (persistenceManagerService.personExists(user)) {
                        persistenceManagerService.addAuthority("GROUP_" + name, user);
                    } else {
                        LOGGER.error(
                                "[IMPORTSERVICE] " + user + " does not exist and cannot be added to " + name);
                    }
                }
            }
            // only return the top level groups to add groups to the DM space
        }
        return groups;
    }
    return new ArrayList<String>(0);
}

From source file:org.craftercms.cstudio.impl.service.translation.workflow.handler.KickoffTranslationWorkflowForWaterfallItemsHandler.java

License:Open Source License

@Override
public String handleState(WorkflowJob job, WorkflowService workflowService) {
    // load the configuration for child sites from site config
    String retState = job.getCurrentStatus();
    String site = job.getSite();/* www. j av a  2 s.  c o m*/

    try {
        // construct a list of file
        List<String> paths = new FastList<String>();

        for (WorkflowItem item : job.getItems()) {
            String path = item.getPath();
            paths.add(path);
            getDependents(site, path, paths);
        }

        // load the translation for the site
        Document siteConfigEl = _siteService.getSiteConfiguration(site);
        String sourceLanguage = siteConfigEl.valueOf("/site-config/translation/sourceLanguage");

        List<Node> targetEls = siteConfigEl.selectNodes("/site-config/translation/targetSites//targetSite");

        if (paths.size() > 0) {
            // for each configuration
            for (Node targetEl : targetEls) {
                String targetSiteId = targetEl.valueOf("id");
                String basePath = targetEl.valueOf("basePath");
                String targetLanguage = targetEl.valueOf("targetLanguage");

                // keep existing properties and add new ones
                Map<String, String> properties = job.getProperties();
                properties.put("sourceSite", site);
                properties.put("sourceLanguage", sourceLanguage);
                properties.put("targetSite", targetSiteId);
                properties.put("basePath", basePath);
                properties.put("targetLanguage", targetLanguage);

                // calculate the intersection
                List<String> targetPaths = _translationService.calculateTargetTranslationSet(site, paths,
                        targetSiteId);

                // submit job
                for (String path : targetPaths) {
                    List<String> submitAsSingleItemList = new ArrayList<String>();
                    submitAsSingleItemList.add(path);

                    workflowService.createJob(targetSiteId, submitAsSingleItemList, "translate", properties);
                }
            }
        }
        retState = WorkflowService.STATE_ENDED;
    } catch (Exception err) {
        logger.error(MSG_ERROR_CREATE_NEW_TRANSLATE_JOB, err, job);
    }

    return retState;
}

From source file:org.craftercms.studio.impl.v1.service.configuration.ServicesConfigImpl.java

License:Open Source License

/**
 * load services configuration//  w  ww .  jav a 2 s .c o  m
 *
 */
@SuppressWarnings("unchecked")
protected SiteConfigTO loadConfiguration(String site) {
    String siteConfigPath = getConfigPath().replaceFirst(StudioConstants.PATTERN_SITE, site);

    Document document = null;
    SiteConfigTO siteConfig = null;
    try {
        document = contentService.getContentAsDocument(site, siteConfigPath + "/" + getConfigFileName());
    } catch (DocumentException e) {
        LOGGER.error("Error while loading configuration for " + site + " at " + siteConfigPath, e);
    }
    if (document != null) {
        Element root = document.getRootElement();
        Node configNode = root.selectSingleNode("/site-config");
        String name = configNode.valueOf("display-name");
        siteConfig = new SiteConfigTO();
        siteConfig.setName(name);
        //siteConfig.setSiteName(configNode.valueOf("name"));
        siteConfig.setWemProject(configNode.valueOf("wem-project"));
        //siteConfig.setDefaultContentType(configNode.valueOf("default-content-type"));
        //String assetUrl = configNode.valueOf("assets-url");
        siteConfig.setTimezone(configNode.valueOf("default-timezone"));
        //siteConfig.setAssetUrl(assetUrl);
        //loadNamespaceToTypeMap(siteConfig, configNode.selectNodes("namespace-to-type-map/namespace"));
        //loadModelConfig(siteConfig, configNode.selectNodes("models/model"));
        //SearchConfigTO searchConfig = _contentTypesConfig.loadSearchConfig(configNode.selectSingleNode("search"));
        //siteConfig.setDefaultSearchConfig(searchConfig);
        loadSiteRepositoryConfiguration(siteConfig, configNode.selectSingleNode("repository"));
        // set the last updated date
        siteConfig.setLastUpdated(new Date());
    } else {
        LOGGER.error("No site configuration found for " + site + " at " + siteConfigPath);
    }
    return siteConfig;
}