Example usage for org.dom4j.tree BaseElement appendAttributes

List of usage examples for org.dom4j.tree BaseElement appendAttributes

Introduction

In this page you can find the example usage for org.dom4j.tree BaseElement appendAttributes.

Prototype

public void appendAttributes(Element element) 

Source Link

Usage

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  w  w  . ja  va2s  .  c o m
public void updateDismax(RecordCollection collection) {
    ensureCore(collection);

    Element dismaxElement = getDismaxElement(collection);
    Document solrConfigDocument = readSolrConfig(collection);
    Element root = solrConfigDocument.getRootElement();

    boolean defaultSearchFieldFound = false;
    // 1. keep only one requestHandler with name DISMAX_ATTRIBUTE_NAME
    for (Iterator<Element> it = root.elementIterator("requestHandler"); it.hasNext();) {
        Element currentRequestHandlerElement = it.next();
        String currentSearchComponentName = currentRequestHandlerElement.attribute("name").getText();
        if (currentSearchComponentName.equals(DISMAX_ATTRIBUTE_NAME)) {
            // first copy other fields that are not defaults if the
            // query was set as default
            if (!defaultSearchFieldFound) {
                Attribute defaultAttribute = currentRequestHandlerElement.attribute("default");
                if (defaultAttribute != null && defaultAttribute.getText().equals("true")) {
                    defaultSearchFieldFound = true;
                    defaultAttribute.setText("false");
                    List<Element> elements = currentRequestHandlerElement.elements();
                    for (Element element : elements) {
                        if (element.attribute("name") != null
                                && !element.attribute("name").getValue().equals("defaults")) {
                            BaseElement cloneElement = new BaseElement(element.getName());
                            cloneElement.appendAttributes(element);
                            cloneElement.appendContent(element);
                            dismaxElement.add(cloneElement);
                        }
                    }
                }
            }
            it.remove();
        }
    }
    if (!defaultSearchFieldFound) {
        // 2. add the parameters of the default RequestHandler to dismax
        // requestHandler (escape the parameter with name="defaults")
        for (Iterator<Element> it = root.elementIterator("requestHandler"); it.hasNext();) {
            Element currentRequestHandlerElement = it.next();
            Attribute defaultAttribute = currentRequestHandlerElement.attribute("default");
            if (defaultAttribute != null && defaultAttribute.getText().equals("true")) {
                defaultAttribute.setText("false");
                List<Element> elements = currentRequestHandlerElement.elements();
                for (Element element : elements) {
                    if (element.attribute("name") != null
                            && !element.attribute("name").getValue().equals("defaults")) {
                        BaseElement cloneElement = new BaseElement(element.getName());
                        cloneElement.appendAttributes(element);
                        cloneElement.appendContent(element);
                        dismaxElement.add(cloneElement);
                    }
                }
                break;
            }
        }
    }

    root.add(dismaxElement);

    writeSolrConfig(collection, solrConfigDocument);
    initCore(collection);
}