Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.cdm.vaadin.view.registration;
10

    
11
import java.util.Collection;
12
import java.util.EnumSet;
13
import java.util.Set;
14
import java.util.UUID;
15
import java.util.stream.Collectors;
16

    
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.hibernate.criterion.Criterion;
20
import org.hibernate.criterion.Restrictions;
21
import org.joda.time.DateTime;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.vaadin.spring.events.EventScope;
24
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
25
import org.vaadin.viritin.fields.LazyComboBox;
26

    
27
import com.vaadin.server.SystemError;
28
import com.vaadin.server.UserError;
29
import com.vaadin.spring.annotation.SpringComponent;
30
import com.vaadin.spring.annotation.ViewScope;
31

    
32
import eu.etaxonomy.cdm.api.service.DeleteResult;
33
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
34
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter;
35
import eu.etaxonomy.cdm.model.permission.CRUD;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37
import eu.etaxonomy.cdm.model.reference.ReferenceType;
38
import eu.etaxonomy.cdm.persistence.permission.CdmAuthority;
39
import eu.etaxonomy.cdm.persistence.permission.Operation;
40
import eu.etaxonomy.cdm.persistence.query.MatchMode;
41
import eu.etaxonomy.cdm.ref.TypedEntityReference;
42
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
43
import eu.etaxonomy.cdm.service.TypifiedEntityFilterablePagingProvider;
44
import eu.etaxonomy.cdm.service.UserHelperAccess;
45
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
46
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
47
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
48
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
49
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
50
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
51
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
52
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
53
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
54

    
55
/**
56
 * @author a.kohlbecker
57
 * @since Jul 11, 2017
58
 *
59
 */
60
@SpringComponent
61
@ViewScope
62
public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
63

    
64
    private static final long serialVersionUID = 2283189121081612574L;
65

    
66
    private final static Logger logger = LogManager.getLogger();
67

    
68
    private ReferencePopupEditor newReferencePopup;
69

    
70
    private Reference newReference;
71

    
72
    private boolean registrationInProgress;
73

    
74
    @Autowired
75
    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
76

    
77
    private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
78

    
79
    public StartRegistrationPresenter (){
80
        super();
81
    }
82

    
83
    @Override
84
    public void handleViewEntered() {
85

    
86
        super.handleViewEntered();
87

    
88
        referencePagingProvider = pagingProviderFactory.referenceEntityReferencePagingProvider(
89
                new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter.LabelType.BIBLIOGRAPHIC),
90
                ReferenceEllypsisFormatter.INIT_STRATEGY
91
                );
92
        TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
93
        // referencePagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
94
        Criterion criterion = Restrictions.not(Restrictions.or(Restrictions.in("type", new ReferenceType[]{ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries})));
95

    
96
        if(!UserHelperAccess.userHelper().userIsAdmin()){
97
            Collection<CdmAuthority> referencePermissions = UserHelperAccess.userHelper().findUserPermissions(Reference.class, Operation.UPDATE);
98
            boolean generalUpdatePermission = referencePermissions.stream().anyMatch(p -> p.getTargetUUID() == null);
99
            if(!generalUpdatePermission){
100
                // exclude unpublished publications
101
                DateTime nowLocal = new DateTime();
102
                String dateString = nowLocal.toString("yyyyMMdd");
103
                logger.debug("dateString:" + dateString);
104
                Criterion pulishedOnly = Restrictions.or(
105
                        Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNull("datePublished.end"), Restrictions.isNull("datePublished.freeText")),
106
                        Restrictions.and(Restrictions.isNotNull("datePublished.start"), Restrictions.sqlRestriction("datePublished_start < " + dateString)),
107
                        Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNotNull("datePublished.end"), Restrictions.sqlRestriction("datePublished_end < " + dateString))
108
                        );
109
                // restrict by allowed reference uuids
110
                Set<UUID> allowedUuids = referencePermissions.stream().filter(p -> p.getTargetUUID() != null).map(CdmAuthority::getTargetUUID).collect(Collectors.toSet());
111
                if(!allowedUuids.isEmpty()){
112
                    Criterion uuidRestriction = Restrictions.in("uuid", allowedUuids);
113
                    criterion = Restrictions.and(criterion, Restrictions.or(pulishedOnly, uuidRestriction));
114
                } else {
115
                    criterion = Restrictions.and(criterion, pulishedOnly);
116
                }
