Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

In this page you can find the example usage for org.dom4j Element addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.haulmont.cuba.core.sys.persistence.PersistenceConfigProcessor.java

License:Apache License

public void create() {
    if (sourceFileNames == null || sourceFileNames.isEmpty())
        throw new IllegalStateException("Source file list not set");
    if (StringUtils.isBlank(outFileName))
        throw new IllegalStateException("Output file not set");

    Map<String, String> classes = new LinkedHashMap<>();
    Map<String, String> properties = new HashMap<>();

    properties.putAll(DbmsSpecificFactory.getDbmsFeatures(storeName).getJpaParameters());

    for (String fileName : sourceFileNames) {
        Document doc = getDocument(fileName);
        Element puElem = findPersistenceUnitElement(doc.getRootElement());
        if (puElem == null)
            throw new IllegalStateException(
                    "No persistence unit named 'cuba' found among multiple units inside " + fileName);
        addClasses(puElem, classes);//w w  w.jav a  2 s .c om
        addProperties(puElem, properties);
    }

    for (String name : AppContext.getPropertyNames()) {
        if (name.startsWith("eclipselink.")) {
            properties.put(name, AppContext.getProperty(name));
        }
    }

    if (!Stores.isMain(storeName))
        properties.put(PersistenceImplSupport.PROP_NAME, storeName);

    File outFile;
    try {
        outFile = new File(outFileName).getCanonicalFile();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    outFile.getParentFile().mkdirs();

    boolean ormXmlCreated = true;
    String disableOrmGenProp = AppContext.getProperty("cuba.disableOrmXmlGeneration");
    if (!Boolean.parseBoolean(disableOrmGenProp)) {
        MappingFileCreator mappingFileCreator = new MappingFileCreator(classes.values(), properties,
                outFile.getParentFile());
        ormXmlCreated = mappingFileCreator.create();
    }

    String fileName = sourceFileNames.get(sourceFileNames.size() - 1);
    Document doc = getDocument(fileName);
    Element rootElem = doc.getRootElement();

    Element puElem = findPersistenceUnitElement(rootElem);
    if (puElem == null)
        throw new IllegalStateException(
                "No persistence unit named 'cuba' found among multiple units inside " + fileName);

    String puName = AppContext.getProperty("cuba.persistenceUnitName");
    if (!StringUtils.isEmpty(puName)) {
        if (!Stores.isMain(storeName))
            puName = puName + "_" + storeName;
        puElem.addAttribute("name", puName);
    }

    for (Element element : new ArrayList<>(Dom4j.elements(puElem, "class"))) {
        puElem.remove(element);
    }

    puElem.addElement("provider").setText("org.eclipse.persistence.jpa.PersistenceProvider");

    if (ormXmlCreated) {
        puElem.addElement("mapping-file").setText("orm.xml");
    }

    for (String className : classes.values()) {
        puElem.addElement("class").setText(className);
    }

    puElem.addElement("exclude-unlisted-classes");

    Element propertiesEl = puElem.element("properties");
    if (propertiesEl != null)
        puElem.remove(propertiesEl);

    propertiesEl = puElem.addElement("properties");
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        Element element = propertiesEl.addElement("property");
        element.addAttribute("name", entry.getKey());
        element.addAttribute("value", entry.getValue());
    }

    log.info("Creating file " + outFile);
    OutputStream os = null;
    try {
        os = new FileOutputStream(outFileName);
        Dom4j.writeDocument(doc, true, os);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopGroupBox.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    if (!isSettingsEnabled()) {
        return false;
    }//  w ww.j  av a 2  s.co m

    Element groupBoxElement = element.element("groupBox");
    if (groupBoxElement != null) {
        element.remove(groupBoxElement);
    }
    groupBoxElement = element.addElement("groupBox");
    groupBoxElement.addAttribute("expanded", BooleanUtils.toStringTrueFalse(isExpanded()));
    return true;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopSplitPanel.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    if (!isSettingsEnabled()) {
        return false;
    }/*from  ww w. j  a v a2s .  co m*/

    if (!positionChanged) {
        return false; // most probably user didn't change the divider location
    }

    int location = impl.getUI().getDividerLocation(impl);
    Element e = element.element("position");
    if (e == null) {
        e = element.addElement("position");
    }
    e.addAttribute("value", String.valueOf(location));
    return true;
}

From source file:com.haulmont.cuba.desktop.gui.components.SwingXTableSettings.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    element.addAttribute("horizontalScroll", String.valueOf(table.isHorizontalScrollEnabled()));

    saveFontPreferences(element);//from w  w w  .ja v  a 2s .c  o m

    Element columnsElem = element.element("columns");
    if (columnsElem != null) {
        element.remove(columnsElem);
    }
    columnsElem = element.addElement("columns");

    final List<TableColumn> visibleTableColumns = table.getColumns();
    final List<Table.Column> visibleColumns = new ArrayList<>();
    for (TableColumn tableColumn : visibleTableColumns) {
        visibleColumns.add((Table.Column) tableColumn.getIdentifier());
    }

    List<TableColumn> columns = table.getColumns(true);
    Collections.sort(columns, new Comparator<TableColumn>() {
        @SuppressWarnings("SuspiciousMethodCalls")
        @Override
        public int compare(TableColumn col1, TableColumn col2) {
            if (col1 instanceof TableColumnExt && !((TableColumnExt) col1).isVisible()) {
                return 1;
            }
            if (col2 instanceof TableColumnExt && !((TableColumnExt) col2).isVisible()) {
                return -1;
            }
            int i1 = visibleColumns.indexOf(col1.getIdentifier());
            int i2 = visibleColumns.indexOf(col2.getIdentifier());
            return Integer.compare(i1, i2);
        }
    });

    for (TableColumn column : columns) {
        Element colElem = columnsElem.addElement("column");
        colElem.addAttribute("id", column.getIdentifier().toString());

        int width = column.getWidth();
        colElem.addAttribute("width", String.valueOf(width));

        if (column instanceof TableColumnExt) {
            Boolean visible = ((TableColumnExt) column).isVisible();
            colElem.addAttribute("visible", visible.toString());
        }
    }

    if (table.getRowSorter() != null) {
        TableColumn sortedColumn = table.getSortedColumn();
        List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
        if (sortedColumn != null && !sortKeys.isEmpty()) {
            columnsElem.addAttribute("sortColumn", String.valueOf(sortedColumn.getIdentifier()));
            columnsElem.addAttribute("sortOrder", sortKeys.get(0).getSortOrder().toString());
        }
    }

    return true;
}

