ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / MergeGroupHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.bulkeditor.handler;
10
11 import java.util.Set;
12
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;
23
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;
35
36 /**
37 * <p>MergeGroupHandler class.</p>
38 *
39 * @author p.ciardelli
40 * @created 25.06.2009
41 * @version 1.0
42 */
43 public class MergeGroupHandler extends AbstractHandler {
44 private static final Logger logger = Logger
45 .getLogger(MergeGroupHandler.class);
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52
53 IEditorPart editor = HandlerUtil.getActiveEditor(event);
54 IEditorInput input = editor.getEditorInput();
55 if (editor instanceof BulkEditor && input instanceof AbstractBulkEditorInput) {
56
57 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
58 LineAnnotationModel model =
59 (LineAnnotationModel) provider.getAnnotationModel(input);
60
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.");
66 return null;
67 }
68
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.");
74 return null;
75 }
76 Object targetEntity = ((IEntityContainer<?>) targetAnnotation).getEntity();
77
78 TeamOrPersonBase teamOrPerson = null;
79 Reference ref = 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);
84 }
85 logger.info("Merging group");
86 // model.printAnnotations();
87 for (LineAnnotation annotation : candidateAnnotations) {
88 //first check whether entities are mergeable
89
90
91 try{
92 if (ref != null){
93 Reference ref2 = HibernateProxyHelper.deproxy(annotation.getEntity(), Reference.class);
94
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.");
98 return null;
99 }
100 }
101 if (teamOrPerson != null){
102 TeamOrPersonBase teamOrPerson2 = HibernateProxyHelper.deproxy(annotation.getEntity(), TeamOrPersonBase.class);
103
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.");
107 return null;
108 }
109 }
110 }catch(MergeException e){
111
112 }
113 ((BulkEditor) editor).removeAnnotatedLine(annotation);
114
115 // Mark entity container for merging with target entity
116 ((IEntityContainer) annotation).markAsMerged(targetEntity);
117 logger.info("Merging " + annotation + " with " + targetAnnotation);
118
119 // Remove annotation from model
120 model.removeAnnotation(annotation);
121 }
122 // model.printAnnotations();
123
124 model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
125 model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_TARGET);
126 }
127 return null;
128 }
129 }