BrightSide Workbench Full Report + Source Code
NewsletterComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.newsletter;
20 
21 import java.io.IOException;
22 import java.util.Date;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.action.Contacts;
26 import org.turro.action.queue.Notifications;
27 import org.turro.auth.Authentication;
28 import org.turro.elephant.context.Application;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.util.Messages;
31 import org.turro.entities.ActionWithEntity;
32 import org.turro.i18n.I_;
33 import org.turro.jpa.composer.EntityComposer;
34 import org.turro.jpa.entity.DaoEntity;
35 import org.turro.mail.message.MailInline;
36 import org.turro.mail.message.MailMessageTemplate;
37 import org.turro.mail.queue.GenericElephantNotification;
38 import org.turro.newsletter.mail.NewsletterSendToSubscribers;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.publication.db.PublicationPU;
41 import org.turro.publication.entity.Newsletter;
42 import org.turro.publication.zul.menu.PublicationMenu;
43 import org.turro.queue.zul.AllCategoriesListbox;
44 import org.turro.zkoss.dialog.SelectionDialog;
45 import org.turro.zkoss.input.DateboxShort;
46 import org.turro.zkoss.locale.LangListbox;
47 import org.turro.zul.frame.Framework;
48 import org.zkoss.zk.ui.Component;
49 import org.zkoss.zk.ui.event.InputEvent;
50 import org.zkoss.zk.ui.select.annotation.Listen;
51 import org.zkoss.zk.ui.select.annotation.Wire;
52 import org.zkoss.zk.ui.util.Clients;
53 import org.zkoss.zul.Checkbox;
54 import org.zkoss.zul.Div;
55 import org.zkoss.zul.Html;
56 import org.zkoss.zul.Textbox;
57 
62 public class NewsletterComposer extends EntityComposer<Newsletter, Long> {
63 
64  private boolean modified;
65 
66  @Wire("#newsDate")
67  private DateboxShort newsDate;
68 
69  @Wire("#newsCategory")
70  private AllCategoriesListbox newsCategory;
71 
72  @Wire("#newsLang")
73  private LangListbox newsLang;
74 
75  @Wire("#newsTemplate")
76  private TemplatesMailListbox newsTemplate;
77 
78  @Wire("#accepted")
79  private Checkbox accepted;
80 
81  @Wire("#newsTitle")
82  private Textbox newsTitle;
83 
84  @Wire("#staticContent")
85  private Checkbox staticContent;
86 
87  @Wire("#sendingChoice")
88  private SendingChoiceListbox sendingChoice;
89 
90  @Wire("#preview")
91  private ActionWithEntity actionWithEntity;
92 
93  @Wire("#sectionGrid")
94  private SectionsGrid sectionsGrid;
95 
96  @Listen("onClick = #preview")
97  public void onPreview() {
98  IContact contact = Contacts.getContact(actionWithEntity.getEntity());
99  if(contact != null && contact.isWebUser()) {
101  MailMessageTemplate mmt = nt.getMailMessage(null, contact);
102  Div div = new Div();
103  div.setStyle("overflow:auto;");
104  Html html = new Html(new MailInline().inline(mmt.getMessage()));
105  div.appendChild(html);
106  SelectionDialog.showComponent(getPage(), I_.get("Preview"), div, "800px", "80%", null);
107  entity.resetGlobalContext();
108  } catch (IOException ex) {
109  Logger.getLogger(NewsletterComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
110  }
111  }
112  }
113 
114  @Listen("onClick = #sendNow")
115  public void onSendNow() {
116  if(entity != null && !entity.isEmpty()) {
118  Clients.showNotification(I_.get("Queued sender started"));
119  }
120  }
121 
122  @Listen("onChange = #pubTitle")
123  public void onTitle(InputEvent event) {
124  entity.setTitle(event.getValue());
125  modified = true;
126  }
127 
128  @Override
129  protected String getAttributeName() {
130  return "newsletter";
131  }
132 
133  @Override
134  protected Newsletter getEntityInstance(Long id) {
135  if(id == null) {
136  entity = new Newsletter();
137  entity.setCreation(new Date());
138  entity.setDate(entity.getCreation());
139  entity.setSendingChoice(SendingChoice.USER_WANTS);
141  } else {
142  entity = new PublicationPU().find(Newsletter.class, id);
143  }
144  return entity;
145  }
146 
147  @Override
148  protected DaoEntity getWrapperInstance(Component comp) {
152  return w;
153  }
154 
155  @Override
156  public void doFinally() throws Exception {
157  super.doFinally();
158  newsDate.setValue(entity.getDate());
159  newsCategory.setCategory(entity.getCategory());
160  newsLang.setObjectValue(entity.getLang());
161  newsTemplate.setObjectValue(entity.getTemplate());
162  newsTitle.setValue(entity.getUnescapedTitle());
163  accepted.setChecked(entity.isAccepted());
164  staticContent.setChecked(entity.isStaticContent());
165  sendingChoice.setObjectValue(entity.getSendingChoice());
166  if(!Application.getApplication().isInRole("publication:accept")) {
167  accepted.setDisabled(true);
168  }
169  sectionsGrid.setNewsletter(entity);
170  actionWithEntity.setRoot("contact");
171  actionWithEntity.setEntity(Authentication.getIContact().getContact());
172  }
173 
174  @Override
175  protected boolean beforeSave() {
176  if(modified && !Application.getApplication().isInRole("publication:accept")) {
177  entity.setAccepted(false);
178  }
179  return super.beforeSave();
180  }
181 
182  @Override
183  protected boolean shouldBeSaved() {
184  return !entity.isEmpty() && super.shouldBeSaved();
185  }
186 
187  @Override
188  protected boolean inSaveRole() {
189  return Application.getApplication().isInRole("publication:edit");
190  }
191 
192  @Override
193  protected boolean inDeleteRole() {
194  return Application.getApplication().isInRole("publication:delete");
195  }
196 
197  @Override
198  public void afterSave() {
199  if(entity != null) {
201  }
202  }
203 
204  @Override
205  protected void doOnDelete() {
206  Messages.confirmDeletion().show(() -> {
208  });
209  }
210 
211 }
static IContact getContact(Object object)
Definition: Contacts.java:109
static NotificationCategory getCategory(String id)
static Messages confirmDeletion()
Definition: Messages.java:87
static String get(String msg)
Definition: I_.java:41
void setNewsletter(Newsletter newsletter)
void setCategory(NotificationCategory category)
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
static Framework getCurrent()
Definition: Framework.java:203
void setSelectedLabel(String text)
Definition: Framework.java:103
void setSelectedTooltiptext(String text)
Definition: Framework.java:107