Spring SPEL collection projection - java

A simple question on SPEL collection selection.
Look at section 10.5.17 Collection Selection on this page
https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == 'Serbian']").getValue(societyContext);
What i need is the selection 'Serbian' to come from outside and not be a fixed hard coded String.
Just for arguments sake consider that, we could get it as "selectedNationality" from the same society class from the same page in the link.
Modified class with selectedNationality
public class Society {
private String name;
public static String Advisors = "advisors";
public static String President = "president";
private List<Inventor> members = new ArrayList<Inventor>();
private Map officers = new HashMap();
// new selector field
private String selectedNationality;
......
}
New Selection
The new selection SPEL would look like
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == selectedNationality]").getValue(societyContext);
When we try that the error is that "selectedNationality" is not a part of the Member object.
Does that mean that for collection selection in spring expression language we would need a hard coded String ? If yes does anyone know why ?

Found out how to do it. So the way is to use variables
see 10.5.11 Variables # [https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html][1]
Set Variable
So in our case we wold do this set variable :
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("selectedNationality ", "Serbian");
New Selection
The new selection SPEL would look like this with #selectedNationality
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == #selectedNationality]").getValue(societyContext);
Works like a charm !

Related

Evaluate FHIR Expression in Java against an object

I'm very new to the FHIR standard and I could use some help figuring out how to evaluate a ruleExpression against an object.
Here is my object:
#ResourceDef(name = "TestObj", profile = "http://hl7.org/fhir/StructureDefinition/TestObj")
#Data
public class TestObj extends DomainResource {
private static final long serialVersionUID = 1L;
#Child(name = "numMarbles")
#Extension(url = "http://hl7.org/fhir/CustomExtension/numMarbles", definedLocally = true, isModifier = false)
#Description(shortDefinition = "The number of marbles I have in my pocket")
private IntegerType numMarbles;
}
I'm trying to figure out how to run a rule evaluation on it. For example:
String ruleExp = "%numMarbles > 3"
In order to try options.. I've setup the following integration test:
#Test
void doRuleEval() throws Exception {
TestObj t = new TestObj();
t.setNumMarbles(4);
String ruleExp = "%numMarbles > '3'";
FhirPathR4 path = new FhirPathR4(fhirContext);
// ?????
Object something = path.evaluate(t, ruleExp, null);
// Line above always fails: "unknown fixed constant %numMarbles"
log.info("something: " + something.toString());
}
I've scoured the FHIR documentation and can't find java examples for how to evaluate dynamic rules against FHIR models. In the Javascript library we used the "compile" method but I can't find the equivalent Java method.
I feel like I'm missing something fundamental here.
The "evaluate" method is not documented - it takes in an "IBase" object which is any FHIR object from what I can tell, and returns some sort of list... who knows.
The FhirPathR4 object also contains a "parse" method that returns nothing and is also un-documented.
Thanks for any help or tips to point me in the right direction for evaluating a "ruleExpression" against an object's fields.
I would go onto the HAPI FHIR Google group as ask about the evaluate method there.
https://groups.google.com/g/hapi-fhir?pli=1

"Variables" that is typed by user in a string java [duplicate]

