view src/main/java/de/mpiwg/itgroup/ismi/entry/beans/CurrentCityBean.java @ 124:28a0c2726466

changed saveEntity() to use new EditIntent. Set up EditIntent for TEXT, PERSON, REFERENCE.
author casties
date Mon, 30 Jan 2017 20:35:36 +0100
parents 99f9a55a4957
children eee2e0ca4032
line wrap: on
line source

package de.mpiwg.itgroup.ismi.entry.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.apache.commons.lang.StringUtils;
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 de.mpiwg.itgroup.ismi.auxObjects.ListenerObject;
import de.mpiwg.itgroup.ismi.util.guiComponents.EntityList;
import de.mpiwg.itgroup.ismi.utils.SelectableObject;


public class CurrentCityBean  extends CodexEditorTemplate  implements Serializable {
	private static final long serialVersionUID = -8609055286714729597L;
	
	private static Logger logger = Logger.getLogger(CurrentCityBean.class);
	
	private List<SelectItem> suggestedTypes;
	
	private List<SelectableObject<Entity>> placesPartOfThis;
	//private List<SelectableObject<Entity>> peopleLivedIn;
    private EntityList aliasList;
		
	public CurrentCityBean() {
		this.reset();
	}
	
	@Override
	public void reset(){
		super.reset();
		this.entity = new Entity(Node.TYPE_ABOX, PLACE, false);
		this.suggestedTypes = new ArrayList<SelectItem>();
		
		this.placesPartOfThis = new ArrayList<SelectableObject<Entity>>();
		//this.peopleLivedIn = new ArrayList<SelectableObject<Entity>>();
		this.aliasList = new EntityList(ALIAS, "alias", getWrapper(), getUserName());
		
		//setDefinition(getDefinition(PLACE));
		setDefObjectClass(PLACE);
		// old types
		String[] types = new String[]{"city","institution", "city_part", "region"};
		// get new types from definition
		Attribute typeAtt = getWrapper().getDefAttributeByOwnValue(PLACE, "type");
		if (typeAtt != null) {
		    types = typeAtt.getPossibleValuesList().toArray(types);
		}
		fillList(suggestedTypes, types);
		
		registerChecker(getCountryLo(), "Country not valid!");
	}
	
	@Override
    public void setEntity(Entity city) {
        this.reset();
        this.entity = city;

        if (this.entity.isPersistent()) {
            // load attributes and relations of Entity
            if (city.isLightweight()) {
                this.entity = getWrapper().getEntityContent(this.entity);
            }
            // load entity attributes into bean
            this.loadAttributes(this.entity);

            /*
             * create list of places this place is part_of
             */
            List<Entity> list = getWrapper().getTargetsForSourceRelation(city, is_part_of, PLACE, -1);
            if (list.size() > 0) {
                this.setCountry(list.get(0));
                // part_of more than one place
                if (list.size() > 1) {
                    addErrorMsg("Inconsistency: this place 'is part of' " + list.size() + " other places!");
                    addErrorMsg("List:");
                    for (Entity place : list) {
                        addErrorMsg(place.getOwnValue() + " [" + place.getId() + "]");
                    }
                }
            }
            
            /*
             * create list of places that are part of this place
             */
            for (Entity place : getWrapper().getSourcesForTargetRelation(city, is_part_of, PLACE, -1)) {
                String label = place.getOwnValue() + " [" + place.getId() + "]";
                this.placesPartOfThis.add(new SelectableObject<Entity>(place, label));
            }

            /*
             * create list of alias names of this place
             */
            for (Entity alias : getWrapper().getSourcesForTargetRelation(city, is_alias_name_of, ALIAS, -1)) {
                this.aliasList.add(alias);
            }

            // load references
            this.loadEndNoteRefs();

            this.setCurrentId(this.entity.getId().toString());
            // check consistency and set checkmark(s)
            this.checkConsistencyFromCountryToCodex();
        }
    }
	
