BrightSide Workbench Full Report + Source Code
EditBlogControl.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.publication.zul.www;
20 
21 import java.util.Date;
22 import org.turro.string.ObjectString;
23 import org.turro.auth.Authentication;
24 import org.turro.elephant.TemplateControl;
25 import org.turro.elephant.context.Application;
26 import org.turro.jpa.Dao;
27 import org.turro.plugin.contacts.IContact;
28 import org.turro.publication.db.PublicationPU;
29 import org.turro.publication.entity.Publication;
30 import org.turro.publication.util.PublicationWrapper;
31 import org.turro.publication.zul.ctrl.BloggerCategoriesListbox;
32 import org.turro.publication.zul.ctrl.BloggerGroupsListbox;
33 import org.turro.zkoss.input.DateboxShort;
34 import org.turro.zkoss.text.WikiEditor;
35 import org.zkoss.zk.ui.select.annotation.Listen;
36 import org.zkoss.zk.ui.select.annotation.Wire;
37 import org.zkoss.zul.Textbox;
38 
43 public class EditBlogControl extends TemplateControl {
44 
45  private Publication publication;
46  private String path;
47 
48  @Wire("#category") private BloggerCategoriesListbox category;
49  @Wire("#group") private BloggerGroupsListbox group;
50  @Wire("#title") private Textbox title;
51  @Wire("#author") private Textbox author;
52  @Wire("#date") private DateboxShort date;
53  @Wire("#summary") private WikiEditor summary;
54  @Wire("#body") private WikiEditor body;
55 
56  @Listen("onSelect=#category")
57  public void onCategory() {
58  publication.setPublicationCategory(category.getObjectValue());
59  }
60 
61  @Listen("onSelect=#group")
62  public void onGroup() {
63  publication.setPublicationGroup(group.getObjectValue());
64  }
65 
66  @Listen("onChange=#title")
67  public void onTitle() {
68  publication.setTitle(title.getValue());
69  }
70 
71  @Listen("onChange=#author")
72  public void onAuthor() {
73  publication.setAuthor(author.getValue());
74  }
75 
76  @Listen("onChange=#date")
77  public void onDate() {
78  publication.setDate(date.getValue());
79  }
80 
81  @Listen("onChange=#summary")
82  public void onSummary() {
83  publication.setWikiSummary(summary.getValue());
84  }
85 
86  @Listen("onChange=#body")
87  public void onBody() {
88  publication.setWikiBody(body.getValue());
89  }
90 
91  @Listen("onClick=#save")
92  public void onSave() {
93  PublicationWrapper dw = new PublicationWrapper(publication);
94  if(!publication.isEmpty()) {
95  publication = dw.save();
96  Application.getApplication().sendRedirect(path + publication.getId());
97  }
98  }
99 
100  @Listen("onClick=#cancel")
101  public void onCancel() {
102  Application.getApplication().sendRedirect(path + publication.getId());
103  }
104 
105  public String getPath() {
106  return path;
107  }
108 
109  public void setPath(String path) {
110  this.path = path;
111  }
112 
113  public void setPublication(Publication publication) {
114  this.publication = publication;
115  }
116 
117  @Override
118  protected void doFinally() {
119  super.doFinally();
120  checkPublication();
121  initComponents();
122  }
123 
124  private void checkPublication() {
125  if(publication == null) {
126  Long pubId = (Long) ObjectString.parseString(Application.getApplication().getConstructor().getParameter("item"), Long.class, true);
127  if(pubId > 0) {
128  publication = getDao().find(Publication.class, pubId);
129  }
130  }
131  IContact contact = Authentication.getIContact();
132  if(publication == null) {
133  publication = new Publication();
134  publication.setIdContact(contact.getId());
135  publication.setAuthor(contact.getName());
136  publication.setCreation(new Date());
137  publication.setDate(publication.getCreation());
138  publication.setPublicationCategory(category.getObjectValue());
139  publication.setPublicationGroup(group.getObjectValue());
140  date.setValue(publication.getDate());
141  author.setValue(publication.getAuthor());
142  }
143  }
144 
145  private void initComponents() {
146  if(publication.getId() != null && publication.getId() > 0) {
147  IContact contact = Authentication.getIContact();
148  if(Application.getApplication().isInRole("publication:all") ||
149  (publication.getPublicationCategory() != null && publication.getPublicationCategory().isBlogger(contact))) {
150  category.setObjectValue(publication.getPublicationCategory());
151  title.setValue(publication.getUnescapedTitle());
152  date.setValue(publication.getDate());
153  author.setValue(publication.getAuthor());
154  summary.setValue(publication.getWikiSummary());
155  body.setValue(publication.getWikiBody());
156  summary.setFileFolder("/_internal/files/publication/" + publication.getId());
157  summary.setImageFolder("/_internal/files/publication/" + publication.getId());
158  body.setFileFolder("/_internal/files/publication/" + publication.getId());
159  body.setImageFolder("/_internal/files/publication/" + publication.getId());
160  } else {
161  Application.getApplication().sendRedirect("/user");
162  }
163  } else {
164  summary.setFileFolder("/_internal/files/publication/empty");
165  summary.setImageFolder("/_internal/files/publication/empty");
166  body.setFileFolder("/_internal/files/publication/empty");
167  body.setImageFolder("/_internal/files/publication/empty");
168  summary.setReadOnlyRepository(true);
169  body.setReadOnlyRepository(true);
170  }
171  }
172 
173  /* Dao */
174 
175  private Dao _dao;
176 
177  private Dao getDao() {
178  if(_dao == null) {
179  _dao = new PublicationPU();
180  }
181  return _dao;
182  }
183 
184 }
abstract void sendRedirect(String uri)
void setPublicationGroup(PublicationGroup publicationGroup)
void setWikiSummary(String wikiSummary)
PublicationCategory getPublicationCategory()
void setPublicationCategory(PublicationCategory publicationCategory)
void setPublication(Publication publication)
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)
void setReadOnlyRepository(boolean readOnlyRepository)