Grails findBy is not working with combination of Or/And Condition - grails

I am working with Grails application. I am using Apache Shiro security plugin for my grails application. I am trying to use findBy query on my User domain class with Or and And condition but it gives me an error.
My Domain Class
class ShiroUser {
String firstName
String lastName
String username
String email
String passwordHash
String userStatus
static hasMany = [ roles: ShiroRole, permissions: String ]
static constraints = {
username(nullable: false, blank: false, unique: true)
email(nullable: false, blank: false, email: true,unique: true)
}}
I have executed following query:
ShiroUser.findByUsernameOrEmailAndUserStatus(params?.username,params?.username,'Active')
I get following error:
Message: No property found for name [usernameOrEmail] for class [class com.chatportal.ShiroUser]
But If I execute query with only Or condition then it works fine.
ShiroUser.findByUsernameOrEmail(params?.username,params?.username)
Anyone please help me that what is wrong with my condition when I used Or and And condition with findBy ?

"You can combine as many criteria as you like, but they must all be combined with And or all Or. If you need to combine And and Or or if the number of criteria creates a very long method name, just convert the query to a Criteria or HQL query."

Related

how to change Grails GORM id column name and use sequence

In a Grails/GORM app, can I rename the implicit domain id column to entity_id and auto-increment using an existing sequence?
class Practitioner {
Long entityId
String providerName
static mapping = {
id name: 'entityId', column: 'entity_id', generator:'sequence', params:[sequence:'hibernate_sequence']
}
}
Attempting to save in console fails, (and yes, sequence exists in my local Postgres database)
Practitioner p = new Practitioner(providerName: 'p')
p.save(flush:true, failOnError: true)
Error
grails.validation.ValidationException: Validation Error(s) occurred during save():
- Field error in object
'com.transcendinsights.dp.measureresult.snapshot.Practitioner' on field 'entityId': rejected value [null]; codes
Super thanks for helping!

Grails Elasticsearch plugin issues

I am new to Elasticsearch and am using the revived grails plugin "elasticsearch:0.0.4.6".
I wanted to implement a search for a User by their firstName, surname or username, which returns the full domain instance.
I have a 2 domain classes:
User:
class User {
String firstName
String surname
static hasOne = [profile:Profile]
static hasMany = [friends:User]
static mappedBy = [ friends: 'friends' ]
static searchable = {
profile component:true
only = ['firstName', 'surname', 'profile']
}
...
Profile:
class Profile {
String username
String status
byte[] photo
static belongsTo = [user:User]
static searchable = true
...
}
I made the classes "searchable = true" and then created the following search:
def res = User.search("${params.friendSearch}").searchResults
This found the correct instances, but now when a user adds a photo to their profile, it suceeds but I recieve a never ending loop of the following error:
ERROR index.IndexRequestQueue - Failed bulk item: MapperParsingException[failed to parse [photo]]; nested: NumberFor
matException[For input string: the photos base64inputstring
I dont really get what is happening, but i figure it must be something to do with elasticsearch being unable to index the photo data. Can somebody provide an explanation?
I then experimented with searchables custom mapping options -
profile index:'no'
all=false
excludeFromAll = true
etc
Nothing worked. Eventually I added
only = ['username']
and it stopped the error from occuring and allowed me to find users based on the criteria i mentioned above. However, because of the "only" limit, it means that the User instances returned by the seach have a photo property equal to null, but i need this value!
What am i doing wrong? Can anyone advise me on the best course of action to take or any misunderstandings i have about Elasticsearch? Thanks
I think you might have to exclude the byte property photo from the searchable fields like so:
class Profile {
String username
String status
byte[] photo
static belongsTo = [user:User]
static searchable = {
except = ['photo']
}
This will exclude the property photo from being indexed and search. Hence the output of converting the byte format to string format will not fail.
Also maybe you might need a custom convertor to change the byte(string) to something more usable in the results?

Why grails domain validation reject a url like http://wctest.jenkins:8080/CRMGateway

Why grails domain validation reject a url like http://wctest.jenkins:8080/CRMGateway. Though the url supplied is correct in structure.Do this want it be a url with no port?
Below is the domain that I have
class Configuration{
String username
String password
String gatewayURL
//constraints
static constraints = {
gatewayUrl nullable: true, blank: false, url: true
}
}
In bootstrap file, code is like
Configuration config = new configuration(username:'abc',password:new
SHA256("w3lc0m3"),"http://wctest.jenkins:8080/CRMGateway")
config.save flush:true
It is because the default url validation rejects wctest.jenkins.
But it accepts wctest.com or wctest.ch.
If it is too restrictive, you will need to customize the validation (as in http://java.dzone.com/articles/grails-goodness-add-extra or using Grails custom validation).

Grails find by property

Okay, I am trying to find all of my Employee that have a given role... However no matter what I try I end up getting the same exception thrown...
enum Role {
SFC("State Fitness Coordinator"),
TRAINING("Training"),
DFC("District Fitness Coordinator"),
final String longName
private Role(String longName) {
this.longName = longName
}
}
class Employee {
static hasMany = [roles: Role]
static constraints = {
}
}
The first Thing I tried was Employee.findAllByRoles(Role.DFC)
Then I tried:
Employee.findAll("FROM Employee e WHERE e.roles IN (:role)", [role: [Role.DFC]])
as well as
Employee.withCriteria {
'in'('roles', [Role.DFC])
}
all resulting in
Class
java.sql.SQLException
Message
Missing IN or OUT parameter at index:: 1
Any direction would be much appreciated.
with grails 2.3.8 and H2
Employee.findAll("FROM Employee e WHERE :role in elements(e.roles) ", [role: Role.DFC.toString()])
this works… even if I think that Role could be a real Domain simplifying all operations
I think the problem is that Role is an enum. Are you sure you want to make it an enum?
I recommend making it a class since new roles might be added. If so, you can easily add a record using bootstrap or SQL query without making a new build to production.
class Role {
String roleName
String description
}
In Bootstrap:
Role sfc = new Role("SFC","State Fitness Coordinator")
Role sfc = new Role("TRAINING," "Training")
Role sfc = new Role("DFC", "District Fitness Coordinator")
N.B. Please make sure you are not using spring security plugin which has its own 'Role' class. If so, please change the name of your 'Role' class to something like 'EmployeeRole'

Adding RequestMap

how can I upgrade an existing grails application with a request map?
I didn't use
grails s2-quickstart package user role requestmap
but only
grails s2-quickstart package user role
AFAIK I have to 1) set a line in Config.groovy:
grails.plugins.springsecurity.securityConfigType = "Requestmap"
and then 2) set all the entries via BootStrap.groovy (according to spring's docs ), I get that, but how does the RequestMap Domain Class have to look like? Anything else to do?
Thanks
It's a simple class; this is what would be generated if you specify package your.package.name and class name Requestmap:
package your.package.name
class Requestmap {
String url
String configAttribute
static mapping = {
cache true
}
static constraints = {
url blank: false, unique: true
configAttribute blank: false
}
}
Once you create this, add this line in Config.groovy:
grails.plugin.springsecurity.requestMap.className = 'your.package.name.Requestmap'

Resources