From source file:com.haulmont.cuba.gui.components.filter.condition.CustomCondition.java

License:Apache License

@Override
public void toXml(Element element, Param.ValueProperty valueProperty) {
    super.toXml(element, valueProperty);

    element.addAttribute("type", ConditionType.CUSTOM.name());

    if (isBlank(caption)) {
        element.addAttribute("locCaption", locCaption);
    }//from  w  w  w. jav a  2s . co  m

    element.addAttribute("entityAlias", entityAlias);

    if (!isBlank(join)) {
        Element joinElement = element.addElement("join");
        joinElement.addCDATA(join);
    }
    if (operator != null) {
        element.addAttribute("operatorType", operator.name());
    }
}

From source file:com.haulmont.cuba.gui.components.filter.FilterDelegateImpl.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    Boolean changed = false;//www.java 2s.c  o  m
    Element e = element.element("defaultFilter");
    if (e == null)
        e = element.addElement("defaultFilter");

    UUID defaultId = null;
    Boolean applyDefault = false;

    for (FilterEntity filter : filterEntities) {
        if (BooleanUtils.isTrue(filter.getIsDefault())) {
            defaultId = filter.getId();
            applyDefault = filter.getApplyDefault();
            break;
        }
    }

    String newDef = defaultId != null ? defaultId.toString() : null;
    Attribute attr = e.attribute("id");
    String oldDef = attr != null ? attr.getValue() : null;
    if (!Objects.equals(oldDef, newDef)) {
        if (newDef == null && attr != null) {
            e.remove(attr);
        } else {
            if (attr == null)
                e.addAttribute("id", newDef);
            else
                attr.setValue(newDef);
        }
        changed = true;
    }
    Boolean newApplyDef = BooleanUtils.isTrue(applyDefault);
    Attribute applyDefaultAttr = e.attribute("applyDefault");
    Boolean oldApplyDef = applyDefaultAttr != null ? Boolean.valueOf(applyDefaultAttr.getValue()) : false;
    if (!Objects.equals(oldApplyDef, newApplyDef)) {
        if (applyDefaultAttr != null) {
            applyDefaultAttr.setValue(newApplyDef.toString());
        } else {
            e.addAttribute("applyDefault", newApplyDef.toString());
        }
        changed = true;
    }

    Element groupBoxExpandedEl = element.element("groupBoxExpanded");
    if (groupBoxExpandedEl == null)
        groupBoxExpandedEl = element.addElement("groupBoxExpanded");

    Boolean oldGroupBoxExpandedValue = Boolean.valueOf(groupBoxExpandedEl.getText());
    Boolean newGroupBoxExpandedValue = groupBoxLayout.isExpanded();
    if (!Objects.equals(oldGroupBoxExpandedValue, newGroupBoxExpandedValue)) {
        groupBoxExpandedEl.setText(newGroupBoxExpandedValue.toString());
        changed = true;
    }

    return changed;
}

