BrightSide Workbench Full Report + Source Code
PublicationComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.publication.zul.ctrl;
20 
21 import java.util.Date;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.auth.Authentication;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.util.Messages;
29 import org.turro.jpa.composer.EntityComposer;
30 import org.turro.jpa.entity.DaoEntity;
31 import org.turro.publication.db.PublicationPU;
32 import org.turro.publication.entity.Publication;
33 import org.turro.publication.util.PublicationWrapper;
34 import org.turro.publication.zul.menu.PublicationMenu;
35 import org.turro.zkoss.locale.LangListbox;
36 import org.turro.zkoss.text.RepositoriesTree;
37 import org.turro.zkoss.text.WikiEditor;
38 import org.turro.zul.frame.Framework;
39 import org.zkoss.zk.ui.Component;
40 import org.zkoss.zk.ui.event.InputEvent;
41 import org.zkoss.zk.ui.select.annotation.Listen;
42 import org.zkoss.zk.ui.select.annotation.Wire;
43 import org.zkoss.zul.Checkbox;
44 import org.zkoss.zul.Datebox;
45 import org.zkoss.zul.Textbox;
46 
51 public class PublicationComposer extends EntityComposer<Publication, Long> {
52 
53  private boolean modified;
54 
55  @Wire("#author")
56  private Textbox author;
57 
58  /*@Wire("#sender")
59  private IContactCombobox sender;*/
60 
61  @Wire("#pubDate")
62  private Datebox pubDate;
63 
64  @Wire("#retainTill")
65  private Datebox retainTill;
66 
67  @Wire("#pubGroup")
68  private GroupListbox pubGroup;
69 
70  @Wire("#pubCategory")
71  private CategoryListbox pubCategory;
72 
73  @Wire("#pubLang")
74  private LangListbox pubLang;
75 
76  @Wire("#accepted")
77  private Checkbox accepted;
78 
79  @Wire("#sent")
80  private Checkbox sent;
81 
82  @Wire("#pubTitle")
83  private Textbox pubTitle;
84 
85  @Wire("#pubWikiSummary")
86  private WikiEditor pubWikiSummary;
87 
88  @Wire("#pubWikiBody")
89  private WikiEditor pubWikiBody;
90 
91  @Listen("onClick = #preview")
92  public void onPreview() {
93  try {
95  } catch (InterruptedException ex) {
96  Logger.getLogger(PublicationComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
97  }
98  }
99 
100  @Listen("onChange = #author")
101  public void onAuthor(InputEvent event) {
102  entity.setAuthor(event.getValue());
103  modified = true;
104  }
105 
106  /*@Listen("onChange = #sender; onSelect = #sender")
107  public void onSender() {
108  entity.setSender(sender.getObjectValue());
109  modified = true;
110  }*/
111 
112  @Listen("onChange = #pubTitle")
113  public void onTitle(InputEvent event) {
114  entity.setTitle(event.getValue());
115  modified = true;
116  }
117 
118  @Listen("onChange = #pubSummary")
119  public void onSummary(InputEvent event) {
120  entity.setSummary(event.getValue());
121  modified = true;
122  }
123 
124  @Listen("onChange = #pubBody")
125  public void onBody(InputEvent event) {
126  entity.setBody(event.getValue());
127  modified = true;
128  }
129 
130  @Listen("onChange = #pubWikiSummary")
131  public void onWikiSummary(InputEvent event) {
132  entity.setWikiSummary(event.getValue());
133  modified = true;
134  }
135 
136  @Listen("onChange = #pubWikiBody")
137  public void onWikiBody(InputEvent event) {
138  entity.setWikiBody(event.getValue());
139  modified = true;
140  }
141 
142  @Override
143  protected String getAttributeName() {
144  return "publication";
145  }
146 
147  @Override
148  protected Publication getEntityInstance(Long id) {
149  if(id == null) {
150  entity = new Publication();
151  entity.setCreation(new Date());
152  entity.setDate(entity.getCreation());
153  entity.setContactAuthor(Authentication.getIContact());
154  } else {
155  entity = new PublicationPU().find(Publication.class, id);
156  }
157  return entity;
158  }
159 
160  @Override
161  protected DaoEntity getWrapperInstance(Component comp) {
165  return w;
166  }
167 
168  @Override
169  public void doFinally() throws Exception {
170  super.doFinally();
171  author.setValue(entity.getAuthor());
172 // sender.setObjectValue(Contacts.getContactById(entity.getSenderId()));
173  pubDate.setValue(entity.getDate());
174  retainTill.setValue(entity.getRetainTill());
175  pubGroup.setGroup(entity.getPublicationGroup());
176  pubCategory.setCategory(entity.getPublicationCategory());
177  pubLang.setObjectValue(entity.getLang());
178  pubTitle.setValue(entity.getUnescapedTitle());
179  pubWikiSummary.setValue(entity.getWikiSummary());
180  pubWikiBody.setValue(entity.getWikiBody());
181  accepted.setChecked(entity.isAccepted());
182  sent.setChecked(entity.isSent());
183  if(!Application.getApplication().isInRole("publication:accept")) {
184  sent.setDisabled(true);
185  accepted.setDisabled(true);
186  }
187  if(Strings.isBlank(pubWikiSummary.getImageFolder())) {
188  pubWikiSummary.setImageFolder(RepositoriesTree.REPOSITORY_PUBLICATION + "/Image");
189  }
190  if(Strings.isBlank(pubWikiBody.getImageFolder())) {
191  pubWikiBody.setImageFolder(RepositoriesTree.REPOSITORY_PUBLICATION + "/Image");
192  }
193  if(Strings.isBlank(pubWikiSummary.getFileFolder())) {
194  pubWikiSummary.setFileFolder(RepositoriesTree.REPOSITORY_PUBLICATION + "/File");
195  }
196  if(Strings.isBlank(pubWikiBody.getFileFolder())) {
197  pubWikiBody.setFileFolder(RepositoriesTree.REPOSITORY_PUBLICATION + "/File");
198  }
199  }
200 
201  @Override
202  protected boolean beforeSave() {
203  if(modified && !Application.getApplication().isInRole("publication:accept")) {
204  entity.setAccepted(false);
205  }
206  return super.beforeSave();
207  }
208 
209  @Override
210  protected boolean shouldBeSaved() {
211  return !entity.isEmpty() && super.shouldBeSaved();
212  }
213 
214  @Override
215  protected boolean inSaveRole() {
216  return Application.getApplication().isInRole("publication:edit");
217  }
218 
219  @Override
220  protected boolean inDeleteRole() {
221  return Application.getApplication().isInRole("publication:delete");
222  }
223 
224  @Override
225  public void afterSave() {
226  if(entity != null) {
228  }
229  }
230 
231  @Override
232  protected void doOnDelete() {
233  Messages.confirmDeletion().show(() -> {
235  });
236  }
237 
238 }
static Messages confirmDeletion()
Definition: Messages.java:87
void setCategory(PublicationCategory category)
void setGroup(PublicationGroup group)
static void previewPublication(Publication publication)
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