view src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/MisattributionTable.java @ 216:93d33f138c9e default tip

update medeniyet image server URL.
author casties
date Wed, 26 Jan 2022 16:39:32 +0100
parents 502ae5b1a07d
children
line wrap: on
line source

package de.mpiwg.itgroup.ismi.util.guiComponents;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Relation;

import de.mpiwg.itgroup.ismi.auxObjects.ListenerObject;
import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean;
import de.mpiwg.itgroup.ismi.utils.SelectableObject;

public class MisattributionTable extends AbstractISMIBean {
	private static final long serialVersionUID = -2756216426560705499L;
	private static Logger logger = Logger.getLogger(MisattributionTable.class);

	private List<SelectableObject<Misattribution>> list = new ArrayList<SelectableObject<Misattribution>>();
	
	private Misattribution misatt;
	
	private ListenerObject lo = new ListenerObject(PERSON, "name_translit");
	//private String attName;
	//private String oc;
	
	public MisattributionTable(){
		/*
		this.attName = attName;
		this.oc = oc;
		*/
	}
	
	public void listenerRemove(ActionEvent event){		
		List<SelectableObject<Misattribution>> toDelete = new ArrayList<SelectableObject<Misattribution>>();
		
		for(SelectableObject<Misattribution> so : this.list){
			if(so.isSelected()){
				toDelete.add(so);
			}
		}
		for(SelectableObject<Misattribution> so : toDelete){
			this.list.remove(so);
		}
	}
	
	public void listenerEditRef(ActionEvent event){
		SelectableObject<Misattribution> so = 
			(SelectableObject<Misattribution>)getRequestBean("item");
		if(so != null){
			this.misatt = so.getObj();
		}
	}
	
	public void listenerSaveRef(ActionEvent event){
		this.misatt = null;
	}
	
	public void listenerCancel(ActionEvent event){
		//@TODO
	}
	
	public void listenerCreate(ActionEvent event){
		if(this.lo.entity != null & this.lo.entity.isPersistent()){
			this.create(this.lo.entity);	
		}
	}
	
	public void load(Entity misattEntity){
		if(!containsEntity(misattEntity)){
			//TODO sort
			//Collections.sort(this.misattList);
			try {
				this.list.add(new SelectableObject<Misattribution>(Misattribution.load(misattEntity, getWrapper(), getUserName())));
			} catch (Exception e) {
				addErrorMsg(e.getMessage());
				e.printStackTrace();
			}
		}else{
			addGeneralMsg("This entity has been already inserted!");
		}
	}
	
	public void create(Entity person){
		try {
			this.list.add(new SelectableObject<Misattribution>(Misattribution.create(person, getWrapper(), getUserName())));
		} catch (Exception e) {
			addErrorMsg(e.getMessage());
			e.printStackTrace();
		}
	}
	
	private boolean containsEntity(Entity ent){
		for(SelectableObject<Misattribution> so : this.list){
			if(so.getObj().getPerson() != null && so.getObj().getPerson().getId().equals(ent.getId())){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Save all misattributions on this text.
	 * 
	 * Deletes removed misattributions.
	 * 
	 * @param text
	 * @return
	 * @throws Exception
	 */
	public Entity saveMisattributions(Entity text) throws Exception {
		// remember old misattributions
		List<Relation> oldRels = text.getSourceRelations(Misattribution.HAS_AUTHOR_MISATT, Misattribution.MISATT);
		List<Entity> misatts = new ArrayList<Entity>();
		// save new misattributions
		for (Misattribution misatt : this.getMisattList()) {
			misatt.saveMisattribution(text);
			misatts.add(misatt.getMisatt());
		}
		// update relations
		this.replaceMultipleSourceRelations(text, misatts, Misattribution.MISATT, Misattribution.HAS_AUTHOR_MISATT);
		// check for orphaned old misattributions
		for (Relation rel : oldRels) {
			boolean found = false;
			for (Entity miEnt : misatts) {
				if (miEnt != null && rel.getTargetId() == miEnt.getId()) {
					found = true;
					break;
				}
			}
			if (!found) {
				Entity miEnt = getWrapper().getEntityById(rel.getTargetId());
				logger.warn("Orphaned MISATTRIBUTION will be deleted: " + miEnt);
				getWrapper().removeCurrentVersionEntity(miEnt, getUserName());
			}
		}
		return text;
	}	
	
	private List<Misattribution> getMisattList() {
		List<Misattribution> list = new ArrayList<Misattribution>();
		for(SelectableObject<Misattribution> so : this.list){
			list.add(so.getObj());
		}
		return list;
	}
	
	public List<SelectableObject<Misattribution>> getList() {
		return list;
	}

	public void setList(List<SelectableObject<Misattribution>> list) {
		this.list = list;
	}

	public Misattribution getMisatt() {
		return misatt;
	}

	public void setMisatt(Misattribution misatt) {
		this.misatt = misatt;
	}

	public ListenerObject getLo() {
		return lo;
	}

	public void setLo(ListenerObject lo) {
		this.lo = lo;
	}
}