JOOQ dynamic set - java

I would like to update only fields that had been updated (they wont be null).
I would like to create a dynamic set, for example (not a valid code):
dslContext.update(table)
if(field1 != null) {
.set(field1, val1)
}
if(field2 != null) {
.set(field2, val2)
}
.where(condition1)
.and(condition2)
.execute();
Is there a way to do this?

While you could certainly achieve this by (ab)using the DSL API, the best way to actually do this is by using one of the following methods:
UpdateSetStep.set(Map)
UpdateSetStep.set(Record)
An example using Record:
Record record = dslContext.newRecord(table);
if (field1 != null)
record.set(field1, val1)
if (field2 != null) {
record.set(field2, val2)
dslContext.update(table)
.set(record)
.where(condition1)
.and(condition2)
.execute();

Related

Is there any way in Quarkus to listen specific #incomming message from Kafka based on the condition (like based on header types)

#Incoming("kafka-consumer-incoming")
#Blocking
public void listen(ConsumerRecord<String, String> record) {
String eventType=null;
if(record.headers()!=null) {
//eventType =IR/VR/CR/MR/BR possible values
Header eventTypeHeader = record.headers().lastHeader("milestoneEvent");
if (eventTypeHeader != null && eventTypeHeader.value() != null) {
eventType = new String(eventTypeHeader.value(), StandardCharsets.UTF_8);
System.out.println("eventType=="+eventType);
}
}
}
eventType having different values,if eventType value is equals to IR/VR,then I need to listen/consume messages from kafka-consumer-incoming topic.
How can we control this in quarkus?
You have to consume from all records; Kafka cannot be queried by headers. What you're doing is correct - check and filter for specific values within each consumed record, then process or ignore, accordingly.

Unable to update "dueAt" or "dueOn" fields on Tasks using Asana API for Java

Im trying to create a task with "dueAt" and "dueOn" fields but Im unable to fill these fields.
I have created the task "copyTask" by Asana website and Im able to fill due dates. But if I try to create another task using Asana API for Java and give it any value to "dueAt" or "dueOn" fields, they aren't filled with anything.
I have used this:
newTask = asanaClient.tasks.createInWorkspace(workspaceId)
.data("name", "MyProofTask")
.data("notes", "MyDescriptionTask")
.data("projects", Arrays.asList(newProject.id))
.data("dueAt", (copyTask.dueAt != null) ? new com.google.api.client.util.DateTime (copyTask.dueAt.getValue()) : null)
.data("dueOn", (copyTask.dueOn != null ? new com.google.api.client.util.DateTime (copyTask.dueOn.getValue()) : null))
.execute();
Any solution?
Use "due_at" and "due_on".
newTask = asanaClient.tasks.createInWorkspace(workspaceId)
.data("name", "MyProofTask")
.data("notes", "MyDescriptionTask")
.data("projects", Arrays.asList(newProject.id))
.data("due_at", (copyTask.dueAt != null) ? new com.google.api.client.util.DateTime (copyTask.dueAt.getValue()) : null)
.data("due_on", (copyTask.dueOn != null ? new com.google.api.client.util.DateTime (copyTask.dueOn.getValue()) : null))
.execute();

Elasticsearch grab all but limit to a certain number with Java API

There isnt an example online with the Java API to show how to limit the rows that come back from a search with ElasticSearch for all items. I tried the Filter Limit but it just wouldnt work because it would bring back more then the limit. I know its per shard to, but is there no way around this. I cant find the from/size query/filter either in the Java API
SearchQuery searchQuery = startQuery(limit, null).build();
Iterable<Statement> iterableStatements = esSpringDataRepository.search(searchQuery);
if (iterableStatements != null) {
return IteratorUtils.toList(iterableStatements.iterator());
}
private NativeSearchQueryBuilder startQuery(int limit, QueryBuilder query) {
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
if(query != null) {
searchQueryBuilder = searchQueryBuilder.withQuery(query);
}
if(limit > 0) {
searchQueryBuilder = searchQueryBuilder.withFilter(FilterBuilders.limitFilter(limit));
}
return searchQueryBuilder;
}
Well I got it to work perfectly with this instead of the limit filter:
searchQueryBuilder = searchQueryBuilder.withPageable(new PageRequest(0, limit));

Hibernate replace string condition

I'm having some trouble to create a query where a necessary condition is to use a like into a field where I need to replace a special character. Something like this:
public List<MyClass> search(Myclass object){
Criteria cri = criteria(MyClass.class);
if(object.getName() != null){
cri.add(Restrictions.sqlRestriction("replace("+cri.getAlias()+".name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));
}
if(dominio.getType()!= null){
cri.add(Restrictions.eq("type", object.getType()));
}
if(dominio.getWorkspace() != null){
cri.add(Restrictions.eq("workspace", object.getWorkspace()));
}
if(dominio.getStatus() != null){
cri.add(Restrictions.eq("status", object.getStatus()));
}
return cri.list();
}
With this code I've got the error below:
this_.ID_MYCLASS as ID_MYCLASS1_33_0_,
this_.NM_MYCLASS as NM_MYCLASS4_33_0_,
this_.ID_STATUS as ID_STATU5_33_0_,
this_.ID_TYPE as ID_TYPE_7_33_0_,
this_.ID_WORKSPACE as ID_WORKS8_33_0_
from KDTB_MYCLASS this_
where replace(this.name,'_', '') like 'COD'
and this_.ID_WORKSPACE=?
ORA-00904: "THIS"."NAME": invalid identifier
What am I doing wrong?
Thanks in advance!
Try this instead:
cri.add(Restrictions.sqlRestriction("replace({alias}.name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));
You could try this as the first parameter of sqlRestriction:
"replace("+cri.getAlias()+"_.name,"
Ultimately, you're pointing out that HQL doesn't support the replace routine. If you have more trouble, consider ditching Criteria & use a native SQL query for this.

How do I check for the null value in Java?

How to look for the value whether it is null or not in JSP. E.g. in below I want to check the description column in pipe table for null values.
String Query1 = "SELECT description FROM pipe where pipe_id='" + id+"' and version_id=2";
SQLResult = SQLStatement.executeQuery(Query1);
SQLResult.first();
description = SQLResult.getString("description");
if(description=="")
{
json_string=json_string+"&desc="+description;
out.println("not null");
} else
{
json_string=json_string;
out.println("null");
}
Well: if (description != null && !description.isEmpty()) ... however are you writing this code within a JSP ? That seems wrong. You should probably do this stuff within a servlet and pass the description as an request attribute to a jsp.
Or you can use StringUtils.isNotEmpty(description).
If you are really using jsp, you should store description in a request attribute and then use <c:if test="${empty description}">.
I use this personal method:
public static boolean isNullOrEmpty(Object obj) {
if (obj == null || obj.toString().length() < 1 || obj.toString().equals(""))
return true;
return false;
}

Categories