LDAP search on multiple fields like an if/else-statement - filter

I have a question regarding LDAP search, i have three attributes that i want to involve in my filter.
I want that the filter always shall search for objectClass, if attribute skaPersonType has a value, look for that, else look for employeeType.
I'm stuck and really don't now how to continue.
Best regards / C

Always search for objectclass
Unnecessary, but (objectClass=*): all LDAP entries have an objectClass.
IF skaPerson=EMP is met, look for that value
(skaPerson=EMP)
ELSE look for employeetype=External
(employeetype=External)
Any ideas how i can manage that?
You're looking for (2) or (3). So:
(|(skaPerson=EMP)(employeetype=External))
If you must have the redundant objectClass test:
(&(objectClass=*)(|(skaPerson=EMP)(employeetype=External)))

Not sure what filter you actually want:
...always shall search for objectClass, if attribute skaPersonType has a
value, look for that, else look for employeeType...
Are you looking for something like this?
(&(objectClass=MyClass)(|(skaPersonType=A)(&(!(skaPersonType=*))(employeeType=B))))
Above filter will get object which:
objectClass equals MyClass, AND
one of following condition is met
skaPersonType equals A, OR
skaPersonType has no value, and employeeType equals B
The code is not tested.

Related

Spring Data JPA fetching list always returns at least a single result

I've noticed a slight problem with how my API is working where I'm using Spring Data JPA.
My query looks something along the lines of:
#Query("SELECT p.id AS id, COUNT(l) AS likes FROM Post p LEFT JOIN Like l ON l.post = p WHERE p.location.id = ?1")
My actual query is bigger, this this contains everything necessary to explain what the issue is. This query will return a list, but assume the location does not exist, it should return null or an empty list, correct? Oh, how wrong you are, my sweet summer child!
This query will instead always return a list of at least one element, regardless of whether or not there are any posts linked to said location.
[{"id": null, "likes": 0}]
That is what the result looks like when serialized to JSON. I am not quite sure what to do about this little predicament, as I obviously don't want to return a list with faulty data, but needing to use processing to filter out duds also seems dumb and unnecessary.
Is there any way to prevent this that I've yet to find? If it is of any relevance, I am using projections currently for my responses.
What I've tried so far:
Adding a not null condition for fields. Does not work, ignored by COUNT.
Adding constraints to all fields #NotNull. Does not work, will still become null.
For what it's worth, I've tried different kinds of joins, though anything but LEFT JOIN doesn't make much sense.
I haven't been able to find any other case which resembles this either, although it most likely exists, but is drowned out by everything else. I'm not quite sure what can be done in this regard, so I'm curious if it's just a quirk with the framework, or if there is an actual solution.
It might be possible to solve through native queries, but I would prefer not to use them.
I'm no SQL expert but I believe that a left join will give you this result if the ID does not exist.
Have you run the query in your DB? Doesn't it give you one row in your result set for IDs that do not exist?
I believe this is intended to say there is a 0 match.
You might want to validate your query before running it. Meaning checking that the location exists first.
As the issue is inherently due to a COUNT and CASE keyword in my real query, resulting in there always being at least one row, and I can't find any method of doing this automatically, the solution I've used is the following:
List<Item> items = repository.customQuery(id);
if (0 < items.size() && null == items.get(0).getId()) {
items.remove(0);
}
The first condition is arbitrary as I know there is always at least one entry, but is done just as a safety measure. A try-catch block would do the trick as well. In the case where you use a primitive int instead of Integer, you'd need to initialize the value in the constructor to something which would normally never be present in the database, such as -1.
If anyone knows of a better method, I'd love to know about it.

xpath conditional match possible?

