Package org.fao.geonet.kernel

Examples of org.fao.geonet.kernel.EditLib


                                        Map<String, String> changes, String currVersion) throws Exception {
        Lib.resource.checkEditPrivilege(context, id);

        String schema = dataManager.getMetadataSchema(id);
        MetadataSchema metadataSchema = dataManager.getSchema(schema);
        EditLib editLib = dataManager.getEditLib();

        // --- check if the metadata has been modified from last time
        if (currVersion != null && !editLib.getVersion(id).equals(currVersion)) {
            Log.error(Geonet.EDITOR, "Version mismatch: had " + currVersion +
                                            " but expected " + editLib.getVersion(id));
            return null;
        }

        // --- get metadata from session
        Element md = getMetadataFromSession(session, id);

        // Store XML fragments to be handled after other elements update
        Map<String, String> xmlInputs = new HashMap<String, String>();
        Map<String, AddElemValue> xmlAndXpathInputs = new HashMap<String, AddElemValue>();

        // --- update elements
        for (Map.Entry<String, String> entry : changes.entrySet()) {
            String ref = entry.getKey().trim();
            String value = entry.getValue().trim();
            String attribute = null;
           
            // Avoid empty key
            if (ref.equals("")) {
                continue;
            }
           
            // Catch element starting with a X to replace XML fragments
            if (ref.startsWith("X")) {
                ref = ref.substring(1);
                xmlInputs.put(ref, value);
                continue;
            } else if (ref.startsWith("P") && ref.endsWith("_xml")) {
                continue;
            } else if (ref.startsWith("P") && !ref.endsWith("_xml")) {
                // Catch element starting with a P for xpath update mode
                String snippet = changes.get(ref + "_xml");

                if(Log.isDebugEnabled(Geonet.EDITOR)) {
                  Log.debug(Geonet.EDITOR, "Add element by XPath: " + value);
                  Log.debug(Geonet.EDITOR, "  Snippet is : " + snippet);
                }

                if (snippet != null && !"".equals(snippet)) {
                    xmlAndXpathInputs.put(value, new AddElemValue(snippet));
                } else {
                    Log.warning(Geonet.EDITOR, "No XML snippet or value found for xpath " + value + " and element ref " + ref);
                }
                continue;
            }

            if (updatedLocalizedTextElement(md, schema, ref, value, editLib)) {
                continue;
            }

            int at = ref.indexOf('_');
            if (at != -1) {
                attribute = ref.substring(at + 1);
                ref = ref.substring(0, at);
            }
           
            Element el = editLib.findElement(md, ref);
            if (el == null) {
                Log.error(Geonet.EDITOR, EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);
                continue;
            }
           
            // Process attribute
            if (attribute != null) {
                Pair<Namespace, String> attInfo = parseAttributeName(attribute, EditLib.COLON_SEPARATOR, id, md, editLib);
                String localname = attInfo.two();
                Namespace attrNS = attInfo.one();
                if (el.getAttribute(localname, attrNS) != null) {
                    el.setAttribute(new Attribute(localname, value, attrNS));
                }
            } else {
                // Process element value
                @SuppressWarnings("unchecked")
                List<Content> content = el.getContent();
               
                for (Iterator<Content> iterator = content.iterator(); iterator.hasNext();) {
                    Content content2 = iterator.next();
                    if (content2 instanceof Text) {
                        iterator.remove();
                    }
                }
                el.addContent(value);
            }
        }
       
        // Deals with XML fragments to insert or update
        if (!xmlInputs.isEmpty()) {
            editLib.addXMLFragments(schema, md, xmlInputs);
        }

        // Deals with XML fragments and XPath to insert or update
        if (!xmlAndXpathInputs.isEmpty()) {
            editLib.addElementOrFragmentFromXpaths(md, xmlAndXpathInputs, metadataSchema, true);
        }
       
        setMetadataIntoSession(session,(Element)md.clone(), id);
       
        // --- remove editing info
        editLib.removeEditingInfo(md);
        editLib.contractElements(md);
       
        return (Element) md.detach();
    }
