Beanio : How to validate a field, depending on another field value? - java

I am parsing a fixed length field flat file to a java bean using beanio library.
I have certain validations provided to number of fields.
For one of the field, I need to add a mandatory validation if another field is having a certain value.
Is there a way to attain this requirement through beanio?

Related

Hibernate Search with Elasticsearch creating fields with .keyword suffix

I just implemented the integration of Hibernate Search with Elasticsearch using hibernate search 5.8 and ES 5.5.
I have several fields created specifically for sorting, and they are all called [field]Sort.
When I was testing it locally, the first time I let Hibernate create the indexes, it created the String sort fields like this:
nameSort -> text
nameSort.keyword -> keyword
I realized that I should use the suffixed field for sorting.
But then, when I destroyed my Elasticsearch cluster, to start over, it didn't create the suffixed fields, it just created the sort fields as keyword directly.
I recreated the cluster 5 or more times again and it never created the suffixed fields again.
When I finally sent my changes to our staging environment, it created the suffixed fields again, causing my queries to fail, because they are trying to sort by a text field, instead of a keyword field.
Now, I'm really not sure of why it sometimes creates the suffix and sometimes doesn't.
Is there any rule?
Is there a way to avoid it creating 2 fields and making it always create only one keyword field with exactly the name I gave it?
Here's an example of a sort field:
#Field(name = "nameSort", analyze = Analyze.NO, store = Store.YES, index = Index.NO)
#SortableField(forField = "nameSort")
public String getNameSort() {
return name != null ? name.toLowerCase(Locale.ENGLISH) : null;
}
Thanks in advance for any help.
Hibernate Search does no such thing as creating a separate keyword field for text fields. It creates either a text field or a keyword field, depending on whether the field should be analyzed. In your case, the field is not analyzed, so it should create a keyword field.
Now, Hibernate Search is not alone here, and this behavior could stem from the Elasticsearch cluster itself. Did you check whether you have particular index templates on your Elasticsearch cluster? It could lead to Elasticsearch creating a keyword field whenever Hibernate Search creates a text property.
On a side note, you may be interested by the fact Hibernate Search 5.8 allows defining normalizers (same thing as Elasticsearch normalizers), which would allow you to annotate the getName() getter directly and avoid doing the lowercase conversion yourself. See this blog post for more information.

Storing Java Object in Schema less solr

Idea:
Convert the POJO into flat map using
ObjectToMapTransformer
and form a solrinputdocument from the map and store it in Solr.
While retrieving get the document from the Solr and convert the Document into map and to POJO using MapToObjectTransformer
Problem:
While saving the SolrinputDocument to Solr the flatten key like A.B[0].c of the POJO is getting converted to A.B_0_.c in Solr.
This alternate form of storage in Solr makes it difficult to deserialize the solrDocument to POJO.
How to solve this problem? Or what is the alternate way of storing queryable Document in Solr which can be deserialized and serialized easily.
You usually wrap the fields in your POJO with the appropriate Solr fields that you're indexing that field into. See Mapping a POJO for Solr.
If you really want to serialize the complete object into Solr, serialize it into a single field, and if possible, use a string field (as that will store your object directly). If you want to actually search string values inside the object as well, you can use a text field - but since everything is imported into a single field, that'll have a few limitations (like if you want to score different fields or search for values in a single property from the objects).
So: Use the #Field annotation from SolrJ.beans to do specific POJO handling, or mangle it into a single field and search for strings in that field.

How to create a column in Parse using code

I want to create a new column to store an array list in Parse, but I am unable to create the column (without using the dashboard). It needs to be created in the default "User" class. I've tried creating a Parse object in the user class and I tried querying for the column(hoping that if it doesn't find it, it will create it). It needs to be a column that can store an array list. I am not getting any errors in my code so I am unsure what to do next.
My experience is with the .NET API, but I suspect the principle is the same.
Parse will not create a new column simply from a read; you must set a value in at least one instance, and save it to the DB. This will create the column. Previously existing rows will contain "Undefined" for the new column value, and will not contain a key for the column.
My practice has been to derive types for my various ParseObjects. One thing this affords is that I can wrap the check for the key in my property getters, and set a default value if it is missing.
A caveat: (I'm speaking C#-ese here, so you'll have to do a mental translation) When you derive from ParseObject, you decorate the class with a ParseClassName attribute that defines the name for the document type in your database that your class is bound to. However, Parse already has a derived type, ParseUser, and when you derive from that, you must bind to the predefined "_User" class. (This is true for "_Session" and "_Role" also.)

Spring MVC form validation Date field

I have a form field that should be converted to a Date object, as follows:
<form:input path="date" />
And I have requirement to make validation in following way:
If user leaves this field es empty. I need to set some default date.
If User enetered in unacceptable format, I need to present an error.
The problem is that I can meet either 1st or 2nd requirement.
I can register PropertyEditor and in case date is unacceptable set null, and in case this field null set some default date. But with this approach I can't meet 2nd requirement because if I convert it to null I won't have ability to register 2nd error to user.
I can set #DateTimeFormat(pattern="dd-MMM-yyyy") annotation and provide appropriate typeMismatch error but it still return this error when user leaves empty value in this field.
Is any good solution to such problem ?
There is a way to do this if you'll move from Spring MVC validators to Hibernate. It's not a big deal because Hibernate validators work well with Spring Validator interface (also you may be interested in SmartValidator intreface which supports groups).
After that you can specify
#NotNull
annotation on the date field.
In properties file just add
NotNull.[formName].[dateField]=This message will be displayed if no date sent
typeMismatch.[formName].[dateField]=This message will be displayed if spring was not able to convert input to date object (format validation error)
For format check you can use CustomDateEditor.
With this solution you will get expected validation error messages in each case you've specified.
When I have to face such a problem, I reverse the logic. I do not set a default value to an empty field, but I pre-load the field with the default value in the render part of the operation. So I can safely valid with just #DateTimeFormat(pattern="dd-MMM-yyyy") because an empty field suppose that the user indendly removed the default value which is an error.
So IMHO you should set your default values in the GET part of you controller for that form and stick to a simple #DateTimeFormat(pattern="dd-MMM-yyyy") annotation.
I have an application using a #ModelAttribute annotated object containing a date field.
In the object class, the date field is annotated on getter and setter with DateTimeFormat(pattern="dd/MM/yyyy"). And Spring (3.2.4) accept an empty value for the date field which simply sets it to null. So you should use #DateTimeFormat(pattern="dd-MMM-yyyy") and still be able to accept null values.

XML Annotation for Derived Value

I know that #XmlJavaTypeAdapter allows me to change a list of xml object to map.
may I know if is possible to compute a value during xml unmarshalling?
for example a date of birth is coming in.
and i have a age field. I wish to compute the age field when xml is unmarshalling.
An XmlAdapter is used any time the representation in your object differs from the XML representation. XmlAdapter gives you a place to insert custom logic. It sounds like the perfect fit for your use case.

Categories

Resources