📄 datamanager.java
字号:
info.isHarvested = "y".equals(record.getChildText("isharvested")); info.createDate = record.getChildText("createdate"); info.changeDate = record.getChildText("changedate"); info.source = record.getChildText("source"); info.title = record.getChildText("title"); info.root = record.getChildText("root"); info.owner = record.getChildText("owner"); info.groupOwner = record.getChildText("groupowner"); String temp = record.getChildText("istemplate"); if ("y".equals(temp)) info.template = MdInfo.Template.TEMPLATE; else if ("s".equals(temp)) info.template = MdInfo.Template.SUBTEMPLATE; else info.template = MdInfo.Template.METADATA; return info; } //-------------------------------------------------------------------------- public String getVersion(String id) { return editLib.getVersion(id); } //-------------------------------------------------------------------------- public String getNewVersion(String id) { return editLib.getNewVersion(id); } //-------------------------------------------------------------------------- public void setTemplate(Dbms dbms, int id, String isTemplate, String title) throws Exception { if (title == null) dbms.execute("UPDATE Metadata SET isTemplate=? WHERE id=?", isTemplate, id); else dbms.execute("UPDATE Metadata SET isTemplate=?, title=? WHERE id=?", isTemplate, title, id); indexMetadata(dbms, Integer.toString(id)); } //-------------------------------------------------------------------------- public void setHarvested(Dbms dbms, int id, String harvestUuid) throws Exception { String value = (harvestUuid != null) ? "y" : "n"; String query = "UPDATE Metadata SET isHarvested=?, harvestUuid=? WHERE id=?"; dbms.execute(query, value, harvestUuid, id); indexMetadata(dbms, Integer.toString(id)); } //-------------------------------------------------------------------------- public void setHarvested(Dbms dbms, int id, String harvestUuid, String harvestUri) throws Exception { String value = (harvestUuid != null) ? "y" : "n"; String query = "UPDATE Metadata SET isHarvested=?, harvestUuid=?, harvestUri=? WHERE id=?"; dbms.execute(query, value, harvestUuid, harvestUri, id); indexMetadata(dbms, Integer.toString(id)); } //--------------------------------------------------------------------------- public String getSiteURL() { String host = settingMan.getValue("system/server/host"); String port = settingMan.getValue("system/server/port"); String locServ = baseURL +"/"+ Jeeves.Prefix.SERVICE +"/en"; return "http://" + host + (port == "80" ? "" : ":" + port) + locServ; } //-------------------------------------------------------------------------- public String autodetectSchema(Element md) { Namespace nons= Namespace.NO_NAMESPACE; Namespace gmd = Namespace.getNamespace("http://www.isotc211.org/2005/gmd"); if (md.getName().equals("MD_Metadata") && md.getNamespace().equals(gmd)) return "iso19139"; if (md.getNamespace().equals(nons)) { if (md.getName().equals("Metadata")) return "iso19115"; if (md.getName().equals("simpledc")) return "dublin-core"; if (md.getName().equals("metadata")) return "fgdc-std"; } return null; } //-------------------------------------------------------------------------- //--- //--- Metadata Insert API //--- //-------------------------------------------------------------------------- /** Create a new metadata duplicating an existing template */ public String createMetadata(Dbms dbms, String templateId, String groupOwner, SerialFactory sf, String source, int owner) throws Exception { String query = "SELECT schemaId, data FROM Metadata WHERE id="+ templateId; List listTempl = dbms.select(query).getChildren(); if (listTempl.size() == 0) throw new IllegalArgumentException("Template id not found : " + templateId); Element el = (Element) listTempl.get(0); String schema = el.getChildText("schemaid"); String data = el.getChildText("data"); String uuid = UUID.randomUUID().toString(); //--- generate a new metadata id int serial = sf.getSerial(dbms, "Metadata"); Element xml = updateFixedInfo(schema, Integer.toString(serial), Xml.loadString(data, false), uuid); //--- store metadata String id = XmlSerializer.insert(dbms, schema, xml, serial, source, uuid, owner, groupOwner); copyDefaultPrivForGroup(dbms, id, groupOwner); //--- store metadata categories copying them from the template List categList = dbms.select("SELECT categoryId FROM MetadataCateg WHERE metadataId = "+templateId).getChildren(); for(int i=0; i<categList.size(); i++) { Element elRec = (Element) categList.get(i); String catId = elRec.getChildText("categoryid"); setCategory(dbms, id, catId); } //--- index metadata and exit indexMetadata(dbms, id); return id; } //-------------------------------------------------------------------------- /** Adds a metadata in xml form (the xml should be validated). This method is * used to add a metadata got from a remote site. Note that neighter permissions * nor lucene indexes are updated. */ public String insertMetadataExt(Dbms dbms, String schema, Element md, SerialFactory sf, String source, String createDate, String changeDate, String uuid, int owner, String groupOwner) throws Exception { //--- generate a new metadata id int id = sf.getSerial(dbms, "Metadata"); return insertMetadataExt(dbms, schema, md, id, source, createDate, changeDate, uuid, owner, groupOwner); } //-------------------------------------------------------------------------- /** @param source the source of the metadata. If null, the local siteId will be used */ public String insertMetadataExt(Dbms dbms, String schema, Element md, int id, String source, String createDate, String changeDate, String uuid, int owner, String groupOwner) throws Exception { if (source == null) source = getSiteID(); //--- force namespace prefix for iso19139 metadata setNamespacePrefix(md); //--- Note: we cannot index metadata here. Indexing is done in the harvesting part return XmlSerializer.insert(dbms, schema, md, id, source, uuid, createDate, changeDate, "n", null, owner, groupOwner); } //-------------------------------------------------------------------------- /** Adds a metadata in xml form (the xml should be validated). The group id is * used to setup permissions. Internal metadata fields are updated. Default * operations are set. */ public String insertMetadata(Dbms dbms, String schema, String groupId, Element xml, SerialFactory sf, String source, String uuid, int owner) throws Exception { return insertMetadata(dbms, schema, groupId, xml, sf, source, uuid, "n", null, owner); } //-------------------------------------------------------------------------- public String insertMetadata(Dbms dbms, String schema, String groupOwner, Element xml, SerialFactory sf, String source, String uuid, String isTemplate, String title, int owner) throws Exception { //--- generate a new metadata id int serial = sf.getSerial(dbms, "Metadata"); if (isTemplate.equals("n")) xml = updateFixedInfo(schema, Integer.toString(serial), xml, uuid); //--- force namespace prefix for iso19139 metadata setNamespacePrefix(xml); //--- store metadata String id = XmlSerializer.insert(dbms, schema, xml, serial, source, uuid, isTemplate, title, owner, groupOwner); copyDefaultPrivForGroup(dbms, id, groupOwner); indexMetadata(dbms, id); return id; } //-------------------------------------------------------------------------- //--- //--- Metadata Get API //--- //-------------------------------------------------------------------------- /** Retrieves a metadata (in xml) given its id; adds editing information if needed */ 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); if (childName.startsWith("_s")) { // add subtemplate String sid = childName.substring(2); Element subtemplate = XmlSerializer.select(dbms, "Metadata", sid); el.addContent(subtemplate); } else { // normal element Element child = editLib.addElement(schema, el, name); if (!childName.equals("")) { // or element String uChildName = editLib.getUnqualifiedName(childName); String prefix = editLib.getPrefix(childName); String ns = editLib.getNamespace(childName,md); if (prefix.equals("")) { prefix = editLib.getPrefix(el.getName()); ns = editLib.getNamespace(el.getName(),md); } Element orChild = new Element(uChildName,prefix,ns); child.addContent(orChild); //--- add mandatory sub-tags editLib.fillElement(schema, child, 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -