view src/main/java/de/mpiwg/itgroup/ismi/event/beans/CopyEvent.java @ 134:25bfcc9d757c

effort to re-use more relations when saving entities. new replaceMultipleSourceRelations() and changes to use it. changes for renamed replaceUniqueSourceRelation().
author casties
date Fri, 24 Feb 2017 20:25:33 +0100
parents 28a0c2726466
children
line wrap: on
line source

package de.mpiwg.itgroup.ismi.event.beans;

import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

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

import de.mpiwg.itgroup.ismi.auxObjects.AliasListenerObject;
import de.mpiwg.itgroup.ismi.auxObjects.ListenerObject;
import de.mpiwg.itgroup.ismi.auxObjects.lo.EventTextLO;
import de.mpiwg.itgroup.ismi.util.guiComponents.Calendar;

public class CopyEvent extends AbstractEvent implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1545705063133894571L;

	public static String OC = "COPY_EVENT";
	
	private ListenerObject personCopiedForLo;
	private ListenerObject personCopyingTextLo;
	
	private Calendar date;
	
	@Override
	public void setEvent(Entity ev){
		reset();
		event = ev;
		if(event != null && event.isPersistent()){
			if(event.isLightweight()){
				event = getWrapper().getEntityContent(event);
			}
			
			this.loadAttributes(this.event);
			this.date = updateCalendar(this.event.getAttributeByName("date"));
			
			for (Relation rel : event.getSourceRelations()) {
				Entity target = null;
				if (rel.getOwnValue().equals(was_copied_for)) {
					//EVENT was_copied_for PERSON
					target = getTargetRelation(rel);
					personCopiedForLo.setEntityAndAttribute(target);
				}else if (rel.getOwnValue().equals(has_person_copying_text)) {
					//EVENT has_person_copying_text PERSON
					target = getTargetRelation(rel);
					personCopyingTextLo.setEntityAndAttribute(target);
				} else if (rel.getOwnValue().equals(was_copied_in)) {
                    // EVENT was_copied_in PLACE
					target = getTargetRelation(rel);
					placeLo.setEntityAndAttributeIfEmpty(target);
                } else if (rel.getOwnValue().equals(was_copied_in_as)) {
                    // EVENT was_copied_in_as ALIAS
                    target = getTargetRelation(rel);
                    placeLo.setEntityAndAttribute(target);
				} else if (rel.getOwnValue().equals(is_a_copy_of)) {
					// EVENT is_a_copy_of WITNESS
					this.witness = getTargetRelation(rel);
					if(witness != null && witness.isPersistent()){
						witnessId = witness.getId();
	                    // WITNESS is_exemplar_of TEXT
						this.textLo.setEntityAndAttribute(getTextOfWitness(witness));
						refreshWitnesses(textLo.entity);
					}
				}
			}
		}
	}
	
	@Override
	public void reset(){
        super.reset();
        this.defObjectClass = OC;

        if (textLo != null) {
            textLo.reset();
        } else {
            textLo = new EventTextLO(TEXT, full_title_translit, this);
        }

        if (personCopiedForLo != null) {
            personCopiedForLo.reset();
        } else {
            personCopiedForLo = new ListenerObject(PERSON, name_translit);
        }

        if (personCopyingTextLo != null) {
            personCopyingTextLo.reset();
        } else {
            personCopyingTextLo = new ListenerObject(PERSON, name_translit);
        }

        if (placeLo != null) {
            placeLo.reset();
        } else {
            placeLo = new AliasListenerObject(PLACE, name, is_alias_name_of);
        }

        this.date = new Calendar();
        this.witnessList = new ArrayList<SelectItem>();
    }
	
	public void listenerSave(ActionEvent event) {
		this.save();
	}
	
	@Override
	public String save(){
		super.save();
		if(!checkConsistency()){
			addGeneralMsg("Either the Witness, the Person or the Place is empty.");
			addGeneralMsg("The event could not be saved.");
			return null;
		}
		
		try{
			getAttributes().put("date", this.date.toJSONString());
			event = updateEntityAttributes(event);
			
			// EVENT -> was_copied_for -> PERSON
			event.replaceUniqueSourceRelation(personCopiedForLo.entity, PERSON, was_copied_for);

			// EVENT -> has_person_copying_text -> PERSON
			event.replaceUniqueSourceRelation(personCopyingTextLo.entity, PERSON, has_person_copying_text);
						
			// EVENT -> was_copied_in -> PLACE
			replaceUniqueAliasSourceRelation(event, placeLo, PLACE, was_copied_in, was_copied_in_as);

			// EVENT -> is_a_copy_of -> WITNESS
            if (witness.isLightweight()) {
                this.witness = getWrapper().getEntityByIdWithContent(witness.getId());
            }
			event.replaceUniqueSourceRelation(witness, WITNESS, is_a_copy_of);
			
			getWrapper().saveEntity(event, getSessionUserName(), null);
			
			printSuccessSavingEntity();
			
		}catch (Exception e) {
			addGeneralMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		saveEnd();
		return null;
	}
	
	public boolean checkConsistency(){
		if(this.witness == null || 
				this.textLo.entity == null || 
				this.personCopiedForLo.entity == null || 
				this.personCopyingTextLo.entity == null){
			return false;
		}
		return true;
	}
	
	public CopyEvent(Entity event) {
		super(event);
	}
	
	public CopyEvent(){
		super(new Entity(Node.TYPE_ABOX, OC, false));
	}
	
	public ListenerObject getPersonCopiedForLo() {
		return personCopiedForLo;
	}

	public void setPersonCopiedForLo(ListenerObject personCopiedForLo) {
		this.personCopiedForLo = personCopiedForLo;
	}

	public ListenerObject getPersonCopyingTextLo() {
		return personCopyingTextLo;
	}

	public void setPersonCopyingTextLo(ListenerObject personCopyingTextLo) {
		this.personCopyingTextLo = personCopyingTextLo;
	}

	public Calendar getDate() {
		return date;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}	
}