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

📄 aligner.java

📁 联合国农粮署牵头开发的geonetwork源代码最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//=============================================================================//===	Copyright (C) 2001-2007 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA//===//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,//===	Rome - Italy. email: geonetwork@osgeo.org//==============================================================================package org.fao.geonet.kernel.harvest.harvester.geonet;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import jeeves.interfaces.Logger;import jeeves.resources.dbms.Dbms;import jeeves.server.context.ServiceContext;import jeeves.utils.BinaryFile;import jeeves.utils.XmlRequest;import org.fao.geonet.GeonetContext;import org.fao.geonet.constants.Geonet;import org.fao.geonet.kernel.DataManager;import org.fao.geonet.kernel.harvest.harvester.CategoryMapper;import org.fao.geonet.kernel.harvest.harvester.GroupMapper;import org.fao.geonet.kernel.harvest.harvester.RecordInfo;import org.fao.geonet.kernel.harvest.harvester.UUIDMapper;import org.fao.geonet.kernel.mef.MEFLib;import org.fao.geonet.kernel.mef.MEFVisitor;import org.fao.geonet.lib.Lib;import org.fao.geonet.util.ISODate;import org.jdom.Element;//=============================================================================public class Aligner{	//--------------------------------------------------------------------------	//---	//--- Constructor	//---	//--------------------------------------------------------------------------	public Aligner(Logger log, ServiceContext context, Dbms dbms, XmlRequest req,						GeonetParams params, Element remoteInfo)	{		this.log     = log;		this.context = context;		this.dbms    = dbms;		this.request = req;		this.params  = params;		GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);		dataMan = gc.getDataManager();		result  = new GeonetResult();		//--- save remote categories and groups into hashmaps for a fast access		List list = remoteInfo.getChild("groups").getChildren("group");		setupLocEntity(list, hmRemoteGroups);	}	//--------------------------------------------------------------------------	private void setupLocEntity(List list, HashMap<String, HashMap<String, String>> hmEntity)	{		for (int i=0; i<list.size(); i++)		{			Element entity= (Element) list.get(i);			String  name  = entity.getChildText("name");			HashMap<String, String> hm = new HashMap<String, String>();			hmEntity.put(name, hm);			List labels = entity.getChild("label").getChildren();			for (int j=0; j<labels.size(); j++)			{				Element el = (Element) labels.get(j);				hm.put(el.getName(), el.getText());			}		}	}	//--------------------------------------------------------------------------	//---	//--- Alignment method	//---	//--------------------------------------------------------------------------	public GeonetResult align(Set<RecordInfo> records) throws Exception	{		log.info("Start of alignment for : "+ params.name);		//-----------------------------------------------------------------------		//--- retrieve all local categories and groups		//--- retrieve harvested uuids for given harvesting node		localCateg = new CategoryMapper(dbms);		localGroups= new GroupMapper(dbms);		localUuids = new UUIDMapper(dbms, params.uuid);		dbms.commit();		//-----------------------------------------------------------------------		//--- remove old metadata		for (String uuid : localUuids.getUUIDs())			if (!exists(records, uuid))			{				String id = localUuids.getID(uuid);				log.debug("  - Removing old metadata with id:"+ id);				dataMan.deleteMetadata(dbms, id);				dbms.commit();				result.locallyRemoved++;			}		//-----------------------------------------------------------------------		//--- insert/update new metadata		for(RecordInfo ri : records)		{			result.totalMetadata++;			if (!dataMan.existsSchema(ri.schema))			{				log.debug("  - Metadata skipped due to unknown schema. uuid:"+ ri.uuid							 +", schema:"+ ri.schema);				result.unknownSchema++;			}			else			{				String id = dataMan.getMetadataId(dbms, ri.uuid);				if (id == null)	addMetadata(ri);					else				updateMetadata(ri, id);			}		}		log.info("End of alignment for : "+ params.name);		return result;	}	//--------------------------------------------------------------------------	//---	//--- Private methods : addMetadata	//---	//--------------------------------------------------------------------------	private void addMetadata(final RecordInfo ri) throws Exception	{		final String  id[] = { null };		final Element md[] = { null };		//--- import metadata from MEF file		File mefFile = retrieveMEF(ri.uuid);		try		{			MEFLib.visit(mefFile, new MEFVisitor()			{				public void handleMetadata(Element mdata) throws Exception				{					md[0] = mdata;				}				//--------------------------------------------------------------------				public void handleInfo(Element info) throws Exception				{					id[0] = addMetadata(ri, md[0], info);				}				//--------------------------------------------------------------------				public void handlePublicFile(String file, String changeDate, InputStream is) throws IOException				{					log.debug("    - Adding remote public file with name:"+ file);					String pubDir = Lib.resource.getDir(context, "public", id[0]);					File outFile = new File(pubDir, file);					FileOutputStream os = new FileOutputStream(outFile);					BinaryFile.copy(is, os, false, true);					outFile.setLastModified(new ISODate(changeDate).getSeconds() * 1000);				}				//--------------------------------------------------------------------				public void handlePrivateFile(String file, String changeDate, InputStream is) {}			});		}		catch(Exception e)		{			//--- we ignore the exception here. Maybe the metadata has been removed just now			log.debug("  - Skipped unretrievable metadata (maybe has been removed) with uuid:"+ ri.uuid);			result.unretrievable++;			e.printStackTrace();		}		finally		{			mefFile.delete();		}	}	//--------------------------------------------------------------------------	private String addMetadata(RecordInfo ri, Element md, Element info) throws Exception	{		Element general = info.getChild("general");		String createDate = general.getChildText("createDate");		String changeDate = general.getChildText("changeDate");		String isTemplate = general.getChildText("isTemplate");		String siteId     = general.getChildText("siteId");		if ("true".equals(isTemplate))	isTemplate = "y";			else 									isTemplate = "n";		log.debug("  - Adding metadata with remote uuid:"+ ri.uuid);		String id = dataMan.insertMetadataExt(dbms, ri.schema, md, context.getSerialFactory(),													 siteId, createDate, changeDate, ri.uuid, 1, null);		int iId = Integer.parseInt(id);		dataMan.setTemplate(dbms, iId, isTemplate, null);		dataMan.setHarvested(dbms, iId, params.uuid);		String pubDir = Lib.resource.getDir(context, "public",  id);		String priDir = Lib.resource.getDir(context, "private", id);		new File(pubDir).mkdirs();		new File(priDir).mkdirs();		addCategories(id);		addPrivileges(id, info.getChild("privileges"));		dbms.commit();		dataMan.indexMetadata(dbms, id);		result.addedMetadata++;		return id;	}	//--------------------------------------------------------------------------	//--- Categories	//--------------------------------------------------------------------------	private void addCategories(String id) throws Exception	{		for(String catId : params.getCategories())		{			String name = localCateg.getName(catId);			if (name == null)				log.debug("    - Skipping removed category with id:"+ catId);			else			{				log.debug("    - Setting category : "+ name);				dataMan.setCategory(dbms, id, catId);			}		}	}	//--------------------------------------------------------------------------	//--- Privileges	//--------------------------------------------------------------------------	private void addPrivileges(String id, Element privil) throws Exception	{		Map<String, Set<String>> groupOper = buildPrivileges(privil);		for (Group remoteGroup : params.getGroupCopyPolicy())		{			//--- get operations allowed to remote group			Set<String> oper = groupOper.get(remoteGroup.name);			//--- if we don't find any match, maybe the remote group has been removed			if (oper == null)				log.info("    - Remote group has been removed or no privileges exist : "+ remoteGroup.name);			else			{				String localGrpId = localGroups.getID(remoteGroup.name);				if (localGrpId == null)				{					//--- group does not exist locally					if (remoteGroup.policy == Group.CopyPolicy.CREATE_AND_COPY)					{						log.debug("    - Creating local group : "+ remoteGroup.name);						localGrpId = createGroup(remoteGroup.name);

⌨️ 快捷键说明

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