Project

General

Profile

Download (5.22 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.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.swt.widgets.Shell;
23

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

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

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

    
49
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
50
        AbstractBulkEditorInput input = editor.getEditorInput();
51

    
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
                BulkEditorQuery lastQuery = editor.getLastQuery();
57
                editor.setLastQuery(null);
58
                editor.save(new NullProgressMonitor(), false);
59
                editor.setLastQuery(lastQuery);
60
            } else {
61
                return ;
62
            }
63
        }
64

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

    
73
        // Check whether group merge target has been set
74
        CdmBase mergeTarget = input.getMergeTarget();
75
        if (mergeTarget == null) {
76
            MessageDialog.openWarning(shell,
77
                    "No group merge target set", "No group merge target has been set.");
78
            return;
79
        }
80

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

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

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

    
113
            }
114
        }
115
        editor.setDirty();
116
        editor.refresh();
117
    }
118

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