view src/main/java/de/mpiwg/itgroup/ismi/entry/beans/CurrentPersonBean.java @ 127:efeab3bd856f

nicer logging.
author casties
date Wed, 01 Feb 2017 18:09:09 +0100
parents 29d40186dfd7
children 25bfcc9d757c
line wrap: on
line source

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

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

import javax.faces.convert.Converter;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;

import org.apache.log4j.Logger;
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 org.mpi.openmind.repository.services.utils.EditIntent;
import org.mpi.openmind.repository.utils.RomanizationLoC;
import org.mpi.openmind.repository.utils.TransliterationUtil;

import de.mpiwg.itgroup.ismi.auxObjects.AliasListenerObject;
/* rich
import com.icesoft.faces.async.render.SessionRenderer;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputHidden;
*/
import de.mpiwg.itgroup.ismi.auxObjects.ListenerObject;
import de.mpiwg.itgroup.ismi.util.guiComponents.AliasEntityList;
import de.mpiwg.itgroup.ismi.util.guiComponents.Calendar;
import de.mpiwg.itgroup.ismi.util.guiComponents.EntityList;

public class CurrentPersonBean extends AbstractISMIBean  implements Serializable{
    private static final long serialVersionUID = -3366510497089165294L;

	private static Logger logger = Logger.getLogger(CurrentPersonBean.class);
	
	//private Entity person;
	private String newAlias;
	
	private String romanizedNameTranslit;
	
	public static String birth_date = "birth_date";
	public static String death_date = "death_date";
	
	private AliasListenerObject birthPlaceLo = new AliasListenerObject(PLACE, name, "is_alias_name_of");
	private AliasListenerObject deathPlaceLo = new AliasListenerObject(PLACE, name, "is_alias_name_of");

	private String valueShortName;
	private Entity shortNameAlias; // is now an alias of type "prime"
	
	private transient Calendar calBirthDate;
	private transient Calendar calDeathDate;

	private Map<String, Boolean> deletedAliases = new HashMap<String, Boolean>();
	
	private EntityList livedInPlaces;
	private EntityList aliasList;
	private EntityList roleList;
	private EntityList studentOfList;
	private EntityList floruitList;

	/** intent to edit all attributes and relations in the form */
	public static EditIntent editPersonIntent = new EditIntent(
			PERSON,
			// attributes
			new String[]{"name", "name_translit", "birth_date", "birth_date_text", "death_date", "death_date_text",
					"url", "authority_id", "notes", "notes_old"},
			// source relations
			new String[]{"was_born_in", "was_born_in_as", "died_in", "died_in_as", "lived_in", "lived_in_as",
					"was_student_of", "has_floruit_date", "has_role"},
			// target relations
			new String[]{"is_prime_alias_name_of", "is_alias_name_of", "is_reference_of"});
	
	
	@Override
	public void reset(){
		super.reset();
		this.entity = new Entity(Node.TYPE_ABOX, PERSON, false);
		
		this.newAlias = new String();

		this.livedInPlaces = new AliasEntityList(PLACE, name, "is_alias_name_of", true);		
		this.aliasList = new EntityList(ALIAS, "alias", getWrapper(), getUserName());		
		this.roleList = new EntityList(ROLE, "name", getWrapper(), getUserName());
		this.studentOfList = new EntityList(PERSON, "name_translit", "Student of");
		this.floruitList = new EntityList(FLORUIT_DATE, "date", getWrapper(), getUserName(), true);
		
		this.valueShortName = "";
		this.shortNameAlias = null;
		
		this.birthPlaceLo.reset();

		this.deathPlaceLo.reset();

		this.deletedAliases = new HashMap<String, Boolean>();
		
		this.calBirthDate = new Calendar();
		this.calDeathDate = new Calendar();
		
	}

	public EntityList getRoleList() {
		return roleList;
	}

	public void setRoleList(EntityList roleList) {
		this.roleList = roleList;
	}

	public EntityList getAliasList() {
		return aliasList;
	}

	public void setAliasList(EntityList aliasList) {
		this.aliasList = aliasList;
	}

	public CurrentPersonBean() {
		this.reset();
		setDefObjectClass(PERSON);
		registerChecker(birthPlaceLo, "Birth place not valid!");
		registerChecker(deathPlaceLo, "Death place not valid!");
	}
	
