# HG changeset patch # User casties # Date 1475238633 -7200 # Node ID fd7beb701724c06536f444945bfdb53e4303626b # Parent e2f6ccc4d3229d0c06767fae28bc672e587775be working on "merge reference" feature. diff -r e2f6ccc4d322 -r fd7beb701724 src/main/java/de/mpiwg/itgroup/ismi/entry/beans/SessionBean.java --- a/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/SessionBean.java Wed Sep 28 10:47:39 2016 +0200 +++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/SessionBean.java Fri Sep 30 14:30:33 2016 +0200 @@ -27,6 +27,7 @@ import de.mpiwg.itgroup.ismi.event.beans.StudyEvent; import de.mpiwg.itgroup.ismi.event.beans.TransferEvent; import de.mpiwg.itgroup.ismi.merge.GeneralMerge; +import de.mpiwg.itgroup.ismi.merge.ReferenceMerge; import de.mpiwg.itgroup.ismi.publicView.DynamicPageEditor; import de.mpiwg.itgroup.ismi.publicView.PublicCodexBean; import de.mpiwg.itgroup.ismi.publicView.PublicCodexView; @@ -107,6 +108,7 @@ //private Entity currentEntity; private GeneralMerge generalMerge; + private ReferenceMerge referenceMerge; //private String last_action; //private Date time_of_lastAction; @@ -239,6 +241,7 @@ logger.info("login " + username + " [remoteAddr=" + remoteAddr + "]"); this.setUser(user); this.generalMerge = new GeneralMerge(); + this.referenceMerge = new ReferenceMerge(); this.defForm = new DefinitionForm(); // refresh the editor of Dirk addSessionBean("CurrentWitness", new CurrentWitnessBean()); @@ -264,6 +267,7 @@ this.password = ""; this.user = null; this.generalMerge = null; + this.referenceMerge = null; this.defForm = null; return PAGE_PUBLIC_CODICES; } @@ -928,6 +932,14 @@ this.generalMerge = generalMerge; } + public ReferenceMerge getReferenceMerge() { + return referenceMerge; + } + + public void setReferenceMerge(ReferenceMerge referenceMerge) { + this.referenceMerge = referenceMerge; + } + public DefinitionForm getDefForm() { return defForm; } diff -r e2f6ccc4d322 -r fd7beb701724 src/main/java/de/mpiwg/itgroup/ismi/merge/ReferenceMerge.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/ismi/merge/ReferenceMerge.java Fri Sep 30 14:30:33 2016 +0200 @@ -0,0 +1,622 @@ +package de.mpiwg.itgroup.ismi.merge; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +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.Relation; +import org.mpi.openmind.repository.services.utils.AttributeFilter; + +import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean; + +public class ReferenceMerge extends AbstractISMIBean implements Serializable{ + + private static Logger logger = Logger.getLogger(ReferenceMerge.class); + + private static final long serialVersionUID = 1L; + public static String FIRST_VALUE = "first value"; + public static String SECOND_VALUE = "second value"; + public static String IGNORE = "ignore"; + public static String TAKE = "take"; + + private boolean showAttributeMapping = false; + private boolean showSrcRelationMapping = false; + private boolean showTarRelationMapping = false; + + private boolean entitiesLoaded = false; + + private Map firstAttMap = new HashMap(); + private Map secondAttMap = new HashMap(); + + private Entity firstEntity; + private String firstBibId; + private Entity secondEntity; + private String secondBibId; + + private Map firstEntityMap; + private Map secondEntityMap; + + private String firstId; + private String secondId; + + private Entity entResult; + private List resultAtts; + private List resultSrcRels; + private List resultTarRels; + + private List attLabels; + private Map selectedAtts; + + Map selectedFirstSrcRelations; + Map selectedSecondSrcRelations; + Map selectedFirstTarRelations; + Map selectedSecondTarRelations; + + public ReferenceMerge(){} + + public void loadFirstEntity(ActionEvent event){ + reset(); + try{ + Long id = new Long(this.firstId); + List filterList = new ArrayList(); + // search using regexp match in endnote-id attribute + filterList.add(new AttributeFilter("endnote-id", "#" + id + "[[:>:]]", REFERENCE, true)); + firstEntityMap = getWrapper().searchEntityByAttributeFilter(filterList, 1000); + if(this.firstEntityMap.isEmpty()){ + addErrorMsg("No references found."); + } else { + // show first entity as example + Entity ent = firstEntityMap.keySet().iterator().next(); + this.firstEntity = ent; + Attribute att = firstEntityMap.get(ent); + this.firstBibId = att.getOwnValue(); + + if(this.secondEntityMap != null && ! this.secondEntityMap.isEmpty()){ + this.deployDifferences(); + } + } + } catch(Exception e) { + addErrorMsg("The first entity could no be loaded."); + } + } + + public void loadSecondEntity(ActionEvent event){ + reset(); + try{ + Long id = new Long(this.secondId); + List filterList = new ArrayList(); + // search using regexp match in endnote-id attribute + filterList.add(new AttributeFilter("endnote-id", "#" + id + "[[:>:]]", REFERENCE, true)); + secondEntityMap = getWrapper().searchEntityByAttributeFilter(filterList, 1000); + if(this.secondEntityMap.isEmpty()){ + addErrorMsg("No references found."); + } else { + // show first entity as example + Entity ent = secondEntityMap.keySet().iterator().next(); + this.secondEntity = ent; + Attribute att = secondEntityMap.get(ent); + this.secondBibId = att.getOwnValue(); + + if(! this.firstEntityMap.isEmpty()){ + this.deployDifferences(); + } + } + }catch(Exception e){ + addErrorMsg( "The second entity could no be loaded."); + } + } + + @Override + public void reset(){ + this.attLabels = new ArrayList(); + this.selectedAtts = new HashMap(); + this.selectedFirstSrcRelations = new HashMap(); + this.selectedSecondSrcRelations = new HashMap(); + this.selectedFirstTarRelations = new HashMap(); + this.selectedSecondTarRelations = new HashMap(); + this.entResult = null; + + this.entitiesLoaded = false; + this.showAttributeMapping = false; + this.showSrcRelationMapping = false; + this.showTarRelationMapping = false; + } + + public void listenerExecuteMerge(){ + this.executeMerge(); + getAppBean().getSimpleSearchCache().setMapDirty(true); + } + + private void deployDifferences(){ + this.showAttributeMapping = true; + this.showSrcRelationMapping = true; + this.showTarRelationMapping = true; + this.entitiesLoaded = true; + if(this.firstEntity != null && this.secondEntity != null){ + + this.attLabels = new ArrayList(); + + for (Entity ent : this.secondEntityMap.keySet()) { + + if (ent.isLightweight()) { + ent = getWrapper().getEntityContent(ent); + } + attLabels.add(ent.getId().toString()); + + } + + /* + if(secondEntity.isLightweight()){ + this.secondEntity = getWrapper().getEntityContent(this.secondEntity); + } + + this.firstEntity = (Entity)firstEntity.clone(); + this.secondEntity = (Entity) secondEntity.clone(); + + //attributes + this.attLabels = new ArrayList(); + this.selectedAtts = new HashMap(); + this.firstAttMap = new HashMap(); + this.secondAttMap = new HashMap(); + + for(Attribute att : this.firstEntity.getAttributes()){ + firstAttMap.put(att.getName(), att.getValue()); + if(!attLabels.contains(att.getName())){ + attLabels.add(att.getName()); + selectedAtts.put(att.getName(), FIRST_VALUE); + } + } + + for(Attribute att : this.secondEntity.getAttributes()){ + secondAttMap.put(att.getName(), att.getValue()); + if(!attLabels.contains(att.getName())){ + attLabels.add(att.getName()); + selectedAtts.put(att.getName(), FIRST_VALUE); + } + } + + //source relations + this.selectedFirstSrcRelations = new HashMap(); + this.selectedSecondSrcRelations = new HashMap(); + + for(Relation rel : this.firstEntity.getSourceRelations()){ + rel.setTarget(getWrapper().getEntityById(rel.getTargetId())); + selectedFirstSrcRelations.put(rel.getId(), TAKE); + } + + + for(Relation rel : this.secondEntity.getSourceRelations()){ + rel.setTarget(getWrapper().getEntityById(rel.getTargetId())); + selectedSecondSrcRelations.put(rel.getId(), TAKE); + } + + //target relations + this.selectedFirstTarRelations = new HashMap(); + this.selectedSecondTarRelations = new HashMap(); + + for(Relation rel : this.firstEntity.getTargetRelations()){ + rel.setSource(getWrapper().getEntityById(rel.getSourceId())); + selectedFirstTarRelations.put(rel.getId(), TAKE); + } + + for(Relation rel : this.secondEntity.getTargetRelations()){ + rel.setSource(getWrapper().getEntityById(rel.getSourceId())); + selectedSecondTarRelations.put(rel.getId(), TAKE); + } + + */ + } + } + + public void preview(ActionEvent event){ + this.generateResultEntity(); + } + + private void executeMerge(){ + + logger.info("Starting merge execution " + firstEntity.getObjectClass() + + " ["+ getUserName() +"]" + + "[firstEntity=" + firstEntity.getId() + + ", secondEntity=" + secondEntity.getId() + "]"); + + try { + this.generateResultEntity(); + if(this.entResult != null){ + + this.printMergeInfo(entResult); + + this.getWrapper().saveEntity(this.entResult, getSessionUser().getEmail() + "_merge"); + + this.getWrapper().removeCurrentVersionEntity(this.firstEntity); + this.getWrapper().removeCurrentVersionEntity(this.secondEntity); + + //the old relations should be removed, before... + this.generateSecundaryOW(this.entResult, getSessionUser().getEmail() + "_merge"); + + this.printMergeInfo(entResult); + + logger.info("Merge execution 'successful' " + + firstEntity.getObjectClass() + + " ["+ getUserName() +"]" + + "[firstEntity=" + firstEntity.getId() + + ", secondEntity=" + secondEntity.getId() + + ", generatedEntity=" + entResult.getId() + "]"); + + this.firstEntity = null; + this.secondEntity = null; + + addGeneralMsg("The entities were merged successfully"); + addGeneralMsg("The new entity has the id " + this.entResult.getId()); + this.reset(); + } + } catch (Exception e) { + printInternalError(e); + logger.error("["+ getUserName() +"] " + e.getMessage(), e); + } + } + + private void printMergeInfo(Entity ent){ + StringBuilder sb = new StringBuilder("\n\n"); + + sb.append("-------------------------------------------"); + sb.append("-----------------------------------------\n"); + sb.append("Merging result [" + getUserName() + "]\n"); + sb.append(ent.toString() + "\n"); + sb.append("Attributes:\n"); + for(Attribute att : ent.getAttributes()){ + sb.append("\t" + att.toString() + "\n"); + } + + sb.append("Src Relations:\n"); + for(Relation src : ent.getSourceRelations()){ + sb.append("\t" + src.toString() + "\n"); + } + + sb.append("Tar Relations:\n"); + for(Relation tar : ent.getTargetRelations()){ + sb.append("\t" + tar.toString() + "\n"); + } + + sb.append("-------------------------------------------"); + sb.append("-----------------------------------------\n"); + logger.info(sb.toString()); + } + + public void actionShowTarRelationMapping(ActionEvent event){ + this.showTarRelationMapping = true; + } + + public void actionHideTarRelationMapping(ActionEvent event){ + this.showTarRelationMapping = false; + } + + public void actionShowSrcRelationMapping(ActionEvent event){ + this.showSrcRelationMapping = true; + } + + public void actionHideSrcRelationMapping(ActionEvent event){ + this.showSrcRelationMapping = false; + } + + public void actionShowAttributeMapping(ActionEvent event){ + this.showAttributeMapping = true; + } + + public void actionHideAttributeMapping(ActionEvent event){ + this.showAttributeMapping = false; + } + + private void generateResultEntity(){ + this.entResult = new Entity(); + this.entResult.setLightweight(false); + this.entResult.setObjectClass(this.firstEntity.getObjectClass()); + + //generating attributes + try{ + for(String attName : this.selectedAtts.keySet()){ + String selected = this.selectedAtts.get(attName); + String value = ""; + if(selected.equals(FIRST_VALUE)){ + value = (firstEntity.getAttributeByName(attName) == null) ? "" : firstEntity.getAttributeByName(attName).getOwnValue(); + }else if(selected.equals(SECOND_VALUE)){ + value = (secondEntity.getAttributeByName(attName) == null) ? "" : secondEntity.getAttributeByName(attName).getOwnValue(); + } + this.entResult.addAttribute(new Attribute(attName, "text", value)); + } + }catch(Exception e){ + e.printStackTrace(); + addErrorMsg("Please inform support of this exception: " + e.getMessage()); + } + + + //generating source relations + for(Relation rel : firstEntity.getSourceRelations()){ + String selectedValue = this.selectedFirstSrcRelations.get(rel.getId()); + if(StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)){ + if(!this.entResult.containsSourceRelation(rel.getOwnValue(), rel.getTargetId())){ + this.entResult.addSourceRelation(generateSrcRelation(rel)); + } + } + } + + for(Relation rel : secondEntity.getSourceRelations()){ + String selectedValue = this.selectedSecondSrcRelations.get(rel.getId()); + if(StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)){ + if(!this.entResult.containsSourceRelation(rel.getOwnValue(), rel.getTargetId())){ + this.entResult.addSourceRelation(generateSrcRelation(rel)); + } + } + } + + //generating target relations + for(Relation rel : firstEntity.getTargetRelations()){ + String selectedValue = this.selectedFirstTarRelations.get(rel.getId()); + if(StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)){ + //ensuring that there is no two equals relations. + if(!this.entResult.containsTargetRelation(rel.getOwnValue(), rel.getSourceId())){ + this.entResult.addTargetRelation(generateTarRelation(rel)); + } + } + } + + for(Relation rel : secondEntity.getTargetRelations()){ + String selectedValue = this.selectedSecondTarRelations.get(rel.getId()); + if(StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)){ + if(!this.entResult.containsTargetRelation(rel.getOwnValue(), rel.getSourceId())){ + this.entResult.addTargetRelation(generateTarRelation(rel)); + } + } + } + } + + private Relation generateSrcRelation(Relation rel){ + + Relation newRel = new Relation(); + newRel.setOwnValue(rel.getOwnValue()); + newRel.setTarget(getWrapper().getEntityById(rel.getTargetId())); + + return newRel; + } + + private Relation generateTarRelation(Relation rel){ + Relation newRel = new Relation(); + newRel.setOwnValue(rel.getOwnValue()); + newRel.setSource(getWrapper().getEntityById(rel.getSourceId())); + return newRel; + } + + public List getAttSelectItems() { + List items = new ArrayList(); + items.add(new SelectItem(FIRST_VALUE)); + items.add(new SelectItem(SECOND_VALUE)); + items.add(new SelectItem(IGNORE)); + return items; + } + + public List getRelSelectItems(){ + List items = new ArrayList(); + items.add(new SelectItem(TAKE)); + items.add(new SelectItem(IGNORE)); + return items; + } + + public Entity getEntResult() { + return entResult; + } + + public void setEntResult(Entity entResult) { + this.entResult = entResult; + } + + public List getResultAtts() { + return resultAtts; + } + + public void setResultAtts(List resultAtts) { + this.resultAtts = resultAtts; + } + + public List getResultSrcRels() { + return resultSrcRels; + } + + public void setResultSrcRels(List resultSrcRels) { + this.resultSrcRels = resultSrcRels; + } + + public List getResultTarRels() { + return resultTarRels; + } + + public void setResultTarRels(List resultTarRels) { + this.resultTarRels = resultTarRels; + } + + public Entity getFirstEntity() { + return firstEntity; + } + + public void setFirstEntity(Entity firstEntity) { + this.firstEntity = firstEntity; + } + + public Entity getSecondEntity() { + return secondEntity; + } + + public void setSecondEntity(Entity secondEntity) { + this.secondEntity = secondEntity; + } + + public String getFirstId() { + return firstId; + } + + public void setFirstId(String firstId) { + this.firstId = firstId; + } + + public String getSecondId() { + return secondId; + } + + public void setSecondId(String secondId) { + this.secondId = secondId; + } + + public List getAttLabels() { + return attLabels; + } + + public void setAttLabels(List attLabels) { + this.attLabels = attLabels; + } + + public Map getFirstAttMap() { + return firstAttMap; + } + + public void setFirstAttMap(Map firstAttMap) { + this.firstAttMap = firstAttMap; + } + + public Map getSecondAttMap() { + return secondAttMap; + } + + public void setSecondAttMap(Map secondAttMap) { + this.secondAttMap = secondAttMap; + } + + public Map getSelectedAtts() { + return selectedAtts; + } + + public void setSelectedAtts(Map selectedAtts) { + this.selectedAtts = selectedAtts; + } + + public boolean isShowAttributeMapping() { + return showAttributeMapping; + } + + public void setShowAttributeMapping(boolean showAttributeMapping) { + this.showAttributeMapping = showAttributeMapping; + } + + public boolean isEntitiesLoaded() { + return entitiesLoaded; + } + + public void setEntitiesLoaded(boolean entitiesLoaded) { + this.entitiesLoaded = entitiesLoaded; + } + + public Map getSelectedFirstSrcRelations() { + return selectedFirstSrcRelations; + } + + public void setSelectedFirstSrcRelations( + Map selectedFirstSrcRelations) { + this.selectedFirstSrcRelations = selectedFirstSrcRelations; + } + + public Map getSelectedSecondSrcRelations() { + return selectedSecondSrcRelations; + } + + public void setSelectedSecondSrcRelations( + Map selectedSecondSrcRelations) { + this.selectedSecondSrcRelations = selectedSecondSrcRelations; + } + public boolean isShowSrcRelationMapping() { + return showSrcRelationMapping; + } + + public void setShowSrcRelationMapping(boolean showSrcRelationMapping) { + this.showSrcRelationMapping = showSrcRelationMapping; + } + + + public boolean isShowTarRelationMapping() { + return showTarRelationMapping; + } + + public void setShowTarRelationMapping(boolean showTarRelationMapping) { + this.showTarRelationMapping = showTarRelationMapping; + } + + public Map getSelectedFirstTarRelations() { + return selectedFirstTarRelations; + } + + public void setSelectedFirstTarRelations( + Map selectedFirstTarRelations) { + this.selectedFirstTarRelations = selectedFirstTarRelations; + } + + public Map getSelectedSecondTarRelations() { + return selectedSecondTarRelations; + } + + public void setSelectedSecondTarRelations( + Map selectedSecondTarRelations) { + this.selectedSecondTarRelations = selectedSecondTarRelations; + } + /* + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public boolean isShowMsgPopup() { + return showMsgPopup; + } + + public void setShowMsgPopup(boolean showMsgPopup) { + this.showMsgPopup = showMsgPopup; + } + + public String getConfirmMsg() { + return confirmMsg; + } + + public void setConfirmMsg(String confirmMsg) { + this.confirmMsg = confirmMsg; + } + + public boolean isShowConfirmPopup() { + return showConfirmPopup; + } + + public void setShowConfirmPopup(boolean showConfirmPopup) { + this.showConfirmPopup = showConfirmPopup; + } + */ + + /** + * @return the firstBibId + */ + public String getFirstBibId() { + return firstBibId; + } + + /** + * @return the secondBibId + */ + public String getSecondBibId() { + return secondBibId; + } +} \ No newline at end of file diff -r e2f6ccc4d322 -r fd7beb701724 src/main/webapp/merge/referenceMerge.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/webapp/merge/referenceMerge.xhtml Fri Sep 30 14:30:33 2016 +0200 @@ -0,0 +1,379 @@ + + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + (loading reference...) + + + + + + + + + + + + + + + + (loading reference...) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + \ No newline at end of file diff -r e2f6ccc4d322 -r fd7beb701724 src/main/webapp/templates/main_template.xhtml --- a/src/main/webapp/templates/main_template.xhtml Wed Sep 28 10:47:39 2016 +0200 +++ b/src/main/webapp/templates/main_template.xhtml Fri Sep 30 14:30:33 2016 +0200 @@ -147,9 +147,12 @@ - Merge + Join Refs + Browse Repository