Merge branch 'hotfix/3.8.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / ConvertTeam2PersonHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 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 org.eclipse.core.commands.AbstractHandler;
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.handlers.HandlerUtil;
21 import org.eclipse.ui.texteditor.IDocumentProvider;
22
23 import eu.etaxonomy.cdm.api.service.AgentServiceImpl;
24 import eu.etaxonomy.cdm.api.service.IAgentService;
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.agent.AgentBase;
27 import eu.etaxonomy.cdm.model.agent.Person;
28 import eu.etaxonomy.cdm.model.agent.Team;
29 import eu.etaxonomy.cdm.strategy.merge.MergeException;
30 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
31 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
32 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
33 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 /**
38 * @author k.luther
39 * @date 12.05.2015
40 *
41 */
42 public class ConvertTeam2PersonHandler extends AbstractHandler {
43
44 /**
45 * {@inheritDoc}
46 */
47 @Override
48 public Person execute(ExecutionEvent event) throws ExecutionException {
49 ISelection selection = HandlerUtil.getCurrentSelection(event);
50
51 IEditorPart editor = HandlerUtil.getActiveEditor(event);
52
53 IEditorInput input = editor.getEditorInput();
54 if (editor.isDirty()){
55 boolean proceed = MessageDialog.openQuestion(null,
56 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
57 if (proceed) {
58 editor.doSave(null);
59 } else {
60 return null;
61 }
62 }
63 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
64
65
66 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
67 LineAnnotationModel model =
68 (LineAnnotationModel) provider.getAnnotationModel(input);
69
70
71 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
72
73 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
74
75 Person person ;
76 for(Object object : structuredSelection.toList()){
77 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
78 person = null;
79 if (object instanceof Team){
80 Team team = HibernateProxyHelper.deproxy(object, Team.class);
81 try {
82 person = CdmStore.getService(IAgentService.class).convertTeam2Person(team);
83 } catch (IllegalArgumentException e) {
84 MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
85 } catch (MergeException e) {
86 MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
87 }
88 }else{
89 MessagingUtils.informationDialog("Can not convert Team to Person", "convertTeam2Person can only be called on a team.");
90 }
91
92 if (person != null){
93 ((BulkEditor) editor).removeAnnotatedLine(annotation);
94 ((BulkEditor) editor).createAnnotatedLine(person);
95 }
96 }
97 }
98 return null;
99 }
100
101 }