Is there any String replacement mechanism in Java, where I can pass objects with a text, and it replaces the string as it occurs?
For example, the text is:
Hello ${user.name},
Welcome to ${site.name}.
The objects I have are user and site. I want to replace the strings given inside ${} with its equivalent values from the objects. This is same as we replace objects in a velocity template.
Use StringSubstitutor from Apache Commons Text.
Dependency import
Import the Apache commons text dependency using maven as bellow:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
Example
Map<String, String> valuesMap = new HashMap<String, String>();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
StringSubstitutor sub = new StringSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
Take a look at the java.text.MessageFormat class, MessageFormat takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.
Object[] params = new Object[]{"hello", "!"};
String msg = MessageFormat.format("{0} world {1}", params);
My preferred way is String.format() because its a oneliner and doesn't require third party libraries:
String message = String.format("Hello! My name is %s, I'm %s.", name, age);
I use this regularly, e.g. in exception messages like:
throw new Exception(String.format("Unable to login with email: %s", email));
Hint: You can put in as many variables as you like because format() uses Varargs
I threw together a small test implementation of this. The basic idea is to call format and pass in the format string, and a map of objects, and the names that they have locally.
The output of the following is:
My dog is named fido, and Jane Doe owns him.
public class StringFormatter {
private static final String fieldStart = "\\$\\{";
private static final String fieldEnd = "\\}";
private static final String regex = fieldStart + "([^}]+)" + fieldEnd;
private static final Pattern pattern = Pattern.compile(regex);
public static String format(String format, Map<String, Object> objects) {
Matcher m = pattern.matcher(format);
String result = format;
while (m.find()) {
String[] found = m.group(1).split("\\.");
Object o = objects.get(found[0]);
Field f = o.getClass().getField(found[1]);
String newVal = f.get(o).toString();
result = result.replaceFirst(regex, newVal);
}
return result;
}
static class Dog {
public String name;
public String owner;
public String gender;
}
public static void main(String[] args) {
Dog d = new Dog();
d.name = "fido";
d.owner = "Jane Doe";
d.gender = "him";
Map<String, Object> map = new HashMap<String, Object>();
map.put("d", d);
System.out.println(
StringFormatter.format(
"My dog is named ${d.name}, and ${d.owner} owns ${d.gender}.",
map));
}
}
Note: This doesn't compile due to unhandled exceptions. But it makes the code much easier to read.
Also, I don't like that you have to construct the map yourself in the code, but I don't know how to get the names of the local variables programatically. The best way to do it, is to remember to put the object in the map as soon as you create it.
The following example produces the results that you want from your example:
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
Site site = new Site();
map.put("site", site);
site.name = "StackOverflow.com";
User user = new User();
map.put("user", user);
user.name = "jjnguy";
System.out.println(
format("Hello ${user.name},\n\tWelcome to ${site.name}. ", map));
}
I should also mention that I have no idea what Velocity is, so I hope this answer is relevant.
Here's an outline of how you could go about doing this. It should be relatively straightforward to implement it as actual code.
Create a map of all the objects that will be referenced in the template.
Use a regular expression to find variable references in the template and replace them with their values (see step 3). The Matcher class will come in handy for find-and-replace.
Split the variable name at the dot. user.name would become user and name. Look up user in your map to get the object and use reflection to obtain the value of name from the object. Assuming your objects have standard getters, you will look for a method getName and invoke it.
There are a couple of Expression Language implementations out there that does this for you, could be preferable to using your own implementation as or if your requirments grow, see for example JUEL and MVEL
I like and have successfully used MVEL in at least one project.
Also see the Stackflow post JSTL/JSP EL (Expression Language) in a non JSP (standalone) context
Handlebars.java might be a better option in terms of a Velocity-like syntax with other server-side templating features.
http://jknack.github.io/handlebars.java/
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hello {{this}}!");
System.out.println(template.apply("Handlebars.java"));
I use GroovyShell in java to parse template with Groovy GString:
Binding binding = new Binding();
GroovyShell gs = new GroovyShell(binding);
// this JSONObject can also be replaced by any Java Object
JSONObject obj = new JSONObject();
obj.put("key", "value");
binding.setProperty("obj", obj)
String str = "${obj.key}";
String exp = String.format("\"%s\".toString()", str);
String res = (String) gs.evaluate(exp);
// value
System.out.println(str);
I created this utility that uses vanilla Java. It combines two formats... {} and %s style from String.format.... into one method call. Please note it only replaces empty {} brackets, not {someWord}.
public class LogUtils {
public static String populate(String log, Object... objects) {
log = log.replaceAll("\\{\\}", "%s");
return String.format(log, objects);
}
public static void main(String[] args) {
System.out.println(populate("x = %s, y ={}", 5, 4));;
}
}
Since Java 15 you have the method String.formatted() (see documentation).
str.formatted(args) is the equivalent of String.format(str, args) with less ceremony.
For the example mentioned in the question, the method could be used as follows:
"Hello %s, Welcome to %s.".formatted(user.getName(), site.getName())
Good news. Java is most likely going to have string templates (probably from version 21).
See the string templates proposal (JEP 430) here.
It will be something along the lines of this:
String name = "John";
String info = STR."I am \{name}";
System.out.println(info); // I am John
P.S. Kotlin is 100% interoperable with Java. It supports cleaner string templates out of the box:
val name = "John"
val info = "I am $name"
println(info) // I am John
Combined with extension functions, you can achieve the same thing the Java template processors (e.g. STR) will do.
There is nothing out of the box that is comparable to velocity since velocity was written to solve exactly that problem. The closest thing you can try is looking into the Formatter
http://cupi2.uniandes.edu.co/site/images/recursos/javadoc/j2se/1.5.0/docs/api/java/util/Formatter.html
However the formatter as far as I know was created to provide C like formatting options in Java so it may not scratch exactly your itch but you are welcome to try :).

Java Object Instance creation problems

