3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
.handler
;
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
;
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
;
38 * <p>MergeGroupHandler class.</p>
44 public class MergeGroupHandler
extends AbstractHandler
{
45 private static final Logger logger
= Logger
46 .getLogger(MergeGroupHandler
.class);
49 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
55 IEditorInput input
= editor
.getEditorInput();
56 if (editor
instanceof BulkEditor
&& input
instanceof AbstractBulkEditorInput
) {
58 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
59 LineAnnotationModel model
=
60 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
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.");
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.");
77 Object targetEntity
= ((IEntityContainer
<?
>) targetAnnotation
).getEntity();
79 TeamOrPersonBase teamOrPerson
= 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);
86 logger
.info("Merging group");
87 // model.printAnnotations();
88 for (LineAnnotation annotation
: candidateAnnotations
) {
89 //first check whether entities are mergeable
94 Reference ref2
= HibernateProxyHelper
.deproxy(annotation
.getEntity(), Reference
.class);
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.");
102 if (teamOrPerson
!= null){
103 TeamOrPersonBase teamOrPerson2
= HibernateProxyHelper
.deproxy(annotation
.getEntity(), TeamOrPersonBase
.class);
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.");
111 }catch(MergeException e
){
114 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
116 // Mark entity container for merging with target entity
117 ((IEntityContainer
) annotation
).markAsMerged(targetEntity
);
118 logger
.info("Merging " + annotation
+ " with " + targetAnnotation
);
120 // Remove annotation from model
121 model
.removeAnnotation(annotation
);
123 // model.printAnnotations();
125 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
126 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_TARGET
);