📄 aligner.java
字号:
//=============================================================================//=== 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.csw;import java.util.List;import java.util.Set;import jeeves.interfaces.Logger;import jeeves.resources.dbms.Dbms;import jeeves.server.context.ServiceContext;import jeeves.utils.Xml;import org.fao.geonet.GeonetContext;import org.fao.geonet.constants.Geonet;import org.fao.geonet.csw.common.Csw.ElementSetName;import org.fao.geonet.csw.common.requests.CatalogRequest;import org.fao.geonet.csw.common.requests.GetRecordByIdRequest;import org.fao.geonet.csw.common.util.CswServer;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.Privileges;import org.fao.geonet.kernel.harvest.harvester.RecordInfo;import org.fao.geonet.kernel.harvest.harvester.UUIDMapper;import org.jdom.Element;//=============================================================================public class Aligner{ //-------------------------------------------------------------------------- //--- //--- Constructor //--- //-------------------------------------------------------------------------- public Aligner(Logger log, ServiceContext sc, Dbms dbms, CswServer server, CswParams params) { this.log = log; this.context = sc; this.dbms = dbms; this.server = server; this.params = params; GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); dataMan = gc.getDataManager(); result = new CswResult(); //--- setup get-record-by-id request request = new GetRecordByIdRequest(); request.setElementSetName(ElementSetName.FULL); CswServer.Operation oper = server.getOperation(CswServer.GET_RECORD_BY_ID); if (oper.postUrl != null) { request.setUrl(oper.postUrl); request.setMethod(CatalogRequest.Method.POST); } else { request.setUrl(oper.getUrl); request.setMethod(CatalogRequest.Method.GET); } if (params.useAccount) request.setCredentials(params.username, params.password); } //-------------------------------------------------------------------------- //--- //--- Alignment method //--- //-------------------------------------------------------------------------- public CswResult 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 local id:"+ id); dataMan.deleteMetadata(dbms, id); dbms.commit(); result.locallyRemoved++; } //----------------------------------------------------------------------- //--- insert/update new metadata for(RecordInfo ri : records) { result.totalMetadata++; 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(RecordInfo ri) throws Exception { Element md = retrieveMetadata(ri.uuid); if (md == null) return; String schema = dataMan.autodetectSchema(md); if (schema == null) { log.debug(" - Metadata skipped due to unknown schema. uuid:"+ ri.uuid); result.unknownSchema++; return; } log.debug(" - Adding metadata with remote uuid:"+ ri.uuid); String id = dataMan.insertMetadataExt(dbms, schema, md, context.getSerialFactory(), params.uuid, ri.changeDate, ri.changeDate, ri.uuid, 1, null); int iId = Integer.parseInt(id); dataMan.setTemplate(dbms, iId, "n", null); dataMan.setHarvested(dbms, iId, params.uuid); addPrivileges(id); addCategories(id); dbms.commit(); dataMan.indexMetadata(dbms, id); result.addedMetadata++; } //-------------------------------------------------------------------------- //--- 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) throws Exception { for (Privileges priv : params.getPrivileges()) { String name = localGroups.getName(priv.getGroupId()); if (name == null) log.debug(" - Skipping removed group with id:"+ priv.getGroupId()); else { log.debug(" - Setting privileges for group : "+ name); for (int opId: priv.getOperations()) { name = dataMan.getAccessManager().getPrivilegeName(opId); //--- allow only: view, dynamic, featured if (opId == 0 || opId == 5 || opId == 6) { log.debug(" --> "+ name); dataMan.setOperation(dbms, id, priv.getGroupId(), opId +""); } else log.debug(" --> "+ name +" (skipped)"); } } } } //-------------------------------------------------------------------------- //--- //--- Private methods : updateMetadata //--- //-------------------------------------------------------------------------- private void updateMetadata(RecordInfo ri, String id) throws Exception { String date = localUuids.getChangeDate(ri.uuid); if (date == null) log.debug(" - Skipped metadata managed by another harvesting node. uuid:"+ ri.uuid +", name:"+ params.name); else { if (!ri.isMoreRecentThan(date)) { log.debug(" - Metadata XML not changed for uuid:"+ ri.uuid); result.unchangedMetadata++; } else { log.debug(" - Updating local metadata for uuid:"+ ri.uuid); Element md = retrieveMetadata(ri.uuid); if (md == null) return; dataMan.updateMetadataExt(dbms, id, md, ri.changeDate); dbms.execute("DELETE FROM OperationAllowed WHERE metadataId=?", Integer.parseInt(id)); addPrivileges(id); dbms.execute("DELETE FROM MetadataCateg WHERE metadataId=?", Integer.parseInt(id)); addCategories(id); dbms.commit(); dataMan.indexMetadata(dbms, id); result.updatedMetadata++; } } } //-------------------------------------------------------------------------- //--- //--- Private methods //--- //-------------------------------------------------------------------------- /** Return true if the uuid is present in the remote node */ private boolean exists(Set<RecordInfo> records, String uuid) { for(RecordInfo ri : records) if (uuid.equals(ri.uuid)) return true; return false; } //-------------------------------------------------------------------------- private Element retrieveMetadata(String uuid) { request.clearIds(); request.addId(uuid); try { log.debug("Getting record from : "+ request.getHost() +" (uuid:"+ uuid +")"); Element response = request.execute(); log.debug("Record got:\n"+Xml.getString(response)); List list = response.getChildren(); //--- maybe the metadata has been removed if (list.size() == 0) return null; response = (Element) list.get(0); return (Element) response.detach(); } catch(Exception e) { log.warning("Raised exception while getting record : "+ e); result.unretrievable++; //--- we don't raise any exception here. Just try to go on return null; } } //-------------------------------------------------------------------------- //--- //--- Variables //--- //-------------------------------------------------------------------------- private Logger log; private ServiceContext context; private Dbms dbms; private CswParams params; private DataManager dataMan; private CswServer server; private CategoryMapper localCateg; private GroupMapper localGroups; private UUIDMapper localUuids; private CswResult result; private GetRecordByIdRequest request;}//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -