162 lines
4.5 KiB
Java
162 lines
4.5 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.invitefriends.client.validation;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
import javax.validation.Path;
|
|
import javax.validation.metadata.ConstraintDescriptor;
|
|
|
|
import org.gcube.portlets.user.invitefriends.client.validation.ValidationErrorsView.UserDetails;
|
|
|
|
import com.google.gwt.activity.shared.AbstractActivity;
|
|
import com.google.gwt.editor.client.EditorDriver;
|
|
import com.google.gwt.event.shared.EventBus;
|
|
import com.google.gwt.user.client.ui.AcceptsOneWidget;
|
|
import com.google.gwt.validation.client.impl.PathImpl;
|
|
|
|
/**
|
|
* Showcase for <code>ErrorEditor</code>s validation decoration of <code>TextBox</code>es.
|
|
*
|
|
* @author ivangsa
|
|
*
|
|
*/
|
|
public class ValidationErrorsActivity extends AbstractActivity implements ValidationErrorsView.Delegate {
|
|
|
|
private ValidationErrorsView view;
|
|
|
|
|
|
/**
|
|
* @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
|
|
*/
|
|
@Override
|
|
public void start(AcceptsOneWidget panel, EventBus eventBus) {
|
|
view = new ValidationErrorsViewImpl(this);
|
|
panel.setWidget(view);
|
|
}
|
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
@Override
|
|
public boolean onSendClick() {
|
|
EditorDriver<UserDetails> editorDriver = view.getEditorDriver();
|
|
UserDetails login = editorDriver.flush();
|
|
Set<ConstraintViolation<UserDetails>> violations = validate(login);
|
|
/* This will decorate widgets implementing HasEditorErrors with validation messages */
|
|
return editorDriver.setConstraintViolations( (Set) violations);
|
|
}
|
|
|
|
private Set<ConstraintViolation<UserDetails>> validate(UserDetails user2InviteBean){
|
|
Set<ConstraintViolation<UserDetails>> violations = new HashSet<ConstraintViolation<UserDetails>>();
|
|
if(user2InviteBean.getEmail() == null || "".equals(user2InviteBean.getEmail().trim())) {
|
|
violations.add(buildNotNullConstraintViolation(user2InviteBean, "email"));
|
|
}
|
|
else if (!isValidEmailAddress(user2InviteBean.getEmail())) {
|
|
violations.add(buildInvalidEmailAddressConstraintViolation(user2InviteBean, "email"));
|
|
}
|
|
return violations;
|
|
}
|
|
|
|
|
|
private ConstraintViolation<UserDetails> buildNotNullConstraintViolation(final UserDetails bean, final String path) {
|
|
return new ConstraintViolation<UserDetails>() {
|
|
@Override
|
|
public String getMessage() {
|
|
return "must not be empty";
|
|
}
|
|
@Override
|
|
public String getMessageTemplate() {
|
|
return null;
|
|
}
|
|
@Override
|
|
public UserDetails getRootBean() {
|
|
return bean;
|
|
}
|
|
@Override
|
|
public Class<UserDetails> getRootBeanClass() {
|
|
return UserDetails.class;
|
|
}
|
|
@Override
|
|
public Object getLeafBean() {
|
|
return bean;
|
|
}
|
|
|
|
@Override
|
|
public Path getPropertyPath() {
|
|
return new PathImpl().append(path);
|
|
}
|
|
|
|
@Override
|
|
public Object getInvalidValue() {
|
|
return null;
|
|
}
|
|
@Override
|
|
public ConstraintDescriptor<?> getConstraintDescriptor() {
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
|
|
private ConstraintViolation<UserDetails> buildInvalidEmailAddressConstraintViolation(final UserDetails bean, final String path) {
|
|
return new ConstraintViolation<UserDetails>() {
|
|
@Override
|
|
public String getMessage() {
|
|
return "email address must be valid";
|
|
}
|
|
@Override
|
|
public String getMessageTemplate() {
|
|
return null;
|
|
}
|
|
@Override
|
|
public UserDetails getRootBean() {
|
|
return bean;
|
|
}
|
|
@Override
|
|
public Class<UserDetails> getRootBeanClass() {
|
|
return UserDetails.class;
|
|
}
|
|
@Override
|
|
public Object getLeafBean() {
|
|
return bean;
|
|
}
|
|
|
|
@Override
|
|
public Path getPropertyPath() {
|
|
return new PathImpl().append(path);
|
|
}
|
|
|
|
@Override
|
|
public Object getInvalidValue() {
|
|
return null;
|
|
}
|
|
@Override
|
|
public ConstraintDescriptor<?> getConstraintDescriptor() {
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
/**
|
|
*
|
|
* @param emailValue
|
|
* @return
|
|
*/
|
|
private boolean isValidEmailAddress(String emailValue) {
|
|
if(emailValue == null) return true;
|
|
|
|
String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.(?:[a-zA-Z]{2,6})$";
|
|
|
|
boolean valid = false;
|
|
|
|
if(emailValue.getClass().toString().equals(String.class.toString())) {
|
|
valid = ((String)emailValue).matches(emailPattern);
|
|
} else {
|
|
valid = ((Object)emailValue).toString().matches(emailPattern);
|
|
}
|
|
|
|
return valid;
|
|
}
|
|
|
|
}
|