2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.bulkeditor
.handler
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.eclipse
.core
.commands
.AbstractHandler
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.jface
.text
.source
.Annotation
;
19 import org
.eclipse
.ui
.IEditorInput
;
20 import org
.eclipse
.ui
.IEditorPart
;
21 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
22 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
27 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
28 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityContainer
;
29 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
30 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
31 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
32 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorConstants
;
33 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 * <p>MergeGroupHandler class.</p>
43 public class MergeGroupHandler
extends AbstractHandler
{
44 private static final Logger logger
= Logger
45 .getLogger(MergeGroupHandler
.class);
48 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
51 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
53 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
54 IEditorInput input
= editor
.getEditorInput();
55 if (editor
instanceof BulkEditor
&& input
instanceof AbstractBulkEditorInput
) {
57 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
58 LineAnnotationModel model
=
59 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
61 // Check whether there are any group annotations
62 Set
<LineAnnotation
> candidateAnnotations
= model
.getAllAnnotationsOfType(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
63 if (candidateAnnotations
.size() == 0) {
64 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
65 "No merge candidates", "No objects have been chosen for merging.");
69 // Check whether group merge target has been set
70 Annotation targetAnnotation
= model
.getFirstAnnotationOfType(IBulkEditorConstants
.TYPE_MERGE_TARGET
);
71 if (targetAnnotation
== null) {
72 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
73 "No group merge target set", "No group merge target has been set.");
76 Object targetEntity
= ((IEntityContainer
<?
>) targetAnnotation
).getEntity();
78 TeamOrPersonBase teamOrPerson
= null;
80 if (targetEntity
instanceof TeamOrPersonBase
){
81 teamOrPerson
= HibernateProxyHelper
.deproxy(targetEntity
, TeamOrPersonBase
.class);
82 } else if(targetEntity
instanceof Reference
){
83 ref
= HibernateProxyHelper
.deproxy(targetEntity
, Reference
.class);
85 logger
.info("Merging group");
86 // model.printAnnotations();
87 for (LineAnnotation annotation
: candidateAnnotations
) {
88 //first check whether entities are mergeable
93 Reference ref2
= HibernateProxyHelper
.deproxy(annotation
.getEntity(), Reference
.class);
95 if (!CdmStore
.getCommonService().isMergeable(ref
, ref2
, null)){
96 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
97 "No merge possible", "A merge of " + ref
.getTitleCache() + " and " + ref2
.getTitleCache() + " is not possible.");
101 if (teamOrPerson
!= null){
102 TeamOrPersonBase teamOrPerson2
= HibernateProxyHelper
.deproxy(annotation
.getEntity(), TeamOrPersonBase
.class);
104 if (!CdmStore
.getCommonService().isMergeable(teamOrPerson
, teamOrPerson2
, null)){
105 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
106 "No merge possible", "A merge of " + teamOrPerson
.getTitleCache() + " and " + teamOrPerson2
.getTitleCache() + " is not possible.");
110 }catch(MergeException e
){
113 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
115 // Mark entity container for merging with target entity
116 ((IEntityContainer
) annotation
).markAsMerged(targetEntity
);
117 logger
.info("Merging " + annotation
+ " with " + targetAnnotation
);
119 // Remove annotation from model
120 model
.removeAnnotation(annotation
);
122 // model.printAnnotations();
124 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
125 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_TARGET
);