📄 datamanager.java.svn-base
字号:
*/ public Element getMetadata(ServiceContext srvContext, String id, boolean forEditing) throws Exception { Dbms dbms = (Dbms) srvContext.getResourceManager().open(Geonet.Res.MAIN_DB); Element md = XmlSerializer.select(dbms, "Metadata", id); if (md == null) return null; String version = null; if (forEditing) { String schema = getMetadataSchema(dbms, id); version = editLib.addEditingInfo(schema, id, md); } md.addNamespaceDeclaration(Edit.NAMESPACE); Element info = buildInfoElem(srvContext, id, version); md.addContent(info); md.detach(); return md; } //-------------------------------------------------------------------------- /** Retrieves a metadata element given it's ref */ public Element getElementByRef(Element md, String ref) { return editLib.findElement(md, ref); } //-------------------------------------------------------------------------- /** Returns true if the metadata exists in the database */ public boolean existsMetadata(Dbms dbms, String id) throws Exception { //FIXME : should use lucene List list = dbms.select("SELECT id FROM Metadata WHERE id="+ id).getChildren(); return list.size() != 0; } //-------------------------------------------------------------------------- /** Returns all the keywords in the system */ public Element getKeywords() throws Exception { Vector keywords = searchMan.getTerms("keyword"); Element el = new Element("keywords"); for(int i=0; i<keywords.size(); i++) el.addContent(new Element("keyword").setText((String)keywords.get(i))); return el; } //-------------------------------------------------------------------------- //--- //--- Metadata Update API //--- //-------------------------------------------------------------------------- /** For Editing : adds an element to a metadata ([add] link) */ public synchronized boolean addElement(Dbms dbms, String id, String ref, String name, String childName, String currVersion) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; 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) throw new IllegalStateException("Element not found at ref = " + ref); //--- remove editing info added by previous call editLib.removeEditingInfo(md); String schema = getMetadataSchema(dbms, id); Element child = editLib.addElement(schema, el, name); if (!childName.equals("")) { Element orChild = new Element(childName, el.getNamespace()); child.addContent(orChild); //--- add mandatory sub-tags editLib.fillElement(schema, orChild); } md = updateFixedInfo(schema, id, md, dbms); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); return true; } //-------------------------------------------------------------------------- public synchronized boolean addAttribute(Dbms dbms, String id, String ref, String name, String currVersion) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; 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) throw new IllegalStateException("Element not found at ref = " + ref); //--- remove editing info added by previous call editLib.removeEditingInfo(md); el.setAttribute(new Attribute(name, "")); String schema = getMetadataSchema(dbms, id); md = updateFixedInfo(schema, id, md, dbms); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); return true; } //-------------------------------------------------------------------------- /** For Editing : removes an element from a metadata ([del] link) */ public synchronized boolean deleteElement(Dbms dbms, String id, String ref, String currVersion) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; 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("Element not found at ref = " + ref); el.detach(); //--- remove editing info added by previous call editLib.removeEditingInfo(md); String schema = getMetadataSchema(dbms, id); md = updateFixedInfo(schema, id, md, dbms); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); return true; } //-------------------------------------------------------------------------- /** For Editing : removes an attribute from a metadata ([del] link) */ public synchronized boolean deleteAttribute(Dbms dbms, String id, String ref, String name, String currVersion) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; 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("Element not found at ref = " + ref); //--- remove editing info added by previous call editLib.removeEditingInfo(md); el.removeAttribute(name); String schema = getMetadataSchema(dbms, id); md = updateFixedInfo(schema, id, md, dbms); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); return true; } //-------------------------------------------------------------------------- /** For Editing : swap a tag with one of its sibling ([up] and [down] links) */ public synchronized boolean swapElement(Dbms dbms, String id, String ref, String currVersion, boolean down) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; 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 swap Element elSwap = editLib.findElement(md, ref); if (elSwap == null) throw new IllegalStateException("Element not found at ref = " + ref); //--- remove editing info added by previous call editLib.removeEditingInfo(md); //-------------------------------------------------------------------- //--- swap elements int iSwapIndex = -1; List list = ((Element) elSwap.getParent()).getChildren(elSwap.getName()); for(int i=0; i<list.size(); i++) if (list.get(i) == elSwap) { iSwapIndex = i; break; } if (iSwapIndex == -1) throw new IllegalStateException("Index not found for element --> " + elSwap); if (down) swapElements(elSwap, (Element) list.get(iSwapIndex +1)); else swapElements(elSwap, (Element) list.get(iSwapIndex -1)); String schema = getMetadataSchema(dbms, id); md = updateFixedInfo(schema, id, md, dbms); XmlSerializer.update(dbms, id, md); return true; } //-------------------------------------------------------------------------- /** For Editing : updates all leaves with new values */ public synchronized boolean updateMetadata(Dbms dbms, String id, String currVersion, Hashtable changes, boolean validate) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); //--- check if the metadata has been deleted if (md == null) return false; editLib.enumerateTree(md); //--- check if the metadata has been modified from last time if (currVersion != null && !editLib.getVersion(id).equals(currVersion)) return false; //-------------------------------------------------------------------- //--- update elements for(Enumeration e=changes.keys(); e.hasMoreElements();) { String ref = ((String) e.nextElement()) .trim(); String val = ((String) changes.get(ref)).trim(); String attr= null; int at = ref.indexOf("_"); if (at != -1) { attr = ref.substring(at +1); ref = ref.substring(0, at); } Element el = editLib.findElement(md, ref); if (el == null) throw new IllegalStateException("Element not found at ref = " + ref); if (attr != null) { if (el.getAttribute(attr) != null) el.setAttribute(new Attribute(attr, val)); } else { List content = el.getContent(); for(int i=0; i<content.size(); i++) { if (content.get(i) instanceof Text) { el.removeContent((Text) content.get(i)); i--; } } el.addContent(val); } } //--- remove editing info added by previous call editLib.removeEditingInfo(md); return updateMetadata(dbms, id, md, validate, currVersion); } //-------------------------------------------------------------------------- public synchronized boolean updateMetadata(Dbms dbms, String id, Element md, boolean validate, String version) throws Exception { //--- check if the metadata has been modified from last time if (version != null && !editLib.getVersion(id).equals(version)) return false; String schema = getMetadataSchema(dbms, id); md = updateFixedInfo(schema, id, md, dbms); if (validate) Xml.validate(md, null, editLib.getSchemaDir(schema) + Geonet.File.SCHEMA); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); return true; } //-------------------------------------------------------------------------- //--- Used by the harvesting procedure public void updateMetadataExt(Dbms dbms, String id, Element md, String changeDate) throws Exception { XmlSerializer.update(dbms, id, md, changeDate); } //-------------------------------------------------------------------------- //--- //--- Metadata Delete API //--- //-------------------------------------------------------------------------- /** Removes a metadata */ public synchronized boolean deleteMetadata(Dbms dbms, String id) throws Exception { if (!existsMetadata(dbms, id)) return false; //--- remove operations deleteAllMetadataOper(dbms, id); //--- remove categories deleteAllMetadataCateg(dbms, id); //--- remove metadata XmlSerializer.delete(dbms, "Metadata", id); //--- update search criteria searchMan.delete("_id", id+"");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -