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.hibernate.criterion.Criterion;
18
import org.hibernate.criterion.Restrictions;
19
import org.joda.time.DateTime;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.vaadin.spring.events.EventScope;
22
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
23
import org.vaadin.viritin.fields.LazyComboBox;
24

    
25
import com.vaadin.server.SystemError;
26
import com.vaadin.server.UserError;
27
import com.vaadin.spring.annotation.SpringComponent;
28
import com.vaadin.spring.annotation.ViewScope;
29

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

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

    
62
    private static final long serialVersionUID = 2283189121081612574L;
63

    
64
    private ReferencePopupEditor newReferencePopup;
65

    
66
    private Reference newReference;
67

    
68
    private boolean registrationInProgress;
69

    
70
    @Autowired
71
    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
72

    
73
    private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
74

    
75
    public StartRegistrationPresenter (){
76
        super();
77
    }
78

    
79

    
80
    /**
81
     * {@inheritDoc}
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
    /**
125
     * @param value
126
     * @return
127
     */
128
    public void updateReferenceSearchMode(MatchMode value) {
129
        if(referencePagingProvider != null && value != null){
130
            referencePagingProvider.setMatchMode(value);
131
            getView().getReferenceCombobox().refresh();
132
        }
133
    }
134

    
135
    /**
136
     * {@inheritDoc}
137
     */
138
    @Override
139
    public void handleViewExit() {
140
        if(!registrationInProgress && newReference != null){
141
            logger.info("Deleting newly created Reference due to canceled registration");
142
            getRepo().getReferenceService().delete(newReference);
143
        }
144
        super.handleViewExit();
145
    }
146

    
147

    
148
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
149
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
150

    
151
        if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
152
            return;
153
        }
154

    
155
        newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
156
        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
157
        refTypes.remove(ReferenceType.Section);
158
        newReferencePopup.withReferenceTypes(refTypes);
159

    
160
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
161
        newReferencePopup.withDeleteButton(true);
162
        newReferencePopup.loadInEditor(null);
163
        newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
164
    }
165

    
166
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
167
    public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
168

    
169
        if(getView().getRemoveNewPublicationButton() != event.getSource()){
170
            return;
171
        }
172
        DeleteResult result = getRepo().getReferenceService().delete(newReference);
173
        if(!result.isOk()){
174
            String message = "";
175
            for(Exception e : result.getExceptions()){
176
                message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
177
            }
178
            getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
179
        }
180

    
181
        getView().getReferenceCombobox().setEnabled(false);
182

    
183
        getView().getRemoveNewPublicationButton().setVisible(false);
184

    
185
        getView().getNewPublicationButton().setVisible(true);
186
        getView().getNewPublicationLabel().setCaption(null);
187
        getView().getNewPublicationLabel().setVisible(false);
188
    }
189

    
190
    @EventBusListenerMethod
191
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
192

    
193
        if(event.getPopup() == newReferencePopup){
194
            if(event.getReason() == Reason.SAVE){
195

    
196
                newReference = newReferencePopup.getBean();
197

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

    
202
                getView().getReferenceCombobox().setValue(null);  // deselect
203
                getView().getReferenceCombobox().setEnabled(false);
204

    
205
                getView().getContinueButton().setEnabled(true);
206

    
207
                getView().getNewPublicationButton().setVisible(false);
208

    
209
                getView().getRemoveNewPublicationButton().setVisible(true);
210
                getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
211
                getView().getNewPublicationLabel().setVisible(true);
212
            }
213

    
214
            newReferencePopup = null;
215
        }
216
    }
217

    
218
    @SuppressWarnings("null")
219
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
220
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
221

    
222
        if(getView().getContinueButton() != event.getSource()){
223
            return;
224
        }
225

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

    
242
    }
243

    
244
    /**
245
     * {@inheritDoc}
246
     */
247
    @Override
248
    protected RegistrationDTO loadBeanById(Object identifier) {
249
        // not needed //
250
        return null;
251
    }
252

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

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    protected void deleteBean(RegistrationDTO bean) {
266
        // not needed //
267
    }
268

    
269
}
(13-13/18)