@@ -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> Třída datového objektu
|
||||
*/
|
||||
public class BindingForm<T> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,6 +28,7 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
|
||||
private Service<T> service;
|
||||
private boolean newRec;
|
||||
private ServiceConstraint<T> constraint;
|
||||
private BindingForm<T> dataForm;
|
||||
|
||||
@Init
|
||||
public void initFormViewModel(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service)
|
||||
@@ -39,6 +40,9 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
|
||||
constraint = new ServiceConstraint<T>();
|
||||
constraint.setDataBean(selected);
|
||||
constraint.setService(service);
|
||||
dataForm = new BindingForm<T>();
|
||||
dataForm.setDataBean(selected);
|
||||
|
||||
if (selected.getId() == 0 && selected.getCreated() == null)
|
||||
{
|
||||
newRec = true;
|
||||
@@ -183,4 +187,8 @@ public class FormViewModel<T extends DataModel> extends DocumentViewModel
|
||||
return true;
|
||||
}
|
||||
|
||||
public BindingForm<T> getDataForm() {
|
||||
return dataForm;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Address> {
|
||||
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<Address> {
|
||||
if (window != null)
|
||||
window.detach();
|
||||
}
|
||||
|
||||
public AddressValidator getValidator() {
|
||||
return validator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user