From source file:com.haulmont.cuba.gui.components.filter.FilterParserImpl.java

License:Apache License

/**
 * Converts filter conditions tree to filter xml
 * @param conditions conditions tree//from  w  w w.  j  a va  2 s .  com
 * @param valueProperty Describes what parameter value will be serialized to xml: current value or default one
 * @return filter xml
 */
@Override
@Nullable
public String getXml(ConditionsTree conditions, Param.ValueProperty valueProperty) {
    String xml = null;
    if (conditions != null && !conditions.getRootNodes().isEmpty()) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("filter");
        Element element = root.addElement("and");
        for (Node<AbstractCondition> node : conditions.getRootNodes()) {
            recursiveToXml(node, element, valueProperty);
        }
        xml = Dom4j.writeDocument(document, true);
    }
    log.trace("toXML: " + xml);
    return xml;
}

From source file:com.haulmont.cuba.gui.components.filter.FilterParserImpl.java

License:Apache License

protected void recursiveToXml(Node<AbstractCondition> node, Element element,
        Param.ValueProperty valueProperty) {
    AbstractCondition condition = node.getData();
    if (condition.isGroup()) {
        if (node.getChildren().size() > 0) {
            Element elem = element.addElement(((GroupCondition) condition).getGroupType().getXml());
            condition.toXml(elem, valueProperty);
            for (Node<AbstractCondition> n : node.getChildren()) {
                recursiveToXml(n, elem, valueProperty);
            }//from w w  w.j a  va2  s.c  om
        }
    } else {
        Element elem = element.addElement("c");
        condition.toXml(elem, valueProperty);
    }
}

From source file:com.haulmont.cuba.gui.components.filter.Param.java

License:Apache License

public void toXml(Element element, ValueProperty valueProperty) {
    Element paramElem = element.addElement("param");
    paramElem.addAttribute("name", getName());
    paramElem.addAttribute("javaClass", getJavaClass().getName());
    if (runtimeEnum != null) {
        paramElem.addAttribute("categoryAttrId", categoryAttrId.toString());
    }//from  w ww  . j a  v a 2s. co  m
    if (isDateInterval) {
        paramElem.addAttribute("isDateInterval", "true");
    }

    paramElem.setText(formatValue(_getValue(valueProperty)));
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String generateSetFilter(Set ids, String entityClass, String componentId, String entityAlias) {
    Document document = DocumentHelper.createDocument();
    Element root = DocumentHelper.createElement("filter");
    Element or = root.addElement("and");

    Element condition = or.addElement("c");
    condition.addAttribute("name", "set");
    condition.addAttribute("inExpr", "true");
    condition.addAttribute("hidden", "true");
    condition.addAttribute("locCaption", "Set filter");
    condition.addAttribute("entityAlias", entityAlias);
    condition.addAttribute("class", entityClass);
    condition.addAttribute("type", ConditionType.CUSTOM.name());

    String listOfId = createIdsString(ids);
    String randomName = RandomStringUtils.randomAlphabetic(10);
    condition.addText(entityAlias + ".id in :component$" + componentId + "." + randomName);

    Element param = condition.addElement("param");
    param.addAttribute("name", "component$" + componentId + "." + randomName);
    param.addText(listOfId);// w ww  .ja v  a  2  s .  c om

    document.add(root);
    return Dom4j.writeDocument(document, true);
}