grails: assigning a domain class to another domain class - groovy

I have a scenario where users are assigned to team.
Different ClientServices are allocated to different teams and
we need to assign user Of these teams to clientservice in RoundRobin fashion
I was trying to solve it as follows to get a map where team name and a list of ClientServiceInstance will be mapped so I can do further processing on it
def teamMap = [:]
clientServicesList.each {clientServiceInstance->
if(teamMap[clientServiceInstance.ownerTeam] == null){
teamMap.putAt(clientServiceInstance.ownerTeam, new ArrayList().push(clientServiceInstance))
}else{
def tmpList = teamMap[clientServiceInstance.ownerTeam]
tmpList.push(clientServiceInstance)
teamMap[clientServiceInstance.ownerTeam] = tmpList
}
}
but instead of pushing clientServiceInstance it pushes true.
Any idea?

I believe another version would be:
def teamMap = clientServicesList.inject( [:].withDefault { [] } ) { map, instance ->
map[ instance.ownerTeam ] << instance
map
}

new ArrayList().push(clientServiceInstance) returns true, which means you're putting that into your teamMap instead of what I assume should be a list? Instead you might want
teamMap.putAt(clientServiceInstance.ownerTeam, [clientServiceInstance])
By the way, your code is not very Groovy ;)
You could rewrite it as something like
def teamMap = [:]
clientServicesList.each { clientServiceInstance ->
if (teamMap[clientServiceInstance.ownerTeam]) {
teamMap[clientServiceInstance.ownerTeam] << clientServiceInstance
} else {
teamMap[clientServiceInstance.ownerTeam] = [clientServiceInstance]
}
}
Although I'm sure there are even better ways to write that.

Related

How to apply filter on finch endpoint without using finagle filters?

I have more than one endpoints.I am able to apply common filters on endpoints using finagle filter.But now I want to apply a filter on a specific endpoint.
How can I achieve this?
I had a similar question (for basic authentication filtering) that popped up while playing with redbubble's finch template which I partially solved in the following way:
class AuthenticatedEndpoint[A](e: Endpoint[A]) extends Endpoint[A] { self =>
final def apply(mapper: Mapper[A]): Endpoint[mapper.Out] = mapper(self)
final def apply(input: Input): Endpoint.Result[A] =
if (checkSession(input.request)) {
e(input)
} else {
// TODO return something meaningful to the caller (if possible?)
EndpointResult.Skipped
}
}
object AuthenticatedEndpoint {
def validSession[A](e: Endpoint[A]): Endpoint[A] = new AuthenticatedEndpoint(e)
}
(with checkSession returning true if all is well with the request). Then my api is defined as:
val api = "v1" :: loginApi :+: validSession(peopleApi :+: healthApi :+: adminApi)
This works well in the sense that requests without a session won't have access to the endpoints passed to validSession, but I have yet to find an easy way to return an error message to the caller, and I'd be curious to know if I chose the right path here.
This is how I got around it. It's probably not ideal but works.
class AuthenticatedEndpoint[A](e: Endpoint[A])(implicit auth: Request => Boolean) extends Endpoint[A] { self =>
final def apply(mapper: Mapper[A]): Endpoint[mapper.Out] = mapper(self)
final def apply(input: Input): Endpoint.Result[A] =
if (auth(input.request)) {
e(input)
} else {
EndpointResult.Matched[Nothing](input, Rerunnable( Unauthorized(new Exception(s"Authentication Failed."))) )
}
}
object AuthenticatedEndpoint {
def validSession[A](e: Endpoint[A]): Endpoint[A] = new AuthenticatedEndpoint(e)
}

How can I retrieve the build parameters from a queued job?