So I've been struggling all day today trying to create an instance of a class called 'Sport'.
I've got my code set up so I run the User Interface, which then runs a constructor, which then runs another constructor which loads the Sport values from a text file.
The problem is, the way I'm apparently creating the objects is wrong. Could really use some help.
public static void seperateValues(String sportDetail)
{
String[] sportDetails = sportDetail.split(",");
System.out.println("Adding new sport to the Sport collection");
System.out.println(sportDetail);
/*
for(int i=0; i<sportDetails.length; i++) //just used for testing whether it was splitting correctly
{
System.out.println(sportDetails[i]);
} */
// name,usagefee,insurance,affiliationfees, then court numbers
//Tennis,44,10,93,10,11,12,13,14,15,16
int vlength;
vlength = sportDetail.length();
String[] sportDetailz;
sportDetailz = new String[vlength];
sportDetailz[0] = sportDetails[0]; //name
sportDetailz[1] = sportDetails[1]; //usage fees
sportDetailz[2] = sportDetails[2]; //insurance
sportDetailz[3] = sportDetails[3]; //afflcationfees
String vSportObjectName;
vSportObjectName = sportDetails[0];
String sportinstance;
sportinstance = sportDetails[0]; //this is the name of the sport which I'm hoping each loop around
//it will give a new name to
Sport sportinstance = new Sport(sportDetails);
//System.out.println(Sport.this.name);
}
Error message: variable sportinstance is already defined in method seperateValues(java.lang.String)
http://puu.sh/2zil9
I'm guessing your issue is that you first declare sportinstance as a String. You then try to define it again as a Sport.
Just remove the following lines and try again (as it doesn't look like they actually are used anywhere else):
String sportinstance;
sportinstance = sportDetails[0];
The other option would be to simply rename either one of your instances of sportinstance.
You are trying to define sportinstance as two different datatypes and Java will not allow this. Either change the name of the Sport definition of sportinstance to another variable name or remove the definition.

Parse a query string parameter to java object

I have query string like that:
ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056
And I have Java Object:
LogObject{
private String ObjectGUId;
private String ObjectType;
private String ObjectTitle;
private String Content;
private String TimeStamp;
}
So i want to parse this query string to this java Object.
I've searched and read many question but not gotten correct answer yet.
Show me what can solve this problem.
Inspired by #bruno.braga, here's a way using Apache http-components. You leverage all the parsing corner cases:
List<NameValuePair> params =
URLEncodedUtils.parse("http://example.com/?" + queryString, Charset.forName("UTF-8"));
That'll give you a List of NameValuePair objects that should be easy to work with.
If you do not really need to push the querystring into your own class (you might want that though), instead of parsing it manually, you could use the URLDecoder, as #Sonrobby has commented:
String qString = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife";
Uri uri = Uri.parse(URLDecoder.decode("http://dummy/?" + qString, "UTF-8"));
if (uri != null) {
for(String key: uri.getQueryParameterNames()) {
System.out.println("key=[" + key + "], value=[" + uri.getQueryParameter(key) + "]");
}
}
The "dummy" looks dirty but it is required if what you only have is the querystring values (qString). If you have the complete URL, just pass it directly to the URLDecoder, and you are done.
Etiquette
You really should be much more specific about what you have tried and why it didn't work.
A proper code sample of your LogObject would really be very helpful here.
Ideally, you would provide a SSCCE so others could easily test your problem themselves.
Answer
You can extract the name:value pairs like this:
String toParse = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056";
String[] fields = toParse.split("&");
String[] kv;
HashMap<String, String> things = new HashMap<String, String>();
for (int i = 0; i < fields.length; ++i)
{
t = fields[i].split("=");
if (2 == kv.length)
{
things.put(kv[0], kv[1]);
}
}
I have chosen to put them into a HashMap, but you could just as easily look at the name part (kv[0]) and choose to do something with it. For example:
if kv[0].equals("ObjectGUId")
{
logObject.setGUId(kv[1]); // example mutator/setter method
}
else if //...
However, all your fields in LogObject are private and you haven't shown us any methods, so I hope you have some way of setting them from outside... bear in mind you will need to store the pairs in a data structure of some kind (as I have done with a HashMap) if you intend to intialise a LogObject with all the fields rather than setting the fields after a constructor call.
Speaking of SSCCEs, I made one for this answer.

Get settings with the help of enum VB.NET

I'm translating a Java program to vb.net where settings in the application is controlled by a enum.
private enum SmsTagRule {
// KEYWORD DOMAIN BusinessClass PREFIX SEARCHNAME SEARCHPARAM SENDEMAIL KEYWORDS...
BAG_TAG("BagTag", "BagTag", "FoundBagTag", "b", "SearchBagTag", "490_TagNumber", true, "BagTag"),
SKI_TAG("SkiTag", "SkiTag", "FoundSkiTag", "a", "SearchSki", "518_LabelNo", false, "SkiTag", "ski"),
PC_TAG("PcTag", "ds", "FoundPC", "", "SearchPcTag", "585_LabelNo", false, "pc");
And depending on witch "TAG" in chosen different settings are getting returned. Is there any way to do this in vb.NET. I have thought about creating one enum for each one of these TAG's but it seems like it should be a better solution to this.
Any Ideas?
Go on and create a custom Type representing your settings:
Class TagRule
Public Shared BAG = new TagRule("BagTag", "BagTag", "FoundBagTag", ...)
Public Shared Ski = new TagRule("SkiTag", "SkiTag", "FoundSkiTag", ...)
...
Dim _keyword As String
Public Property Keyword as String
Public Get
return _keyword
End
Private Set
_keyword = value
End
End Property
...
Private Sub New(keyword as String, domain as String, businessclass as String, ...)
_keyword = keyword
_domain = domain
....
End Sub
End Class
And you can use it like this:
Dim setting As TagRule = Nothing
If somecondition Then
setting = TagRule.Ski
Else
setting = TagRule.BAG
End If
Dim keyword = setting.Keyword
Dim domain = setting.Domain

Categories