client-superclass.vm

From: Marcin Skladaniec (marci..sh.com.au)
Date: Thu Dec 15 2005 - 19:57:44 EST

  • Next message: Andrus Adamchik: "Re: XMLEncoder behavior"

    Hello
    For our project I added few lines to client-superclass template.
    Methods I added are getting property keys and data types in generic
    way (and more), we need them to populate JComboBoxes and some lists.
    We are using ant-cgen to process the template.
    Maybe someone would need this functionality, so I post it in here.

    To use those features in nice way your PersistentObjects should
    implement interface which define the methods. Unfortunately the
    interface has to be hard-coded in the client-superclass.vm. Maybe
    those changes could be added to cayenne...but I don't know how...

    So:

    add those lines:

    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.List;

    modify this line:
    public class ${entityUtils.superClassName} extends $
    {entityUtils.baseClassName} implements ##YOUR.CUSTOM.INTERFACE## {

    and add those lines:

            /**
             *..eturn a list of all property keys.
             */
            public List getAllPropertyKeys() {
                    Vector v=new Vector() ;
    #foreach( $attr in ${objEntity.DeclaredAttributes} )
                    v.add(${stringUtils.capitalizedAsConstant($attr.Name)}_PROPERTY);
    #end
    #foreach( $rel in ${objEntity.DeclaredRelationships} )
                 v.add(${stringUtils.capitalizedAsConstant($rel.Name)}_PROPERTY);
    #end
                    return v;
            }
            
            /**
            *..eturn number of property keys.
            */
            public int getPropertyCount() {
                    return getAllPropertyKeys().size();
            }
            
            /**
             *..eturn a hashtable of all properties.
             */
            public Hashtable getPropertyHashtable() {
                    Hashtable t=new Hashtable() ;
    #foreach( $attr in ${objEntity.DeclaredAttributes} )
                    if (get${stringUtils.capitalized($attr.Name)}() != null) {
                            t.put(${stringUtils.capitalizedAsConstant($attr.Name)}_PROPERTY,
    get${stringUtils.capitalized($attr.Name)}());
                    }
    #end
    #foreach( $rel in ${objEntity.DeclaredRelationships} )
                    if (get${stringUtils.capitalized($rel.Name)}() != null) {
                         t.put(${stringUtils.capitalizedAsConstant($rel.Name)}
    _PROPERTY, get${stringUtils.capitalized($rel.Name)}());
                 }
    #end
                    return t;
            }
            
            /**
             * Gets value converted to String for given key.
             *..aram key - the property/attribute key
             *..eturn the human readable version
             */
            public String getValueForKey(String key) {
                    return (getPropertyHashtable().get(key)).toString();
            }
            
            /**
            * Gets data type for given key.
            *..eturn Class - data type for given key
            *..aram key - Property String
            */
            public Class getDataTypeForKey(String key) {
            
    #foreach( $attr in ${objEntity.DeclaredAttributes} )
                 if(key.equals("${attr.Name}")) return
    $importUtils.formatJavaType(${attr.Type}) .class;
    #end
            return null;
            }

    (Please be aware that added methods are just working versions, there
    is no error handling if wrong key is passed etc.)

    Marcin Skladaniec



    This archive was generated by hypermail 2.0.0 : Thu Dec 15 2005 - 20:33:10 EST