Custom ID Generator

44 views
Skip to first unread message

richar...@taskize.co.uk

unread,
Oct 16, 2015, 12:51:48 PM10/16/15
to Sculptor Generator
Hi -

I am preparing to make a change to how sculptor manages the generation of IDs.

We would like to generate our own IDs and this means a number of changes.

We want the IDs to be 128 bits. 

I believe we can make this change in sculptor-generator.properties by setting ID.TYPE approprioatly (UUID in this case).

However what I am wondering is this.

1. How can we expose the setID to set the ID Manually.
2. How can we turn off Auto Generation of the ID within Sculptor. At present IDs are automatically generated with GeneratorType.AUTO.

Thanks,
Richie. 

Torsten Juergeleit

unread,
Oct 18, 2015, 11:03:10 AM10/18/15
to Sculptor Generator
1. How can we expose the setID to set the ID Manually.
2. How can we turn off Auto Generation of the ID within Sculptor. At present IDs are automatically generated with GeneratorType.AUTO.
 
The domain objects ID setter (with its JPA annotations) is generated in DomainObjectAttributeTmpl.idPropertySetter(). So you can override this method to come up with your own ID setter.

/Torsten

richar...@taskize.co.uk

unread,
Oct 20, 2015, 10:07:17 AM10/20/15
to Sculptor Generator
Ok - I have created my first scultor generator override class and project.

I have completed everything as per the advanced guide. Here is my class.

All I want to do initially it set the setId(...) method from protected to public.

However it does not see to work. My class is below. Any ideas, its my first time trying this process. 

package generator


import org.sculptor.generator.template.domain.DomainObjectAttributeTmpl

import sculptormetamodel.Attribute


class DomainObjectAtrributeTmplOverride extends DomainObjectAttributeTmpl {

     new(DomainObjectAttributeTmpl next) {

             super(next)

     }

      

       override String idPropertySetter(Attribute it) {

               '''

               /**

               * The id is not intended to be changed or assigned manually, but

               * for test purpose it is allowed to assign the id.

               */

               public void set«name.toFirstUpper()»(it.getTypeName() «name») {

                  if ((this.«name» != null) && !this.«name».equals(«name»)) {

                       throw new IllegalArgumentException("Not allowed to change the id property.");

               }

               this.«name» = «name»;

        }

     '''

     }

}

richar...@taskize.co.uk

unread,
Oct 20, 2015, 12:08:12 PM10/20/15
to Sculptor Generator
Seem to be missing @ChainOverride... will try again :)

Richard Walsh

unread,
Oct 20, 2015, 12:41:51 PM10/20/15
to Sculptor Generator
Ok. Getting further...

For some reason the method getTypeName is not available on Attribute it....

But it clearly is for the DomainIbjectAttributeTmpl idPropertySetter(Attribute it)...

Any ideas why?



Taskize Limited - Innovative solutions for Financial Services Technology.
Registered address: 33 Cannon Street, London, EC4M 5SB. Registered in England No. 7921239. This message may contain information that is privileged or confidential. If you are not the intended recipient please delete it and inform the sender immediately.

--
You received this message because you are subscribed to a topic in the Google Groups "Sculptor Generator" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sculptorgenerator/SOhuxfDM-Cg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sculptorgenera...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Taskize Limited - Innovative solutions for Financial Services Technology.
Registered address: 33 Cannon Street, London, EC4M 5SB. Registered in England No. 7921239. This message may contain information that is privileged or confidential. If you are not the intended recipient please delete it and inform the sender immediately.

Richard Walsh

unread,
Oct 20, 2015, 12:43:36 PM10/20/15
to Sculptor Generator
Full class below.

package generator

import com.google.inject.Inject

import org.sculptor.generator.chain.ChainOverride

import org.sculptor.generator.template.domain.DomainObjectAttributeAnnotationTmpl

import org.sculptor.generator.template.domain.DomainObjectAttributeTmpl

import sculptormetamodel.Attribute

@ChainOverride