Ok basically I'm trying to use XPath to find a match "given a condition." basically, with the following xml:
<stuff1>
<stuff2>abc</stuff2>
</stuff1>
<stuff3>
<stuff4>abc</stuff4>
<stuff5>true</stuff5>
</stuff3>
<stuff3>
<stuff4>abc</stuff4>
<stuff5>false</stuff5>
<stuff6>extra stuff</stuff6>
</stuff3>
what I'd like to do is select stuff1's that match stuff3's based on stuff2==stuff4, but also where the stuff3's have a "stuff5" value of false, but not true.
I know //stuff1[stuff2/text()=../stuff3/stuff4/text()] will select me the stuff1's that match the stuff3's, but how do I specify that the stuff3's must have a stuff5 value of false? Sorry if this elementary or answered elsewhere, random searching didn't seem to reveal the answer easily.
Thank you.
You're almost there, if you want to only match stuff3 elements with a stuff5 value of false, you can do that by adding the predicate [stuff5/text()="false"]to the stuff3 match.
The complete XPath should look something like the following:
//stuff1[stuff2/text()=../stuff3[stuff5/text()="false"]/stuff4/text()]

Prefix the result of a XPATH query

I use libxmljs to parse some html.
I have a xpath query which has an "or" conjunction to retrieve basically the information of two queries
Example
doc.find("//div[contains(#class,'important') or contains(#class,'overdue')]")
this returns all the divs with either important or overdue...
Can I prefix or see within my result set which comes from which condition?
The result could be an array with an index for the match 0 for the first condition and 1 for the 2... Is this possible...
Or how can I find out which result comes from which query condition...
Thanks for any help...
P.S.: this is a simplified exampled of a sequence of elements which either have an important or an overdue item ... both, one or none of them... So I cannot go by looking for every second entry ... etc
This is the result I want to get...
message:{},
message:{
.....
important: "some immportant text",
overdue: "overdue date,
.....
}
There is no way to know which clause of an or XPath query caused a particular result to be included. It's simply not information that's kept around.
You'll either need to do entirely separate queries for important and overdue, or do one large query to get the entire result set (as you are now) and then further test each result's class to find out which one it is.

LINQ - OR two SqlMethods.Like clauses

I need to OR two SqlMethods.Like statements in LINQ, and I'm not sure how to accomplish it (or if it's the right way to go about it).
I've got vendor ID and vendor name fields, but I've only got a generic vendor search that allows a user to search for a vendor based on their name or ID. I also allow wildcards in the search, so I need to find vendors whose ID or name is like the user's input.
I want to do something like below, but obviously it's not correct. (EDIT: It does work as written.)
results = results.Where(p => SqlMethods.Like(p.VendorId, inputVendor.Replace("*", "%") ||
SqlMethods.Like(p.VendorName, inputVendor.Replace("*", "%"));
Background: I add where statements depending on the search parameters entered by the user, hence the results = results.Where part.
Any help would be appreciated!
It's not clear to me why this is "obviously" not correct. Presumably it's not working, otherwise you wouldn't have posted, but it's not obvious how it's not working.
I would suggest performing the replacement before the query, like this:
string vendorPattern = inputVendor.Replace("*", "%");
But then I'd expect this to work:
results = results.Where(p => SqlMethods.Like(p.VendorId, vendorPattern) ||
SqlMethods.Like(p.VendorName, vendorPattern));
Of course you're limited to where wildcards can appear in a SQL LIKE query, but that's a separate problem. (I'm not sure of the behaviour offhand if it's not at the start or end.)
If that doesn't help, please update the question with what happens when you try this.

solr query for field value starting with a number

I have to modify a query that searches for a value starting with a letter (relevant snippet fo the query): &fq=Organization:"+letter+"*&
If I pass 'A' as the letter param I'll get 'ABC Hardware', something that start with an A.
How would i modify the letter variable to return only something that starts with a number, as '1A Widgets'.
Tried things like letter = '[0 TO 5]', but I honestly have no idea if I'm on the right track with that.
Seems like a dupe of this question
For cases like this, my favourite approach is to index another boolean field called "StartsWithNumber" and then it's a simple boolean filter. This might not work for you if you can't reindex all of your documents.
For a brute force approach, you could do something like:
fq=Organization:0* OR Organization:1* OR Organization:2* OR .. etc
Not pretty, but fq's get cached so at least it should be fast.

Resources