view src/de/mpiwg/itgroup/eSciDoc/Tools/Pubman/ReplaceAffiliation_local.java @ 14:94ea06e32003

minor
author dwinter
date Thu, 13 Sep 2012 13:41:32 +0200
parents ad5c0748bd08
children
line wrap: on
line source

package de.mpiwg.itgroup.eSciDoc.Tools.Pubman;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;

public class ReplaceAffiliation_local {

	/**
	 * @param args
	 * @throws ESciDocXmlObjectException
	 * @throws JDOMException
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	public static void main(String[] args) throws IllegalStateException,
			IOException, JDOMException, ESciDocXmlObjectException {

		Logger logger = Logger.getRootLogger();
		logger.setLevel(Level.DEBUG);
		EScidocBasicHandler connector = new EScidocBasicHandler(
				"escidoc.mpiwg-berlin.mpg.de", 8080, "dwinter", "XXXX");

		if(args.length<2){
			System.out.println("Usage: startrecord maximumrecords");
			System.exit(-1);
		}
		String MAX_REC = args[1];
		String start = args[0];
		String objectXPath = "//escidocItem:item";

		String query = "?maximumRecords=" + String.valueOf(MAX_REC)
				+ "&startRecord=" + String.valueOf(start);
		String command = "/ir/context/escidoc:38279/resources/members";
		for (eSciDocXmlObject obj : connector.getObjectsFromFilterResult(
				command + query, objectXPath)) {

			Document doc = obj.getDocument();
			Boolean changed=false;
			XPath personXPath = EScidocTools.getESciDocXpath("//eterms:creator/person:person");
			XPath organizationXPath = EScidocTools.getESciDocXpath("./organization:organization");
			XPath titleXPath = EScidocTools.getESciDocXpath("./dc:title");
			XPath identifierXPath = EScidocTools.getESciDocXpath("./dc:identifier");
			
			@SuppressWarnings("unchecked")
			List<Element> persons = personXPath.selectNodes(doc);
			for (Element person : persons) {
				Element organization = (Element) organizationXPath.selectSingleNode(person);
				if (organization!=null){
					Element title = (Element) titleXPath.selectSingleNode(organization);
					
					String titleString = title.getTextTrim();
					if (titleString
							.startsWith("Max Planck Society")) {
						title.setText("Max Planck Institute for the History of Science");
						Element identifier = (Element) identifierXPath.selectSingleNode(organization);
						identifier.setText("escidoc:14002");
						changed=true;
					
					}
					}
			}
			
			//System.out.println(obj.printXML());
			if (changed){
				
				Boolean retVal = connector.updateItem(obj);
				System.out.println("Replaced:"+obj.getESciDocId());
				HttpResponse retValu = connector.submitAnObject(obj, "changed affiliation of persons");
				
				System.out.println(EScidocBasicHandler.convertStreamToString(retValu.getEntity().getContent()));
				HttpResponse resObj = connector.eScidocGet(obj.getESciDocId());
				HttpEntity ent = resObj.getEntity();
				if (ent!=null){
				obj= new eSciDocXmlObject(ent.getContent());
				} else {
					System.out.println("Can not retrieve:" + obj.getESciDocId());
					continue;
				}
				
				HttpResponse reValue2 = connector.releaseAnObject(obj, "changed affiliation of persons");
				System.out.println(EScidocBasicHandler.convertStreamToString(reValue2.getEntity().getContent()));

			}
		}

	}
}