⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datamanager.java.svn-base

📁 由国外的一个著名的geonetwork修改而来
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
//==============================================================================//===//=== DataManager//===//=============================================================================//===	Copyright (C) 2001-2005 Food and Agriculture Organization of the//===	United Nations (FAO-UN), United Nations World Food Programme (WFP)//===	and United Nations Environment Programme (UNEP)//===//===	This program is free software; you can redistribute it and/or modify//===	it under the terms of the GNU General Public License as published by//===	the Free Software Foundation; either version 2 of the License, or (at//===	your option) any later version.//===//===	This program is distributed in the hope that it will be useful, but//===	WITHOUT ANY WARRANTY; without even the implied warranty of//===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU//===	General Public License for more details.//===//===	You should have received a copy of the GNU General Public License//===	along with this program; if not, write to the Free Software//===	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//===//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,//===	Rome - Italy. email: GeoNetwork@fao.org//==============================================================================package org.fao.geonet.kernel;import java.util.Enumeration;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.UUID;import java.util.Vector;import jeeves.resources.dbms.Dbms;import jeeves.server.context.ServiceContext;import jeeves.utils.Log;import jeeves.utils.SerialFactory;import jeeves.utils.Xml;import org.fao.geonet.constants.Edit;import org.fao.geonet.constants.Geonet;import org.fao.geonet.kernel.schema.MetadataSchema;import org.fao.geonet.kernel.search.SearchManager;import org.fao.geonet.util.ISODate;import org.jdom.Attribute;import org.jdom.Element;import org.jdom.Text;//=============================================================================/** Handles all operations on metadata (select,insert,update,delete etc...)  */public class DataManager{	//--------------------------------------------------------------------------	//--- vars	private EditLib editLib = new EditLib(this);	private AccessManager accessMan;	private SearchManager searchMan;	// site information needed by updateFixedinfo	private String siteURL;	private String siteID;	//--------------------------------------------------------------------------	//---	//--- Constructor	//---	//--------------------------------------------------------------------------	/** initializes the search manager and index not-indexed metadata	  */	public DataManager(SearchManager sm, AccessManager am, Dbms dbms, String su, String si) throws Exception	{		searchMan = sm;		accessMan = am;		siteURL   = su;		siteID    = si;		// get all metadata from DB		Element result = dbms.select("SELECT id, changeDate FROM Metadata ORDER BY id ASC");		List list = result.getChildren();		// System.out.println("DB CONTENT:\n" + Xml.getString(result)); // DEBUG		// get all metadata from index		Hashtable docs = searchMan.getDocs();		// System.out.println("INDEX CONTENT:"); // DEBUG		// index all metadata in DBMS if needed		for(int i = 0; i < list.size(); i++)		{			// get metadata			Element record = (Element) list.get(i);			String  id     = record.getChildText("id");			// System.out.println("- record (" + id + ")"); // DEBUG			Hashtable idxRec = (Hashtable)docs.get(id);			// if metadata is not indexed index it			if (idxRec == null)				indexMetadata(dbms, id);			// else, if indexed version is not the latest index it			else			{				docs.remove(id);				String lastChange = record.getChildText("changedate");				String idxLastChange = (String)idxRec.get("_changeDate");				// System.out.println("  - lastChange: " + lastChange); // DEBUG				// System.out.println("  - idxLastChange: " + idxLastChange); // DEBUG				if (!idxLastChange.equalsIgnoreCase(lastChange)) // date in index contains 't', date in DBMS contains 'T'					indexMetadata(dbms, id);			}		}		// System.out.println("INDEX SURPLUS:"); // DEBUG		// remove from index metadata not in DBMS		for (Enumeration i = docs.keys(); i.hasMoreElements(); )		{			String id = (String)i.nextElement();			searchMan.delete("_id", id);			// System.out.println("- record (" + id + ")"); // DEBUG		}	}	//--------------------------------------------------------------------------	public void indexMetadata(Dbms dbms, String id) throws Exception	{		Vector moreFields = new Vector();		// get metadata table fields		Element md         = XmlSerializer.select(dbms, "Metadata", id);		String  root       = md.getName();		String query ="SELECT schemaId, createDate, changeDate, source, isTemplate, uuid, "+									"isHarvested FROM Metadata WHERE id = " + id;		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");		moreFields.add(makeField("_root",        root,        true, true, false));		moreFields.add(makeField("_schema",      schema,      true, true, false));		moreFields.add(makeField("_createDate",  createDate,  true, true, false));		moreFields.add(makeField("_changeDate",  changeDate,  true, true, false));		moreFields.add(makeField("_source",      source,      true, true, false));		moreFields.add(makeField("_isTemplate",  isTemplate,  true, true, false));		moreFields.add(makeField("_uuid",        uuid,        true, true, false));		moreFields.add(makeField("_isHarvested", isHarvested, true, true, false));		// get privileges		List operations = dbms.select("SELECT groupId, operationId FROM OperationAllowed "+												"WHERE metadataId = " + id + " ORDER BY operationId ASC").getChildren();		for (Iterator iter = operations.iterator(); iter.hasNext(); )		{			Element operation   = (Element)iter.next();			String  groupId     = operation.getChildText("groupid");			String  operationId = operation.getChildText("operationid");			moreFields.add(makeField("_op" + operationId, groupId, true, true, false));		}		// get 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();			String  categoryName = category.getChildText("name");			moreFields.add(makeField("_cat", categoryName, true, true, false));		}		searchMan.index(schema, md, id, moreFields);	}	//--------------------------------------------------------------------------	private Element makeField(String name, String value, boolean store, boolean index, boolean token)	{		Element field = new Element("Field");		field.setAttribute("name",   name);		field.setAttribute("string", value);		field.setAttribute("store",  store+"");		field.setAttribute("index",  index+"");		field.setAttribute("token",  token+"");		return field;	}	//--------------------------------------------------------------------------	//---	//--- Schema management API	//---	//--------------------------------------------------------------------------	public void addSchema(String id, String xmlSchemaFile, String xmlSuggestFile) throws Exception	{		editLib.addSchema(id, xmlSchemaFile, xmlSuggestFile);	}	//--------------------------------------------------------------------------	public MetadataSchema getSchema(String name)	{		return editLib.getSchema(name);	}	//--------------------------------------------------------------------------	public Iterator getSchemas()	{		return editLib.getSchemas();	}	//--------------------------------------------------------------------------	public boolean existsSchema(String name)	{		return editLib.existsSchema(name);	}	//--------------------------------------------------------------------------	public String getSchemaDir(String name)	{		return editLib.getSchemaDir(name);	}	//--------------------------------------------------------------------------	public void validate(String schema, Element md) throws Exception	{		Xml.validate(md, null, editLib.getSchemaDir(schema) + Geonet.File.SCHEMA);	}	//--------------------------------------------------------------------------	//---	//--- General purpose API	//---	//--------------------------------------------------------------------------	public String extractUUID(String schema, Element md) throws Exception	{		String styleSheet = editLib.getSchemaDir(schema) + Geonet.File.EXTRACT_UUID;		String uuid       = Xml.transform(md, styleSheet).getText().trim();		Log.debug(Geonet.DATA_MANAGER, "Extracted UUID '"+ uuid +"' for schema '"+ schema +"'");		//--- needed to detach md from the document		md.detach();		return uuid;	}	//--------------------------------------------------------------------------	public String getVersion(String id)	{		return editLib.getVersion(id);	}	//--------------------------------------------------------------------------	public String getNewVersion(String id)	{		return editLib.getNewVersion(id);	}	//--------------------------------------------------------------------------	public void setTemplateBit(Dbms dbms, String id, boolean yesno) throws Exception	{		String value = (yesno) ? "y" : "n";		dbms.execute("UPDATE Metadata SET isTemplate='"+value+"' WHERE id="+id);		indexMetadata(dbms, id);	}	//--------------------------------------------------------------------------	public String getSiteURL()	{		return siteURL;	}	//--------------------------------------------------------------------------	//---	//--- Metadata Insert API	//---	//--------------------------------------------------------------------------	/** Create a new metadata duplicating an existing template	  */	public String createMetadata(Dbms dbms, String templateId, String groupId,										  SerialFactory sf, String source) 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, source);		//--- store metadata		String id = XmlSerializer.insert(dbms, schema, xml, serial, source, uuid);		copyDefaultPrivForGroup(dbms, id, groupId);		//--- 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, String sourceUri) throws Exception	{		//--- generate a new metadata id		int id = sf.getSerial(dbms, "Metadata");		return insertMetadataExt(dbms, schema, md, id, source, createDate, changeDate, uuid, sourceUri);	}	//--------------------------------------------------------------------------	public String insertMetadataExt(Dbms dbms, String schema, Element md, int id,											  String source, String createDate, String changeDate,											  String uuid, String sourceUri) throws Exception	{		//--- Note: we cannot index metadata here. Indexing is done in the harvesting part		//---       (MetadataSync)		return XmlSerializer.insert(dbms, schema, md, id, source, uuid, createDate,											 changeDate, sourceUri);	}	//--------------------------------------------------------------------------	/** 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) throws Exception	{		//--- generate a new metadata id		int serial = sf.getSerial(dbms, "Metadata");		xml = updateFixedInfo(schema, Integer.toString(serial), xml, uuid, source);		//--- store metadata		String id = XmlSerializer.insert(dbms, schema, xml, serial, source, uuid);		copyDefaultPrivForGroup(dbms, id, groupId);		indexMetadata(dbms, id);		return id;	}	//--------------------------------------------------------------------------	//---	//--- Metadata Get API	//---	//--------------------------------------------------------------------------	/** Retrieves a metadata (in xml) given its id; adds editing information if needed

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -