fix #7231 replacing SpringApplicationEventBus by scoped Vaadin4Spring Event Bus
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / name / NameTypeDesignationPresenter.java
1 /**
2 * Copyright (C) 2018 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.Arrays;
12 import java.util.HashSet;
13 import java.util.List;
14
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.context.annotation.Scope;
17
18 import com.vaadin.spring.annotation.SpringComponent;
19
20 import eu.etaxonomy.cdm.api.service.IService;
21 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
22 import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
23 import eu.etaxonomy.cdm.model.name.TaxonName;
24 import eu.etaxonomy.cdm.model.reference.Reference;
25 import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
26 import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
27 import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
28 import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
29 import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
30 import eu.etaxonomy.cdm.vaadin.security.UserHelper;
31 import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
32 import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
33 import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
34 import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
35
36 /**
37 * @author a.kohlbecker
38 * @since Jan 26, 2018
39 *
40 */
41 @SpringComponent
42 @Scope("prototype")
43 public class NameTypeDesignationPresenter
44 extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
45
46 @Autowired
47 private IRegistrationWorkingSetService registrationWorkingSetService;
48
49 HashSet<TaxonName> typifiedNamesAsLoaded;
50
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 protected NameTypeDesignation loadBeanById(Object identifier) {
57 if(identifier instanceof Integer || identifier == null){
58 return super.loadBeanById(identifier);
59 } else {
60 TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
61 RegistrationDTO regDTO = registrationWorkingSetService.loadDtoById(idset.registrationId);
62 // find the working set
63 TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.baseEntityRef);
64
65 // NameTypeDesignation bameTypeDesignation = regDTO.getNameTypeDesignation(typeDesignationWorkingSet.getBaseEntityReference());
66 if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(TaxonName.class)){
67 throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
68 }
69 // TypeDesignationWorkingSet for NameTyped only contain one item!!!
70 int nameTypeDesignationId = typeDesignationWorkingSet.getTypeDesignations().get(0).getId();
71 return super.loadBeanById(nameTypeDesignationId);
72 }
73 }
74
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 protected NameTypeDesignation loadCdmEntityById(Integer identifier) {
81 List<String> initStrategy = Arrays.asList(new String []{
82 "$",
83 "typifiedNames.typeDesignations", // important !!
84 "typeName.$",
85 "citation.authorship.$",
86 }
87 );
88
89 NameTypeDesignation typeDesignation;
90 if(identifier != null){
91 typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(identifier, initStrategy);
92 } else {
93 if(beanInstantiator != null){
94 typeDesignation = beanInstantiator.createNewBean();
95 } else {
96 typeDesignation = NameTypeDesignation.NewInstance();
97 }
98 }
99
100 typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
101
102 return typeDesignation;
103 }
104
105
106
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public void handleViewEntered() {
113
114 CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
115 getView().getTypeStatusSelect().setContainerDataSource(selectFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
116 getView().getTypeStatusSelect().setItemCaptionPropertyId("description");
117
118 getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
119 CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
120 getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
121 getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
122 getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
123
124 CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
125 getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
126 getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
127 getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
128
129 getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
130
131 }
132
133
134 /**
135 * {@inheritDoc}
136 */
137 @Override
138 protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
139 if(crud != null){
140 newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
141 }
142 }
143
144 /**
145 * {@inheritDoc}
146 */
147 @Override
148 protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
149 // TODO Auto-generated method stub
150
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 @Override
157 protected IService<NameTypeDesignation> getService() {
158 // TODO Auto-generated method stub
159 return null;
160 }
161
162
163 /**
164 * {@inheritDoc}
165 */
166 @Override
167 protected NameTypeDesignation handleTransientProperties(NameTypeDesignation bean) {
168
169 // the typifiedNames can only be set on the name side, so we need to
170 // handle changes explicitly here
171 HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
172
173 // handle adds
174 for(TaxonName name : typifiedNames){
175 if(!name.getTypeDesignations().contains(bean)){
176 name.addTypeDesignation(bean, false);
177 }
178 }
179 // handle removed
180 for(TaxonName name : typifiedNamesAsLoaded){
181 if(!typifiedNames.contains(name)){
182 name.removeTypeDesignation(bean);
183 }
184 // FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
185 }
186
187 return bean;
188 }
189
190
191
192 }