19 package org.turro.polls;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.apache.commons.mail.EmailException;
26 import org.turro.action.IEntityHolder;
27 import org.turro.assistant.Assistant;
28 import org.turro.assistant.AssistantSet;
29 import org.turro.auth.Authentication;
30 import org.turro.command.Command;
31 import org.turro.command.Context;
32 import org.turro.elephant.context.Application;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.entities.db.Poll;
36 import org.turro.elephant.util.Messages;
37 import org.turro.entities.Entities;
38 import org.turro.i18n.I_;
39 import org.turro.mail.message.MailMessageTemplate;
40 import org.turro.mail.pool.MailMessagePool;
41 import org.turro.mail.provider.MailProviders;
42 import org.turro.marker.ElephantMarker;
43 import org.turro.plugin.contacts.ContactList;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.mail.recipients.IMailRecipients;
46 import org.turro.zkoss.input.DateboxShort;
47 import org.turro.zkoss.input.GenericBandbox;
48 import org.turro.zkoss.input.GenericCombobox;
49 import org.turro.zkoss.input.GenericListbox;
50 import org.turro.zkoss.input.KnowsID;
51 import org.turro.zkoss.input.Percentbox;
52 import org.turro.zkoss.text.WikiEditor;
53 import org.zkoss.zk.ui.Executions;
54 import org.zkoss.zk.ui.HtmlBasedComponent;
55 import org.zkoss.zk.ui.IdSpace;
56 import org.zkoss.zk.ui.event.Event;
57 import org.zkoss.zk.ui.event.Events;
58 import org.zkoss.zk.ui.event.InputEvent;
59 import org.zkoss.zk.ui.ext.AfterCompose;
60 import org.zkoss.zk.ui.select.Selectors;
61 import org.zkoss.zk.ui.select.annotation.Listen;
62 import org.zkoss.zk.ui.select.annotation.Wire;
63 import org.zkoss.zul.Checkbox;
64 import org.zkoss.zul.Datebox;
65 import org.zkoss.zul.Hlayout;
66 import org.zkoss.zul.Intbox;
67 import org.zkoss.zul.Label;
68 import org.zkoss.zul.Listbox;
69 import org.zkoss.zul.Longbox;
70 import org.zkoss.zul.Panel;
71 import org.zkoss.zul.Space;
72 import org.zkoss.zul.Textbox;
73 import org.zkoss.zul.Timebox;
79 public class PollControl extends Panel implements IdSpace, AfterCompose {
87 private Textbox title;
90 private Textbox location;
93 private Datebox deadline;
95 @Wire(
"#maxPerOption")
96 private Intbox maxPerOption;
98 @Wire(
"#maxVotedOptions")
99 private Intbox maxVotedOptions;
105 private Hlayout maxmin;
107 @Wire(
"#showParticipants")
108 private Checkbox showParticipants;
111 private Checkbox showVotes;
113 @Wire(
"#allowInsertions")
114 private Checkbox allowInsertions;
116 @Wire(
"#allowNegatives")
117 private Checkbox allowNegatives;
128 @Wire(
"#attachments")
134 @Listen(
"onSelect = #polls")
139 @Listen(
"onChange = #title")
141 if(poll !=
null) poll.
setTitle(event.getValue());
144 @Listen(
"onChange = #location")
146 if(poll !=
null) poll.
setLocation(event.getValue());
149 @Listen(
"onChange = #deadline")
151 if(poll !=
null) poll.
setDeadline(deadline.getValue());
154 @Listen(
"onChange = #maxPerOption")
156 if(poll !=
null) poll.
setMaxPerOption(Integer.valueOf(event.getValue()));
159 @Listen(
"onChange = #maxVotedOptions")
164 @Listen(
"onSelect = #optionType")
168 createMaxmin(
null,
null);
172 @Listen(
"onCheck = #showParticipants")
177 @Listen(
"onCheck = #showVotes")
179 if(poll !=
null) poll.
setShowVotes(showVotes.isChecked());
182 @Listen(
"onCheck = #allowInsertions")
187 @Listen(
"onCheck = #allowNegatives")
192 @Listen(
"onChange = #pollWiki")
194 if(poll !=
null) poll.
setWiki(event.getValue());
197 @Listen(
"onClick = #createPoll")
205 @Listen(
"onClick = #savePoll")
215 @Listen(
"onClick = #deletePoll")
226 @Listen(
"onClick = #sendTest")
232 public Object execute(Context context) {
245 @Listen(
"onClick = #send")
255 private void updateControls() {
258 title.setPlaceholder(
I_.
get(
"Title"));
259 title.setReadonly(
false);
261 location.setReadonly(
false);
263 deadline.setReadonly(
false);
265 maxPerOption.setReadonly(
false);
267 maxVotedOptions.setReadonly(
false);
269 optionType.setDisabled(
false);
279 if(poll.
getId() !=
null && poll.
getId() > 0) {
297 title.setValue(
null);
298 title.setPlaceholder(
null);
299 title.setReadonly(
true);
300 location.setValue(
null);
301 location.setReadonly(
true);
302 deadline.setValue(
null);
303 deadline.setReadonly(
true);
304 maxPerOption.setValue(0);
305 maxPerOption.setReadonly(
true);
306 maxVotedOptions.setValue(0);
307 maxVotedOptions.setReadonly(
true);
308 optionType.setObjectValue(OptionDataType.OPTION_STRING);
309 optionType.setDisabled(
true);
314 showParticipants.setChecked(
false);
315 showVotes.setChecked(
false);
316 allowInsertions.setChecked(
false);
317 allowNegatives.setChecked(
false);
333 Executions.createComponents(
"/WEB-INF/_zul/bs/comps/poll/pollControl.zul",
this,
null);
334 Selectors.wireComponents(
this,
this,
false);
335 Selectors.wireEventListeners(
this,
this);
372 for(
Assistant assistant : participants) {
376 mmt.
addTo(assistant.email, assistant.name);
378 em.
put(
"poll", poll);
379 em.
put(
"contact", assistant.contact);
380 em.
put(
"email", assistant.email);
381 em.
put(
"notifier",
this);
385 mmt.
setMessage(em,
"poll", assistant.contact);
388 }
catch (EmailException ex) {
397 private void createMaxmin(Object min, Object max) {
399 maxmin.getChildren().clear();
401 minEditor.addEventListener(Events.ON_CHANGE, (Event event) -> {
402 poll.setOptionStart(getFormattedString(poll.getOptionClass(), getEditorValue(minEditor)));
404 maxmin.appendChild(
new Label(
I_.
get(
"Min value")));
405 maxmin.appendChild(minEditor);
407 maxEditor.addEventListener(Events.ON_CHANGE, (Event event) -> {
408 poll.setOptionEnd(getFormattedString(poll.getOptionClass(), getEditorValue(maxEditor)));
410 maxmin.appendChild(
new Space());
411 maxmin.appendChild(
new Label(
I_.
get(
"Max value")));
412 maxmin.appendChild(maxEditor);
418 public static HtmlBasedComponent
getEditor(String optionClass,
boolean readonly, Object value) {
422 db.setReadonly(readonly);
427 db.setReadonly(readonly);
431 db.setReadonly(readonly);
435 db.setReadonly(readonly);
438 Textbox tb =
new Textbox((String) value);
439 tb.setReadonly(readonly);
440 tb.setMaxlength(255);
450 if(hbc instanceof Checkbox) {
451 return ((Checkbox) hbc).isChecked();
454 }
else if(hbc instanceof Longbox) {
455 return ((Longbox) hbc).getValue();
456 }
else if(hbc instanceof Datebox) {
457 return ((Datebox) hbc).getValue();
458 }
else if(hbc instanceof Timebox) {
459 return ((Timebox) hbc).getValue();
460 }
else if(hbc instanceof
KnowsID) {
461 return ((
KnowsID) hbc).getObjectId();
468 }
else if(hbc instanceof Listbox) {
469 return ((Listbox) hbc).getSelectedItem().getValue();
471 return ((Textbox) hbc).getValue();
void addContacts(Collection< IContact > contacts)
static IContact getIContact()
IConstructor getConstructor()
static Application getApplication()
static String getSiteName()
static String logMsg(String msg)
boolean isAllowNegatives()
AssistantSet getParticipants()
void setTitle(String title)
void setMaxPerOption(int maxPerOption)
void setAllowNegatives(boolean allowNegatives)
void setMaxVotedOptions(int maxVotedOptions)
boolean isAllowInsertions()
void setShowParticipants(boolean showParticipants)
boolean isShowParticipants()
void setEntityPath(String entityPath)
void setWiki(String wiki)
void setLocation(String location)
void setShowVotes(boolean showVotes)
void setOptionClass(String optionClass)
void setDeadline(Date deadline)
void setAllowInsertions(boolean allowInsertions)
static Messages confirmDeletion()
Messages add(String word)
static IElephantEntity getController(String path)
static String get(String msg)
void setMessage(ElephantMarker marker, IContact contact)
static MailMessageTemplate of(MailProvider provider)
void setRoot(String root)
Email addTo(String email, String name)
void setSubject(String subject)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailProvider getProvider()
MailMessagePool getGeneric()
static MailProviders instance()
static void selectRecipients(Collection< IContact > recipients, Command command)
Collection< IContact > getRecipients()
Object put(Object key, Object value)
void onTitleChange(InputEvent event)
void doSend(AssistantSet participants)
void onMaxPerOptionChange(InputEvent event)
static Object getEditorValue(HtmlBasedComponent hbc)
void onCheckParticipants()
static HtmlBasedComponent getEditor(String optionClass, boolean readonly, Object value)
void onWikiChange(InputEvent event)
void onLocationChange(InputEvent event)
void setEntityPath(String entityPath)
void onMaxVotedOptionsChange(InputEvent event)
static String getFormattedString(String optionClass, Object value)
void setEntity(Object entity)
static String getOptionTemplate(Poll poll)
static List< Poll > getPolls(String entityPath)
static void removePoll(Poll poll)
static Poll savePoll(Poll poll, List< PollOption > pos)
void setAllowDeletions(boolean allowDeletions)
void setAllowInsertions(boolean allowInsertions)
void setReadOnly(boolean readOnly)
void setImageFolder(String folder)
void setValue(String value)
void setFileFolder(String folder)
void setReadOnlyRepository(boolean readOnlyRepository)
String getString(Object value)
static OptionDataType getOptionType(String value)
void setEntity(Object entity)