diff src/main/java/de/mpiwg/itgroup/ismi/entry/beans/CurrentWitnessBean.java @ 143:1c09041f1182

110: The title-as-written-on-manuscript in the witness form does not save Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/ismi/ticket/110
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Fri, 23 Jun 2017 20:40:18 +0200
parents 502ae5b1a07d
children 8049becb05ca
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/CurrentWitnessBean.java	Fri Jun 16 20:16:30 2017 +0200
+++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/CurrentWitnessBean.java	Fri Jun 23 20:40:18 2017 +0200
@@ -111,7 +111,8 @@
 					"notes_on_ownership", "source_of_information"},
 			// source relations
 			new String[] {"is_exemplar_of", "is_part_of", "has_author_misidentification", "was_copied_by",
-					"was_copied_in", "was_copied_in_as", "had_patron", "was_studied_by"},
+					"was_copied_in", "was_copied_in_as", "had_patron", "was_studied_by",
+					"has_author_written_as", "has_title_written_as"},
 			// target relations
 			new String[] {"is_reference_of"});
 
@@ -255,8 +256,9 @@
 			
 			// legacy relations(?)
 			this.entity.removeAllSourceRelationsByName(is_possible_exemplar_of);
-			this.entity.removeAllSourceRelations(rel_has_title_written_as, ALIAS);
-			this.entity.removeAllSourceRelations(rel_has_author_written_as, ALIAS);
+			
+			// update title/author_written_as relations
+			this.updateWrittenAliases();
 			
 			// WITNESS -> is_part_of_codex 
 			if(this.isSelectedSaveAsNew()){
@@ -283,6 +285,85 @@
 		return PAGE_EDITOR;
 	}
 
