I have a following classes:
class Document{
Map<EnumChannelType, Channel> data;
//some more fields
}
class Channel{
String topic;
//some more fields
}
enum EnumChannelType{
BASIC_CHANNEL(1), ADVANCED_CHANNEL(2),......;
int value;
//constructor and some methods
}
Now I want to query on topic
inside Channel
. If channelType
is known, we can easily query as below:
Query<Document> createQuery(EnumChannelType channelType, String topic){
Query<Document> query = dao.createQuery().disableValidation();
query.field("data." + channelType.name() + ".topic").equal(topic);
return query;
}
But what if I want to get query for given only topic
(channelType
can be anything)? How can we create query for this?
One option is using or
as follows:
Query<Document> createQueryForTopic(String topic) {
Query<Document> query = dao.createQuery().disableValidation();
// add all possible Channel Types
query.or(query.criteria("data." + EnumChannelType.BASIC_CHANNEL.name() + ".topic").equal(topic),
query.criteria("data." + EnumChannelType.ADVANCED_CHANNEL.name() + ".topic").equal(topic),
/*...add criteria for all possible channel types*/);
return query;
}
But this is not feasible if EnumChannelType
is changing over time or if EnumChannelType
has large number of members (like BASIC_CHANNEL(1), ADVANCED_CHANNEL(2),....
).
I'm looking for something like...
Query<Document> createQuery(String topic){
Query<Document> query = dao.createQuery().disableValidation();
// use some regex instead of ????
query.field("data." + ???? + ".topic").equal(topic);
return query;
}