Validate Search Criteria : Java - java

I have to implement search based on almost 12 different fields.
For validation and processing, I am facing a lot of challenges. Following are those with which I am seeking inputs/help.
Classes:
SearchCriteria
(has) UserCriteria and AddressCriteria
PS. A few fields are related to user e.g. First Name, Date of Birth etc. which goes in UserCriteria and a Few fields are related to address e.g. Street Name, Building Number etc. which will go into AddressCriteria
Based on these criteria I have to search users.
Validate that at least one parameter is not null/not empty. I do not
want to keep checking each and every field for null and emptiness.
Before search validate based on which criteria you need to initiate
search. e.g. User or Address?
Implement validations such a way that you need not to worry about
specific field validation before adding it in query criteria
EDIT:
NOTE : I need to prepare a message with all the valid fields which will be passed to the module which helps me finding all records, I am not dealing with DB directly.

What if both of your objects (UserCriteria and AddressCriteria) has a method like,
public ArrayList<String> getSearchableFields() {
//return a ArrayList of searchable fields
}
Then you'll be able to call getSearchableFields() on each method and see what fields are available to use for your search.

Related

MongoDb Query In springBoot using MongoRepository

I am having Multiple Query Parameters (like name , age , gender, location etc...n numbers) in my GET api. Now I need to query my mongo Database using these query values . Now user can send from 0 to n query parameters.
I am trying to use Something Like
findByNameAndAge(String Name , String Age)
Or
findByNameAndAgeAndGender(String Name , String Age, String Gender)
But the problem is I will have to write multiple queries considering all the permutation and combination user can send.
Is there any better way to do it?
You will need to make your hands dirty with Mongo operations class and build the criteria query dynamically based on passed model parameters. To make things easier you can stick to parameter name maps to entity property convention.
In your case you can use
<S extends T> Iterable<S> findAll(Example<S> example);
So first step is to create an example from your request parameters, to do that you need to create an instance of your entity that have the value of desired attributes to be queried and null for other attributes.
For example if you have in query name and age as parameters. So you just set the name and age of the entity and let null the rest attributes. If you have your entity you should just create the example and query it.
Example<ENTITY_CLASS> example = Example.of(createdEntity);
return repository.findAll((example);
There is some other options to use Example and ExampleMatcher to make case-sensitive startwith contains and regex. You can also set if he need to use OR or AND operators between fields.
You can take a look here

A way to filter data at selection on DB with spring and JPA

I'm looking for a simple and practice solution to have the following:
a) filter data on DB on a certain field stored on a Table
(example: if field status is greater than 5 then it would never return)
b) this filter has to work with field with values and null values (for example Date types)
c) I would forgot for the future to remember every time when I write a select or JPA method that has access to this table to add this "filter".
Concrete Example:
I have a Repository for Users where I access and get back the list of all users.
Now I whant to add a condition/filter that say that only users with status 5 have to return. Then I can write ewryting like this:
List<User> findAllAndTypeGreatherThan(int status);
Now when I came back in a few months and I have to add a new method like
List<User> findByName(String name);
I musst remember that I have to filter out the types greater than 5 or I forget this.
I don't whant remeber this..... How can I solve this?

How to select a list of objects as part of the constructor argument for a Hibernate group by query

I'm trying to figure out how to select a list of objects as part of a Hibernate group-by query. I know how to do it a harder way, but I'm curious if there is some special sugar syntax that achieves the same thing.
Basically, I have a query of this structure:
select com.myapp.domain.TagSummary(
tag.id, tag.term, tag.description, tag.synonyms, count(user)
)
from User user
join user.tags tag
I'd like to store the tag.synonyms as a List<Tag>. Is that possible, or do I need to query the cross product and do the separation manually after the query results come back?
Alternatively, what I really want in the end is a list of synonym terms separated by commas. So if a tag is spring and it has synonym terms spring-framework and spring-framework-3.1, it would be great to put into the constructor the string spring-framework, spring-framework-3.1. Is that possible?
EDIT: I have learned that I can use group_concat() to achieve the second half of the functionality, but it's only available in MySQL. Is there a way to make it available in hsqldb as well? In Spring 3.1, how do I add this function to Hibernate? I know I should call something on Configuration, but I don't know what bean to access it by.
for (Object[]> result : query.list()) {
Tag tag = (Tag ) result[3];
User user = (User) result[4];
}
You can get more information from this link
https://derrickpetzold.com/p/in-and-group-by-count-hibernate/

Codebook OTHER value

I have a problem regarding my current CodeBook.
CodeBook is an entity that consists of two attributes, the Code and Description.
It is an abstract class. I extended that class with the Class Domain.
Domain class is also an abstract class and contains enum DomainType, which has values:
e.g. NATIONALITY, NATIVE_LANGUAGE, DISEAS.. etc
Now I have to make "document" entities which will later be filled with data.
So for example, the client has to pick the nationality. It will select one from the list of the values that has been entered in the codebook DomainNationality (Extends Domain) for the domainType.NATIONALITY, BUT what if I don't have his nationality in my CodeBook? I will put an option OTHER, that he can select,but I also need to put the field where he will type his OTHER nationality.
How do I keep a record of that other nationality?
Right now,my document entities has associative links to DomainNationality.DomainSex, DomainNativeLanguage etc.. Is the String field next to every domain in my document the best and easiest solution? I don't want to let clients enter new stuff in to the CodeBook.
Thank you!
This is mainly your design-decision. You can do it which-ever way you design your object-responsibilities. But here are my thoughts: Since you say it is possible that the client's nationality is not among those which you offer and want to offer him/her a field to enter a custom nationality you should just store the nationality as a string in the object (no enum). You can always check afterwards if the nationality of a client is among the pre-defined ones.

solrj: how to store and retrieve List<POJO> via multivalued field in index

My use case is an index which holds titles of online media. The provider of the data associates a list of categories with each title. I am using SolrJ to populate the index via an annotated POJO class
e.g.
#Field("title")
private String title;
#Field("categories")
private List<Category> categoryList;
The associated POJO is
public class Category {
private Long id;
private String name;
...
}
My question has two parts:
a) is this possible via SolrJ - the docs only contain an example of #Field using a List of String, so I assume the serialization/marshalling only supports simple types ?
b) how would I set up the schema to hold this. I have a naive assumption I just need to set
multiValued=true on the required field & it will all work by magic.
I'm just starting to implement this so any response would be highly appreciated.
The answer is as you thought:
a) You have only simple types available. So you will have a List of the same type e.g. String. The point is you cant represent complex types inside the lucene document so you wont deserialize them as well.
b) The problem is what you are trying is to represent relational thinking in a "document store". That will probably work only to a certain point. If you want to represent categories inside a lucene document just use the string it is not necessary to store a id as well.
The only point to store an id as well is: if you want to do aside the search a lookup on a RDBMS. If you want to do this you need to make sure that the id and the category name is softlinked. This is not working for every 1:n relation. (Every 1:n relation where the n related table consists only of required fields is possible. If you have an optional field you need to put something like a filling emptyconstant in the field if possible).
However if these 1:n relations are not sparse its possible actually if you maintain the order in which you add fields to the document. So the case with the category relation can be probably represented if you dont sort the lists.
You may implement a method which returns this Category if you instantiate it with the values at position 0...n. So the solution would be if you want to have the first category it will be at position 0 of every list related to this category.

Categories

Resources