BrightSide Workbench Full Report + Source Code
DossierComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.dossier;
20 
21 import java.util.Date;
22 import org.turro.action.queue.ConstraintKeys;
23 import org.turro.command.Command;
24 import org.turro.command.Context;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.dossier.dossier.DossierWrapper;
27 import org.turro.dossier.entity.DescriptorDefinition;
28 import org.turro.dossier.entity.DescriptorValue;
29 import org.turro.dossier.entity.Dossier;
30 import org.turro.dossier.entity.DossierType;
31 import org.turro.dossier.entity.Participant;
32 import org.turro.dossier.project.ChangePhaseNotification;
33 import org.turro.dossier.project.NewParticipantNotification;
34 import org.turro.dossier.zul.menu.DossierMenu;
35 import org.turro.elephant.context.Application;
36 import org.turro.i18n.I_;
37 import org.turro.jpa.composer.EntityComposer;
38 import org.turro.jpa.entity.DaoEntity;
39 import org.turro.mail.queue.QueueManager;
40 import org.turro.zkoss.dialog.SelectionDialog;
41 import org.turro.zkoss.text.WikiEditor;
42 import org.turro.zul.frame.Framework;
43 import org.zkoss.lang.Strings;
44 import org.zkoss.zk.ui.Component;
45 import org.zkoss.zk.ui.event.Event;
46 import org.zkoss.zk.ui.select.annotation.Listen;
47 import org.zkoss.zk.ui.select.annotation.Wire;
48 
53 public class DossierComposer extends EntityComposer<Dossier, Long> {
54 
55  private DescriptorDefinition lastDD;
56  private int oldPhase;
57 
58  @Wire("#wiki")
59  private WikiEditor wiki;
60 
61  @Wire("#descriptors")
62  private DescriptorsListbox descriptors;
63 
64  @Listen("onClick = #addIssue")
65  public void onAddIssue() {
67  @Override
68  public Object execute(Context context) {
71  return null;
72  }
73  });
74  }
75 
76  @Listen("onSelect = #descriptors")
77  public final void onSelectDescriptor(Event event) {
78  if(entity.getId() != null && entity.getId() > 0 && descriptors != null) {
79  if(lastDD != null) {
80  DescriptorValue dv = lastDD.getValue();
81  if(dv == null) {
82  dv = new DescriptorValue();
83  dv.setDossier(entity);
84  entity.getDescriptors().add(dv);
85  lastDD.setValue(dv);
86  }
87  dv.setWiki(wiki.getValue());
88  dv.setText(wiki.getHtml());
89  }
90  DescriptorDefinition dd = descriptors.getObjectValue();
91  if(dd != null) {
92  wiki.setImageFolder("/_internal/files/dossier/" + entity.getId());
93  wiki.setFileFolder("/_internal/files/dossier/" + entity.getId());
94  wiki.setReadonly(false);
95  DescriptorValue dv = dd.getValue();
96  if(dv != null) {
97  wiki.setValue(dv.getWiki());
98  } else {
99  wiki.setValue(null);
100  }
101  } else {
102  wiki.setImageFolder("/_internal/files/dossier/empty");
103  wiki.setFileFolder("/_internal/files/dossier/empty");
104  wiki.setReadonly(true);
105  wiki.setValue(null);
106  }
107  lastDD = dd;
108  }
109  }
110 
111  @Listen("onChange = #wiki")
112  public void onWikiChange() {
113  DescriptorDefinition dd = descriptors.getObjectValue();
114  DescriptorValue dv = dd.getValue();
115  if(dv == null) {
116  dv = new DescriptorValue();
117  dv.setDossier(entity);
118  entity.getDescriptors().add(dv);
119  dd.setValue(dv);
120  }
121  dv.setWiki(wiki.getValue());
122  dv.setText(wiki.getHtml());
123  }
124 
125  @Listen("onClick = #allParticipants")
126  public void onAllParticipants() {
127  ParticipantGrid pg = new ParticipantGrid();
128  pg.setAddToolbar(false);
129  entity.resetParticipants();
130  pg.setDossier(entity);
131  SelectionDialog.showComponent(getPage(),
132  I_.get("All participants"),
133  pg, "90%", "90%", null);
134  }
135 
136  @Override
137  protected String getAttributeName() {
138  return "dossier";
139  }
140 
141  @Override
142  protected Dossier getEntityInstance(Long id) {
143  if(id == null) {
144  entity = new Dossier();
145  entity.setPublishable(true);
147  } else {
148  entity = new DossierPU().find(Dossier.class, id);
149  }
150  return entity;
151  }
152 
153  @Override
154  protected DaoEntity getWrapperInstance(Component comp) {
158  return w;
159  }
160 
161  @Override
162  protected boolean beforeSave() {
163  if(entity != null && entity.getProject() != null) {
164  if(oldPhase != entity.getProject().getPhase()) {
165  entity.getProject().setChangePhase(new Date());
167  }
168  for(Participant participant : entity.getParticipants()) {
169  if(!participant.isEmpty() && !Strings.isBlank(participant.getDiscriminator()) &&
170  (participant.getId() == null || participant.getId() == 0L)) {
172  }
173  }
174  }
175  return super.beforeSave();
176  }
177 
178  @Override
179  public void afterSave() {
180  if(entity != null) {
181  for(Participant participant : entity.getParticipants()) {
182  new QueueManager().subscribeDefaults(ConstraintKeys.from(participant.getIContact()));
183  }
184  DossierMenu.showDossier(entity.getId());
185  }
186  }
187 
188  @Override
189  public void doFinally() throws Exception {
190  super.doFinally();
191  onSelectDescriptor(null);
192  if(entity.getProject() != null) {
193  oldPhase = entity.getProject().getPhase();
194  }
195  }
196 
197  @Override
198  protected boolean shouldBeSaved() {
199  return !entity.isEmpty() && (entity.getId() == null || entity.getId() == 0) || super.shouldBeSaved();
200  }
201 
202  @Override
203  protected boolean inSaveRole() {
204  return Application.getApplication().isInRole("dossier:edit");
205  }
206 
207  @Override
208  protected boolean inDeleteRole() {
209  return Application.getApplication().isInRole("dossier:delete");
210  }
211 
212 }
213 
static ConstraintKeys from(IContact contact)
void setValue(DescriptorValue descriptorValue)
static void addDossierInformation(Dossier dossier, Command command)
static void showDossier(Dossier dossier)
static String get(String msg)
Definition: I_.java:41
void subscribeDefaults(ConstraintKeys keys)
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
void setReadonly(boolean value)
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)
static Framework getCurrent()
Definition: Framework.java:203
void setSelectedLabel(String text)
Definition: Framework.java:103
void setSelectedTooltiptext(String text)
Definition: Framework.java:107