view src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/Misattribution.java @ 121:6f0e9a333c15

misidentification of witness mostly works now.
author casties
date Mon, 09 Jan 2017 20:37:29 +0100
parents d172201d24ad
children 28a0c2726466
line wrap: on
line source

package de.mpiwg.itgroup.ismi.util.guiComponents;

import java.io.Serializable;
import java.util.List;

import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Node;
import org.mpi.openmind.repository.bo.Relation;

public class Misattribution implements Serializable {
	private static final long serialVersionUID = -1840193000833171154L;

	public static String MISATT = "MISATTRIBUTION";
	public static String MISATTRIBUTED_TO = "misattributed_to";
	public static String IS_REFERENCE_OF = "is_reference_of";
	public static String HAS_AUTHOR_MISATT = "has_author_misattribution"; 
	
	private Entity misatt;
	private Entity person;
	private Reference ref;
	private WrapperService ot;
	private String userName;
	
	public Misattribution(WrapperService ot, String userName){
		this.ot = ot;
		this.userName = userName;
	}
	
	public Entity saveAndGetMisattribution(Entity text) throws Exception{

	    // create misattribution if necessary
		if(misatt == null){
			misatt = new Entity(Node.TYPE_ABOX, MISATT, false);
		}
		
		// save reference
		Entity entityRef = this.ref.getEnt();
		ot.saveEntity(entityRef, userName);

		// set person relation to misattribution
		this.misatt.removeAllSourceRelations(MISATTRIBUTED_TO, "PERSON");
		new Relation(misatt, person, MISATTRIBUTED_TO);
		
		// set reference relation to misattribution
		this.misatt.removeAllTargetRelations(IS_REFERENCE_OF, "REFERENCE");
		new Relation(entityRef, misatt, IS_REFERENCE_OF);
		
		// set text relation to misattribution
		this.misatt.removeAllTargetRelations(HAS_AUTHOR_MISATT, "TEXT");
		new Relation(text, misatt, HAS_AUTHOR_MISATT);
		
		// save misattribution
		ot.saveEntity(misatt, userName);
		
		return text;
	}
	
	public static Misattribution create(Entity person, WrapperService ot, String userName) {
		Misattribution obj = new Misattribution(ot, userName);
		
		obj.setPerson(person);
		obj.setRef(new Reference(null));
		
		return obj;
	}
	
	public static Misattribution load(Entity misatt, WrapperService ot, String userName) throws Exception{
		
		Misattribution obj = new Misattribution(ot, userName); 
		
		if(misatt.isLightweight()){
			obj.setMisatt(ot.getEntityByIdWithContent(misatt.getId()));
		}
		//loading person. Person can be Light Weight
		List<Entity> tmpList = ot.getTargetsForSourceRelation(misatt, MISATTRIBUTED_TO, "PERSON", -1);
		if(tmpList.size() > 1){
			throw new Exception("Misattribution (entity) can not has more than one person associated. " + misatt.toString());
		}else if(tmpList.size() > 0){
			obj.setPerson(tmpList.get(0));
		}		
		
		tmpList = ot.getSourcesForTargetRelation(misatt, IS_REFERENCE_OF, "REFERENCE", -1);
		if(tmpList.size() > 0){
			Entity ref = tmpList.get(0);
			if(ref.isLightweight()){
				ref = ot.getEntityByIdWithContent(ref.getId());
			}
			obj.setRef(new Reference(ref));
		}		
		
		//this.person = person;
		//this.ref = new Reference(ref);
		return obj;
	}
	
	public Entity getPerson() {
		return person;
	}
	public void setPerson(Entity person) {
		this.person = person;
	}
	public Reference getRef() {
		return ref;
	}
	public void setRef(Reference ref) {
		this.ref = ref;
	}
	public Entity getMisatt() {
		return misatt;
	}
	public void setMisatt(Entity misatt) {
		this.misatt = misatt;
	}
	
}