Project

General

Profile

Download (4.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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.List;
12

    
13
import javax.inject.Named;
14

    
15
import org.eclipse.e4.core.di.annotations.CanExecute;
16
import org.eclipse.e4.core.di.annotations.Execute;
17
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.dialogs.MessageDialog;
21
import org.eclipse.swt.widgets.Shell;
22

    
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.strategy.merge.MergeException;
28
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
29
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31

    
32
/**
33
 * Note: This merging is persisted only when saved. This handler only checks
34
 * if merging is possible and removes only the list items but <b>not</b> the
35
 * CDM entitites
36
 * @author pplitzner
37
 * @date 11.09.2017
38
 *
39
 */
40
public class MergeGroupHandlerE4 {
41

    
42
    @Execute
43
    public void execute(
44
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
45
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
46

    
47
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
48
        AbstractBulkEditorInput input = editor.getEditorInput();
49

    
50
        if(editor.isDirty()){
51
            boolean proceed = MessageDialog.openQuestion(null,
52
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
53
            if (proceed) {
54
                editor.save();
55
            } else {
56
                return ;
57
            }
58
        }
59

    
60
        // Check whether there are any group annotations
61
        List<CdmBase> mergeCandidates = input.getMergeCandidates();
62
        if (mergeCandidates.size() == 0) {
63
            MessageDialog.openWarning(shell,
64
                    "No merge candidates", "No objects have been chosen for merging.");
65
            return;
66
        }
67

    
68
        // Check whether group merge target has been set
69
        CdmBase mergeTarget = input.getMergeTarget();
70
        if (mergeTarget == null) {
71
            MessageDialog.openWarning(shell,
72
                    "No group merge target set", "No group merge target has been set.");
73
            return;
74
        }
75

    
76
        TeamOrPersonBase teamOrPerson = null;
77
        Reference ref = null;
78
        if (mergeTarget instanceof TeamOrPersonBase){
79
            teamOrPerson = HibernateProxyHelper.deproxy(mergeTarget, TeamOrPersonBase.class);
80
        } else if(mergeTarget instanceof Reference){
81
            ref = HibernateProxyHelper.deproxy(mergeTarget, Reference.class);
82
        }
83
        for (CdmBase item : mergeCandidates) {
84
            //first check whether entities are mergeable
85
            try{
86
                if (ref != null){
87
                    Reference ref2 = HibernateProxyHelper.deproxy(item, Reference.class);
88

    
89
                    if (!CdmStore.getCommonService().isMergeable(ref, ref2, null)){
90
                        MessageDialog.openWarning(shell,
91
                                "No merge possible", "A merge of " + ref.getTitleCache() + " and " + ref2.getTitleCache() + " is not possible.");
92
                        return;
93
                    }
94
                    input.getModel().remove(item);
95
                }
96
                if (teamOrPerson != null){
97
                    TeamOrPersonBase teamOrPerson2 = HibernateProxyHelper.deproxy(item, TeamOrPersonBase.class);
98

    
99
                    if (!CdmStore.getCommonService().isMergeable(teamOrPerson, teamOrPerson2, null)){
100
                        MessageDialog.openWarning(shell,
101
                                "No merge possible", "A merge of " + teamOrPerson.getTitleCache() + " and " + teamOrPerson2.getTitleCache() + " is not possible.");
102
                        return;
103
                    }
104
                    input.getModel().remove(item);
105
                }
106
            }catch(MergeException e){
107

    
108
            }
109
        }
110
        editor.setDirty();
111
        editor.refresh();
112
    }
113

    
114
    @CanExecute
115
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
116
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
117
            MHandledMenuItem menuItem) {
118
        boolean canExecute = false;
119
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
120
        canExecute = !editor.getEditorInput().getMergeCandidates().isEmpty()
121
                && editor.getEditorInput().getMergeTarget()!=null;
122
        menuItem.setVisible(canExecute);
123
        return canExecute;
124
    }
125
}
(4-4/9)