class DomainObjectAttributeTmplOverride extends DomainObjectAttributeTmpl {

@Inject private var DomainObjectAttributeAnnotationTmpl domainObjectAttributeAnnotationTmpl

new(DomainObjectAttributeTmpl next) {

    super(next)

}

override String idPropertySetter(Attribute it) {

'''

«domainObjectAttributeAnnotationTmpl.propertySetterAnnotations(it

public void set«name.toFirstUpper()»(«it.getTypeName()» «name») {

Torsten Juergeleit

unread,
Oct 20, 2015, 5:02:15 PM10/20/15
to Sculptor Generator
For some reason the method getTypeName is not available on Attribute it....

This extension method is provided via HelperBase. Your class needs

@Inject extension HelperBase helperBase


/Torsten


On Tuesday, October 20, 2015 at 6:41:51 PM UTC+2, Richard Walsh wrote:
Ok. Getting further...

For some reason the method getTypeName is not available on Attribute it....

But it clearly is for the DomainIbjectAttributeTmpl idPropertySetter(Attribute it)...

Any ideas why?

Torsten Juergeleit

unread,
Oct 20, 2015, 5:07:03 PM10/20/15
to Sculptor Generator
Please remove the constructor. It's generated by the @ChainOverride annotation.
An in-depth explanation with example of Sculptors override extension mechanism can be found in this blog post.

/Torsten


On Tuesday, October 20, 2015 at 6:43:36 PM UTC+2, Richard Walsh wrote:
Full class below.

package generator

import com.google.inject.Inject

import org.sculptor.generator.chain.ChainOverride

import org.sculptor.generator.template.domain.DomainObjectAttributeAnnotationTmpl

import org.sculptor.generator.template.domain.DomainObjectAttributeTmpl

import sculptormetamodel.Attribute

@ChainOverride

class DomainObjectAttributeTmplOverride extends DomainObjectAttributeTmpl {

@Inject private var DomainObjectAttributeAnnotationTmpl domainObjectAttributeAnnotationTmpl

new(DomainObjectAttributeTmpl next) {

    super(next)

}

override String idPropertySetter(Attribute it) {

'''

«domainObjectAttributeAnnotationTmpl.propertySetterAnnotations(it

public void set«name.toFirstUpper()»(«it.getTypeName()» «name») {

  if ((this.«name» != null) && !this.«name».equals(«name»)) {

    throw new IllegalArgumentException("Not allowed to change the id property.");

}

this.«name» = «name»;

}

'''

}

}

On 20 October 2015 at 17:41, Richard Walsh richard.walsh wrote:
Ok. Getting further...

For some reason the method getTypeName is not available on Attribute it....

But it clearly is for the DomainIbjectAttributeTmpl idPropertySetter(Attribute it)...

Any ideas why?

Richard Walsh

unread,
Oct 21, 2015, 6:44:36 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
Hi Torsten - 

All good then, I am getting the hang of this. 

I also want to override idAnnotations....

The code is below but it doesnt seem to work. 

What is the best way to debug this? It will save me coming back with questions to you.

Additionally can you see anything wrong as most of this code is a basic copy and paste from the superclass with some minor modifications. 

package generator;

import com.google.inject.Inject

import org.sculptor.generator.chain.ChainOverride

import org.sculptor.generator.ext.DbHelper

import org.sculptor.generator.ext.Properties

import org.sculptor.generator.template.domain.DomainObjectAttributeAnnotationTmpl

import sculptormetamodel.Attribute

@ChainOverride

public class DomainObjectAttributeAnnotationTmplOverride extends DomainObjectAttributeAnnotationTmpl {

@Inject extension DbHelper dbHelper

@Inject extension Properties properties

    override String idAnnotations(Attribute it) {

        '''

            @javax.persistence.Id

            «IF isJpaProviderAppEngine()»

                @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)

            «ELSE»

                @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)

@javax.persistence.GeneratedValue(strategy = GenerationType.AUTO, generator="hibernate_sequence")

@javax.persistence.SequenceGenerator(name = "hibernate_sequence", sequenceName = "«it.name»")

            «ENDIF»

            @javax.persistence.Column(name="«it.getDatabaseName()»")

        '''

    }

}


--
You received this message because you are subscribed to a topic in the Google Groups "Sculptor Generator" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sculptorgenerator/SOhuxfDM-Cg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sculptorgenera...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Walsh

unread,
Oct 21, 2015, 6:46:56 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
Oooppps.. small change.

This is the code.

package generator;


import com.google.inject.Inject

import org.sculptor.generator.chain.ChainOverride

import org.sculptor.generator.ext.DbHelper

import org.sculptor.generator.ext.Properties

import org.sculptor.generator.template.domain.DomainObjectAttributeAnnotationTmpl

import sculptormetamodel.Attribute


@ChainOverride

public class DomainObjectAttributeAnnotationTmplOverride extends DomainObjectAttributeAnnotationTmpl {


@Inject extension DbHelper dbHelper

@Inject extension Properties properties

    override String idAnnotations(Attribute it) {

        '''

            @javax.persistence.Id

            «IF isJpaProviderAppEngine()»

                @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)

            «ELSE»

@javax.persistence.GeneratedValue(strategy = GenerationType.AUTO, generator="hibernate_sequence")

Richard Walsh

unread,
Oct 21, 2015, 7:09:35 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
In addition I want to expose ID to be set on the Builder.

This seems to be slightly more complex..

We want to do this in order to take more control over how the ObjectID is set and Updated. 

Richard Walsh

unread,
Oct 21, 2015, 8:13:32 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
Having read more of the code this appears to be whats happening regarding exposing ID in the builder.

The BuilderHelper has a method called getBuilderAttributes(...)

This seems to filter out the idAttributes.

Although this isn't a Tmpl class it does appear to be overridable.

So I presume if I do override this class I can perhaps unfilder idAttribute. So will give this a try.

Richard Walsh

unread,
Oct 21, 2015, 8:32:02 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
Ok. I have now got much more of this working than before. 

Hold off for now with any responses as most of the issues are with my understanding of this xtend language and the Transformation mechanism. 

Richard Walsh

unread,
Oct 21, 2015, 9:07:45 AM10/21/15
to Torsten Juergeleit, Sculptor Generator
Ok. It appears that I have this all working now. It was down to a few silly errors on my side. Thanks. 

Torsten Juergeleit

unread,
Oct 21, 2015, 10:12:09 AM10/21/15
to Sculptor Generator
What is the best way to debug this?

Please check the section "Unit testing the Sculptor override template" in this blog post and the corresponding mongodb shipping example in Sculptors source code.

/Torsten
Reply all
Reply to author
Forward
0 new messages