fix #7570 implementing intitution editor with basic set of fields
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / common / InstitutionEditorPresenter.java
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.common;
10
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.springframework.context.annotation.Scope;
16 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
17
18 import com.vaadin.spring.annotation.SpringComponent;
19
20 import eu.etaxonomy.cdm.api.service.IService;
21 import eu.etaxonomy.cdm.model.agent.AgentBase;
22 import eu.etaxonomy.cdm.model.agent.Institution;
23 import eu.etaxonomy.cdm.model.occurrence.Collection;
24 import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
25 import eu.etaxonomy.cdm.service.UserHelperAccess;
26 import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
27 import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
28 import eu.etaxonomy.cdm.vaadin.event.InstitutionEditorAction;
29 import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
30 import eu.etaxonomy.cdm.vaadin.model.common.InstitutionDTO;
31 import eu.etaxonomy.vaadin.mvp.AbstractCdmDTOEditorPresenter;
32 import eu.etaxonomy.vaadin.mvp.BoundField;
33 import eu.etaxonomy.vaadin.ui.view.PopupView;
34
35 /**
36 * @author a.kohlbecker
37 * @since Dec 21, 2017
38 *
39 */
40 @SpringComponent
41 @Scope("prototype")
42 public class InstitutionEditorPresenter extends AbstractCdmDTOEditorPresenter<InstitutionDTO, Institution, InstitutionPopupEditorView> {
43
44 private static final long serialVersionUID = -1996365248431425021L;
45
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 protected Institution loadCdmEntity(UUID identifier) {
52
53 List<String> initStrategy = Arrays.asList(new String []{
54
55 "$",
56 "contact.$",
57 "isPartOf.$",
58 }
59 );
60
61 Institution bean;
62 if(identifier != null){
63 bean = (Institution) getRepo().getAgentService().load(identifier, initStrategy);
64 } else {
65 bean = Institution.NewInstance();
66 }
67 return bean;
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 @Override
74 protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
75 if(crud != null){
76 newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(Collection.class, identifier, crud, null);
77 }
78
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 protected void guaranteePerEntityCRUDPermissions(Institution bean) {
86 if(crud != null){
87 newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
88 }
89
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 protected IService<Institution> getService() {
97 // TODO Auto-generated method stub
98 return null;
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 @Override
105 public void handleViewEntered() {
106 super.handleViewEntered();
107
108 CdmFilterablePagingProvider<AgentBase, Institution> collectionPagingProvider = new CdmFilterablePagingProvider<AgentBase, Institution>(getRepo().getAgentService(), Institution.class);
109 getView().getPartOfCombobox().getSelect().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
110 getView().getPartOfCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Institution>(getView().getPartOfCombobox(), this));
111
112 }
113
114 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
115 public void onInstitutionEditorActionAdd(InstitutionEditorAction event) {
116
117 if(!checkFromOwnView(event)){
118 return;
119 }
120
121 InstitutionPopupEditor intitutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
122
123 intitutionPopuEditor.grantToCurrentUser(this.crud);
124 intitutionPopuEditor.withDeleteButton(true);
125 intitutionPopuEditor.loadInEditor(null);
126 }
127
128 @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
129 public void onCollectionEditorActionEdit(InstitutionEditorAction event) {
130
131 if(!checkFromOwnView(event)){
132 return;
133 }
134
135 InstitutionPopupEditor intitutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
136
137 intitutionPopuEditor.grantToCurrentUser(this.crud);
138 intitutionPopuEditor.withDeleteButton(true);
139 intitutionPopuEditor.loadInEditor(event.getEntityUuid());
140 }
141
142 @EventBusListenerMethod()
143 public void onEntityChangeEvent(EntityChangeEvent<?> event){
144
145 BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
146
147 if(boundTargetField != null){
148 if(boundTargetField.matchesPropertyIdPath("isPartOf")){
149 if(event.isCreateOrModifiedType()){
150
151 Institution newInstitution = (Institution) event.getEntity();
152 getCache().load(newInstitution);
153 if(event.isCreatedType()){
154 getView().getPartOfCombobox().setValue(newInstitution);
155 } else {
156 getView().getPartOfCombobox().reload();
157 }
158 }
159
160 }
161 }
162 }
163
164 /**
165 * {@inheritDoc}
166 */
167 @Override
168 protected InstitutionDTO createDTODecorator(Institution cdmEntitiy) {
169 return new InstitutionDTO(cdmEntitiy);
170 }
171
172
173
174 }