Project

General

Profile

Download (6.08 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.swt.widgets.Shell;
24

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

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

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

    
51
        BulkEditor editor = (BulkEditor) activePart.getObject();
52
        AbstractBulkEditorInput input = editor.getEditorInput();
53
        Set<CdmBase> mergedCandidates = new HashSet<>();
54

    
55
        if(editor.isDirty()){
56
            String[] labels = {Messages.MergeGroupHandler_save, Messages.MergeGroupHandler_continue, Messages.MergeGroupHandler_cancel};
57

    
58
            MessageDialog dialog = new MessageDialog(null, Messages.MergeGroupHandler_SaveChanges, null, Messages.MergeGroupHandler_description,
59
                    MessageDialog.QUESTION_WITH_CANCEL, 0, labels);
60
            dialog.open();
61
            int proceed = dialog.getReturnCode();
62

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

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

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

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

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

    
112
                }
113
                if (teamOrPerson != null){
114
                    TeamOrPersonBase<?> teamOrPerson2 = HibernateProxyHelper.deproxy(item, TeamOrPersonBase.class);
115

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

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