	/**
	 * 
	 * @param event
	 */
	public void shortNameChangeListener(ValueChangeEvent event) {
		try {
			if (event.getNewValue() == null) {
				return;
			}
			if (event.getNewValue().equals(event.getOldValue())) {
				return;
			}

			String newShortName = (String) event.getNewValue();

			if (shortNameAlias == null) {
				shortNameAlias = new Entity(Node.TYPE_ABOX, ALIAS, false);
			}

			Attribute attr = shortNameAlias.getAttributeByName(alias);
			if (attr == null) {
				shortNameAlias.addAttribute(new Attribute(alias, TEXT, newShortName));
			} else {
				attr.setValue(newShortName);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	public String translitNameAction() {
		String pn = getAttributes().get(name);

		String translit = TransliterationUtil.getTransliteration(pn);
		this.getAttributes().put(name_translit, translit);
		
		return PAGE_EDITOR;
	}

	public Calendar getCalBirthDate() {
		return calBirthDate;
	}

	public void setCalBirthDate(Calendar calBirthDate) {
		this.calBirthDate = calBirthDate;
	}

	public Calendar getCalDeathDate() {
		return calDeathDate;
	}

	public void setCalDeathDate(Calendar calDeathDate) {
		this.calDeathDate = calDeathDate;
	}

	@Override
	public void setEntity(Entity person) {
		this.reset();
		
		this.entity = person;
		
		if(this.entity != null && this.entity.isPersistent()){
			if (this.entity.isLightweight()) {
				this.entity = this.getWrapper().getEntityContent(person);
			}

			this.calDeathDate = updateCalendar(this.entity.getAttributeByName(death_date));
			this.calBirthDate = updateCalendar(this.entity.getAttributeByName(birth_date));
			
			this.setCurrentId(this.entity.getId().toString());
			this.loadAttributes(this.entity);
			
			for (Relation rel : this.entity.getSourceRelations()) {
                String relName = rel.getOwnValue();
				Entity target = getWrapper().getEntityById(rel.getTargetId());
                if (relName.equals("was_born_in")) {
					this.birthPlaceLo.setEntityAndAttributeIfEmpty(target);
                } else if (relName.equals("was_born_in_as")) {
                    this.birthPlaceLo.setEntityAndAttribute(target);
                } else if (relName.equals("died_in")) {
                    this.deathPlaceLo.setEntityAndAttributeIfEmpty(target);
                } else if (relName.equals("died_in_as")) {
                    this.deathPlaceLo.setEntityAndAttribute(target);
				} else if (relName.equals(lived_in)) {
					this.livedInPlaces.add(target, rel.getAttributeByName("date"));
                } else if (relName.equals(lived_in_as)) {
                    this.livedInPlaces.add(target, rel.getAttributeByName("date"));
				} else if (relName.equals(was_student_of)) {
					this.studentOfList.add(target);
				} else if (relName.equals(has_role)) {
					this.roleList.add(target);
				} else if (relName.equals("has_floruit_date")) {
					Attribute calAtt = getWrapper().getAttributeByName(target.getId(), "date");
					this.floruitList.add(target, calAtt);
				}
			}
			
			for (Relation rel : this.entity.getTargetRelations()) {
				if (rel.getOwnValue().equals("was_created_by") && rel.getSourceObjectClass().equals(TEXT)) {
					//Entity title = getWrapper().getEntityById(rel.getSourceId());
				} else if (rel.getOwnValue().equals("is_alias_name_of")) {
					Entity alias = getWrapper().getEntityById(rel.getSourceId());
					this.aliasList.add(alias);
				} else if (rel.getOwnValue().equals("is_prime_alias_name_of")) {
					Entity alias = getWrapper().getEntityByIdWithContent(rel.getSourceId());
					this.shortNameAlias = alias;
					this.valueShortName = alias.getAttributeByName("alias").getValue();
				}
			}
			
			//this.loadReferences(this.entity);
			this.loadEndNoteRefs();
			this.displayUrl = generateDisplayUrl(person, null, null, getAppBean().getRoot());
				
		}
	}

	/**
	 * Saves the current Person. TODO save the target's relations.
	 * 
	 * @return
	 */
	@Override
	public String save() {
		super.save();
		try {
			
			long start = System.currentTimeMillis();
			
			CheckResults cr = getCheckResults();
			if (cr.hasErrors){
				getSessionBean().setErrorMessages(cr);
				getSessionBean().setDisplayError(true);
				return "SAVE_ERROR";
			}
			
			getAttributes().put(birth_date, this.calBirthDate.toJSONString());
			getAttributes().put(death_date, this.calDeathDate.toJSONString());
			
			this.entity = updateEntityAttributes(this.entity);

			// PERSON -> was_born_in -> PLACE
			replaceAliasSourceRelation(this.entity, birthPlaceLo, PLACE, "was_born_in", "was_born_in_as");            
            
            // PERSON -> was_died_in -> PLACE
            replaceAliasSourceRelation(this.entity, deathPlaceLo, PLACE, "died_in", "died_in_as");            

			// ALIAS -> is_alias_name_of -> PERSON
			this.entity.removeAllTargetRelationsByName(is_alias_name_of);
			for(Entity alias : this.aliasList.getEntities()){
				Entity alias0 = getWrapper().getEntityByIdWithContent(alias.getId());
				new Relation(alias0, this.entity, is_alias_name_of);
			}
			
            // ALIAS -> is_prime_alias_name_of/is_alias_name_of -> PERSON
			this.entity.removeAllTargetRelationsByName(is_prime_alias_name_of);
			if (shortNameAlias != null) {
				if (shortNameAlias.getAttributeByName(alias) == null) {
					logger.error("ALIAS without Attribute! Trying to fix "+shortNameAlias);
					if (valueShortName != null) {
						shortNameAlias.addAttribute(new Attribute(alias, TEXT, valueShortName));
					}
				}
				shortNameAlias = getWrapper().saveEntity(shortNameAlias, getSessionUser().getEmail(), null);
				// save as is_alias_name_of
				new Relation(shortNameAlias, this.entity, is_alias_name_of);
			}
			
			// PERSON -> lived_in -> PLACE (manyToMany)
			this.entity.removeAllSourceRelationsByName(lived_in);
            this.entity.removeAllSourceRelationsByName(lived_in_as);
			for(Entity place : this.livedInPlaces.getEntities()) {
				Entity placeEnt = getWrapper().getEntityByIdWithContent(place.getId());
				if (placeEnt.getObjectClass().equals(PLACE)) {
				    // PERSON -> lived_in -> PLACE
    				Relation livedIn = new Relation(this.entity, placeEnt, lived_in);
    				Calendar cal = livedInPlaces.getCalendar(place.getId());
    				if(cal != null){
    					livedIn.addAttribute(new Attribute("date", "date", cal.toJSONString()));
    				}
				} else if (placeEnt.getObjectClass().equals(ALIAS)) {
				    // PERSON -> lived_in_as -> ALIAS
                    Relation livedInAs = new Relation(this.entity, placeEnt, lived_in_as);
                    Calendar cal = livedInPlaces.getCalendar(place.getId());
                    if (cal != null) {
                        livedInAs.addAttribute(new Attribute("date", "date", cal.toJSONString()));
                    }
                    // ALIAS -> is_alias_name_of -> PLACE
                    List<Entity> places = getWrapper().getTargetsForSourceRelation(placeEnt, is_alias_name_of, PLACE, 1);
                    if (! places.isEmpty()) {
                        // PERSON -> lived_in -> PLACE
                        Entity realPlace = places.get(0);
                        Relation livedIn = new Relation(this.entity, realPlace, lived_in);
                        if (cal != null) {
                            livedIn.addAttribute(new Attribute("date", "date", cal.toJSONString()));
                        }
                    }
                }
			}
			
			// Person -> has_floruit_date -> FLORUIT DATE
			this.entity.removeAllSourceRelationsByName(has_floruit_date);
			for(Entity floruitDate : this.floruitList.getEntities()){
				Entity floruitDate0 = getWrapper().getEntityByIdWithContent(floruitDate.getId());
				Calendar cal = floruitList.getCalendar(floruitDate.getId());
				
				if(floruitDate0.getAttributeByName("date") == null){
					floruitDate0.addAttribute(new Attribute("date", "date", cal.toJSONString()));
				}else{
					floruitDate0.getAttributeByName("date").setValue(cal.toJSONString());
				}
				getWrapper().saveAssertion(floruitDate0, getUserName());
				new Relation(this.entity, floruitDate0, has_floruit_date);
			}
			
			//PERSON -> student of manyToMany -> PERSON
			this.entity.removeAllSourceRelationsByName(was_student_of);
			for(Entity teacher : this.studentOfList.getEntities()){
				Entity teacher0 = getWrapper().getEntityByIdWithContent(teacher.getId()); 
				new Relation(this.entity, teacher0, was_student_of);
			}
			
			//PERSON -> has_role -> ROLE
			this.entity.removeAllSourceRelationsByName(has_role);
			for(Entity role : this.roleList.getEntities()){
				Entity role0 = getWrapper().getEntityByIdWithContent(role.getId());
				new Relation(this.entity, role0, has_role);
			}
			
			//REFERENCE -> is_reference_of -> THIS
			this.prepareEndNoteRefs2Save();
			
			// save entity
			this.entity = getWrapper().saveEntity(this.entity, getSessionUser().getEmail(), editPersonIntent);
			this.setEntity(this.entity);
			this.getCache().setMapDirty(true);
			this.getAppBean().getBiographyBean().makeDirty();
			
			logger.info("Entity saved - Time = " + (System.currentTimeMillis() - start) + ", " + entity);
			this.printSuccessSavingEntity();
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			this.printInternalError(e);
		}	
		saveEnd();
		return PAGE_EDITOR;
	}

	public void listenerEditCalendarForLivedInPlaces(ActionEvent event){
		Entity entity = (Entity)getRequestBean("item");
		if(entity != null){
			Calendar cal = this.livedInPlaces.getCalendar(entity.getId());
			getSessionBean().editCalendar(cal, livedInPlaces, entity.getId());
		}
	}
	
	public void listenerEditCalendarForFloruitDate(ActionEvent event){
		Entity entity = (Entity)getRequestBean("item");
		if(entity != null){
			Calendar cal = this.floruitList.getCalendar(entity.getId());
			getSessionBean().editCalendar(cal, floruitList, entity.getId());
		}
	}
	
	public void listenerRomanizeNameTranslit(AjaxBehaviorEvent event){
		if(getAttributes().get("name_translit") != null)
			this.romanizedNameTranslit = RomanizationLoC.convert(getAttributes().get("name_translit"));
	}

	public Entity getShortName() {
		return shortNameAlias;
	}

	public void setShortName(Entity shortName) {
		this.shortNameAlias = shortName;
	}

	public String getValueShortName() {
		return valueShortName;
	}

	public void setValueShortName(String valueShortName) {
		this.valueShortName = valueShortName;
	}
	
	public void setToDate(Object x) {
		//System.out.println("hello");
	}

	public Converter getToDate() {
		return new myDateTimeConverter();

	}
	
	public Map<String, Boolean> getDeletedAliases() {
		return deletedAliases;
	}

	public void setDeletedAliases(Map<String, Boolean> deletedAliases) {
		this.deletedAliases = deletedAliases;
	}

	public void setNewAlias(String newAlias) {
		this.newAlias = newAlias;
	}

	public String getNewAlias() {
		return newAlias;
	}

	public String editMe(){
		if(this.entity != null){
			getSessionBean().editEntity(this.entity);
			return "entry_edit_entity";
		}
		return "";
	}

	public void setBirthPlaceLo(AliasListenerObject birthPlaceLo) {
		this.birthPlaceLo = birthPlaceLo;
	}

	public ListenerObject getBirthPlaceLo() {
		return birthPlaceLo;
	}

	public ListenerObject getDeathPlaceLo() {
		return deathPlaceLo;
	}

	public void setDeathPlaceLo(AliasListenerObject deathPlaceLo) {
		this.deathPlaceLo = deathPlaceLo;
	}

	public EntityList getLivedInPlaces() {
		return livedInPlaces;
	}

	public void setLivedInPlaces(EntityList livedInPlaces) {
		this.livedInPlaces = livedInPlaces;
	}

	public EntityList getStudentOfList() {
		return studentOfList;
	}


	public void setStudentOfList(EntityList studentOfList) {
		this.studentOfList = studentOfList;
	}

	public EntityList getFloruitList() {
		return floruitList;
	}

	public void setFloruitList(EntityList floruitList) {
		this.floruitList = floruitList;
	}

	public String getRomanizedNameTranslit() {
		return romanizedNameTranslit;
	}

	public void setRomanizedNameTranslit(String romanizedNameTranslit) {
		this.romanizedNameTranslit = romanizedNameTranslit;
	}	
}