using and/or in JasperReport expression - java

i want to use and/or in JasperReport expression
i tried the following but it doesn't work:
($P{pId} == $F{id1}) or
($P{pId} == $F{id2} and F{return}=true)
? "good" : "bad"
but i am getting following exception:
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector#1c9f37d net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_report1_1318247835062_860381: 191: unexpected token: or # line 191, column 144. 1 error     at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:88)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188)     at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:131)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:509)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
any ideas why i am getting this exception and how to solve it.

and and or are not valid Java operators. JasperReports uses Java expressions. And since all Jasper parameters and fields are objects, I doubt you want to compare them with ==. Use equals instead.
($P{pId}.equals($F{id1}) ||
($P{pId}.equals($F{id2}) && F{return}.booleanValue()))
? "good" : "bad"

Related

Thymeleaf style attribute

I am working with thymleaf and I am trying to apply style directly from a spring object. I am trying to use the th:style attribute and pass in the value directly from the pojo.
I found this code online that works
th:style="${color == 'yellow' ? 'background:yellow' : 'background:blue'}"
But I want to pass in the value direct from the object.
Like this
th:style="${color == 'yellow' ? 'background:${color}' : 'background:blue'}"
or like this directly
th:style="background:${color}"
both ways throw up errors.
Any help appreciated.
The Thymeleaf docs have many examples of String concatenation... the easiest in this case would be literal substitution.
th:style="|background: ${color}|"
but you can just use a regular string concatenation as well.
th:style="${'background: ' + color}"
Why you don't use th:class="${color == 'yellow' ? 'yellowClass' : 'blueClass'}"
And style to both class.
If you already have class attr, you can using th:classappend

VS code - SonarLint:Simplify the expression produce invalid syntax

I'm getting S1125 for a code smell:
MyVO vo;
public boolean is() {
return vo == null ? false : vo.is();
}
Remove the unnecessary boolean literal.sonarlint(java:S1125)
But when choosing Quick Fix -> SonarLint:Simplify the expression it return invalid java syntax:
return !vo == null && vo.is();
Is it Sonar Lint bug ? is my vs code settings is wrong?
Obviously it should fix to:
return vo != null && vo.is();
(Using latest VS code and sonar lint plugin)
I didn't find any issue in sonar community/Jira
Opened a question in Sonar community https://community.sonarsource.com/t/sonarlint-simplify-the-expression-wrong-syntax-on-java-s1125/62467
Which reported as SONARJAVA-4241
S1125: erroneous quick fix suggestion when negating a binary operation
and probably will be removed
It seems that covering all the cases seems tricky. We might want to simplify and simply don't suggest a quick fix if the expression is a binary operation.

JDK 8 Optional Features

Am sorry, if am not clear :
I am working on implementing Restful Services using Spring Boot.
I am building a URI based on the request parameters. I am checking for the parameter values and based on that I need to build the URI using the parameter values. If it is NOT null I want to add it to the URI parameter.
host/hello?abc="somevalue"
MultiValueMap<String,String> params=new LinkedMultiValueMap<String,String>();
if (null != abc) {
params.add("abc","123");
}
I am new to JDK 8 features.
1) How can I do this using JDK 8 optional features?
2) Is it possible both to throw an exception and log it using Optional?
I frankly see no point in using an Optional here. The code you have is fine (we can always discuss the Yoda condition).
If you insist, you may write:
Optional.ofNullable(abc).ifPresent(abc -> params.add("abc","123"));
Link: Yoda Conditions: To Yoda or Not to Yoda

Assert the String has certain length (Java)

Is there any way to assert in a method that input String has certain length?
I tried assert stringName[4]; but seems like it doesn't work
If you just want to use the Java's assert keyword and not any library like JUnit, then you can probably use:
String myStr = "hello";
assert myStr.length() == 5 : "String length is incorrect";
From the official docs:
The assertion statement has two forms. The first, simpler form is:
assert Expression1;
where Expression1 is a boolean expression. When
the system runs the assertion, it evaluates Expression1 and if it is
false throws an AssertionError with no detail message.
The second form of the assertion statement is:
assert Expression1 : Expression2 ;
where:
Expression1 is a boolean expression. Expression2 is an expression that
has a value. (It cannot be an invocation of a method that is declared
void.)
You can use the following if you're using a testing library like JUnit:
String myStr = "hello";
assertEquals(5, myStr.length());
Update:
As correctly pointed out in the comments by AxelH, after compilation, you run the first solution as java -ea AssertionTest. The -ea flag enables assertions.
Instead of using assert, I'd recommend using Exception to check the state of the variables as a simple demo:
String[] arr = new String[3];
if (arr.length != 4) {
throw new IllegalStateException("Array length is not expected");
}
This will directly give the hint by exceptions and you don't need to bother with assert in jvm options.
Exception in thread "main" java.lang.IllegalStateException: Array length is not expected
at basic.AssertListLength.main(AssertListLength.java:7)
If you are using hamcrest, then you can do:
assertThat("text", hasLength(4))
See http://hamcrest.org/JavaHamcrest/javadoc/2.2/ > CharSequenceLength
Good thing about this is that it will have a proper error message, among others including the string itself.
Use AssertJ hasSize():
assertThat(stringName).hasSize(4)

Using Java Stream anymatch with filter for null check

I am using Stream.anymatch to check if any of the four string is empty or null- Can we get handle to the list of Strings that are null as a part of this check for subsequent logic
if(Stream.of(stringA, stringB,stringC, stringD)
.anyMatch(field -> field == null || field.trim().isEmpty()))
I would suggest you use StringUtils.isBlank() from Apache Commons libraries https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
Then you can use a temporary value to store Objects with blank values using
List<String> blank = Stream.of(stringA, stringB,stringC, stringD)
.filter(StringUtils::isBlank)
.collect(Collectors.toList());
if (blank.size() > 0) {
// your code here
}
I suggest you use multiple if(StringUtils.isBlank(stringX)){} statements if you need to execute different code in each case.

Categories