I would like to write a system groovy script which inspects the queued jobs in Jenkins, and extracts the build parameters (and build cause as a bonus) supplied as the job was scheduled. Ideas?
Specifically:
def q = Jenkins.instance.queue
q.items.each { println it.task.name }
retrieves the queued items. I can't for the life of me figure out where the build parameters live.
The closest I am getting is this:
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
it.task.properties.each { key, val ->
println(" ${key}=${val}")
}
}
This gets me this:
4.1.next-build-launcher:
com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty$ScannerJobPropertyDescriptor#b299407=com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty#5e04bfd7
com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty$DescriptorImpl#40d04eaa=com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty#16b308db
hudson.model.ParametersDefinitionProperty$DescriptorImpl#b744c43=hudson.mod el.ParametersDefinitionProperty#440a6d81
...
The params property of the queue element itself contains a string with the parameters in a property file format -- key=value with multiple parameters separated by newlines.
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
println("Parameters: ${it.params}")
}
yields:
dbacher params:
Parameters:
MyParameter=Hello world
BoolParameter=true
I'm no Groovy expert, but when exploring the Jenkins scripting interface, I've found the following functions to be very helpful:
def showProps(inst, prefix="Properties:") {
println prefix
for (prop in inst.properties) {
def pc = ""
if (prop.value != null) {
pc = prop.value.class
}
println(" $prop.key : $prop.value ($pc)")
}
}
def showMethods(inst, prefix="Methods:") {
println prefix
inst.metaClass.methods.name.unique().each {
println " $it"
}
}
The showProps function reveals that the queue element has another property named causes that you'll need to do some more decoding on:
causes : [hudson.model.Cause$UserIdCause#56af8f1c] (class java.util.Collections$UnmodifiableRandomAccessList)

How to find String elemets of a format in a Groovy list

I have a groovy list as below
def permissions = [
TESTENTITY.ADMINISTER,
TESTENTITY.CREATE,
TESTENTITY.DELETE,
TESTENTITY.READ,
TESTENTITY.UPDATE,
TESTBEAN.ADMINISTER,
TESTBEAN.CREATE,
TESTBEAN.DELETE,
TESTBEAN.READ,
TESTBEAN.UPDATE
]
Am trying to find all the elements in the list of format "TESTENTITY" with less code. Is it doable?
This is the way I interpret the question.
Not considering each element as a String but a collection of Enum.
enum TestEntity {
ADMINISTER, CREATE, DELETE, READ, UPDATE
}
enum TestBean {
ADMINISTER, CREATE, DELETE, READ, UPDATE, DONOTHING
}
def permissions = [
TestEntity.ADMINISTER, TestEntity.CREATE,
TestEntity.DELETE, TestEntity.READ, TestEntity.UPDATE,
TestBean.ADMINISTER, TestBean.CREATE,
TestBean.DELETE, TestBean.READ, TestBean.UPDATE, TestBean.DONOTHING
]
permissions.findAll { it in TestEntity }
Note:
DONOTHING is added to TestBean just to show the difference.
You can do something like this...
def matchingResults = permissions.findAll { perm ->
// perm will be one element from permissions.
// inspect it and return true if it should be
// include in the results.
}
If those are strings in the permissions list, that might look something like this...
def matchingResults = permissions.findAll { perm ->
perm.startsWith 'TESTENTITY'
}
Is that what you are looking for?

groovy null safe operator, identifying what was null?

The null safe operator in Groovy is great for reducing code and making things more readable. We can go from this:
def customer = getCustomer(custNo)
if(!customer)
throw new Exception("Invalid customer: ${custNo}")
def policy = customer.getPolicy(policyNo)
if(!policy)
throw new Exception("Invalid policy: ${policyNo}")
def claim = policy.getClaim(claimNo)
if(!claim)
throw new Exception("Invalid claim: ${claimNo}")
..to this...
def claim = getCustomer(custNo)?.getPolicy(policyNo)?.getClaim(claimNo)
But nothing's for free; using null/safe navigation, if claim is null, it's not immediately obvious what caused it: either custNo, policyNo, or claimNo may be invalid.
We could go back and start checking what's null, but that's counterproductive, and actually, it's not even possible since intermediate objects are not stored in variables.
So the question is: Is it possible to identify what was null when chaining method calls using null/safe navigation?
UPDATE
I took another stab at this using dynamic method invocation. It takes an init target (usually a dao) to initialize the object (customer in this case), and a map containing method names as strings (with arguments as values). Using an iterator, invokeChain simply traverses the map (chain); if anything in the chain returns null, identifying the method that caused it becomes trivial.
def invokeChain = { initTarget, chain ->
def obj
chain.eachWithIndex{ it, idx ->
//init obj from dao on first iteration only,
//remaining iterations get from obj itself
obj = (!idx) ? initTarget."$it.key"(it.value) : obj?."$it.key"(it.value)
if(!obj)
throw new Exception("${it.key}(${it.value}) returned null")
}
obj
}
Usage
Mock a customer dao for initTarget...I've inserted null as return type for getClaim(), which should throw an exception.
def static getCustomer = { custNo ->
[ getPolicy: { p ->
[getClaim:{ c ->
null //"Claim #${c}"
}]
}]
}
..using invokeChain, easy as pie:
def claim = invokeChain(this, [getCustomer:123, getPolicy:456, getClaim:789])
...throws exception, as expected:
Exception in thread "main" java.lang.Exception: getClaim(789) returned null
I like this approach because it's compact, readable, and easy to use; what do you think?
I think there is no clear way to do so.
I can be wrong, will check sources later, but safe navigation is a syntax sugar for if statement.
As a hack you can wrap your code with interceptor, trace last method call inside, and then use that info to proide error message.
It will not be cheap, and will cost you some code to realize interception and some performance while running. But you can achieve something like
mayFail("getCusomer", "getPolicy", "getClaim") {
getCustomer(custNo)?.getPolicy(policyNo)?.getClaim(claimNo)
} == "getPolicy" // failed on second step
EDIT: As #tim_yates proved, ?. is a syntax sugar with if construction behind. Thanks Vorg van Geir for the link, I have copied it here, to an answer. He say, it's outdated, and it looks like he is right. I have managed to make ProxyMetaClass work(in Groovy 2.0.6), so given way isn't totally broken. Now I need to specify exact classes to intercept, and I can not find a way to catch inherited method calls.(to simply intercept java.lang.Object)
def logInterceptor = new TracingInterceptor()
logInterceptor.writer = new StringWriter()
def intProxy = ProxyMetaClass.getInstance(Integer)
def stringProxy = ProxyMetaClass.getInstance(String)
intProxy.setInterceptor(logInterceptor)
stringProxy.setInterceptor(logInterceptor)
intProxy.use {
stringProxy.use {
println(("Hello" + "world").size().hashCode())
} }
println(logInterceptor.writer.toString())
All that hell may be wrapped in some utility code, but I highly doubt in necessarity of this. Performance overhead will be awful and some boilerplate code will remain.
The game is not worth the candle.
What about attributions and asserts?
def policy = customer?.policy
def claim = policy?.claim
def number = claim?.number
assert customer, "Invalid customer"
assert policy, 'Invalid policy'
assert claim, 'Invalid claim'
UPDATE:
You already found the solution, but i'd like to contribute with an interceptor idea:
The mock:
def dao = [
getCustomer : { custNo ->
[ getPolicy: { p ->
[getClaim:{ c ->
null //"Claim #${c}"
}]
}]
}
]
The interceptor:
class MethodCallInterceptor {
def delegate
def invokeMethod(String method, args) {
def result = delegate.invokeMethod(method, args)
if (result == null) {
throw new RuntimeException("$method returned null")
}
else {
new MethodCallInterceptor(delegate: result)
}
}
def getProperty(String property ) {
delegate[ property ]
}
void setProperty(String property, value) {
delegate[ property ] = value
}
}
The test:
def interceptedDao = new MethodCallInterceptor(delegate: dao)
try {
interceptedDao.getCustomer(123).getPolicy(456).getClaim(789)
assert false
} catch (e) {
assert e.message.contains( 'getClaim returned null' )
}

Can I use nested scoping when specifying environments in ConfigSlurper?

Using Groovy 2.0.7, when I have a config.groovy such as:-
def configText = """
switch(environment) {
case 'localhost':
PROXY {
HOST = "localproxy"
}
break
}
PROXY {
HOST = "defaultproxy"
}"""
def config = new ConfigSlurper("localhost").parse(configText)
, I get an assertion failure when I do this:-
assert "localproxy" == config.PROXY.HOST
If I remove the "defaultproxy" line then the environment value is correctly returned.
Am I doing something wrong? This to me is a standard requirement, to have a default value specified for config.PROXY.HOST but be able to override it in the environments switch block.
I know I can use the environments constructor to override the values but that is no use to me as it doesn't allow me to evaluate values, e.g. if I had:-
PROXY {
HOST = "defaultproxy"
URL = "http://" + HOST
}
then the URL would always be http://defaultproxy even if I specified the "localhost" environment.
I need the features from both really! Anyone know how I can achieve this?
You could also put the switch statement after the default proxy.host property...
def configText = """
PROXY {
HOST = "defaultproxy"
}
switch(environment) {
case 'localhost':
PROXY {
HOST = "localproxy"
}
break
}
"""
def config = new ConfigSlurper("localhost").parse(configText)
assert "localproxy" == config.PROXY.HOST
or you could leverage the environment property like this:
def configText = """
PROXY {
HOST = "defaultproxy"
}
environments{
localhost{
PROXY.HOST='localproxy'
}
}
"""
def config = new ConfigSlurper("localhost").parse(configText)
assert "localproxy" == config.PROXY.HOST
OK, I have a solution which works.
I wrapped the switch statement into a function, then called the function at the top of the script (below the function, above the nested scoped properties) and then called the function again at the bottom.
Crude, horrible, but works. If anyone has a better solution, please let me know!

Resources