Omnifaces Validation - omnifaces

Is there a way to add a custom validation method when using omnifaces validation? I need a dynamic message which I generate from inside my bean.
public boolean validate(FacesContext facesContext, List<UIInput> components, List<Object> values) {
Messages.addGlobalError(customDynamicMessage);
return false;
}

Related

Spring MVC 4.3 Rest dynamic filtering of fields

I'm using Spring MVC 4.3.2 with #RestController and I would like to be able to dynamically filter JSON properties specified by our clients in the request.
To keep things easy, let's say I have a Person POJO that contains firstName and lastName fields. This POJO is generated, that's why you will see that I'm using a mixin.
Let's say one client want to retrieve all the persons' firstName, he will call our rest endpoint like this : http://localhost/persons&fields=firstName. And the json will only contains the firstName, not the lastName fields.
Here is my Controller:
#RestController
public class RestPersonController implements PersonApi {
#Autowired
private PersonService personService;
#Autowired
private ObjectMapper objectMapper;
#Override
#GetMapping(path = "/persons")
public ResponseEntity<List<Person>> getPersons(
#RequestParam(required = false) String fields) {
List<Person> persons = this.personService.getPersons();
// Filter fields
if (StringUtils.isNotBlank(fields)) {
configureFilter(fields);
}
return ResponseEntity.status(HttpStatus.OK).body(persons);
}
private void configureFilter(String fields) {
Set<String> fieldsToKeep = new HashSet<>();
fieldsToKeep.add(fields);
objectMapper.addMixIn(Person.class, PersonDynamicFilterMixIn.class);
FilterProvider filterProvider = new SimpleFilterProvider().
addFilter("dynamicFilter", SimpleBeanPropertyFilter.filterOutAllExcept(fieldsToKeep));
objectMapper.setFilterProvider(filterProvider);
}
}
As you can see, the return of the method is ResponseEntity<List<Person>> (I can't change it to JacksonMappingValue, it is from the PersonApi interface) and we are returning with ResponseEntity.status(HttpStatus.OK).body(persons) (so I can't manually write the JSON response with the objectMapper myself);.
Under the hood, Spring is using Jackson ObjectMapper to do the conversion. I would like to configure that ObjectMapper when the fields request parameter is specified.
I also want the ObjectMapper configuration to be the same as Spring's default.
I can't use JsonViews because otherwise it is not dynamic anymore.
Could someone show me how I need to configure Spring to do that ? I tried to use these kind of things in SpringConfiguration, but then I end up with some of my tests not working anymore. For some tests, if I run a single test it passes, if I run the whole test suite that test doesn't pass anymore ...
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
}
#Bean
public ObjectMapper objectMapper() {
return Jackson2ObjectMapperBuilder.json().build();
}
Thank you for your help !

Injecting Handlers in Custom Widget

I have a handler classes, I need to inject it in the custom widgets.
I tried the bind() method in ClientModule class, but it is not getting injected.
What am I supposed to do, do get the class injected.
public class ExtendedTextBoxBase extends TextBox {
public ExtendedTextBoxBase() {
super.addBlurHandler(textBoxBlurHandler);
}
#Inject
TextBoxBlurHandler textBoxBlurHandler; /* custom handler */
}
custom handler:
public class TextBoxBlurHandler implements BlurHandler {
#Inject
public TextBoxBlurHandler() {
}
#Override
public void onBlur(BlurEvent event) {
}
}
Thanks,
Bennet.
Initial reaction: did you include and #Inject statement in the method (likely constructor) where you would like to inject the handler?
If yes: could you be more specific with some code snippets?
I see two potential errors:
1. You have code in constructor:
super.addBlurHandler(textBoxBlurHandler);
so you should inject handler by constructor not by field.
Gin first crate object than inject fields into class, so your handler textBoxBlurHandler is null.
2. You crate your ExtendedTextBoxBase by uibinder. If yes, you should add annotation uiField provided=true, and inject this field:
#Inject
#UiField(provided=true)
ExtendedTextBoxBase extendedTextBoxBase;

Is there a PickList widget in GWT?

There is a PickList widget in PrimeFaces JSF component library?
Does GWT (or any other GWT component library) has such a widget?
Have a look at SmartGWT's featured example on Databound Dragging. You can also view its source. But since gwt does not have such widget, the best solution is to create your own custom component with the help of CellList.
I prefer to create my own PickList as it's easy and straightforward, below is what it looks like:
public abstract class PickList<T> extends Composite {
//The renderer provide the flexibility for client class customize the cell
public PickList(SafeHtmlRenderer<T> renderer) {
...
}
public void setCandidates(List<T> candidates, List<T> selected) {
//todo
}
public List<T> getSelectedValues() {
//todo
}
//Below two abstract method can facilitate getting values from view or rendering view from value
protected abstract T fromIdentity(String identity);
protected abstract String toIdentity(T value);
}

How to bind a list of objects to ListBox in GWT?

I want to bind my services to ListBox but I can't bind it.
//Service class
public class Service {
private String serviceName;
public Service(String serviceName) {
this.serviceName = serviceName;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
}
// SignUpBean class
public class SignUpBean {
private List<Service> services;
public List<Service> getServices() {
return services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
The following is my Main Editor
public class SignUpEditor extends SimplePanelimplements Editor<SignUpBean> {
public ListBox services;
public void SignUpEditor (){
services.addItem("Service1");
services.addItem("Service2");
setWidget(services);
}
}
I am not getting any error but I think I have to use ListEditor or CompositeEditor. But i don't know about it. Can anyone suggest how to bind Service to ListBox
ListBox suppose to be a LeafValueEditor. Conceptually we don't edit the list in the ListBox. Rather We select a value from the list which will be displayed in the selection. So, ListBox holds only one value and thus ListBox must be leaf value Editor.
Currently there is no GWT support to directly bind the list to ListBox. We have to write an adapter which extends ListEditor ( Refer HasDataEditor class for more detail ). This approach is strange.
Simple approach is to write an adapter which implements LeafValueEditor< List < String > >. In adapter's setValue method we should iterate over the list and call listBox.addItem for each value in the list.
I don't recommend either approaches simply because ListBox's LIST is NON EDITABLE and only VALUE is EDITABLE. I recommend doing addItems manually without using Editors.
UPDATED : HasDataAdapter is an example ListEditor. Refer that implementation. You may get some idea.

Converting a bean path to a simple string property in Ext GWT grid

I am working on an Ext GWT 3 (beta) application.
I am trying to display a simple value of a dependent bean in a Grid.
My data beans look like this:
public class MyBean {
private String content;
private MyOtherBean otherBean;
// getters and setters here...
}
public class MyOtherBean {
private String otherBeanContent;
// getter and setter here...
}
The PropertyAccess looks like this:
interface MyBeanProperties extends PropertyAccess<MyBean> {
ModelKeyProvider<MyBean> key();
ValueProvider<MyBean, String> content();
ValueProvider<MyBean, MyOtherBean> otherBean();
}
With the corresponding ColumnConfiguration, my grid now displays one column with content of MyBean and one column with MyOtherBean's toString().
But I want to display MyOtherBean.otherBeanContent instead (without changing MyOtherBean's toString()).
I think I need some kind of value converter and register it for the column? Or am I taking the wrong approach here?
This did the trick:
columnConfig.setCell(new PropertyDisplayCell<MyOtherBean>(new PropertyEditor<MyOtherBean>() {
#Override
public MyOtherBean parse(CharSequence text) throws ParseException {
return otherBean.setOtherBeanContentFromText(text);
}
#Override
public String render(MyOtherBean otherBean) {
return otherBean == null ? "" : otherBean.getOtherBeanContent();
}
}));