BrightSide Workbench Full Report + Source Code
CreateTopicControl.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.forum.www;
20 
21 import java.util.Date;
22 import org.turro.string.Strings;
23 import org.turro.auth.Authentication;
24 import org.turro.collections.KeyValueMap;
25 import org.turro.elephant.TemplateControl;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.db.ElephantPU;
29 import org.turro.elephant.entities.db.Topic;
30 import org.turro.entities.EntityCombobox;
31 import org.turro.forum.ForumContext;
32 import org.turro.forum.Forums;
33 import org.turro.forum.db.UniquePath;
34 import org.turro.html.HTMLEntities;
35 import org.turro.i18n.I_;
36 import org.turro.jpa.Dao;
37 import org.turro.marker.MarkerHelper;
38 import org.turro.plugin.contacts.IContact;
39 import org.zkoss.zk.ui.event.InputEvent;
40 import org.zkoss.zk.ui.select.annotation.Listen;
41 import org.zkoss.zk.ui.select.annotation.Wire;
42 import org.zkoss.zul.Label;
43 import org.zkoss.zul.Textbox;
44 
49 public class CreateTopicControl extends TemplateControl {
50 
51  private String contextPath;
52 
53  @Wire("#entity") private EntityCombobox entity;
54  @Wire("#text") private Textbox text;
55  @Wire("#counter") private Label counter;
56 
57  @Listen("onChanging=#text")
58  public void onTexting(InputEvent event) {
59  counter.setValue(I_.get("Minimum") +
60  " " + event.getValue().length() +
61  "/100");
62  }
63 
64  @Listen("onClick=#save")
65  public void onSave() {
66  Topic topic = new Topic();
67  String topicText = text.getValue(),
68  entityPath = entity.getEntityPath();
69  if(entityPath != null && !Strings.isBlank(topicText) && topicText.length() > 100) {
71  topic.setText(HTMLEntities.escape(topicText));
72  topic.setCreation(new Date());
73  topic.setAuthorId(contact.getId());
74  topic.setEntityPath(entityPath);
75  if(!topic.isEmpty()) {
76  topic = getDao().saveObject(topic);
78  Forums.markAsUnseenExceptFor(topic, contact);
79  Forums.notify(topic);
80  Application.getApplication().sendRedirect("/user/forums?" +
81  MarkerHelper.setObfuscatedRightNowPars("item=" + topic.getId()));
82  }
83  }
84  }
85 
86  @Listen("onClick=#cancel")
87  public void onCancel() {
88  Application.getApplication().sendRedirect(contextPath);
89  }
90 
91  public String getContextPath() {
92  return contextPath;
93  }
94 
95  public void setContextPath(String contextPath) {
96  this.contextPath = contextPath;
97  }
98 
99  @Override
100  protected void doFinally() {
101  super.doFinally();
103  initComponents();
104  } else {
105  Application.getApplication().sendRedirect(contextPath);
106  }
107  }
108 
109  private void initComponents() {
110  KeyValueMap kvm = new KeyValueMap();
112  entity.setRoot(ForumContext.getAllowedRoots(constructor));
113  String dossierCategories = ForumContext.getDossierCategories(constructor);
114  if(!Strings.isBlank(dossierCategories)) {
115  kvm.put("dossier-categories", dossierCategories);
116  }
117  entity.setParameters(kvm);
118  }
119 
120  /* Dao */
121 
122  private Dao _dao;
123 
124  private Dao getDao() {
125  if(_dao == null) {
126  _dao = new ElephantPU();
127  }
128  return _dao;
129  }
130 
131 }
abstract void sendRedirect(String uri)
void setCreation(Date creation)
Definition: Topic.java:114
void setEntityPath(String entityPath)
Definition: Topic.java:88
void setAuthorId(String authorId)
Definition: Topic.java:106
void setParameters(KeyValueMap kvm)
static boolean getAllowNew(IConstructor constructor)
static String getAllowedRoots(IConstructor constructor)
static String getDossierCategories(IConstructor constructor)
static void markAsUnseenExceptFor(ITreeEntity entity, IContact contact)
Definition: Forums.java:56
static void notify(Topic topic)
Definition: Forums.java:66
static void normalizeUniquePaths(Topic topic)
Definition: UniquePath.java:35
static String escape(String html)
static String get(String msg)
Definition: I_.java:41
static String setObfuscatedRightNowPars(String parameters)