View Full Code Here


    String  schema = dataManager.getMetadataSchema(id);
    //--- get metadata from session
    Element md = getMetadataFromSession(session, id);

    //--- ref is parent element so find it
    EditLib editLib = dataManager.getEditLib();
        Element el = editLib.findElement(md, ref);
    if (el == null)
      throw new IllegalStateException(EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);

    //--- locate the geonet:element and geonet:info elements and clone for
    //--- later re-use
    Element refEl = (Element)(el.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE)).clone();
    Element info = (Element)(md.getChild(Edit.RootChild.INFO,Edit.NAMESPACE)).clone();
    md.removeChild(Edit.RootChild.INFO,Edit.NAMESPACE);

    Element child = null;
    MetadataSchema mds = dataManager.getSchema(schema);
    if (childName != null) {
      if (childName.equals("geonet:attribute")) {
        String defaultValue = "";
        @SuppressWarnings("unchecked")
                List<Element> attributeDefs = el.getChildren(Edit.RootChild.ATTRIBUTE, Edit.NAMESPACE);
        for (Element attributeDef : attributeDefs) {
          if (attributeDef != null && attributeDef.getAttributeValue(Edit.Attribute.Attr.NAME).equals(name)) {
            Element defaultChild = attributeDef.getChild(Edit.Attribute.Child.DEFAULT, Edit.NAMESPACE);
            if (defaultChild != null) {
              defaultValue = defaultChild.getAttributeValue(Edit.Attribute.Attr.VALUE);
            }
          }
        }
       
        Pair<Namespace, String> attInfo = parseAttributeName(name, ":", id, md, editLib);
          //--- Add new attribute with default value
                el.setAttribute(new Attribute(attInfo.two(), defaultValue, attInfo.one()));
               
        // TODO : add attribute should be false and del true after adding an attribute
        child = el;
      } else {
        //--- normal element
        child = editLib.addElement(mds, el, name);
        if (!childName.equals(""))
        {
          //--- or element
          String uChildName = editLib.getUnqualifiedName(childName);
              String prefix     = editLib.getPrefix(childName);
              String ns         = editLib.getNamespace(childName,md,mds);
              if (prefix.equals("")) {
                 prefix = editLib.getPrefix(el.getName());
                 ns = editLib.getNamespace(el.getName(),md,mds);
              }
              Element orChild = new Element(uChildName,prefix,ns);
              child.addContent(orChild);

              //--- add mandatory sub-tags
              editLib.fillElement(schema, child, orChild);
        }
      }
    }
        else {
      child = editLib.addElement(mds, el, name);
    }
    //--- now enumerate the new child (if not a simple attribute)
    if (childName == null || !childName.equals("geonet:attribute")) {
      //--- now add the geonet:element back again to keep ref number
      el.addContent(refEl);

      int iRef = editLib.findMaximumRef(md);
      editLib.expandElements(schema, child);
      editLib.enumerateTreeStartingAt(child, iRef+1, Integer.parseInt(ref));

      //--- add editing info to everything from the parent down
      editLib.expandTree(mds,el);

    }
    //--- attach the info element to the child
    child.addContent(info);
