Project

General

Profile

Download (6.36 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.HashSet;
12
import java.util.Set;
13

    
14
import javax.inject.Named;
15

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

    
26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.strategy.merge.MergeException;
31
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
32
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditor;
33
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
34
import eu.etaxonomy.taxeditor.event.EventUtility;
35
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
36
import eu.etaxonomy.taxeditor.l10n.Messages;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38

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

    
49
    @Execute
50
    public void execute(
51
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
52
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
53

    
54
        BulkEditor editor = (BulkEditor) activePart.getObject();
55
        AbstractBulkEditorInput input = editor.getEditorInput();
56
        Set<CdmBase> mergedCandidates = new HashSet<>();
57

    
58
        if(editor.isDirty()){
59
            String[] labels = {Messages.MergeGroupHandler_save, Messages.MergeGroupHandler_continue, Messages.MergeGroupHandler_cancel};
60

    
61
            MessageDialog dialog = new MessageDialog(null, Messages.MergeGroupHandler_SaveChanges, null, Messages.MergeGroupHandler_description,
62
                    MessageDialog.QUESTION_WITH_CANCEL, 0, labels);
63
            dialog.open();
64
            int proceed = dialog.getReturnCode();
65

    
66
            boolean save = false;
67
            if (proceed == 0) {
68
                save = true;
69
            } else if (proceed == 2){
70
                return;
71
            }
72
            BulkEditorQuery lastQuery = editor.getLastQuery();
73
            editor.setLastQuery(null);
74
            editor.save(new NullProgressMonitor(), save);
75
            editor.setLastQuery(lastQuery);
76
        }
77

    
78
        // Check whether there are any group annotations
79
        Set<CdmBase> candidatesToMerge = input.getMergeCandidates();
80
        if (candidatesToMerge.size() == 0) {
81
            MessageDialog.openWarning(shell,
82
                    "No merge candidates", "No objects have been chosen for merging.");
83
            return;
84
        }
85

    
86
        // Check whether group merge target has been set
87
        CdmBase mergeTarget = input.getMergeTarget();
88
        if (mergeTarget == null) {
89
            MessageDialog.openWarning(shell,
90
                    "No group merge target set", "No group merge target has been set.");
91
            return;
92
        }
93

    
94
        TeamOrPersonBase<?> teamOrPerson = null;
95
        Reference ref = null;
96
        if (mergeTarget instanceof TeamOrPersonBase){
97
            teamOrPerson = HibernateProxyHelper.deproxy(mergeTarget, TeamOrPersonBase.class);
98
        } else if(mergeTarget instanceof Reference){
99
            ref = HibernateProxyHelper.deproxy(mergeTarget, Reference.class);
100
        }
101
        for (CdmBase item : candidatesToMerge) {
102
            //first check whether entities are mergeable
103
            try{
104
                if (ref != null){
105
                    Reference ref2 = HibernateProxyHelper.deproxy(item, Reference.class);
106

    
107
                    if (!CdmStore.getCommonService().isMergeable(ref, ref2, null)){
108
                        MessageDialog.openWarning(shell,
109
                                "No merge possible", "A merge of " + ref.getTitleCache() + " and " + ref2.getTitleCache() + " is not possible.");
110
                        return;
111
                    }
112
                    input.getModel().remove(item);
113
                    mergedCandidates.add(item);
114

    
115
                }
116
                if (teamOrPerson != null){
117
                    TeamOrPersonBase<?> teamOrPerson2 = HibernateProxyHelper.deproxy(item, TeamOrPersonBase.class);
118

    
119
                    if (!CdmStore.getCommonService().isMergeable(teamOrPerson, teamOrPerson2, null)){
120
                        MessageDialog.openWarning(shell,
121
                                "No merge possible", "A merge of " + teamOrPerson.getTitleCache() + " and " + teamOrPerson2.getTitleCache() + " is not possible.");
122
                        return;
123
                    }
124
                    input.getModel().remove(item);
125
                    mergedCandidates.add(item);
126
                }
127
            }catch(MergeException e){
128
                candidatesToMerge.clear();
129
                MessageDialog.openWarning(shell,
130
                        "No merge possible", "A merge is not possible." + e.getLocalizedMessage());
131
                return;
132
            }
133
        }
134
        if (!candidatesToMerge.isEmpty()){
135
            input.getMergedEntities().put(mergeTarget, mergedCandidates);
136
            input.getMergeCandidates().clear();
137
            input.setMergeTarget(null);
138
        }
139
        editor.setDirty();
140
        editor.refresh();
141

    
142
        EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, new StructuredSelection(mergeTarget));
143
    }
144

    
145
    @CanExecute
146
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
147
            MHandledMenuItem menuItem) {
148
        boolean canExecute = false;
149
        BulkEditor editor = (BulkEditor) activePart.getObject();
150
        canExecute = !editor.getEditorInput().getMergeCandidates().isEmpty()
151
                && editor.getEditorInput().getMergeTarget()!=null;
152
        menuItem.setVisible(canExecute);
153
        return canExecute;
154
    }
155
}
(5-5/10)