📄 datamanager.java.svn-base
字号:
return true; } //-------------------------------------------------------------------------- /** Remove all operations stored for a metadata */ public void deleteAllMetadataOper(Dbms dbms, String id) throws Exception { String query = "DELETE FROM OperationAllowed WHERE metadataId=?"; Vector args = new Vector(); args.add(new Integer(id)); dbms.execute(query, args); } //-------------------------------------------------------------------------- /** Remove all categories stored for a metadata */ public void deleteAllMetadataCateg(Dbms dbms, String id) throws Exception { String query = "DELETE FROM MetadataCateg WHERE metadataId=?"; Vector args = new Vector(); args.add(new Integer(id)); dbms.execute(query, args); } //-------------------------------------------------------------------------- //--- //--- Metadata thumbnail API //--- //-------------------------------------------------------------------------- public Element getThumbnails(Dbms dbms, String id) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); if (md == null) return null; md.detach(); String schema = getMetadataSchema(dbms, id); //--- do an XSL transformation String styleSheet = editLib.getSchemaDir(schema) + Geonet.File.EXTRACT_THUMBNAILS; Element result = Xml.transform(md, styleSheet); result.addContent(new Element("id").setText(id)); return result; } //-------------------------------------------------------------------------- public void setThumbnail(Dbms dbms, String id, boolean small, String file) throws Exception { int pos = file.lastIndexOf("."); String ext = (pos == -1) ? "???" : file.substring(pos +1); Element env = new Element("env"); env.addContent(new Element("file").setText(file)); env.addContent(new Element("ext").setText(ext)); manageThumbnail(dbms, id, small, env, Geonet.File.SET_THUMBNAIL); } //-------------------------------------------------------------------------- public void unsetThumbnail(Dbms dbms, String id, boolean small) throws Exception { Element env = new Element("env"); manageThumbnail(dbms, id, small, env, Geonet.File.UNSET_THUMBNAIL); } //-------------------------------------------------------------------------- private void manageThumbnail(Dbms dbms, String id, boolean small, Element env, String styleSheet) throws Exception { Element md = XmlSerializer.select(dbms, "Metadata", id); if (md == null) return; md.detach(); String schema = getMetadataSchema(dbms, id); //----------------------------------------------------------------------- //--- remove thumbnail from metadata //--- setup environment String type = small ? "thumbnail" : "large_thumbnail"; env.addContent(new Element("type").setText(type)); //--- setup root element Element root = new Element("root"); root.addContent(md); root.addContent(env); //--- do an XSL transformation styleSheet = editLib.getSchemaDir(schema) + styleSheet; md = Xml.transform(root, styleSheet); XmlSerializer.update(dbms, id, md); //--- update search criteria indexMetadata(dbms, id); } //-------------------------------------------------------------------------- //--- //--- Privileges API //--- //-------------------------------------------------------------------------- /** Adds a permission to a group */ public void setOperation(Dbms dbms, String mdId, String grpId, String opId) throws Exception { Vector vArgs = new Vector(); vArgs.add(new Integer(mdId)); vArgs.add(new Integer(grpId)); vArgs.add(new Integer(opId)); Element elRes = dbms.select("SELECT metadataId FROM OperationAllowed " + "WHERE metadataId=? AND groupId=? AND operationId=?", vArgs); if (elRes.getChildren().size() == 0) dbms.execute("INSERT INTO OperationAllowed(metadataId, groupId, operationId) " + "VALUES(?,?,?)", vArgs); } //-------------------------------------------------------------------------- //--- //--- Categories API //--- //-------------------------------------------------------------------------- /** Adds a category to a metadata */ public void setCategory(Dbms dbms, String mdId, String categId) throws Exception { Vector vArgs = new Vector(); vArgs.add(new Integer(mdId)); vArgs.add(new Integer(categId)); Element elRes = dbms.select("SELECT metadataId FROM MetadataCateg " + "WHERE metadataId=? AND categoryId=?", vArgs); if (elRes.getChildren().size() == 0) dbms.execute("INSERT INTO MetadataCateg(metadataId, categoryId) VALUES(?,?)", vArgs); } //-------------------------------------------------------------------------- public boolean isCategorySet(Dbms dbms, String mdId, String categId) throws Exception { Vector vArgs = new Vector(); vArgs.add(new Integer(mdId)); vArgs.add(new Integer(categId)); Element elRes = dbms.select("SELECT metadataId FROM MetadataCateg " + "WHERE metadataId=? AND categoryId=?", vArgs); return (elRes.getChildren().size() != 0); } //-------------------------------------------------------------------------- public void unsetCategory(Dbms dbms, String mdId, String categId) throws Exception { Vector vArgs = new Vector(); vArgs.add(new Integer(mdId)); vArgs.add(new Integer(categId)); dbms.execute("DELETE FROM MetadataCateg WHERE metadataId=? AND categoryId=?", vArgs); } //-------------------------------------------------------------------------- public Element getCategories(Dbms dbms, String mdId) throws Exception { Vector vArgs = new Vector(); vArgs.add(new Integer(mdId)); return dbms.select("SELECT id, name "+ "FROM Categories, MetadataCateg "+ "WHERE id=categoryId AND metadataId=?", vArgs); } //-------------------------------------------------------------------------- //--- //--- Private methods //--- //-------------------------------------------------------------------------- /** Used for editing : swaps children of 2 tags */ private void swapElements(Element el1, Element el2) { if (el1.getChildren().size() != 0) { Vector v1 = collectElements(el1); Vector v2 = collectElements(el2); addElements(el1, v2); addElements(el2, v1); } else { //--- swap text String sValue1 = el1.getText(); String sValue2 = el2.getText(); el1.setText(sValue2); el2.setText(sValue1); } } //-------------------------------------------------------------------------- /** Collects all children of a tag, removing them from the parent */ private Vector collectElements(Element el) { Vector v = new Vector(); List list = el.getChildren(); for(int i=0; i<list.size(); i++) v.add(list.get(i)); el.removeContent(); return v; } //-------------------------------------------------------------------------- /** Add all tags in a vector to a tag */ private void addElements(Element el, Vector v) { for(int i=0; i<v.size(); i++) el.addContent((Element) v.get(i)); } //-------------------------------------------------------------------------- private Element updateFixedInfo(String schema, String id, Element md, Dbms dbms) throws Exception { Element rec = dbms.select("SELECT uuid, source FROM Metadata WHERE id = " + id).getChild("record"); String uuid = rec.getChildText("uuid"); String source = rec.getChildText("source"); return updateFixedInfo(schema, id, md,uuid, source); } //-------------------------------------------------------------------------- public Element updateFixedInfo(String schema, String id, Element md, String uuid, String source) throws Exception { //--- setup environment Element env = new Element("env"); env.addContent(new Element("id") .setText(id)); env.addContent(new Element("uuid") .setText(uuid)); env.addContent(new Element("currDate").setText(new ISODate().toString())); env.addContent(new Element("siteURL") .setText(siteURL)); env.addContent(new Element("siteID") .setText(siteID)); env.addContent(new Element("source") .setText(source)); //--- setup root element Element root = new Element("root"); root.addContent(md); root.addContent(env); //--- do an XSL transformation String styleSheet = editLib.getSchemaDir(schema) + Geonet.File.UPDATE_FIXED_INFO; return Xml.transform(root, styleSheet); } //-------------------------------------------------------------------------- private Element buildInfoElem(ServiceContext srvContext, String id, String version) throws Exception { Dbms dbms = (Dbms) srvContext.getResourceManager().open(Geonet.Res.MAIN_DB); String query ="SELECT schemaId, createDate, changeDate, source, isTemplate, "+ "uuid, isHarvested FROM Metadata WHERE id = " + id; // add Metadata table infos: schemaId, createDate, changeDate, source, Element rec = dbms.select(query).getChild("record"); String schema = rec.getChildText("schemaid"); String createDate = rec.getChildText("createdate"); String changeDate = rec.getChildText("changedate"); String source = rec.getChildText("source"); String isTemplate = rec.getChildText("istemplate"); String uuid = rec.getChildText("uuid"); String isHarvested= rec.getChildText("isharvested"); Element info = new Element(Edit.RootChild.INFO, Edit.NAMESPACE); addElement(info, Edit.Info.Elem.ID, id); addElement(info, Edit.Info.Elem.SCHEMA, schema); addElement(info, Edit.Info.Elem.CREATE_DATE, createDate); addElement(info, Edit.Info.Elem.CHANGE_DATE, changeDate); addElement(info, Edit.Info.Elem.IS_TEMPLATE, isTemplate); addElement(info, Edit.Info.Elem.SOURCE, source); addElement(info, Edit.Info.Elem.UUID, uuid); addElement(info, Edit.Info.Elem.IS_HARVESTED,isHarvested); if (version != null) addElement(info, Edit.Info.Elem.VERSION, version); // add operations HashSet hsOper = accessMan.getOperations(srvContext, id, srvContext.getIpAddress()); addElement(info, Edit.Info.Elem.VIEW, String.valueOf(hsOper.contains(AccessManager.OPER_VIEW))); addElement(info, Edit.Info.Elem.ADMIN, String.valueOf(hsOper.contains(AccessManager.OPER_ADMIN))); addElement(info, Edit.Info.Elem.EDIT, String.valueOf(hsOper.contains(AccessManager.OPER_EDIT))); addElement(info, Edit.Info.Elem.NOTIFY, String.valueOf(hsOper.contains(AccessManager.OPER_NOTIFY))); addElement(info, Edit.Info.Elem.DOWNLOAD, String.valueOf(hsOper.contains(AccessManager.OPER_DOWNLOAD))); addElement(info, Edit.Info.Elem.DYNAMIC, String.valueOf(hsOper.contains(AccessManager.OPER_DYNAMIC))); addElement(info, Edit.Info.Elem.FEATURED, String.valueOf(hsOper.contains(AccessManager.OPER_FEATURED))); // add categories List categories = dbms.select("SELECT id, name FROM MetadataCateg, Categories "+ "WHERE metadataId = " + id + " AND categoryId = id ORDER BY id").getChildren(); for (Iterator iter = categories.iterator(); iter.hasNext(); ) { Element category = (Element)iter.next(); addElement(info, Edit.Info.Elem.CATEGORY, category.getChildText("name")); } return info; } //-------------------------------------------------------------------------- private static void addElement(Element root, String name, String value) { root.addContent(new Element(name).setText(value)); } //-------------------------------------------------------------------------- private String getMetadataSchema(Dbms dbms, String id) throws Exception { List list = dbms.select("SELECT schemaId FROM Metadata WHERE id = " +id).getChildren(); if (list.size() == 0) throw new IllegalArgumentException("Metadata not found for id : " +id); else { // get metadata Element record = (Element) list.get(0); return record.getChildText("schemaid"); } } //-------------------------------------------------------------------------- private void copyDefaultPrivForGroup(Dbms dbms, String id, String groupId) throws Exception { //--- store access operations for group setOperation(dbms, id, groupId, AccessManager.OPER_VIEW); setOperation(dbms, id, groupId, AccessManager.OPER_DOWNLOAD); setOperation(dbms, id, groupId, AccessManager.OPER_EDIT); setOperation(dbms, id, groupId, AccessManager.OPER_NOTIFY); setOperation(dbms, id, groupId, AccessManager.OPER_ADMIN); setOperation(dbms, id, groupId, AccessManager.OPER_DYNAMIC);// setOperation(dbms, id, groupId, AccessManager.OPER_FEATURED); //--- store default operations// List listDef = dbms.select("SELECT groupId, operationId FROM DefaultOper "+// "WHERE groupId > 1 AND groupId <> "+groupId).getChildren();//// for(int i=0; i<listDef.size(); i++)// {// Element elRec = (Element) listDef.get(i);//// String sGrp = elRec.getChildText("groupid");// String sOper = elRec.getChildText("operationid");//// setOperation(dbms, id, sGrp, sOper);// } }}//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -