view src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/Misidentification.java @ 136:502ae5b1a07d

fixing bugs from re-use efforts. misc cleanups.
author casties
date Thu, 02 Mar 2017 19:48:58 +0100
parents 28a0c2726466
children
line wrap: on
line source

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

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

import org.apache.commons.lang.StringUtils;
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.Node;

public class Misidentification implements Serializable {
	private static final long serialVersionUID = -1840193000833171153L;

	public static String TITLE = "title";
	public static String TITLE_TRANSLIT = "title_translit";
	public static String MISIDENT = "MISIDENTIFICATION";
	public static String MISATTRIBUTED_TO = "misattributed_to";
	public static String IS_REFERENCE_OF = "is_reference_of";
	public static String HAS_MISIDENT = "has_misidentification";

	private Entity misident;
	private String title;
	private String title_translit;
	private Entity person;
	private Reference ref;
	private WrapperService ot;
	private String userName;

	public Misidentification(WrapperService ot, String userName) {
		this.ot = ot;
		this.userName = userName;
	}

	public Entity saveMisidentification(Entity witness) throws Exception {

		// create misidentification if necessary
		if (misident == null) {
			misident = new Entity(Node.TYPE_ABOX, MISIDENT, false);
		}

		// set title
		if (!misident.containsAttribute(TITLE)) {
			misident.addAttribute(new Attribute(TITLE, "arabic", title));
		} else {
			misident.getAttributeByName(TITLE).setOwnValue(title);
		}
		if (!misident.containsAttribute(TITLE_TRANSLIT)) {
			misident.addAttribute(new Attribute(TITLE_TRANSLIT, "text", title_translit));
		} else {
			misident.getAttributeByName(TITLE_TRANSLIT).setOwnValue(title_translit);
		}

		// save reference
		Entity entityRef = this.ref.getEnt();
		if (StringUtils.isEmpty(entityRef.getAttributeByName("endnote-id").getOwnValue())) {
			entityRef = null;
		} else {
			ot.saveEntity(entityRef, userName, null);
		}

		// set reference relation to misidentification
		misident.replaceUniqueTargetRelation(entityRef, "REFERENCE", IS_REFERENCE_OF);

		// set person relation to misidentification
		if (person.isLightweight()) {
			person = ot.getEntityContent(person);
		}
		misident.replaceUniqueSourceRelation(person, "PERSON", MISATTRIBUTED_TO);

		// set text relation to misidentification
		misident.replaceUniqueTargetRelation(witness, "WITNESS", HAS_MISIDENT);

		// save misidentification 
		ot.saveEntity(misident, userName, null);

		return witness;
	}

	public static Misidentification create(String title, String title_translit, Entity person, WrapperService ot,
			String userName) {
		Misidentification obj = new Misidentification(ot, userName);
		obj.setTitle(title);
		obj.setTitle_translit(title_translit);
		obj.setPerson(person);
		obj.setRef(new Reference(null));

		return obj;
	}

	public static Misidentification load(Entity misident, WrapperService ot, String userName) throws Exception {

		Misidentification obj = new Misidentification(ot, userName);

		if (misident.isLightweight()) {
			misident = ot.getEntityByIdWithContent(misident.getId());
		}
		obj.setMisident(misident);
		
		// load title
		if (misident.containsAttribute(TITLE)) {
			String t = misident.getAttributeByName(TITLE).getOwnValue();
			obj.setTitle(t);
		}
		if (misident.containsAttribute(TITLE_TRANSLIT)) {
			String tt = misident.getAttributeByName(TITLE_TRANSLIT).getOwnValue();
			obj.setTitle_translit(tt);
		}
		
		// loading person. Person can be Light Weight
		List<Entity> tmpList = ot.getTargetsForSourceRelation(misident, MISATTRIBUTED_TO, "PERSON", -1);
		if (tmpList.size() > 1) {
			throw new Exception(
					"Misidentification (entity) can not have more than one person associated. " + misident.toString());
		} else if (tmpList.size() == 1) {
			obj.setPerson(tmpList.get(0));
		}

		// load references
		tmpList = ot.getSourcesForTargetRelation(misident, 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));
		} else {
			// create empty reference
			obj.setRef(new Reference(null));
		}

		// 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 getMisident() {
		return misident;
	}

	public void setMisident(Entity misident) {
		this.misident = misident;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle_translit() {
		return title_translit;
	}

	public void setTitle_translit(String title_translit) {
		this.title_translit = title_translit;
	}

}