diff --git a/src/main/java/info/bukova/isspst/ui/BindingForm.java b/src/main/java/info/bukova/isspst/ui/BindingForm.java new file mode 100644 index 00000000..b380fd5b --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/BindingForm.java @@ -0,0 +1,68 @@ +package info.bukova.isspst.ui; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.beanutils.BeanUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.SimpleForm; + + +/** + * Třída middle objektu pro validaci formuláře. Umožňuje uložit formulář do datového objektu bez volání validace. + * Do definice formuláře v ZUL souboru je potřeba uvést: {@code @init(vm.dataForm) }. + * + * @author pepa + * + * @param Třída datového objektu + */ +public class BindingForm extends SimpleForm { + + /** + * + */ + private static final long serialVersionUID = -9052402254058071780L; + private T dataBean; + + private static final Logger logger = LoggerFactory.getLogger(BindingForm.class); + + + /** + * Uloží formulář do předaného objektu. + * + * @param object Datový objekt + */ + public void bindTo(T object) { + for (String key : getFieldNames()) { + try { + BeanUtils.setProperty(object, key, getField(key)); + } catch (IllegalAccessException e) { + logger.warn("Cannot bind value", e); + } catch (InvocationTargetException e) { + logger.warn("Cannot bind value", e); + } + } + } + + + /** + * Uloží formulář do členského datového objektu. + */ + public void bind() { + bindTo(dataBean); + } + + public T getDataBean() { + return dataBean; + } + + /** + * Nastaví datový objekt této instance {@link BindingForm} + * + * @param dataBean Datový objekt + */ + public void setDataBean(T dataBean) { + this.dataBean = dataBean; + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index da5b3dcd..49d1105e 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -28,6 +28,7 @@ public class FormViewModel extends DocumentViewModel private Service service; private boolean newRec; private ServiceConstraint constraint; + private BindingForm dataForm; @Init public void initFormViewModel(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) @@ -39,6 +40,9 @@ public class FormViewModel extends DocumentViewModel constraint = new ServiceConstraint(); constraint.setDataBean(selected); constraint.setService(service); + dataForm = new BindingForm(); + dataForm.setDataBean(selected); + if (selected.getId() == 0 && selected.getCreated() == null) { newRec = true; @@ -183,4 +187,8 @@ public class FormViewModel extends DocumentViewModel return true; } + public BindingForm getDataForm() { + return dataForm; + } + } diff --git a/src/main/java/info/bukova/isspst/ui/addressbook/AddressForm.java b/src/main/java/info/bukova/isspst/ui/addressbook/AddressForm.java index e3cdb3e7..aa187ee1 100644 --- a/src/main/java/info/bukova/isspst/ui/addressbook/AddressForm.java +++ b/src/main/java/info/bukova/isspst/ui/addressbook/AddressForm.java @@ -5,6 +5,7 @@ import info.bukova.isspst.services.IsspstException; import info.bukova.isspst.services.addressbook.AdbService; import info.bukova.isspst.services.addressbook.AddressFinder; import info.bukova.isspst.ui.FormViewModel; +import info.bukova.isspst.validators.AddressValidator; import java.util.HashMap; import java.util.List; @@ -28,15 +29,18 @@ public class AddressForm extends FormViewModel
{ private AddressFinder addressFinderAres; @WireVariable private AddressFinder addressFinderTaxID; + private AddressValidator validator; @Init(superclass = true) public void init() { - + validator = new AddressValidator(); } @Command @NotifyChange("dataBean") public void searchAres() { + getDataForm().bind(); + if (getDataBean().getIc() != 0) { try { adbService.fillFoundData(addressFinderTaxID, getDataBean()); @@ -73,5 +77,9 @@ public class AddressForm extends FormViewModel
{ if (window != null) window.detach(); } + + public AddressValidator getValidator() { + return validator; + } } diff --git a/src/main/java/info/bukova/isspst/validators/AddressValidator.java b/src/main/java/info/bukova/isspst/validators/AddressValidator.java new file mode 100644 index 00000000..2d8d5758 --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/AddressValidator.java @@ -0,0 +1,32 @@ +package info.bukova.isspst.validators; + +import org.zkoss.bind.ValidationContext; + +public class AddressValidator extends BaseValidator { + + private static final String EMAIL_PATTERN = + "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; + private static final String URL_PATTERN = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; + + @Override + public void validate(ValidationContext ctx) { + if (!validateIsNotNullOrNotEmpty(ctx, "company", "CompanyIsEmpty", "company")) { + return; + } + + if (!validateIsNotNullOrNotEmpty(ctx, "city", "CityIsEmpty", "city")) { + return; + } + + if (!validateRegExp(ctx, "email", "EmailFormatIncorrect", "email", EMAIL_PATTERN)) { + return; + } + + if (!validateRegExp(ctx, "web", "WebUrlFormatIncorrect", "web", URL_PATTERN)) { + return; + } + + } + +} diff --git a/src/main/java/info/bukova/isspst/validators/BaseValidator.java b/src/main/java/info/bukova/isspst/validators/BaseValidator.java index d413106c..664f1a98 100644 --- a/src/main/java/info/bukova/isspst/validators/BaseValidator.java +++ b/src/main/java/info/bukova/isspst/validators/BaseValidator.java @@ -1,5 +1,8 @@ package info.bukova.isspst.validators; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import info.bukova.isspst.StringUtils; import org.slf4j.Logger; @@ -94,4 +97,31 @@ public abstract class BaseValidator extends AbstractValidator return true; } + + protected boolean validateRegExp(ValidationContext ctx, String property, String errMessage, String componentId, String regExpPattern) { + String value = getStringValue(ctx, property); + + if (value != null && !value.isEmpty()) { + Pattern pattern = Pattern.compile(regExpPattern); + Matcher matcher = pattern.matcher(value); + + if (!matcher.matches()) { + errorMsg(ctx, StringUtils.localize(errMessage), componentId); + return false; + } + } + + return true; + } + + protected String getStringValue(ValidationContext ctx, String property) { + Property prop = ctx.getProperties(property)[0]; + Object value = prop.getValue(); + + if (value == null || !(value instanceof String)) { + return null; + } + + return (String)value; + } } diff --git a/src/main/webapp/WEB-INF/locales/validators.properties b/src/main/webapp/WEB-INF/locales/validators.properties index 8ddd11dc..914abfec 100644 --- a/src/main/webapp/WEB-INF/locales/validators.properties +++ b/src/main/webapp/WEB-INF/locales/validators.properties @@ -3,9 +3,15 @@ DataTypeErr=Chybný datový typ... UserPasswordIsEmpty=Uživatelské heslo musí být zadané... UserPasswordsAreNotSame=Znovu zadané heslo není stejné... + RequirementCenterIsEmpty=Musíte zadat středisko... TripDateIsEmpty=Musíte zadat datum cesty... EndDateIsEmpty=Musíte zadat datum konce cesty... FromIsEmpty=Musíte zadat místo začátku cesty... ToIsEmpty=Musíte zadat cíl cesty... VehicleIsEmpty=Musíte zadat dopravní prostředek... + +CompanyIsEmpty=Název firmy musí být zadaný... +CityIsEmpty=Město musí být zadané... +EmailFormatIncorrect=E-mailová adresa musí být správně zadaná... +WebUrlFormatIncorrect=Adresa webu musí být správně zadaná... diff --git a/src/main/webapp/lists/addressbook/address.zul b/src/main/webapp/lists/addressbook/address.zul index ad41213b..cea5d67e 100644 --- a/src/main/webapp/lists/addressbook/address.zul +++ b/src/main/webapp/lists/addressbook/address.zul @@ -3,7 +3,7 @@ - + @@ -12,52 +12,52 @@