Java 8 Date Time: Convert from ISO 8601 String with TZ info, in this eg. UTC (javascript json friendly) to zone aware java date time object and render as local system date time

Whole groovy sample (paste into the groovyconsole)

import java.time.ZonedDateTime
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

final String UTC_DATE_TIME="2017-08-30T13:12:46.824Z"

ZonedDateTime zdt = ZonedDateTime.parse(UTC_DATE_TIME,DateTimeFormatter.ISO_DATE_TIME)
println zdt.getZone() // 'Z' => UTC

println ZoneId.systemDefault() // "Europe/London"

assert ZoneId.systemDefault().equals(ZonedDateTime.now().getZone()) // Defaults to JVM TZ 
//on windows, use tzutil.exe to get/set the system time zone
//override for JVM with -Duser.timezone

ZonedDateTime londonZdt = zdt.withZoneSameInstant(ZoneId.of("Europe/London"))
ZonedDateTime rioZdt = zdt.withZoneSameInstant(ZoneId.of("America/Argentina/Buenos_Aires"))

assert(!londonZdt.equals(rioZdt))
assert(!londonZdt.isBefore(rioZdt))
assert(!londonZdt.isAfter(rioZdt))
assert(londonZdt.isEqual(rioZdt))

println zdt //2017-08-30T13:12:46.824Z
println londonZdt //2017-08-30T14:12:46.824+01:00[Europe/London]
println rioZdt //2017-08-30T10:12:46.824-03:00[America/Argentina/Buenos_Aires]
println "-----"

println zdt.toLocalDateTime() //2017-08-30T13:12:46.824
println londonZdt.toLocalDateTime() //2017-08-30T14:12:46.824
println rioZdt.toLocalDateTime() //2017-08-30T10:12:46.824

 

output on my machine :

Z
Europe/London
2017-08-30T13:12:46.824Z
2017-08-30T14:12:46.824+01:00[Europe/London]
2017-08-30T10:12:46.824-03:00[America/Argentina/Buenos_Aires]
-----
2017-08-30T13:12:46.824
2017-08-30T14:12:46.824
2017-08-30T10:12:46.824

Why is AngularJS 1.x tutorial not working for me in a Grails 2.x app?

This wasted 40 mins of my time before a ‘duh’ moment

I had set up a minimal app using ‘grails create-app’ which gave me a single GSP and a main.gsp sitemesh layout

My single gsp looked like this:

<html>
 <head>
  <meta name="layout" content="main">
  <r:require modules="application, angularjs"/>
 </head>
 <body ng-app>
  <p>{{1 + 3}}</p>
 </body>
</html>

…but even though the resource modules were configured correctly in my ApplicationResources.groovy, angurlarjs was not getting initialized. I looked at the rendered html and the body tag was bare, no attribute

As per the sitemesh layout template’s directives:

<g:layoutHead/>
<g:layoutBody/>

the contents of my GSPs head and body elements were being rendered into the response but not the actual head, body (or html) elements themselves from my GSP, they were coming from the layouts file.

So, I just moved my ng-app attribute into a newly created div, but it could go into the layouts file either.

The take away is to not decorate head, body (or html) elements in GSP page templates if a layout file is in use.

Large Font issue in MySQL Workbench on Retina Mac book pro

logged in this bug report. http://bugs.mysql.com/bug.php?id=67042

Workaround: edit /Users/$USER/Library/Application Support/MySQL/Workbench/wb_options.xml

replace occurrences of ‘Andale Mono 13’ with ‘Andale Mono 7’ (suggested)

GORM Grails example: cascading deletes many-to-one and manual deletes many-to-many

My self-learning excercise for Grails GORM  (cascading) deletes using a set of 3 models and a test script executed using the excellent grails console (instantiating the groovy ui console).

The models:

package rmdb

class Movie {
String title
static hasMany = [actors: Actor, chapters: Chapter]
static mapping = {chapters cascade: "all-delete-orphan"}
}

class Actor {
String name
static hasMany = [movies: Movie]
static belongsTo = Movie
}

class Chapter {
String title
Movie movie
static belongsTo = Movie
}

With those models the following script illustrates adding and deleting from the three models

import rmdb.*

def movie = new Movie(title: 'The Guard')
movie.addToChapters(new Chapter(title: 'Chapter 1'))
movie.save(flush: true)

movie = new Movie(title: 'In Bruges')
movie.addToChapters(new Chapter(title: 'Chapter 1'))
movie.save(flush: true)

assert(Chapter.count() == 2)
assert(Movie.count() == 2)
assert(Actor.count() == 0)

movie = new Movie(title: 'Intermission')
movie.addToActors(new Actor(name: 'Colin Farrell'))
movie.addToActors(new Actor(name: 'Cillian Murphy'))
movie.save(flush: true)

assert(Chapter.count() == 2)
assert(Movie.count() == 3)
assert(Actor.count() == 2)

// many to many => has a join table => clears out the join table
// does not cascade deletes to Actors but has disassociated the models.
// thereby facilitating deletes without FK constraint exceptions
Movie.all.each {
it.actors?.clear()
it.save(flush: true)
}

// Assert that the above hasn't changed anything (except the join table)
assert(Chapter.count() == 2)
assert(Movie.count() == 3)
assert(Actor.count() ==2)

// now we can delete
Actor.executeUpdate("delete from Actor")

assert(Chapter.count() == 2)
assert(Movie.count() == 3)
assert(Actor.count() ==0)

// We can cascade delete many-to-one Chapters (bi-dir with belongsTo and no join table)
// N.B. cascade delete is courtesy of: Movie static mapping = {chapters cascade: "all-delete-orphan"}
// Also the explicit belongsTo back ref to Movie in Chapters is not needed for cascading saves from the
// owning Movie domain class, just for facilitating bi-directional nav from a chapter to a movie instance
// and for delete cascade.

Movie.all.each {
it.chapters?.clear()
it.save(flush: true)
}

// Hmmm... re above: if there was no back ref.i.e. no explicit 'movie' member of Chapter,
// then grails would have used a join table.
// In this case the delete would NOT have cascaded (need to check this)

assert(Chapter.count() == 0)
assert(Movie.count() == 3)
assert(Actor.count() ==0)

// and finally
Movie.executeUpdate("delete from Movie")

assert(Chapter.count() == 0)
assert(Movie.count() == 0)
assert(Actor.count() ==0)

Grails, GORM, isDirty, flush, and multiple instances

Given domain class:

package rmdb

class Actor {
String name;
}


import rmdb.*;
Actor.executeUpdate("delete from Actor")

new Actor(name: 'William Shatner').save(flush: true)

a1 = Actor.findWhere(name: 'William Shatner')
assert(!a1.isDirty())

a2 = Actor.findWhere(name: 'William Shatner')
assert(!a2.isDirty())

// a1 and a2, refer to the same in memory db row proxy GORM object
a1.name = 'Bill Shatner'
assert(a1.isDirty())
assert(a2.isDirty())
assert(a1.name == a2.name)

// despite no explicit save, by default the in mem object 'Bill Shatner'
// is persisted unless it ia explicitly discarded
// a1.discard() // => 'William Shatner'

Grails, GORM, isDirty and flush

Given domain class:

package rmdb

class Actor {
String name;
}

Some observations on saving, flushing and isDirty.

Scenario 1: Creating a new Domain class instance in mem

import rmdb.*;
Actor.executeUpdate("delete from Actor")

def actorA = new Actor(name: 'William Shatner')

// Even though a new actor has been created, it is not dirty
// 'dirty' => read 'inconsistent with DB' as opposed to
// 'is not persisted in the DB in current state'
// A record needs to have an ID/primary key before an
// ORM can make a determination on whether
// the in mem version is consistent or not with its DB saved version
assert(!actorA.isDirty())

actorA.name = 'Will Shatner'
assert(!actorA.isDirty()) // still 'not inconsistent with itself in the DB'
assert(actorA.save()) //no auto-flush, no actual save
println actorA.id // => '1'
assert(!actorA.isDirty())
actorA.name = "Bill Shatner"
assert(actorA.isDirty()) // Now, record actorA is 'not inconsistent with DB'

// If I don't want "Bill Shatner" persisted, I have to
// explicitly discard actorA, otherwise, in mem version will be flushed
// to the DB automatically at the end of request (when the session is flushed/closed)
actorA.discard()

// btw, this implicit save on flush only applies to non-transient domain classes
// ie those domain class instances attached to a hibernate session (with a db ID)
// transient domain class instances are not auto saved.

//aside: oddly actorA.isDirty() now causes an NPE

Scenario 2 – loading an instance from the DB

// Note we didn't save Bill
def actorB = Actor.findWhere(name: 'Will Shatner')
assert(!actorB.isDirty())
actorB.name = "Willie Shatner"
assert(actorB.isDirty())
actorB.save()
assert(actorB.isDirty()) //still dirty after a save (no flush)
actorB.save(flush: true) //save with flush
assert(!actorB.isDirty()) // now, we're good.

Conventions for evaluating a set of applicable access control (acl) policy statements

‘Access Denied’ or ‘Access Granted’ – Assuming an authenticated request, should it be authorized?

Questions:

What happens when an authenticated request attempts to access a system which controls access to resources based on a set of (potentially conflicting) policy statements?

What are the rules of precedence normally used to decide the result of the aggregated authorization request?

Is it the order of the policies?
Is it specificity of the polciy statement?
Is there a priority/weighting associated with each statement?

Answer:
Typically, it works like this:

All statements in the policy are evaluated against the attributes of the  request: (Principal – who wants to do it, Action – what do they want to do, Resource – on what object). Each statement will evaluate to EXPLICIT_ALLOW, EXPLICIT_DENY or DEFAULT_DENY. These policy statements evaluations are then aggregated into an overall ALLOW or DENY

  • Policy statements which do not explicitly pertain to the request attributes ( in terms of the Principal, Action and Resource) evaluate to DEFAULT_DENY
  • All the policies are applicable and evaluates to either EXPLITI_DENY or EXPLICIT_ALLOW

Aggregating the result:

if EXPLICIT_DENY.count > 0
return DENY

if EXPLICIT_ALLOW.count > 0
return ALLOW

return DENY;