2 * Copyright (C) 2015 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
;
11 import org
.eclipse
.core
.commands
.AbstractHandler
;
12 import org
.eclipse
.core
.commands
.ExecutionEvent
;
13 import org
.eclipse
.core
.commands
.ExecutionException
;
14 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
15 import org
.eclipse
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
17 import org
.eclipse
.ui
.IEditorInput
;
18 import org
.eclipse
.ui
.IEditorPart
;
19 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
20 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
27 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
28 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
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
.internal
.TaxeditorBulkeditorPlugin
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 public class ConvertPerson2TeamHandler
extends AbstractHandler
{
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
50 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
52 IEditorInput input
= editor
.getEditorInput();
53 if (editor
.isDirty()){
54 boolean proceed
= MessageDialog
.openQuestion(null,
55 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
62 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
65 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
66 LineAnnotationModel model
=
67 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
70 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
72 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
75 for(Object object
: structuredSelection
.toList()){
76 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
77 UpdateResult result
= null;
79 if (object
instanceof Person
){
80 Person person
= HibernateProxyHelper
.deproxy(object
, Person
.class);
82 result
= CdmStore
.getService(IAgentService
.class).convertPerson2Team(person
.getUuid());
83 } catch (IllegalArgumentException e
) {
84 MessagingUtils
.errorDialog("Can not convert Person into a Team", null, e
.getLocalizedMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
,e
, true);
85 } catch (MergeException e
) {
86 MessagingUtils
.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
89 MessagingUtils
.informationDialog("Can not convert Person into a Team", "convert Person to Team can only be called on a person.");
91 if (result
!= null &&result
.isOk()){
92 team
= (Team
)result
.getCdmEntity();
95 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
96 ((BulkEditor
) editor
).createAnnotatedLine(team
);