Project

General

Profile

Download (3.04 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 javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
17
import org.eclipse.e4.ui.services.IServiceConstants;
18
import org.eclipse.jface.dialogs.MessageDialog;
19

    
20
import eu.etaxonomy.cdm.api.service.IAgentService;
21
import eu.etaxonomy.cdm.api.service.UpdateResult;
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.agent.Team;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.strategy.merge.MergeException;
26
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
27
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.store.CdmStore;
30

    
31
/**
32
 * @author k.luther
33
 * @date 12.05.2015
34
 *
35
 */
36
public class ConvertPerson2TeamHandlerE4 {
37

    
38

    
39
    @Execute
40
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
41
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
42

    
43
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
44

    
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();
50
            } else {
51
                return;
52
            }
53
        }
54

    
55
        Team team =null;
56
        UpdateResult result = null;
57
        try {
58
            result = CdmStore.getService(IAgentService.class).convertPerson2Team(selection.getUuid());
59
        } catch (IllegalArgumentException e) {
60
            MessagingUtils.errorDialog("Can not convert Person into a Team", null, e.getLocalizedMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID,e, true);
61
        } catch (MergeException e) {
62
            MessagingUtils.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
63
        }
64
        if (result != null &&result.isOk()){
65
            team = (Team)result.getCdmEntity();
66
        }
67
        if (team != null){
68
            editor.refresh();
69
        }
70
    }
71

    
72
    @CanExecute
73
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
74
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
75
            MHandledMenuItem menuItem){
76
        boolean canExecute = false;
77
        canExecute = ((BulkEditorE4)activePart.getObject()).getEditorInput().isConvertingEnabled() && selection instanceof Person;
78
        menuItem.setVisible(canExecute);
79
        return canExecute;
80
    }
81

    
82
}
(1-1/9)