view src/main/java/org/mpi/openmind/scripts/recovery/DomXmlRecovery.java @ 51:d2833ab25c54

better logging of entity deletion.
author casties
date Fri, 28 Oct 2016 19:58:18 +0200
parents 615d27dce9b3
children
line wrap: on
line source

package org.mpi.openmind.scripts.recovery;

import java.text.DecimalFormat;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.bo.Attribute;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Relation;
import org.mpi.openmind.repository.services.ServiceRegistry;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DomXmlRecovery {
	
	static XPathFactory xPathfactory = XPathFactory.newInstance();
	static XPath xpath = xPathfactory.newXPath();
	
	public static void execute(String file){
		RecoveryCache cache = loadXMLFile(file);
		ServiceRegistry services = new ServiceRegistry();
		WrapperService ws = services.getWrapper();
		
		removingEntsFromDB(ws, cache);
		createNewEnts(ws, cache);
	}
	
	private static void removingEntsFromDB(WrapperService ws, RecoveryCache cache){
		print("################# Removing ################");
		for(Entity def : ws.getConcepts()){
			print(def.getOwnValue());
			for(Entity dbEnt : ws.getEntitiesByDef(def.getOwnValue())){
				if(!cache.existEnt(dbEnt.getId())){
					print("this must be removed from DB=" + dbEnt);
					try {
						ws.removeCurrentVersionEntity(dbEnt, "DomXmlRecovery");
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
			}
		}
	}
	
	private static void refreshEnts(WrapperService ws, RecoveryCache cache){
		
	}
	
	private static void createNewEnts(WrapperService ws, RecoveryCache cache){
		for(Entity xmlEnt : cache.getEnts()){
			if(!ws.existEntity(xmlEnt.getId())){
				print("This entity must be created " + xmlEnt);
			}
		}
	}
	
	
	private static String percentage(int index, int total){
		double p = (100* (double)index) / (double)total;
		DecimalFormat df = new DecimalFormat("#.#");
		return df.format(p);
	}
	
	private static RecoveryCache loadXMLFile(String file){
		
		
		
		RecoveryCache cache = new RecoveryCache();
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(file);
			
			
			
			NodeList nl = (NodeList) xpath.compile("/openmind-data/entities/entity").evaluate(doc, XPathConstants.NODESET);
			print("Reading " + nl.getLength() + " entities.");
			
			for(int i=0; i< nl.getLength(); i++){
				try {
					Entity ent = xml2Ent(nl.item(i));
					cache.addEnt(ent);
					
					if(i % 100 == 0){
						print(i + " entities loaded. " + percentage(i, nl.getLength()) + "%.");
					}
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			nl = (NodeList) xpath.compile("/openmind-data/relations/relation").evaluate(doc, XPathConstants.NODESET);
			print("Reading " + nl.getLength() + " relations.");
			for(int i=0; i< nl.getLength(); i++){
				try {
					Relation rel = xml2Rel(nl.item(i));
					cache.addRel(rel);
					
					if(i % 100 == 0){
						print(i + " relations loaded. " + percentage(i, nl.getLength()) + "%.");
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cache;
	}
	
	private static Relation xml2Rel(Node node)throws Exception{
		Relation rel = new Relation();
		String ov = (String)xpath.compile("./@own-value").evaluate(node, XPathConstants.STRING);
		Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING));
		Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING));
		Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING));
		
		Long srcId = Long.parseLong((String)xpath.compile("./@source-id").evaluate(node, XPathConstants.STRING));
		Long tarId = Long.parseLong((String)xpath.compile("./@target-id").evaluate(node, XPathConstants.STRING));
		
		rel.setId(id);
		rel.setOwnValue(ov);
		rel.setVersion(version);
		rel.setModificationTime(modTime);
		rel.setTargetId(tarId);
		rel.setSourceId(srcId);
		
		return rel;
	}
	
	private static Entity xml2Ent(Node node) throws Exception{
		
		String oc = (String)xpath.compile("./@object-class").evaluate(node, XPathConstants.STRING);
		Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING));
		Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING));
		Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING));
		String ov = (String)xpath.compile("./@own-value").evaluate(node, XPathConstants.STRING);
		
		Entity ent = new Entity(org.mpi.openmind.repository.bo.Node.TYPE_ABOX, oc, false);
		ent.setId(id);
		ent.setOwnValue(ov);
		ent.setModificationTime(modTime);
		ent.setVersion(version);
		
		NodeList nl = (NodeList) xpath.compile("./attributes/attribute").evaluate(node, XPathConstants.NODESET);
		for(int i=0; i< nl.getLength(); i++){
			Attribute att = xml2Att(nl.item(i));
			ent.addAttribute(att);
			//print(att);
		}
		
		//print(ent);
		
		return ent;
	}
	
	private static Attribute xml2Att(Node node) throws Exception{
		Attribute att = new Attribute();
		
		Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING));
		String name = (String)xpath.compile("./@name").evaluate(node, XPathConstants.STRING);
		String value = (String)xpath.compile("./@value").evaluate(node, XPathConstants.STRING);
		String contentType = (String)xpath.compile("./@content-type").evaluate(node, XPathConstants.STRING);
		Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING));
		Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING));
		
		att.setId(id);
		att.setName(name);
		att.setValue(value);
		att.setContentType(contentType);
		att.setVersion(version);
		att.setModificationTime(modTime);
		
		return att;
	}
	
	private static void print(Object s){
		System.out.println(s);
	}
	
	public static void main(String[] args) {
		execute("/Users/jurzua/Projects/max-planck/db_backup/xml_backup/2013.07.12-ENT.xml");
	}
}