+	private void updateWrittenAliases() throws Exception {
+
+		User user = getSessionUser();
+
+		/*
+		 * WITNESS -> has_title_written_as -> ALIAS
+		 */
+		if (StringUtils.isNotEmpty(valueTextWritten)) {
+			// check if text is already an alias
+			Entity alias = null;
+			if (this.suggestedTitlesWritten != null) {
+				for (SelectItem item : this.suggestedTitlesWritten) {
+					Long id = (Long) item.getValue();
+					if (id != null) {
+						Entity candidate = getWrapper().getEntityById(id);
+						if (candidate != null && valueTextWritten.equals(candidate.getOwnValue())) {
+							alias = candidate;
+							break;
+						}
+					}
+				}
+			}
+
+			if (alias == null) {
+				// create new alias
+				alias = new Entity(Node.TYPE_ABOX, ALIAS, false);
+				alias.addAttribute(new Attribute(ALIAS, "text", valueTextWritten));
+				alias = getWrapper().saveEntity(alias, user.getEmail(), null);
+				new Relation(alias, this.titleLo.getEntity(), "is_alias_title_of");
+				alias = getWrapper().saveEntity(alias, user.getEmail(), null);
+			}
+			if (alias.isLightweight()) {
+				alias = getWrapper().getEntityByIdWithContent(alias.getId());
+			}
+			// update relation
+			this.entity.replaceUniqueSourceRelation(alias, ALIAS, rel_has_title_written_as);
+		} else {
+			// remove relation
+			this.entity.removeAllSourceRelations(rel_has_title_written_as, ALIAS);
+		}
+
+		/*
+		 * WITNESS -> has_author_written_as -> ALIAS
+		 */
+		if (StringUtils.isNotEmpty(this.valueAuthorWritten)) {
+			// check if text is already an alias
+			Entity alias = null;
+			if (suggestedAuthorsWritten != null) {
+				for (SelectItem item : this.suggestedAuthorsWritten) {
+					Long id = (Long) item.getValue();
+					if (id != null) {
+						Entity candidate = getWrapper().getEntityById(id);
+						if (candidate != null && valueAuthorWritten.equals(candidate.getOwnValue())) {
+							alias = candidate;
+							break;
+						}
+					}
+				}
+			}
+
+			if (alias == null) {
+				// create new alias
+				alias = new Entity(Node.TYPE_ABOX, ALIAS, false);
+				alias.addAttribute(new Attribute(ALIAS, "text", valueAuthorWritten));
+				alias = getWrapper().saveEntity(alias, user.getEmail(), null);
+				new Relation(alias, this.authorLo.getEntity(), "is_alias_name_of");
+				alias = getWrapper().saveEntity(alias, user.getEmail(), null);
+			}
+			if (alias.isLightweight()) {
+				alias = getWrapper().getEntityByIdWithContent(alias.getId());
+			}
+			// update relation 
+			this.entity.replaceUniqueSourceRelation(alias, ALIAS, rel_has_author_written_as);
+		} else {
+			// remove relation
+			this.entity.removeAllSourceRelations(rel_has_author_written_as, ALIAS);
+		}
+	}
+	
 	public String saveAsNewEntity() {
 		this.setSelectedSaveAsNew(true);
 		return save();
@@ -333,7 +414,7 @@
 
 	private List<SelectItem> getTitleAliases(Entity title) {
 		List<SelectItem> items = new ArrayList<SelectItem>();
-		this.valueTextWritten = null;
+		// I don't think so: this.valueTextWritten = null;
 		if(title != null && title.isPersistent()){
 			items.add(new SelectItem(new Long(-1), "-- choose --"));
 			List<Entity> aliasTitleList = getWrapper().getSourcesForTargetRelation(title, is_alias_title_of, ALIAS, -1);
@@ -351,9 +432,9 @@
 	}
 	private List<SelectItem> getAuthorAliases(Entity author) {
 		List<SelectItem> items = new ArrayList<SelectItem>();
-		this.valueAuthorWritten = null;
+		// I don't think so: this.valueAuthorWritten = null;
 		if(author != null && author.isPersistent()){
-			//items.add(new SelectItem(null, "-- choose --"));
+			items.add(new SelectItem(new Long(-1), "-- choose --"));
 			List<Entity> aliasAuthorList = getWrapper().getSourcesForTargetRelation(author, is_alias_name_of, ALIAS, -1);
 			for(Entity alias : aliasAuthorList){
 				items.add(new SelectItem(alias.getId(), alias.getOwnValue()));
@@ -391,57 +472,56 @@
 			// Loading the relations
 			// TODO loading target's relations
 			for (Relation rel : witness.getSourceRelations()) {
-				Entity target = null;
 				String relName = rel.getOwnValue();
 				if (relName.equals(is_exemplar_of)) {
 					// WITNESS -> is_exemplar_of -> TEXT
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					titleLo.setEntityAndAttribute(target);
 					this.updateTitle();
 				} else if (relName.equals(is_possible_exemplar_of)) {
 					// WITNESS -> is_possible_exemplar_of -> TEXT
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					possibleExamplerOfList.add(target);					
 				} else if (relName.equals(rel_was_studied_by)) {
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					studiedByList.add(target);
 				} else if (relName.equals(rel_was_copied_by)) {
 					// WITENSS -> was_copied_by -> PERSON
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					copyistLo.setEntityAndAttribute(target);
 				} else if (relName.equals("was_copied_in")) {
 					// WITENSS -> was_copied_in -> PLACE
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					// don't use place if there is an alias
 					copyPlaceLo.setEntityAndAttributeIfEmpty(target);
                 } else if (relName.equals("was_copied_in_as")) {
                     // WITENSS -> was_copied_in_as -> ALIAS
-                    target = getTargetRelation(rel);
+                	Entity target = getTargetRelation(rel);
                     copyPlaceLo.setEntityAndAttribute(target);
 				} else if (relName.equals(rel_had_patron)) {
 					// WITNESS -> had_patron -> PERSON
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					patronageLo.setEntityAndAttributeName(target, name_translit);
 				} else if (relName.equals(is_part_of)) {
 					// WITNESS -> is_part_of -> CODEX
 					if (rel.getTargetObjectClass().equals(CODEX)) {
-						target = getTargetRelation(rel);
+						Entity target = getTargetRelation(rel);
 						this.setCodex(target);
 					}
 				} else if (relName.equals(Misidentification.HAS_MISIDENT)) {
 					// WITNESS -> has_misidentification -> MISIDENTIFICATION
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					this.misidentTable.load(target);
 					
 				} else if (relName.equals(rel_has_author_written_as)) {
 					//TODO this relation is no in the definitions
-					Entity target2 = getTargetRelation(rel);
-					Attribute alias = getTargetAttribute(target2, "alias");
+					Entity target = getTargetRelation(rel);
+					Attribute alias = getTargetAttribute(target, "alias");
 					this.valueAuthorWritten = (alias != null) ? alias.getValue() : null;
 								
 				} else if (relName.equals(rel_has_title_written_as)) {
 					//TODO this relation is no in the definitions
-					target = getTargetRelation(rel);
+					Entity target = getTargetRelation(rel);
 					//this.textWritten = target;
 					Attribute alias  = getTargetAttribute(target, "alias");
 					this.valueTextWritten = (alias != null) ? alias.getValue() : null;
@@ -519,7 +599,7 @@
 			Long aliasId = (Long) event.getNewValue();
 			if(aliasId != null){
 				if(aliasId.equals(-1)){
-					this.valueTextWritten = null;
+					//I don't think so: this.valueTextWritten = null;
 				}else{
 					Entity alias = getWrapper().getEntityByIdWithContent(aliasId);
 					if(alias != null){
@@ -539,7 +619,7 @@
 			Long aliasId = (Long) event.getNewValue();
 			if(aliasId != null){
 				if(aliasId.equals(-1)){
-					this.valueAuthorWritten = null;
+					//I don't think so: this.valueAuthorWritten = null;
 				}else{
 					Entity alias = getWrapper().getEntityByIdWithContent(aliasId);
 					if(alias != null){