Project

General

Profile

Download (3.68 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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 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;
21

    
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.model.MessagingUtils;
33
import eu.etaxonomy.taxeditor.store.CdmStore;
34

    
35
/**
36
 * @author k.luther
37
 * @date 12.05.2015
38
 *
39
 */
40
public class ConvertTeam2PersonHandler extends AbstractHandler {
41

    
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	@Override
46
	public Person execute(ExecutionEvent event) throws ExecutionException {
47
		ISelection selection = HandlerUtil.getCurrentSelection(event);
48

    
49
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
50

    
51
		IEditorInput input = editor.getEditorInput();
52
		if (editor.isDirty()){
53
			boolean proceed = MessageDialog.openQuestion(null,
54
					"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
55
			if (proceed) {
56
				editor.doSave(null);
57
			} else {
58
				return null;
59
			}
60
		}
61
		if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
62

    
63

    
64
			IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
65
			LineAnnotationModel model =
66
					(LineAnnotationModel) provider.getAnnotationModel(input);
67

    
68

    
69
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
70

    
71
			IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
72

    
73
			Person person ;
74
			for(Object object : structuredSelection.toList()){
75
				LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
76
				person = null;
77
				UpdateResult result = null;
78
				if (object instanceof Team){
79
					Team team = HibernateProxyHelper.deproxy(object, Team.class);
80
					try {
81
						result = CdmStore.getService(IAgentService.class).convertTeam2Person(team.getUuid());
82
					} catch (IllegalArgumentException e) {
83
						MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
84
					} catch (MergeException e) {
85
						MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
86
					}
87
				}else{
88
					MessagingUtils.informationDialog("Can not convert Team to Person", "convertTeam2Person can only be called on a team.");
89
				}
90
				if (result != null && result.isOk()){
91
				    person = (Person)result.getCdmEntity();
92
				}
93
				if (person != null){
94
					((BulkEditor) editor).removeAnnotatedLine(annotation);
95
					((BulkEditor) editor).createAnnotatedLine(person);
96
				}
97
			}
98
		}
99
		return null;
100
	}
101

    
102
}
(2-2/9)