Project

General

Profile

Download (3.54 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.e4.handler;
10

    
11
import java.util.Iterator;
12

    
13
import javax.inject.Named;
14

    
15
import org.eclipse.core.runtime.NullProgressMonitor;
16
import org.eclipse.e4.core.di.annotations.CanExecute;
17
import org.eclipse.e4.core.di.annotations.Execute;
18
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20
import org.eclipse.e4.ui.services.IServiceConstants;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.IStructuredSelection;
23
import org.eclipse.jface.viewers.StructuredSelection;
24

    
25
import eu.etaxonomy.cdm.api.service.IAgentService;
26
import eu.etaxonomy.cdm.api.service.UpdateResult;
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.bulkeditor.e4.BulkEditorE4;
31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * @author k.luther
36
 * @date 12.05.2015
37
 *
38
 */
39
public class ConvertTeam2PersonHandlerE4 {
40

    
41
    @Execute
42
	public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
43

    
44
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
45
        if (editor.isDirty()){
46
            boolean proceed = MessageDialog.openQuestion(null,
47
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
48
            if (proceed) {
49
                editor.save(new NullProgressMonitor());
50
            } else {
51
                return;
52
            }
53
        }
54
        IStructuredSelection selection = editor.getSelection();
55
        Iterator iterator = selection.iterator();
56
        for(selection.iterator();iterator.hasNext();){
57
            Team team = (Team) iterator.next();
58
            Person person = null;
59
            UpdateResult result = null;
60

    
61
            try {
62
                result = CdmStore.getService(IAgentService.class).convertTeam2Person(team.getUuid());
63
            } catch (IllegalArgumentException e) {
64
                MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
65
            } catch (MergeException e) {
66
                MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
67
            }
68
            if (result != null && result.isOk()){
69
                person = (Person)result.getCdmEntity();
70
                editor.getEditorInput().getModel().remove(team);
71
                editor.getEditorInput().getModel().add(person);
72
            }
73
        }
74
    }
75

    
76
    @CanExecute
77
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
78
            MHandledMenuItem menuItem){
79
        boolean canExecute = false;
80
        BulkEditorE4 bulkEditorE4 = (BulkEditorE4)activePart.getObject();
81
        StructuredSelection selection = (StructuredSelection)bulkEditorE4.getSelection();
82
        canExecute = bulkEditorE4.getEditorInput().isConvertingEnabled();
83
        Iterator iterator = selection.iterator();
84
        for(selection.iterator();iterator.hasNext();){
85
            if(!(iterator.next() instanceof Team)){
86
                canExecute = false;
87
                break;
88
            }
89
        }
90
        menuItem.setVisible(canExecute);
91
        return canExecute;
92
    }
93

    
94
}
(2-2/10)