BrightSide Workbench Full Report + Source Code
EditProjectControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.zul.www;
20 
21 import java.util.Date;
22 import java.util.EnumSet;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import org.turro.string.ObjectString;
26 import org.turro.string.Strings;
27 import org.turro.action.IAgreements;
28 import org.turro.action.Plugins;
29 import org.turro.auth.Authentication;
30 import org.turro.collections.CollectionUtil;
31 import org.turro.dossier.db.DossierPU;
32 import org.turro.dossier.dossier.DescriptorSet;
33 import org.turro.dossier.dossier.DossierWrapper;
34 import org.turro.dossier.entity.Category;
35 import org.turro.dossier.entity.DescriptorType;
36 import org.turro.dossier.entity.Dossier;
37 import org.turro.dossier.entity.DossierStatus;
38 import org.turro.dossier.entity.DossierType;
39 import org.turro.dossier.entity.Participant;
40 import org.turro.dossier.entity.Project;
41 import org.turro.dossier.project.NewIdeaNotification;
42 import org.turro.elephant.TemplateControl;
43 import org.turro.elephant.context.Application;
44 import org.turro.jpa.Dao;
45 import org.turro.phase.PhaseType;
46 import org.turro.plugin.contacts.IContact;
47 import org.turro.tags.TagItem;
48 import org.turro.tags.Tags;
49 import org.turro.tags.TagsChosenbox;
50 import org.turro.util.CompareUtil;
51 import org.turro.zkoss.input.ExpressionInput;
52 import org.zkoss.zk.ui.event.InputEvent;
53 import org.zkoss.zk.ui.select.annotation.Listen;
54 import org.zkoss.zk.ui.select.annotation.Wire;
55 import org.zkoss.zul.Div;
56 import org.zkoss.zul.Textbox;
57 
62 public class EditProjectControl extends TemplateControl {
63 
64  private Dossier dossier;
65  private String grouping, path;
66  private boolean required;
67 
68  @Wire("#category") private ProjectCategoryListbox category;
69  @Wire("#title") private Textbox title;
70  @Wire("#estimatedBudget") private ExpressionInput estimatedBudget;
71  @Wire("#goal") private Textbox goal;
72  @Wire("#searching") private Textbox searching;
73  @Wire("#tagsdiv") private Div tagsdiv;
74  @Wire("#tags") private TagsChosenbox tags;
75  @Wire("#descs") private Div descs;
76 
77  @Listen("onSelect=#category")
78  public void onCategory() {
80  "?item=" + dossier.getId() +
81  "&cat=" + category.getObjectValue().getId());
82  //dossier.setCategory(category.getObjectValue());
83  }
84 
85  @Listen("onChange=#title")
86  public void onTitle() {
87  dossier.setDescription(title.getValue());
88  dossier.getProject().setProjectTitle(title.getValue());
89  }
90 
91  @Listen("onChange=#estimatedBudget")
92  public void onBudget() {
93  dossier.getProject().setEstimatedBudget(estimatedBudget.getDoubleValue());
94  estimatedBudget.setValue(dossier.getProject().getEstimatedBudget());
95  }
96 
97  @Listen("onChange=#goal")
98  public void onGoal() {
99  dossier.getProject().setGoal(goal.getValue());
100  }
101 
102  @Listen("onChange=#searching")
103  public void onSearching() {
104  dossier.setSearching(searching.getValue());
105  }
106 
107  @Listen("onSelect=#tags")
108  public void onTag() {
109  String entityPath = DossierPU.getObjectPath(dossier);
110  Tags.setTags(entityPath, tags.getObjectValues()
111  .stream().map(s -> s.getTagName()).collect(Collectors.toSet()));
112  }
113 
114  @Listen("onSearch=#tags")
115  public void onNewTag(InputEvent event) {
116  String value = event.getValue();
117  String entityPath = DossierPU.getObjectPath(dossier);
118  if(!Strings.isBlank(value)) {
119  Tags.addTag(entityPath, value);
120  tags.addToModel(new TagItem(value, 0));
121  }
122  }
123 
124  @Listen("onClick=#save")
125  public void onSave() {
126  boolean wasNew = dossier.getId() == null || dossier.getId() < 1;
127  DossierWrapper dw = new DossierWrapper(dossier);
128  if(!dossier.isEmpty() && !dw.isEmptyDescriptors(grouping, required)) {
129  dossier = dw.save();
130  if(wasNew) {
131  new NewIdeaNotification(dossier).sendNotification();
132  }
133  Application.getApplication().sendRedirect(path + dossier.getId());
134  }
135  }
136 
137  public String getGrouping() {
138  return grouping;
139  }
140 
141  public void setGrouping(String grouping) {
142  this.grouping = grouping;
143  }
144 
145  public String getPath() {
146  return path;
147  }
148 
149  public void setPath(String path) {
150  this.path = path;
151  }
152 
153  public boolean isRequired() {
154  return required;
155  }
156 
157  public void setRequired(boolean required) {
158  this.required = required;
159  }
160 
161  public void setDossier(Dossier dossier) {
162  this.dossier = dossier;
163  }
164 
166  checkDossier();
167  DescriptorSet ds = dossier.getDescriptorSet();
168  if(!"all".equals(grouping)) {
169  CollectionUtil.from(ds).remove(e -> {
170  return CompareUtil.compare(grouping, e.getGrouping()) != 0 ||
171  CompareUtil.compare(required, e.isRequired()) != 0;
172  });
173  }
175  CollectionUtil.from(ds).remove(e -> {
176  return !DescriptorType.OPENING_DESCRIPTOR.equals(e.getType());
177  });
178  }
179  return ds;
180  }
181 
182  @Override
183  protected void doFinally() {
184  super.doFinally();
185  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
186  agreements.setContact(Authentication.getIContact());
187  if(!agreements.isValid() || !agreements.canAct("project-new")) {
188  Application.getApplication().sendRedirect(agreements.getPendingActURL("project-new"));
189  } else {
190  checkDossier();
191  initComponents();
192  }
193  }
194 
195  private void checkDossier() {
196  if(dossier == null) {
197  Long dossierId = (Long) ObjectString.parseString(Application.getApplication().getConstructor().getParameter("item"), Long.class, true);
198  if(dossierId > 0) {
199  dossier = getDao().find(Dossier.class, dossierId);
200  }
201  }
202  IContact contact = Authentication.getIContact();
203  if(dossier == null) {
204  dossier = new Dossier();
205  dossier.setPublishable(true);
206  dossier.setStatus(DossierStatus.DOSSIER_OPENED);
207  dossier.setType(DossierType.TYPE_PROJECT);
208  dossier.setCategory(getDefaultCategory());
209  Project project = new Project();
210  project.setPhase(0);
211  dossier.setProject(project);
212  project.setDossier(dossier);
213  project.setChangePhase(new Date());
214  Participant p = dossier.addAssistant(contact);
215  p.setDriver(true);
216  p.setOfferer(true);
217  } else {
218  Long catId = (Long) ObjectString.parseString(Application.getApplication().getConstructor().getParameter("cat"), Long.class, true);
219  if(catId > 0) {
220  Category cat = getDao().find(Category.class, catId);
221  if(cat != null) dossier.setCategory(cat);
222  }
223  }
224  }
225 
226  private void initComponents() {
227  IContact contact = Authentication.getIContact();
228  if(dossier.getFullParticipants().isOwner(contact) ||
229  dossier.getFullParticipants().isDriver(contact) ||
230  Application.getApplication().isInRole("dossier:all")) {
231  title.setValue(dossier.getProject().getProjectTitle());
232  estimatedBudget.setValue(dossier.getProject().getEstimatedBudget());
233  goal.setValue(dossier.getProject().getGoal());
234  searching.setValue(dossier.getSearching());
235  category.setObjectValue(dossier.getCategory());
236  String entityPath = DossierPU.getObjectPath(dossier);
237  tags.setObjectValues(Tags.getAsItemFrom(entityPath));
238  tagsdiv.setVisible(!dossier.isNew());
239  } else {
240  Application.getApplication().sendRedirect("/user");
241  }
242  }
243 
244  /* Dao */
245 
246  private Dao _dao;
247 
248  private Dao getDao() {
249  if(_dao == null) {
250  _dao = new DossierPU();
251  }
252  return _dao;
253  }
254 
255  private Category getDefaultCategory() {
256  Long catId = (Long) ObjectString.parseString(Application.getApplication().getConstructor().getParameter("cat"), Long.class, true);
257  if(catId > 0) {
258  Category cat = getDao().find(Category.class, catId);
259  if(cat != null) return cat;
260  }
261  List<Category> list = getDao().getResultList(
262  "select c from Category c where ownsProjects = TRUE order by description"
263  );
264  if(!list.isEmpty()) {
265  return list.get(0);
266  }
267  return null;
268  }
269 
270 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static String getObjectPath(Object object)
Definition: DossierPU.java:66
boolean isEmptyDescriptors(String grouping, boolean required)
void setCategory(Category category)
Definition: Dossier.java:115
DescriptorSet getDescriptorSet()
Definition: Dossier.java:650
void setType(DossierType type)
Definition: Dossier.java:211
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Dossier.java:399
void setPublishable(boolean publishable)
Definition: Dossier.java:203
void setProject(Project project)
Definition: Dossier.java:131
void setDescription(String description)
Definition: Dossier.java:147
void setSearching(String searching)
Definition: Dossier.java:223
void setStatus(DossierStatus status)
Definition: Dossier.java:195
Participant addAssistant(IContact contact)
Definition: Dossier.java:548
abstract void sendRedirect(String uri)
boolean anyTypeMatch(Collection< PhaseType > types)
void addToModel(TagItem value)
Set< TagItem > getObjectValues()
void setObjectValues(Collection< TagItem > internalValues)
static void addTag(Object entity, String tagName)
Definition: Tags.java:188
static void setTags(Object entity, String tags)
Definition: Tags.java:149
void setContact(IContact contact)
String getPendingActURL(String action)
boolean canAct(String action)