Project

General

Profile

Download (8.08 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.name;
10

    
11
import java.util.Set;
12

    
13
import org.hibernate.Session;
14
import org.vaadin.viritin.fields.AbstractElementCollection;
15

    
16
import eu.etaxonomy.cdm.model.common.VersionableEntity;
17
import eu.etaxonomy.cdm.model.location.Country;
18
import eu.etaxonomy.cdm.model.name.Registration;
19
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
20
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
21
import eu.etaxonomy.cdm.model.occurrence.Collection;
22
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
23
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
24
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
25
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
28
import eu.etaxonomy.cdm.vaadin.component.SelectFieldFactory;
29
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
30
import eu.etaxonomy.cdm.vaadin.model.registration.DerivationEventTypes;
31
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists;
32
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
33
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
34
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
35
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
36
/**
37
 * SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method,
38
 * see {@link #prepareAsFieldGroupDataSource()} for details.
39
 *
40
 * @author a.kohlbecker
41
 * @since Jun 13, 2017
42
 *
43
 */
44
public class SpecimenTypeDesignationWorkingsetEditorPresenter
45
    extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView> {
46

    
47
    private static final long serialVersionUID = 4255636253714476918L;
48

    
49

    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @SuppressWarnings("serial")
54
    @Override
55
    public void handleViewEntered() {
56

    
57
        SelectFieldFactory selectFactory = new SelectFieldFactory(getRepo());
58
        getView().getCountrySelectField().setContainerDataSource(selectFactory.buildBeanItemContainer(Country.uuidCountryVocabulary));
59

    
60
        getView().getTypeDesignationsCollectionField().setEditorInstantiator(new AbstractElementCollection.Instantiator<SpecimenTypeDesignationDTORow>() {
61

    
62
            CdmFilterablePagingProvider<Collection> collectionPagingProvider = new CdmFilterablePagingProvider<Collection>(getRepo().getCollectionService());
63

    
64
            CdmFilterablePagingProvider<Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference>(getRepo().getReferenceService());
65

    
66
            @Override
67
            public SpecimenTypeDesignationDTORow create() {
68

    
69
                SpecimenTypeDesignationDTORow row = new SpecimenTypeDesignationDTORow();
70

    
71
                row.derivationEventType.setContainerDataSource(selectFactory.buildTermItemContainer(
72
                        RegistrationTermLists.DERIVATION_EVENT_TYPE_UUIDS())
73
                        );
74
                row.derivationEventType.setNullSelectionAllowed(false);
75

    
76
                row.derivationEventType.addValueChangeListener(e -> {
77
                    SpecimenTypeDesignationDTORow currentRow = row;
78
                    updateRowItemEnablement(currentRow);
79
                });
80

    
81
                row.typeStatus.setContainerDataSource(selectFactory.buildTermItemContainer(
82
                        RegistrationTermLists.SPECIMEN_TYPE_DESIGNATION_STATUS_UUIDS())
83
                        );
84
                row.typeStatus.setNullSelectionAllowed(false);
85

    
86

    
87
                row.collection.loadFrom(
88
                        collectionPagingProvider,
89
                        collectionPagingProvider,
90
                        collectionPagingProvider.getPageSize()
91
                        );
92
                row.collection.getSelect().setCaptionGenerator(
93
                        new CdmTitleCacheCaptionGenerator<Collection>()
94
                        );
95

    
96
                row.mediaSpecimenReference.loadFrom(
97
                        referencePagingProvider,
98
                        referencePagingProvider,
99
                        collectionPagingProvider.getPageSize()
100
                        );
101
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(
102
                        new CdmTitleCacheCaptionGenerator<Reference>()
103
                        );
104

    
105
                getView().applyDefaultComponentStyle(row.components());
106

    
107
                updateRowItemEnablement(row);
108

    
109
                return row;
110
            }
111

    
112
            private void updateRowItemEnablement(SpecimenTypeDesignationDTORow row) {
113

    
114
                DerivationEventType derivationEventType = (DerivationEventType)row.derivationEventType.getValue();
115

    
116
                boolean publishedImageType = derivationEventType != null && derivationEventType.equals(DerivationEventTypes.PUBLISHED_IMAGE());
117
                boolean unPublishedImageType = derivationEventType != null && derivationEventType.equals(DerivationEventTypes.UNPUBLISHED_IMAGE());
118

    
119
                row.mediaSpecimenReference.setEnabled(publishedImageType);
120
                row.mediaSpecimenReferenceDetail.setEnabled(publishedImageType);
121
                row.mediaUri.setEnabled(unPublishedImageType);
122

    
123
            }
124
        });
125
    }
126

    
127
    /**
128
     * {@inheritDoc}
129
     *
130
     * see also {@link SpecimenTypeDesignationWorkingsetPopupEditor#showInEditor()}
131
     */
132
    @Override
133
    protected SpecimenTypeDesignationWorkingSetDTO prepareAsFieldGroupDataSource(SpecimenTypeDesignationWorkingSetDTO bean) {
134

    
135
        if(bean.getFieldUnit() == null){
136
            // in case the base unit of the working set is not a FieldUnit all contained TypeDesignations must be modified
137
            // so that they are based on an empty FieldUnit with an associated Gathering Event
138
            if(Registration.class.isAssignableFrom(bean.getOwner().getClass())){
139
                // FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations
140
                logger.info("Basing all typeDesignations on a new fieldUnit");
141
                Session session = getSession();
142
                Registration reg = getRepo().getRegistrationService().find(bean.getOwner().getId());
143
                RegistrationDTO regDTO = new RegistrationDTO(reg);
144

    
145
                FieldUnit fieldUnit = FieldUnit.NewInstance();
146
                GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
147
                fieldUnit.setGatheringEvent(gatheringEvent);
148
                getRepo().getOccurrenceService().save(fieldUnit);
149

    
150
                VersionableEntity baseEntity = bean.getBaseEntity();
151
                Set<TypeDesignationBase> typeDesignations = regDTO.getTypeDesignationsInWorkingSet(
152
                        new TypedEntityReference(baseEntity.getClass(), baseEntity.getId(), baseEntity.toString())
153
                        );
154
                for(TypeDesignationBase td : typeDesignations){
155
                    DerivationEvent de = DerivationEvent.NewInstance();//
156
                    de.addOriginal(fieldUnit);
157
                    de.addDerivative(((SpecimenTypeDesignation)td).getTypeSpecimen());
158
                    de.setType(DerivationEventType.GATHERING_IN_SITU());
159
                }
160

    
161
                getRepo().getRegistrationService().saveOrUpdate(reg);
162
                session.flush();
163
                session.close();
164
            } else {
165
                throw new RuntimeException("Usupported owner type " + bean.getOwner().getClass() + ", needs to be implemented.");
166
            }
167
        }
168
        return super.prepareAsFieldGroupDataSource(bean);
169
    }
170

    
171

    
172

    
173
    /**
174
     * {@inheritDoc}
175
     */
176
    @Override
177
    protected void saveBean(SpecimenTypeDesignationWorkingSetDTO bean) {
178
        // TODO Auto-generated method stub
179

    
180
    }
181

    
182
    /**
183
     * {@inheritDoc}
184
     */
185
    @Override
186
    protected void deleteBean(SpecimenTypeDesignationWorkingSetDTO bean) {
187
        // TODO Auto-generated method stub
188

    
189
    }
190

    
191

    
192
}
(2-2/7)