From 2819cc125ce230983401bdb9dc310b4165d2308b Mon Sep 17 00:00:00 2001 From: Franta Pribyl Date: Tue, 14 Jul 2015 20:17:32 +0200 Subject: [PATCH] =?UTF-8?q?Implementov=C3=A1n=20valid=C3=A1tor=20pro=20Glo?= =?UTF-8?q?b=C3=A1ln=C3=AD=20nastaven=C3=AD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #236 --- .../java/info/bukova/isspst/Constants.java | 2 + .../java/info/bukova/isspst/RegExUtils.java | 31 ++ .../isspst/mail/EntityMessageBuilder.java | 3 +- .../bukova/isspst/ui/BindingViewModel.java | 19 + .../info/bukova/isspst/ui/FormViewModel.java | 15 +- .../isspst/ui/settings/GlobalSettingsVM.java | 57 ++- .../isspst/validators/BaseValidator.java | 45 +- .../validators/GlobalSettingValidator.java | 155 ++++++ src/main/webapp/settings/global/email.zul | 440 +++++++++++------- src/main/webapp/settings/globalSettings.zul | 13 +- 10 files changed, 584 insertions(+), 196 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/RegExUtils.java create mode 100644 src/main/java/info/bukova/isspst/ui/BindingViewModel.java create mode 100644 src/main/java/info/bukova/isspst/validators/GlobalSettingValidator.java diff --git a/src/main/java/info/bukova/isspst/Constants.java b/src/main/java/info/bukova/isspst/Constants.java index 44040098..d52178c8 100644 --- a/src/main/java/info/bukova/isspst/Constants.java +++ b/src/main/java/info/bukova/isspst/Constants.java @@ -169,6 +169,8 @@ public class Constants { public final static int LEN_TEXT = 255; public final static int LEN_DESCRIPTION = 8192; public final static int LEN_RESULT_MESSAGE = 8192; + + public final static String MAIL_URL_KEYWORD = "-url-"; public final static String KEY_SIGN_DATA = "SIGN_DATA"; public final static String KEY_SIGN_GUID = "SIGN_GUID"; diff --git a/src/main/java/info/bukova/isspst/RegExUtils.java b/src/main/java/info/bukova/isspst/RegExUtils.java new file mode 100644 index 00000000..d0678083 --- /dev/null +++ b/src/main/java/info/bukova/isspst/RegExUtils.java @@ -0,0 +1,31 @@ +package info.bukova.isspst; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegExUtils { + public static List getMatches(String input, String regEx, int group) { + List list = new ArrayList(); + + if (!StringUtils.isNullOrEmpty(input) && !StringUtils.isNullOrEmpty(regEx)) { + Pattern pattern = Pattern.compile(regEx); + Matcher matcher = pattern.matcher(input); + int groupCount = matcher.groupCount() + 1; + + if ((groupCount >= 0) && (group < groupCount)) { + while (matcher.find()) { + String item = matcher.group(group); + list.add(item); + } + } + } + + return list; + } + + public static List getMatches(String input, String regEx) { + return RegExUtils.getMatches(input, regEx, 0); + } +} diff --git a/src/main/java/info/bukova/isspst/mail/EntityMessageBuilder.java b/src/main/java/info/bukova/isspst/mail/EntityMessageBuilder.java index 74eadf36..58d6ae17 100644 --- a/src/main/java/info/bukova/isspst/mail/EntityMessageBuilder.java +++ b/src/main/java/info/bukova/isspst/mail/EntityMessageBuilder.java @@ -1,5 +1,6 @@ package info.bukova.isspst.mail; +import info.bukova.isspst.Constants; import info.bukova.isspst.EntityUrlResolver; import info.bukova.isspst.UrlResolverHolder; @@ -57,7 +58,7 @@ public class EntityMessageBuilder implements MessageBuilder { for (String p : properties) { try { - if (p.equals("-url-")) { + if (p.equals(Constants.MAIL_URL_KEYWORD)) { ret = ret.replaceAll("\\[" + p + "\\]", getUrl(data)); } else { ret = ret.replaceAll("\\[" + p + "\\]", BeanUtils.getProperty(data, p)); diff --git a/src/main/java/info/bukova/isspst/ui/BindingViewModel.java b/src/main/java/info/bukova/isspst/ui/BindingViewModel.java new file mode 100644 index 00000000..32a64a73 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/BindingViewModel.java @@ -0,0 +1,19 @@ +package info.bukova.isspst.ui; + +import org.zkoss.bind.annotation.Init; + +public class BindingViewModel extends DocumentViewModel { + + private BindingForm dataForm; + + @Init + public void initBindingViewModel() { + super.initDocumentViewModel(); + + dataForm = new BindingForm(); + } + + public BindingForm getDataForm() { + return this.dataForm; + } +} diff --git a/src/main/java/info/bukova/isspst/ui/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index 49d1105e..4d429cb8 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -20,28 +20,26 @@ import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Window; -public class FormViewModel extends DocumentViewModel +public class FormViewModel extends BindingViewModel { - private T dataBean; private Map errMessages; private Service service; private boolean newRec; private ServiceConstraint constraint; - private BindingForm dataForm; @Init public void initFormViewModel(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) { - super.initDocumentViewModel(); + super.initBindingViewModel(); this.dataBean = selected; this.service = service; constraint = new ServiceConstraint(); constraint.setDataBean(selected); constraint.setService(service); - dataForm = new BindingForm(); - dataForm.setDataBean(selected); + + this.getDataForm().setDataBean(selected); if (selected.getId() == 0 && selected.getCreated() == null) { @@ -186,9 +184,4 @@ public class FormViewModel extends DocumentViewModel { return true; } - - public BindingForm getDataForm() { - return dataForm; - } - } diff --git a/src/main/java/info/bukova/isspst/ui/settings/GlobalSettingsVM.java b/src/main/java/info/bukova/isspst/ui/settings/GlobalSettingsVM.java index 3577e450..3c147e51 100644 --- a/src/main/java/info/bukova/isspst/ui/settings/GlobalSettingsVM.java +++ b/src/main/java/info/bukova/isspst/ui/settings/GlobalSettingsVM.java @@ -6,13 +6,13 @@ import info.bukova.isspst.data.Address; import info.bukova.isspst.data.Requirement; import info.bukova.isspst.data.SettingsData; import info.bukova.isspst.data.Vehicle; -import info.bukova.isspst.mail.MailMessage; import info.bukova.isspst.services.settings.GlobalSettingsService; import info.bukova.isspst.sort.ReflectionTools; import info.bukova.isspst.storage.FileStorage; -import info.bukova.isspst.ui.DocumentViewModel; +import info.bukova.isspst.ui.BindingViewModel; import info.bukova.isspst.ui.LocaleConverter; import info.bukova.isspst.ui.SecurityHelper; +import info.bukova.isspst.validators.GlobalSettingValidator; import java.awt.image.RenderedImage; import java.io.IOException; @@ -23,6 +23,10 @@ import java.util.Set; import javax.imageio.ImageIO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.SimpleForm; +import org.zkoss.bind.Validator; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.ContextParam; @@ -33,9 +37,10 @@ import org.zkoss.zk.ui.event.UploadEvent; import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zul.Window; -public class GlobalSettingsVM extends DocumentViewModel +public class GlobalSettingsVM extends BindingViewModel { - + private final static Logger log = LoggerFactory.getLogger(GlobalSettingsVM.class.getName()); + @WireVariable private GlobalSettingsService settingsService; private SettingsData settings; @@ -44,12 +49,17 @@ public class GlobalSettingsVM extends DocumentViewModel private List refundsHours; @WireVariable private FileStorage storage; + + private Validator globalSettingValidator; @Init(superclass = true) - public void init() { + public void init(@BindingParam("window") Window mainWindow) { settings = settingsService.getSettings(); locConverter = new LocaleConverter(); setRefundsHours(settings.getRefunds().keySet()); + + this.getDataForm().setDataBean(this.settings); + this.globalSettingValidator = new GlobalSettingValidator(mainWindow); } public SettingsData getSettings() { @@ -108,18 +118,37 @@ public class GlobalSettingsVM extends DocumentViewModel return locConverter; } + private void insertString(SimpleForm form, String property, String field) { + if (form == null) { + log.warn("SimpleForm neexistuje !!!"); + return; + } + + Set set = form.getFieldNames(); + + if (set.contains(property)) { + String text = (String) form.getField(property); + text += "[" + field + "]"; + form.setField(property, text); + this.getDataForm().bind(); + return; + } + + log.warn("SimpleForm property '" + property + "' neexistuje !!!"); + } + @Command @NotifyChange("settings") - public void insertField(@BindingParam("field") String field, @BindingParam("message") MailMessage message) { - message.setText(message.getText() + "[" + field + "]"); + public void insertField2Fx(@BindingParam("form") SimpleForm form, @BindingParam("property") String property, @BindingParam("field") String field) { + this.insertString(form, property, field); } @Command @NotifyChange("settings") - public void insertUrl(@BindingParam("message") MailMessage message) { - message.setText(message.getText() + "[-url-]"); + public void insertUrl2Fx(@BindingParam("form") SimpleForm form, @BindingParam("property") String property) { + this.insertString(form, property, Constants.MAIL_URL_KEYWORD); } - + @Command @NotifyChange("settings") public void addAddress() { @@ -185,4 +214,12 @@ public class GlobalSettingsVM extends DocumentViewModel return null; } } + + public Validator getGlobalSettingValidator() { + return globalSettingValidator; + } + + public void setGlobalSettingValidator(Validator globalSettingValidator) { + this.globalSettingValidator = globalSettingValidator; + } } diff --git a/src/main/java/info/bukova/isspst/validators/BaseValidator.java b/src/main/java/info/bukova/isspst/validators/BaseValidator.java index 664f1a98..633e7705 100644 --- a/src/main/java/info/bukova/isspst/validators/BaseValidator.java +++ b/src/main/java/info/bukova/isspst/validators/BaseValidator.java @@ -1,11 +1,12 @@ package info.bukova.isspst.validators; +import info.bukova.isspst.StringUtils; + import java.util.regex.Matcher; import java.util.regex.Pattern; -import info.bukova.isspst.StringUtils; - import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.zkoss.bind.Property; import org.zkoss.bind.ValidationContext; import org.zkoss.bind.validator.AbstractValidator; @@ -17,7 +18,11 @@ import org.zkoss.zul.Messagebox; public abstract class BaseValidator extends AbstractValidator { - HtmlBasedComponent htmlComponent; + private final static Logger log = LoggerFactory.getLogger(BaseValidator.class.getName()); + + protected ValidationContext ctx; + + private HtmlBasedComponent htmlComponent; protected Logger getLogger() { @@ -124,4 +129,38 @@ public abstract class BaseValidator extends AbstractValidator return (String)value; } + + protected String getStringProperty(String propertyName) { + if (this.ctx == null) { + log.warn("Neznámý ValidationContext!!!"); + return ""; + } + + if (StringUtils.isNullOrTrimmedEmpty(propertyName)) { + log.warn("Chybné propertyName!!!"); + return ""; + } + + Property property = ctx.getProperties(propertyName)[0]; + + if (property == null) { + log.warn("Neexistující propertyName!!!"); + return ""; + } + + Object o = property.getValue(); + + if (o instanceof String) { + return (String) o; + } + else { + log.warn("Chybný typ property '" + propertyName + "'!!!"); + return ""; + } + } + + @Override + public void validate(ValidationContext ctx) { + this.ctx = ctx; + } } diff --git a/src/main/java/info/bukova/isspst/validators/GlobalSettingValidator.java b/src/main/java/info/bukova/isspst/validators/GlobalSettingValidator.java new file mode 100644 index 00000000..ac0d231c --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/GlobalSettingValidator.java @@ -0,0 +1,155 @@ +package info.bukova.isspst.validators; + +import info.bukova.isspst.Constants; +import info.bukova.isspst.RegExUtils; +import info.bukova.isspst.StringUtils; +import info.bukova.isspst.data.Requirement; +import info.bukova.isspst.data.SettingsData; +import info.bukova.isspst.sort.ReflectionTools; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.ValidationContext; +import org.zkoss.zk.ui.Component; +import org.zkoss.zul.Tab; +import org.zkoss.zul.Tabbox; +import org.zkoss.zul.Window; + +public class GlobalSettingValidator extends BaseValidator { + + private final static Logger log = LoggerFactory.getLogger(GlobalSettingValidator.class.getName()); + + private Window mainWindow; + + private SettingsData settingsData; + + public GlobalSettingValidator(Window mainWindow) { + this.mainWindow = mainWindow; + } + + private String SwitchTabs(String idIncludedPage, String idTabBox, String idTab, boolean select) { + if (this.mainWindow == null) { + log.warn("Neznámé okno globálního nastavení!!!"); + return ""; + } + + if (StringUtils.isNullOrEmpty(idTabBox) || StringUtils.isNullOrEmpty(idTab)) { + log.warn("Neznámý Tabbox nebo Tab identifikátor globálního nastavení!!!"); + return ""; + } + + Component component = this.mainWindow; + + if (!StringUtils.isNullOrEmpty(idIncludedPage)) { + Component includedPageComponent = component.getFellow(idIncludedPage); + + if (includedPageComponent != null) { + component = includedPageComponent; + } + } + + Tabbox tabBox = (Tabbox) component.getFellowIfAny(idTabBox, true); + + if (tabBox == null) { + log.warn("Neznámý Tabbox identifikátor '" + idTabBox + "' globálního nastavení!!!"); + return ""; + } + else { + Tab tab = (Tab) component.getFellowIfAny(idTab, true); + + if (tab == null) { + log.warn("Neznámý Tab identifikátor '" + idTab + "' globálního nastavení!!!"); + return ""; + } + else { + + if (select) { + tabBox.setSelectedTab(tab); + } + + return tab.getLabel(); + } + } + } + + private void validateMailMessage(String text, String idTab, String idComponent) { + if (this.ctx == null) { + log.warn("Neznámý ValidationContext!!!"); + return; + } + + List properties = ReflectionTools.getEntityFields(Requirement.class); + properties.add(Constants.MAIL_URL_KEYWORD); + + if (StringUtils.isNullOrEmpty(text)) { + return; + } + + List matches; + final String regex = "\\[([^\\[\\]]*?)\\]"; + + matches = RegExUtils.getMatches(text, regex, 1); + String message = ""; + + for (int i = 0; i < matches.size(); i++) { + String propertyCandidate = matches.get(i); + + if (!properties.contains(propertyCandidate)) { + message = "Vlastnost [" + propertyCandidate + "] neexistuje!"; + break; + } + } + + if (StringUtils.isNullOrEmpty(message)) { + text = text.replaceAll(regex, ""); + } + + if (StringUtils.isNullOrEmpty(message) && text.contains("[")) { + message = "Chybně použitý speciální znak '[' !\n"; + } + + if (StringUtils.isNullOrEmpty(message) && text.contains("]")) { + message = "Chybně použitý speciální znak ']' !\n"; + } + + boolean badCondition = !StringUtils.isNullOrEmpty(message); + + if (badCondition) { + this.SwitchTabs("", "idTabBox", "idTabEmails", true); + this.SwitchTabs("idIncludedPageEmails", "idTabBoxEmails", idTab, true); + this.errorMsg(this.ctx, message, idComponent); + } + } + + @Override + public void validate(ValidationContext ctx) { + + super.validate(ctx); + + // Map map = ctx.getProperties(); + // + // for (Map.Entry entry : map.entrySet()) { + // String key = entry.getKey(); + // Property[] values = entry.getValue(); + // } + + String value; + + value = this.getStringProperty("newReqTemplate.text"); + this.validateMailMessage(value, "idTabEmailsNewRequirement", "idEmailNewReqTemplateText"); + + value = this.getStringProperty("authReqTemplate.text"); + this.validateMailMessage(value, "idTabEmailsAuthRequirement", "idEmailAuthReqTemplateText"); + + value = this.getStringProperty("confReqTemplate.text"); + this.validateMailMessage(value, "idTabEmailsConfirmRequirement", "idEmailConfReqTemplateText"); + + value = this.getStringProperty("reqPassenger.text"); + this.validateMailMessage(value, "idTabEmailsReqTripPassengers", "idEmailReqPassengerTemplateText"); + + value = this.getStringProperty("confReqTripPassenger.text"); + this.validateMailMessage(value, "idTabEmailsConfirmTripPassengers", "idEmailConfReqTripPassengerTemplateText"); + } +} diff --git a/src/main/webapp/settings/global/email.zul b/src/main/webapp/settings/global/email.zul index 4bfc4e4d..054b62cb 100644 --- a/src/main/webapp/settings/global/email.zul +++ b/src/main/webapp/settings/global/email.zul @@ -1,172 +1,276 @@ - - - - - - - - - - - - - - - - - - - - - - - - -