view src/main/java/de/mpiwg/itgroup/ismi/event/beans/TransferEvent.java @ 213:38c13c8e9b78

change transfer event from witness to codex.
author casties
date Tue, 03 Dec 2019 19:08:31 +0100
parents 25bfcc9d757c
children
line wrap: on
line source

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

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

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

import org.mpi.openmind.repository.bo.Attribute;
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.entry.beans.CodexEditorTemplate;
import de.mpiwg.itgroup.ismi.util.guiComponents.Calendar;

public class TransferEvent  extends AbstractEvent implements Serializable{
	
	private static final long serialVersionUID = -3606229569295260209L;

	public static String OC = "TRANSFER_EVENT";
	
	private ListenerObject personFromLo;
	
	private ListenerObject personToLo;
	
	private AliasListenerObject placeOriginalLocationLo;
	
	private AliasListenerObject placeNewLocationLo;
	
	private List<SelectItem> suggestedTransferOptions = new ArrayList<SelectItem>();

	private CodexEditorTemplate codexEditor;
	
	public TransferEvent(Entity event) {
		super(event);
		this.refreshTransferOptions();
	}
	
	public TransferEvent(){
		super(new Entity(Node.TYPE_ABOX, OC, false));
		this.refreshTransferOptions();
	}
	
	@Override
	public void setEvent(Entity ev){
		if (codexEditor == null) codexEditor = new CodexEditorTemplate();
		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 = getTargetRelation(rel);
                if (rel.getOwnValue().equals(was_transferred_from)) {
                    // EVENT -> was_transferred_from -> PERSON
                    personFromLo.setEntityAndAttribute(target);
                } else if (rel.getOwnValue().equals(was_transferred_to)) {
                    // EVENT -> was_transferred_to -> PERSON
                    personToLo.setEntityAndAttribute(target);
                } else if (rel.getOwnValue().equals(has_original_location)) {
                    // EVENT -> has_original_location -> PLACE
                    placeOriginalLocationLo.setEntityAndAttributeIfEmpty(target);
                } else if (rel.getOwnValue().equals(has_original_location_as)) {
                    // EVENT -> has_original_location_as -> ALIAS
                    placeOriginalLocationLo.setEntityAndAttribute(target);
                } else if (rel.getOwnValue().equals(has_new_location)) {
                    // EVENT -> has_new_location -> PLACE
                    placeNewLocationLo.setEntityAndAttributeIfEmpty(target);
                } else if (rel.getOwnValue().equals(has_new_location_as)) {
                    // EVENT -> has_new_location_as -> ALIAS
                    placeNewLocationLo.setEntityAndAttribute(target);
                } else if (rel.getOwnValue().equals(is_a_transfer_of)) {
                    // EVENT is_a_transfer_of CODEX
                	Entity codex = target;
                    if (codex != null && codex.isPersistent()) {
                    	setCodex(codex);
                    }
                    /*this.witness = target;
                    if (witness != null && witness.isPersistent()) {
                        witnessId = witness.getId();
                        this.textLo.setEntityAndAttribute(getTextOfWitness(witness));
                        refreshWitnesses(textLo.entity);
                    } */
                } else if (rel.getOwnValue().equals(was_transferred_in)) {
                    this.placeLo.setEntityAndAttributeIfEmpty(target);
                } else if (rel.getOwnValue().equals(was_transferred_in_as)) {
                    this.placeLo.setEntityAndAttribute(target);
                }
            }
		}
	}
	
	@Override
	public void reset(){
		super.reset();
		if (codexEditor != null) codexEditor.reset();
		
		this.defObjectClass = OC;
		if (personFromLo != null) {
		    personFromLo.reset();
		} else {
		    personFromLo = new ListenerObject(PERSON, name_translit);
		}
		
        if (personToLo != null) {
            personToLo.reset();
        } else {
            personToLo = new ListenerObject(PERSON, name_translit);
        }
		
        if (textLo != null) {
            textLo.reset();
        } else {
            textLo = new EventTextLO(TEXT, full_title_translit, this);
        }

        if (placeLo != null) {
            placeLo.reset();
        } else {
            placeLo = new AliasListenerObject(PLACE, name, is_alias_name_of);
        }		
		
		this.date = new Calendar();
		this.witnessList = new ArrayList<SelectItem>();		
		
		this.placeNewLocationLo = new AliasListenerObject(PLACE, name, is_alias_name_of);
		this.placeOriginalLocationLo = new AliasListenerObject(PLACE, name, is_alias_name_of);
	}
	
	
	public void setCodex(Entity codex){
		this.codexEditor.getCodexLo().setEntityAndAttributeName(codex, "identifier");
		
		if(this.codexEditor.getCodexLo().entity != null && this.codexEditor.getCodexLo().entity.isPersistent()){
			
			List<Entity> list = getWrapper().getTargetsForSourceRelation(codex, is_part_of, COLLECTION, 1);
			if(list.size() > 0){
				this.codexEditor.setCollection(list.get(0));
			}
		}
	}
	
	public void listenerRefreshTransferOptions(ActionEvent event){
		this.refreshTransferOptions();
	}
	
	private void refreshTransferOptions(){
		this.suggestedTransferOptions = new ArrayList<SelectItem>();
		suggestedTransferOptions.add(new SelectItem(null, "--choose--"));
		Attribute options = getWrapper().getDefAttributeByOwnValue(OC, "options_for_transfer");
		if(options != null){
			for(String s : options.getPossibleValuesList()){
				this.suggestedTransferOptions.add(new SelectItem(s));
			}
		}
	}
	
	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 -> is_a_transfer_of -> CODEX
			Entity codex = codexEditor.getCodexLo().getEntity();
			if (codex.isLightweight()) {
				codex = getWrapper().getEntityByIdWithContent(codex.getId());
			}
			event.replaceUniqueSourceRelation(codex, CODEX, is_a_transfer_of);
			
			// EVENT -> was_transferred_from -> PERSON
			event.replaceUniqueSourceRelation(personFromLo.entity, PERSON, was_transferred_from);

			// EVENT -> was_transferred_to -> PERSON
			event.replaceUniqueSourceRelation(personToLo.entity, PERSON, was_transferred_to);
			
			// EVENT -> has_original_location -> PLACE
			event.replaceUniqueSourceRelation(placeOriginalLocationLo.entity, PLACE, has_original_location);
			
			// EVENT -> has_new_location -> PLACE
			event.replaceUniqueSourceRelation(placeNewLocationLo.entity, PLACE, has_new_location);
			
			// EVENT -> was_transferred_in -> PLACE
			event.replaceUniqueSourceRelation(placeLo.entity, PLACE, was_transferred_in);
			
			getWrapper().saveEntity(event, getSessionUserName(), null);
			
			printSuccessSavingEntity();
			
		} catch (Exception e) {
			addGeneralMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}	
		saveEnd();
		return null;
	}
	
	public boolean checkConsistency(){
		//TODO
		if (codexEditor.getCodexLo().getEntity() == null) {
			return false;
		}
		return true;
	}
	


	public List<SelectItem> getSuggestedTransferOptions() {
		return suggestedTransferOptions;
	}

	public void setSuggestedTransferOptions(
			List<SelectItem> suggestedTransferOptions) {
		this.suggestedTransferOptions = suggestedTransferOptions;
	}

	public ListenerObject getPersonFromLo() {
		return personFromLo;
	}

	public void setPersonFromLo(ListenerObject personFromLo) {
		this.personFromLo = personFromLo;
	}

	public ListenerObject getPersonToLo() {
		return personToLo;
	}

	public void setPersonToLo(ListenerObject personToLo) {
		this.personToLo = personToLo;
	}

	public ListenerObject getPlaceOriginalLocationLo() {
		return placeOriginalLocationLo;
	}

	public void setPlaceOriginalLocationLo(AliasListenerObject placeOriginalLocationLo) {
		this.placeOriginalLocationLo = placeOriginalLocationLo;
	}

	public ListenerObject getPlaceNewLocationLo() {
		return placeNewLocationLo;
	}

	public void setPlaceNewLocationLo(AliasListenerObject placeNewLocationLo) {
		this.placeNewLocationLo = placeNewLocationLo;
	}

	public CodexEditorTemplate getCodexEditor() {
		return codexEditor;
	}

	public void setCodexEditor(CodexEditorTemplate codexEditor) {
		this.codexEditor = codexEditor;
	}

}