view src/main/java/de/mpiwg/itgroup/ismi/event/beans/StudyEvent.java @ 113:8e3f1f81acfb

small cleanup.
author casties
date Wed, 14 Dec 2016 15:29:05 +0100
parents 59f26a5ef2b3
children 28a0c2726466
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.util.guiComponents.Calendar;

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

	public static String OC = "STUDY_EVENT";
	
	private ListenerObject personLo;
	private ListenerObject advisorLo;
	
	private List<SelectItem> suggestedEngagementOptions = new ArrayList<SelectItem>();
	
    public StudyEvent() {
        super(new Entity(Node.TYPE_ABOX, OC, false));
        refreshEngagementOptions();
    }

    public StudyEvent(Entity event) {
        super(event);
        refreshEngagementOptions();
    }

    public void listenerRefreshEngagementOptions(ActionEvent event) {
        this.refreshEngagementOptions();
    }

    private void refreshEngagementOptions() {
        this.suggestedEngagementOptions = new ArrayList<SelectItem>();
        suggestedEngagementOptions.add(new SelectItem(null, "--choose--"));
        Attribute binding = getWrapper().getDefAttributeByOwnValue(OC, "options_for_engagement");
        if (binding != null) {
            for (String s : binding.getPossibleValuesList()) {
                this.suggestedEngagementOptions.add(new SelectItem(s));
            }
        }
    }
	
	@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 = getTargetRelation(rel);
				String relName = rel.getOwnValue();
                if (relName.equals(was_studied_by)) {
					//EVENT was_studied_by PERSON
					personLo.setEntityAndAttribute(target);
				} else if (relName.equals(was_advised_by)) {
					//EVENT was_advised_by PERSON
					advisorLo.setEntityAndAttribute(target);
				} else if (relName.equals(was_studied_in)) {
                    //EVENT was_studied_in PLACE
					placeLo.setEntityAndAttributeIfEmpty(target);	
                } else if (relName.equals(was_studied_in_as)) {
                    //EVENT was_studied_in_as ALIAS
                    placeLo.setEntityAndAttribute(target);  
				} else if (relName.equals(is_a_study_of)) {
					//EVENT study_of WITNESS
					//WITNESS is_exemplar_of TEXT
					this.witness = target;
                    if (witness != null && witness.isPersistent()) {
                        witnessId = witness.getId();
                        this.textLo.setEntityAndAttribute(getTextOfWitness(witness));
                        refreshWitnesses(textLo.entity);
                    }
				}
			}
		}
	}
	
	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_studied_by -> PERSON
			event.replaceSourceRelation(personLo.entity, PERSON, was_studied_by);

			// EVENT -> was_advised_by -> PERSON
			event.replaceSourceRelation(advisorLo.entity, PERSON, was_advised_by);
						
			// EVENT -> was_studied_in -> PLACE
			replaceAliasSourceRelation(event, placeLo, PLACE, was_studied_in, was_studied_in_as);

			// EVENT -> is_a_study_of -> WITNESS
			if(witness.isLightweight()){
				witness = getWrapper().getEntityByIdWithContent(witness.getId());
			}
			event.replaceSourceRelation(witness, WITNESS, is_a_study_of);
			
			getWrapper().saveEntity(event, getSessionUserName());
			
			printSuccessSavingEntity();
			
		} catch (Exception e) {
			addGeneralMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		saveEnd();
		return null;
	}
	
	@Override
    public void reset() {
        super.reset();
        this.defObjectClass = OC;
        
        if (personLo != null) {
            personLo.reset();
        } else {
            personLo = new ListenerObject(PERSON, name_translit);
        }

        if (advisorLo != null) {
            advisorLo.reset();
        } else {
            advisorLo = 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.refreshEngagementOptions();
    }
	
	public boolean checkConsistency(){
		if(this.witness == null || this.textLo.entity == null || this.placeLo.entity == null || this.personLo.entity == null){
			return false;
		}
		return true;
	}
	
	public ListenerObject getPersonLo() {
		return personLo;
	}

	public void setPersonLo(ListenerObject personLo) {
		this.personLo = personLo;
	}

	public ListenerObject getAdvisorLo() {
		return advisorLo;
	}

	public void setAdvisorLo(ListenerObject advisorLo) {
		this.advisorLo = advisorLo;
	}	

	public List<SelectItem> getSuggestedEngagementOptions() {
		return suggestedEngagementOptions;
	}

	public void setSuggestedEngagementOptions(
			List<SelectItem> suggestedEngagementOptions) {
		this.suggestedEngagementOptions = suggestedEngagementOptions;
	}
	
}