view src/main/java/de/mpiwg/itgroup/ismi/event/beans/CopyEvent.java @ 112:59f26a5ef2b3

AliasListenerObject adds aliases to ListenerObject. Change all forms to enable ALIAS for (historical) PLACE fields. Remove REPOSITORY from event forms.
author casties
date Tue, 13 Dec 2016 19:04:45 +0100
parents 22a18bfc66b0
children 8e3f1f81acfb
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 listenerChangePersonCopyingText(ValueChangeEvent event) {
		this.personCopyingText = changeListener(event, personCopyingText, PERSON, name_translit);
	}
	
	public void listenerChangePersonCopiedFor(ValueChangeEvent event) {
		this.personCopiedFor = changeListener(event, personCopiedFor, PERSON, name_translit);
	}
	
	public void listenerChangePlace(ValueChangeEvent event) {
		this.place = changeListener(event, place, PLACE, name);
	}
	
	public void listenerChangeRepository(ValueChangeEvent event) {
		this.repository = changeListener(event, repository, REPOSITORY, name);
	}*/
	
	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.replaceSourceRelation(personCopiedForLo.entity, PERSON, was_copied_for);

			// EVENT -> has_person_copying_text -> PERSON
			event.replaceSourceRelation(personCopyingTextLo.entity, PERSON, has_person_copying_text);
						
			// EVENT -> was_copied_in -> PLACE
			replaceAliasSourceRelation(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.replaceSourceRelation(witness, WITNESS, is_a_copy_of);
			
			getWrapper().saveEntity(event, getSessionUserName());
			
			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;
	}	
}