	/* (non-Javadoc)
	 * @see de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean#save()
	 */
	@Override
	public String save(){
		super.save();
		try {
		
			CheckResults cr = getCheckResults();
			if (cr.hasErrors){
				getSessionBean().setErrorMessages(cr);
				getSessionBean().setDisplayError(true);
				this.setSelectedSaveAsNew(false);
				return "SAVE_ERROR";
			}
			
			this.entity = this.updateEntityAttributes(this.entity);
			
			String cityName = (this.entity.getAttributeByName(name) != null) ? this.entity.getAttributeByName(name).getValue() : "";
			if(StringUtils.isNotEmpty(cityName)){
			    // check if name is unique
				if(!checkUnityCity(cityName, (isSelectedSaveAsNew()) ? null : this.entity.getId(), this.getCountryLo().entity)){
					this.renderUnityCheckerDialog();
					this.setSelectedSaveAsNew(false);
					return "SAVE_ERROR";
				}
			}else{
				this.addErrorMsg("This place has not been saved, because its name was empty.");
				this.addErrorMsg("Please enter a name.");
				return "SAVE_ERROR";
			}
			
			this.entity.replaceSourceRelation(this.getCountryLo().entity, PLACE, is_part_of);
						
			this.entity.removeAllTargetRelationsByName(is_part_of);
			for(SelectableObject<Entity> so : this.placesPartOfThis){
				Entity place = getWrapper().getEntityByIdWithContent(so.getObj().getId());
				new Relation(place, this.entity, is_part_of);
			}
			
			/*
			this.entity.removeAllTargetRelationsByName(lived_in);
			for(SelectableObject<Entity> so : this.peopleLivedIn){
				Entity person = getWrapper().getEntityByIdWithContent(so.getObj().getId());
				Relation livedIn = new Relation(person, this.entity, lived_in);
			}
			*/
			
            //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);
            }
            
			//REFERENCE -> is_reference_of -> THIS
			//this.entity = this.prepareReferencesToSave(this.entity);
			this.prepareEndNoteRefs2Save();		
			
			//String lastAction = "";
			if(this.isSelectedSaveAsNew()){
				//this.entity.removeAllTargetRelations(is_in, REPOSITORY);
				//this.entity = getWrapper().saveEntityAsNew(this.entity, getSessionUser().getEmail());
				this.setSelectedSaveAsNew(false);
				//lastAction = "Save place as new entity";
			}else{
				this.entity = getWrapper().saveEntity(this.entity, getSessionUser().getEmail(), null);
				//lastAction = "Save place";
			}
			//setActionInfo(lastAction);
			this.updateRelatedOW(this.entity, getSessionUser().getEmail());
			this.setCurrentId(this.entity.getId().toString());
			
			logger.debug("Place saved - Time = " + (System.currentTimeMillis() - start) + ", " + entity);
			this.printSuccessSavingEntity();
			
			this.setSelectedSaveAsNew(false);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			this.printInternalError(e);
		}
		saveEnd();
		
		return PAGE_EDITOR;
	}
	
	public void listenerRemovePlacesPartOfThis(){
		for(SelectableObject<Entity> so : new ArrayList<SelectableObject<Entity>>(this.placesPartOfThis)){
			if(so.isSelected()){
				this.placesPartOfThis.remove(so);
			}
		}
	}
	
	/* public void listenerRemovePeopleLivedIn(){
		for(SelectableObject<Entity> so : new ArrayList<SelectableObject<Entity>>(this.peopleLivedIn)){
			if(so.isSelected()){
				this.peopleLivedIn.remove(so);
			}
		}
	} */
	
    public void removeThisCountryAction(ActionEvent event) {
        if (this.getCountryLo().entity != null) {
            this.setCountryLo(new ListenerObject());
        }
    }	    

	
	
	public String saveAsNewEntity(){
		this.setSelectedSaveAsNew(true);
		return save();
	}
	public List<SelectItem> getSuggestedTypes() {
		return suggestedTypes;
	}
	public void setSuggestedTypes(List<SelectItem> suggestedTypes) {
		this.suggestedTypes = suggestedTypes;
	}

	public List<SelectableObject<Entity>> getPlacesPartOfThis() {
		return placesPartOfThis;
	}

	public void setPlacesPartOfThis(List<SelectableObject<Entity>> placesPartOfThis) {
		this.placesPartOfThis = placesPartOfThis;
	}

	/* public List<SelectableObject<Entity>> getPeopleLivedIn() {
		return peopleLivedIn;
	}

	public void setPeopleLivedIn(List<SelectableObject<Entity>> peopleLivedIn) {
		this.peopleLivedIn = peopleLivedIn;
	} */

    /**
     * @return the aliasList
     */
    public EntityList getAliasList() {
        return aliasList;
    }

    /**
     * @param aliasList the aliasList to set
     */
    public void setAliasList(EntityList aliasList) {
        this.aliasList = aliasList;
    }
}