117
            }
118
        }
119
        referencePagingProvider.addCriterion(criterion);
120
        getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
121
        getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
122
    }
123

    
124
    public void updateReferenceSearchMode(MatchMode value) {
125
        if(referencePagingProvider != null && value != null){
126
            referencePagingProvider.setMatchMode(value);
127
            getView().getReferenceCombobox().refresh();
128
        }
129
    }
130

    
131
    @Override
132
    public void handleViewExit() {
133
        if(!registrationInProgress && newReference != null){
134
            logger.info("Deleting newly created Reference due to canceled registration");
135
            getRepo().getReferenceService().delete(newReference);
136
        }
137
        super.handleViewExit();
138
    }
139

    
140

    
141
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
142
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
143

    
144
        if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
145
            return;
146
        }
147

    
148
        newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
149
        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
150
        refTypes.remove(ReferenceType.Section);
151
        newReferencePopup.withReferenceTypes(refTypes);
152

    
153
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
154
        newReferencePopup.withDeleteButton(true);
155
        newReferencePopup.loadInEditor(null);
156
        newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
157
    }
158

    
159
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
160
    public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
161

    
162
        if(getView().getRemoveNewPublicationButton() != event.getSource()){
163
            return;
164
        }
165
        DeleteResult result = getRepo().getReferenceService().delete(newReference);
166
        if(!result.isOk()){
167
            String message = "";
168
            for(Exception e : result.getExceptions()){
169
                message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
170
            }
171
            getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
172
        }
173

    
174
        getView().getReferenceCombobox().setEnabled(false);
175

    
176
        getView().getRemoveNewPublicationButton().setVisible(false);
177

    
178
        getView().getNewPublicationButton().setVisible(true);
179
        getView().getNewPublicationLabel().setCaption(null);
180
        getView().getNewPublicationLabel().setVisible(false);
181
    }
182

    
183
    @EventBusListenerMethod
184
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
185

    
186
        if(event.getPopup() == newReferencePopup){
187
            if(event.getReason() == Reason.SAVE){
188

    
189
                newReference = newReferencePopup.getBean();
190

    
191
                // TODO the bean contained in the popup editor is not yet updated at this point.
192
                //      so we reload it using the uuid since new beans will not have an Id at this point.
193
                newReference = getRepo().getReferenceService().load(newReference.getUuid());
194

    
195
                getView().getReferenceCombobox().setValue(null);  // deselect
196
                getView().getReferenceCombobox().setEnabled(false);
197

    
198
                getView().getContinueButton().setEnabled(true);
199

    
200
                getView().getNewPublicationButton().setVisible(false);
201

    
202
                getView().getRemoveNewPublicationButton().setVisible(true);
203
                getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
204
                getView().getNewPublicationLabel().setVisible(true);
205
            }
206

    
207
            newReferencePopup = null;
208
        }
209
    }
210

    
211
    @SuppressWarnings("null")
212
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
213
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
214

    
215
        if(getView().getContinueButton() != event.getSource()){
216
            return;
217
        }
218

    
219
        UUID referenceUuid = null;
220
        LazyComboBox<TypedEntityReference<Reference>> referenceCombobox = getView().getReferenceCombobox();
221
        referenceCombobox.commit();
222
        if(newReference != null){
223
            referenceUuid = newReference.getUuid();
224
       // } else if(referenceCombobox.getValue() != null) {
225
        } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
226
            referenceUuid = event.getEntityUuid();
227
        }
228
        if(referenceUuid == null){
229
            getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
230
            getView().getContinueButton().setEnabled(false);
231
        }
232
        registrationInProgress = true;
233
        viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
234

    
235
    }
236

    
237
    /**
238
     * {@inheritDoc}
239
     */
240
    @Override
241
    protected RegistrationDTO loadBeanById(Object identifier) {
242
        // not needed //
243
        return null;
244
    }
245

    
246
    /**
247
     * {@inheritDoc}
248
     */
249
    @Override
250
    protected void saveBean(RegistrationDTO bean) {
251
        // not needed //
252
    }
253

    
254
    /**
255
     * {@inheritDoc}
256
     */
257
    @Override
258
    protected void deleteBean(RegistrationDTO bean) {
259
        // not needed //
260
    }
261

    
262
}
(13-13/18)