changeset 128:aef031e76278

105: MISATTRIBUTION should be deleted when detached Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/ismi/ticket/105
author casties
date Wed, 01 Feb 2017 18:20:04 +0100
parents efeab3bd856f
children 42193ad7b28c
files src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/MisattributionTable.java
diffstat 1 files changed, 29 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/MisattributionTable.java	Wed Feb 01 18:09:09 2017 +0100
+++ b/src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/MisattributionTable.java	Wed Feb 01 18:20:04 2017 +0100
@@ -5,7 +5,9 @@
 
 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;
@@ -13,6 +15,7 @@
 
 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>>();
 	
@@ -97,32 +100,42 @@
 		return false;
 	}
 	
-	/*
-	public void listenerChanged(ValueChangeEvent event) {
-		this.lo = changeListener(event, lo, this.oc, this.attName);
-		if(lo.entity != null && lo.entity.isPersistent()){
-			lo.statusImage.setStatus(StatusImage.STATUS_OK);
-		}else{
-			lo.statusImage.setStatus(StatusImage.STATUS_UNSET);
-		}
-	}
-	*/
-
-	
 	/**
 	 * Save all misattributions.
 	 * 
+	 * Deletes removed misattributions.
+	 * 
 	 * @param text
 	 * @return
 	 * @throws Exception
 	 */
-	public Entity saveMisattributions(Entity text) throws Exception{
-		for(Misattribution misatt : this.getMisattList()){
+	public Entity saveMisattributions(Entity text) throws Exception {
+		// remember old misattributions
+		List<Relation> oldRels = text.getSourceRelations(Misattribution.HAS_AUTHOR_MISATT, Misattribution.MISATT);
+		// delete old relations
+		text.removeAllSourceRelationsByName(Misattribution.HAS_AUTHOR_MISATT);
+		// create new misattributions
+		for (Misattribution misatt : this.getMisattList()) {
 			text = misatt.saveAndGetMisattribution(text);
 		}
+		// check for orphaned old misattributions
+		for (Relation rel : oldRels) {
+			boolean match = false;
+			for (Misattribution mi : this.getMisattList()) {
+				Entity miEnt = mi.getMisatt();
+				if (miEnt != null && rel.getTargetId() == miEnt.getId()) {
+					match = true;
+					break;
+				}
+			}
+			if (!match) {
+				Entity ent = getWrapper().getEntityById(rel.getTargetId());
+				logger.warn("Orphaned MISATTRIBUTION will be deleted: " + ent);
+				getWrapper().removeCurrentVersionEntity(ent, getUserName());
+			}
+		}
 		return text;
-	}
-	
+	}	
 	
 	private List<Misattribution> getMisattList() {
 		List<Misattribution> list = new ArrayList<Misattribution>();