BrightSide Workbench Full Report + Source Code
ProjectGrantComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.dossier.grant;
20 
21 import java.util.stream.Collectors;
22 import org.turro.string.Strings;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.entity.ProjectGrant;
25 import org.turro.dossier.zul.menu.DossierMenu;
26 import org.turro.elephant.context.Application;
27 import org.turro.jpa.composer.EntityComposer;
28 import org.turro.jpa.entity.DaoEntity;
29 import org.turro.tags.TagItem;
30 import org.turro.tags.Tags;
31 import org.turro.tags.TagsChosenbox;
32 import org.turro.zkoss.text.WikiEditor;
33 import org.turro.zul.frame.Framework;
34 import org.zkoss.zk.ui.Component;
35 import org.zkoss.zk.ui.event.InputEvent;
36 import org.zkoss.zk.ui.select.annotation.Listen;
37 import org.zkoss.zk.ui.select.annotation.Wire;
38 
43 public class ProjectGrantComposer extends EntityComposer<ProjectGrant, Long> {
44 
45  @Wire("#goal") private WikiEditor goal;
46  @Wire("#deadline") private WikiEditor deadline;
47  @Wire("#beneficiaries") private WikiEditor beneficiaries;
48  @Wire("#elegible") private WikiEditor elegible;
49  @Wire("#features") private WikiEditor features;
50  @Wire("#intensity") private WikiEditor intensity;
51  @Wire("#detail #tags") private TagsChosenbox tags;
52 
53  @Listen("onChange = #goal")
54  public void onGoalChange() {
55  entity.getGoal().setWiki(goal.getValue());
56  entity.getGoal().setText(goal.getHtml());
57  }
58 
59  @Listen("onChange = #deadline")
60  public void onDeadlineChange() {
61  entity.getDeadline().setWiki(deadline.getValue());
62  entity.getDeadline().setText(deadline.getHtml());
63  }
64 
65  @Listen("onChange = #beneficiaries")
66  public void onBeneficiariesChange() {
67  entity.getBeneficiaries().setWiki(beneficiaries.getValue());
68  entity.getBeneficiaries().setText(beneficiaries.getHtml());
69  }
70 
71  @Listen("onChange = #elegible")
72  public void onElegibleChange() {
73  entity.getElegible().setWiki(elegible.getValue());
74  entity.getElegible().setText(elegible.getHtml());
75  }
76 
77  @Listen("onChange = #features")
78  public void onFeaturesChange() {
79  entity.getFeatures().setWiki(features.getValue());
80  entity.getFeatures().setText(features.getHtml());
81  }
82 
83  @Listen("onChange = #intensity")
84  public void onIntensityChange() {
85  entity.getIntensity().setWiki(intensity.getValue());
86  entity.getIntensity().setText(intensity.getHtml());
87  }
88 
89  @Listen("onSearch = #detail #tags")
90  public void onNewTag(InputEvent event) {
91  String value = event.getValue();
92  if(!Strings.isBlank(value)) {
93  tags.addToModel(new TagItem(value, 0));
94  }
95  }
96 
97  @Override
98  protected String getAttributeName() {
99  return "projectGrant";
100  }
101 
102  @Override
103  protected ProjectGrant getEntityInstance(Long id) {
104  if(id == null) {
105  entity = new ProjectGrant();
106  } else {
107  entity = new DossierPU().find(ProjectGrant.class, id);
108  }
109  return entity;
110  }
111 
112  @Override
113  protected DaoEntity getWrapperInstance(Component comp) {
117  return w;
118  }
119 
120  @Override
121  protected boolean beforeSave() {
122  if(entity != null) {
123  // Notify?
124  }
125  return super.beforeSave();
126  }
127 
128  @Override
129  public void afterSave() {
130  if(entity != null) {
131  String entityPath = DossierPU.getObjectPath(entity);
132  Tags.setTags(entityPath, tags.getObjectValues()
133  .stream().map(s -> s.getTagName()).collect(Collectors.toSet()));
135  }
136  }
137 
138  @Override
139  public void doFinally() throws Exception {
140  super.doFinally();
141  if(entity.getGoal() != null) goal.setValue(entity.getGoal().getWiki());
142  if(entity.getDeadline() != null) deadline.setValue(entity.getDeadline().getWiki());
143  if(entity.getBeneficiaries() != null) beneficiaries.setValue(entity.getBeneficiaries().getWiki());
144  if(entity.getElegible() != null) elegible.setValue(entity.getElegible().getWiki());
145  if(entity.getFeatures() != null) features.setValue(entity.getFeatures().getWiki());
146  if(entity.getIntensity()!= null) intensity.setValue(entity.getIntensity().getWiki());
147  tags.setCreatable(false);
149  }
150 
151  @Override
152  protected boolean shouldBeSaved() {
153  return !entity.isEmpty() && (entity.getId() == null || entity.getId() == 0) || super.shouldBeSaved();
154  }
155 
156  @Override
157  protected boolean inSaveRole() {
158  return Application.getApplication().isInRole("dossier:edit");
159  }
160 
161  @Override
162  protected boolean inDeleteRole() {
163  return Application.getApplication().isInRole("dossier:edit");
164  }
165 
166 }
167 
static String getObjectPath(Object object)
Definition: DossierPU.java:66
void addToModel(TagItem value)
Set< TagItem > getObjectValues()
void setObjectValues(Collection< TagItem > internalValues)
static void setTags(Object entity, String tags)
Definition: Tags.java:149
static List< TagItem > getAsItemFrom(String entityPath)
Definition: Tags.java:116
void setValue(String value)
Definition: WikiEditor.java:98
static Framework getCurrent()
Definition: Framework.java:203
void setSelectedLabel(String text)
Definition: Framework.java:103
void setSelectedTooltiptext(String text)
Definition: Framework.java:107