Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / MergeGroupHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.bulkeditor.handler;
11
12 import java.util.Set;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.text.source.Annotation;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.handlers.HandlerUtil;
23 import org.eclipse.ui.texteditor.IDocumentProvider;
24
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27 import eu.etaxonomy.cdm.model.reference.Reference;
28 import eu.etaxonomy.cdm.strategy.merge.MergeException;
29 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityContainer;
30 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
31 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
32 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
33 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
34 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 /**
38 * <p>MergeGroupHandler class.</p>
39 *
40 * @author p.ciardelli
41 * @created 25.06.2009
42 * @version 1.0
43 */
44 public class MergeGroupHandler extends AbstractHandler {
45 private static final Logger logger = Logger
46 .getLogger(MergeGroupHandler.class);
47
48 /* (non-Javadoc)
49 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
50 */
51 /** {@inheritDoc} */
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 IEditorPart editor = HandlerUtil.getActiveEditor(event);
55 IEditorInput input = editor.getEditorInput();
56 if (editor instanceof BulkEditor && input instanceof AbstractBulkEditorInput) {
57
58 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
59 LineAnnotationModel model =
60 (LineAnnotationModel) provider.getAnnotationModel(input);
61
62 // Check whether there are any group annotations
63 Set<LineAnnotation> candidateAnnotations = model.getAllAnnotationsOfType(IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
64 if (candidateAnnotations.size() == 0) {
65 MessageDialog.openWarning(HandlerUtil.getActiveShell(event),
66 "No merge candidates", "No objects have been chosen for merging.");
67 return null;
68 }
69
70 // Check whether group merge target has been set
71 Annotation targetAnnotation = model.getFirstAnnotationOfType(IBulkEditorConstants.TYPE_MERGE_TARGET);
72 if (targetAnnotation == null) {
73 MessageDialog.openWarning(HandlerUtil.getActiveShell(event),
74 "No group merge target set", "No group merge target has been set.");
75 return null;
76 }
77 Object targetEntity = ((IEntityContainer<?>) targetAnnotation).getEntity();
78
79 TeamOrPersonBase teamOrPerson = null;
80 Reference ref = null;
81 if (targetEntity instanceof TeamOrPersonBase){
82 teamOrPerson = HibernateProxyHelper.deproxy(targetEntity, TeamOrPersonBase.class);
83 } else if(targetEntity instanceof Reference){
84 ref = HibernateProxyHelper.deproxy(targetEntity, Reference.class);
85 }
86 logger.info("Merging group");
87 // model.printAnnotations();
88 for (LineAnnotation annotation : candidateAnnotations) {
89 //first check whether entities are mergeable
90
91
92 try{
93 if (ref != null){
94 Reference ref2 = HibernateProxyHelper.deproxy(annotation.getEntity(), Reference.class);
95
96 if (!CdmStore.getCommonService().isMergeable(ref, ref2, null)){
97 MessageDialog.openWarning(HandlerUtil.getActiveShell(event),
98 "No merge possible", "A merge of " + ref.getTitleCache() + " and " + ref2.getTitleCache() + " is not possible.");
99 return null;
100 }
101 }
102 if (teamOrPerson != null){
103 TeamOrPersonBase teamOrPerson2 = HibernateProxyHelper.deproxy(annotation.getEntity(), TeamOrPersonBase.class);
104
105 if (!CdmStore.getCommonService().isMergeable(teamOrPerson, teamOrPerson2, null)){
106 MessageDialog.openWarning(HandlerUtil.getActiveShell(event),
107 "No merge possible", "A merge of " + teamOrPerson.getTitleCache() + " and " + teamOrPerson2.getTitleCache() + " is not possible.");
108 return null;
109 }
110 }
111 }catch(MergeException e){
112
113 }
114 ((BulkEditor) editor).removeAnnotatedLine(annotation);
115
116 // Mark entity container for merging with target entity
117 ((IEntityContainer) annotation).markAsMerged(targetEntity);
118 logger.info("Merging " + annotation + " with " + targetAnnotation);
119
120 // Remove annotation from model
121 model.removeAnnotation(annotation);
122 }
123 // model.printAnnotations();
124
125 model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
126 model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_TARGET);
127 }
128 return null;
129 }
130 }