View Full Code Here

    //--- locate the geonet:info element and clone for later re-use
    Element info = (Element)(md.getChild(Edit.RootChild.INFO,Edit.NAMESPACE)).clone();
    md.removeChild(Edit.RootChild.INFO,Edit.NAMESPACE);

    //--- get element to remove
        EditLib editLib = dataManager.getEditLib();
    Element el = editLib.findElement(md, ref);

    if (el == null)
      throw new IllegalStateException(EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);


    String uName = el.getName();
    Namespace ns = el.getNamespace();
    Element parent = el.getParentElement();
    Element result = null;
    if (parent != null) {
      int me = parent.indexOf(el);

      //--- check and see whether the element to be deleted is the last one of its kind
      Filter elFilter = new ElementFilter(uName,ns);
      if (parent.getContent(elFilter).size() == 1) {

        //--- get geonet child element with attribute name = unqualified name
        Filter chFilter = new ElementFilter(Edit.RootChild.CHILD, Edit.NAMESPACE);
        @SuppressWarnings("unchecked")
                List<Element> children = parent.getContent(chFilter);

        for (Element ch : children) {
          String name = ch.getAttributeValue("name");
          if (name != null && name.equals(uName)) {
            result = (Element) ch.clone();
            break;
          }
        }
       
        // -- now delete the element as requested
        parent.removeContent(me);
       
        //--- existing geonet child element not present so create it and insert it
        //--- where the last element was deleted
        if (result == null) {
          result = editLib.createElement(schema,el,parent);
          parent.addContent(me,result);
        }

        result.setAttribute(Edit.ChildElem.Attr.PARENT,parentRef);
        result.addContent(info);
View Full Code Here

    //--- get metadata from session
    Element md = getMetadataFromSession(session, id);

    //--- get element to remove
        EditLib editLib = dataManager.getEditLib();
    Element el = editLib.findElement(md, elementId);

    if (el != null) {
        Pair<Namespace, String> attInfo = parseAttributeName(attributeName, ":", id, md, editLib);
        el.removeAttribute(attInfo.two(), attInfo.one());
    }
View Full Code Here

    //--- get metadata from session
    Element md = getMetadataFromSession(session, id);

    //--- get element to swap
        EditLib editLib = dataManager.getEditLib();
    Element elSwap = editLib.findElement(md, ref);

    if (elSwap == null)
      throw new IllegalStateException(EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);

    //--- swap the elements
View Full Code Here

    //--- get metadata from session and clone it for validation
    Element realMd = getMetadataFromSession(session, id);
    Element md = (Element)realMd.clone();

    //--- remove editing info
        EditLib editLib = dataManager.getEditLib();
    editLib.removeEditingInfo(md);
    editLib.contractElements(md);
        String parentUuid = null;
        md = dataManager.updateFixedInfo(schema, Optional.of(Integer.valueOf(id)), null, md, parentUuid, UpdateDatestamp.NO, context);

    //--- do the validation on the metadata
    return dataManager.doValidate(session, schema, id, md, lang, false).one();
View Full Code Here

    //--- check if the metadata has been deleted
    if (md == null)
      return false;

    String schema = dataManager.getMetadataSchema(id);
        EditLib editLib = dataManager.getEditLib();
    editLib.expandElements(schema, md);
    editLib.enumerateTree(md);

    //--- check if the metadata has been modified from last time
    if (currVersion != null && !editLib.getVersion(id).equals(currVersion))
      return false;

    //--- get element to add
    Element el = editLib.findElement(md, ref);

    if (el == null)
      Log.error(Geonet.DATA_MANAGER, EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);
      //throw new IllegalStateException("Element not found at ref = " + ref);

    //--- remove editing info added by previous call
    editLib.removeEditingInfo(md);

        if (el != null) {
            el.setAttribute(new Attribute(name, ""));
        }

        editLib.contractElements(md);
        String parentUuid = null;
    md = dataManager.updateFixedInfo(schema, Optional.of(Integer.valueOf(id)), null, md, parentUuid, UpdateDatestamp.NO, context);
        String changeDate = null;
        xmlSerializer.update(id, md, changeDate, false, null, context);
View Full Code Here

    //--- check if the metadata has been deleted
    if (md == null)
      return false;

    String schema = dataManager.getMetadataSchema(id);
        EditLib editLib = dataManager.getEditLib();
    editLib.expandElements(schema, md);
    editLib.enumerateTree(md);

    //--- check if the metadata has been modified from last time
    if (currVersion != null && !editLib.getVersion(id).equals(currVersion))
      return false;

    //--- get element to remove
    Element el = editLib.findElement(md, ref);

    if (el == null)
      throw new IllegalStateException(EditLib.MSG_ELEMENT_NOT_FOUND_AT_REF + ref);

    //--- remove editing info added by previous call
    editLib.removeEditingInfo(md);

    el.removeAttribute(name);

    editLib.contractElements(md);
        String parentUuid = null;
        md = dataManager.updateFixedInfo(schema, Optional.of(Integer.valueOf(id)), null, md, parentUuid, UpdateDatestamp.NO, context);

        String changeDate = null;
        xmlSerializer.update(id, md, changeDate, false, null, context);
View Full Code Here

    }

    @Test
    public void testConformityString() throws Exception {
        final Element testMetadata = Xml.loadStream(AbstractInspireTest.class.getResourceAsStream(INSPIRE_VALID_ISO19139_XML));
        new EditLib(null).enumerateTree(testMetadata);
        Map<String, String> languageMap = new HashMap<String, String>();
        languageMap.put("De", "ger");
        languageMap.put("En", "eng");
        languageMap.put("Fr", "fre");
        languageMap.put("It", "ita");
View Full Code Here

TOP

Related Classes of org.fao.geonet.kernel.EditLib

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.