Re: where to put Cayenne.xml

From: WONDER (mmmmmmmmm5..eb.de)
Date: Sat Apr 01 2006 - 14:46:33 EST

  • Next message: Mike Kienenberger: "Re: where to put Cayenne.xml"

    Hi,

    Adding the xml files to Class directory worked. Unfortunateley They must be
    directly in the src directery, I cant put them in thier own directery like
    "Model.Cayenne". Similer to EOF.

    Sure, here are the points I faced and solved.
    Its REALLY worth to try.

    ============================================================================
    1: WO need NSArray for repetion something like customer.projects
    Solutions: use custom subclass.vm

    ----------------------------------------------------------------------------
    ----------------------------------------

    #if( ${classGen.isUsingPackage()} )
    package ${classGen.packageName};
    #end

    #if( ${classGen.isUsingSuperPackage()} )
    import
    ${classGen.superPackageName}.${classGen.superPrefix}${classGen.className};
    #end
    import org.objectstyle.cayenne.access.*;

    public class ${classGen.className} extends
    ${classGen.superPrefix}${classGen.className}
    {
        public ${classGen.className} ()
        {
            super();
        }
        public ${classGen.className}(DataContext ec) throws CustomException
        {
            ec.registerNewObject( this );
            this.setDefaultValues();
        }
        public void setDefaultValues( )
        {

        }
    }
    ----------------------------------------------------------------------------
    ----------------------------------------
    and custom superclass.vm

    #if( ${classGen.isUsingPackage()} )
    package ${classGen.packageName};

    #end
    #if( ${classGen.isContainingDeclaredListProperties()} )
    import java.util.List;
    import com.webobjects.foundation.NSArray;
    #end
    import de.mrer.base.CayenneCustomDataObject;
    /** Class ${classGen.superPrefix}${classGen.className} was generated by
    Cayenne.
      * It is probably a good idea to avoid changing this class manually,
      * since it may be overwritten next time code is regenerated.
      * If you need to make any customizations, please use subclass.
      */
    public class ${classGen.superPrefix}${classGen.className} extends
    $classGen.superClassName {

    ## Create property names
    #foreach( $attr in ${classGen.Entity.DeclaredAttributes} )
    #set( $classGen.Prop = $attr.Name )## let controller know about current
    property
        public static final String ${classGen.propAsConstantName}_PROPERTY =
    "${attr.Name}";
    #end
    #foreach( $rel in ${classGen.Entity.DeclaredRelationships} )
    #set( $classGen.Prop = $rel.Name )## let controller know about current
    property
        public static final String ${classGen.propAsConstantName}_PROPERTY =
    "${rel.Name}";
    #end

    #if( $classGen.Entity.DbEntity )
    #foreach( $idAttr in ${classGen.Entity.DbEntity.PrimaryKey} )
    #set( $classGen.Prop = $idAttr.Name )## let controller know about current
    property
        public static final String ${classGen.propAsConstantName}_PK_COLUMN =
    "${idAttr.Name}";
    #end
    #end

    ## Create attribute set/get methods
    #foreach( $attr in ${classGen.Entity.DeclaredAttributes} )
    #set( $classGen.Prop = $attr.Name )## let controller know about current
    property
    #if ("true" != "${classGen.getEntity().isReadOnly()}")
        public void
    set${classGen.cappedProp}($classGen.formatJavaType(${attr.Type})
    $classGen.formatVariableName(${attr.Name}))
        {
            writeProperty("${attr.Name}",
    $classGen.formatVariableName(${attr.Name}));
        }
    #end
        public $classGen.formatJavaType(${attr.Type}) ${attr.Name}()
        {
            return
    ($classGen.formatJavaType(${attr.Type}))readProperty("${attr.Name}");
        }

        public $classGen.formatJavaType(${attr.Type})
    get${classGen.cappedProp}()
        {
            return
    ($classGen.formatJavaType(${attr.Type}))readProperty("${attr.Name}");
        }
    #end
    ##
    ## Create list add/remove/get methods
    #foreach( $rel in ${classGen.Entity.DeclaredRelationships} )
    #set( $classGen.Prop = $rel.Name )## let controller know about current
    property
    #if( $rel.ToMany )
    #if ( ! $rel.ReadOnly )
        public void
    addTo${classGen.cappedProp}($classGen.formatJavaType(${rel.TargetEntity.Clas
    sName}) obj)
        {
            addToManyTarget("${rel.name}", obj, true);
        }
        public void
    removeFrom${classGen.cappedProp}($classGen.formatJavaType(${rel.TargetEntity
    .ClassName}) obj)
        {
            removeToManyTarget("${rel.name}", obj, true);
        }
    #end
        public List ${rel.name}()
        {
            return (List)readProperty("${rel.name}");
        }

        public NSArray wo${classGen.cappedProp}()
        {
            List l = this.${rel.name}();
            return new NSArray( l.toArray() );
        }

        public List get${classGen.cappedProp}()
        {
            return (List)readProperty("${rel.name}");
        }
    #else
    #if ( !${classGen.getEntity().isReadOnly()} && !$rel.ReadOnly )
        public void
    set${classGen.cappedProp}($classGen.formatJavaType(${rel.TargetEntity.ClassN
    ame}) $classGen.formatVariableName(${rel.name}))
        {
            setToOneTarget("${rel.name}",
    $classGen.formatVariableName(${rel.name}), true);
        }
    #end

        public $classGen.formatJavaType(${rel.TargetEntity.ClassName})
    ${rel.name}()
        {
            return
    ($classGen.formatJavaType(${rel.TargetEntity.ClassName}))readProperty("${rel
    .name}");
        }

        public $classGen.formatJavaType(${rel.TargetEntity.ClassName})
    get${classGen.cappedProp}()
        {
            return
    ($classGen.formatJavaType(${rel.TargetEntity.ClassName}))readProperty("${rel
    .name}");
        }
    #end

    #end
    }

    ----------------------------------------------------------------------------
    ----------------------------------------
    ============================================================================
    2: WO uses the NSMutableDictionary, and its very handy to use it as
    lookup.name, lookup.password, etc..
    Cayenne needs Map.
    Solution: Convert the lookup into Map:

        private static Map convertDicToMap(NSMutableDictionary lookup)
        {
            Map<String, Object> params = new HashMap<String, Object>();
            if( lookup.count() < 1 )
            {
                return params;
            }
            NSArray keys = lookup.allKeys();

            for(int i = 0; i < keys.count(); i++)
            {
                String key = (String)keys.objectAtIndex( i );
                Object value = lookup.objectForKey( key );
                if( value instanceof String )
                {
                    value = ((String)value).trim();
                }
                params.put( key, value );
            }
            return params;
        }

    ============================================================================
    3: I had an existing Application and then decided to switch, I think you
    want to do the same.
    i.e. You have to change all xxx.saveChange() into xxx.commitChanges. No
    need. here is the solution :)

        public BaseComponent saveChanges( ) throws CustomException
        {
            return this.commitChanges();
        }

    prallel with addToManyTargets and AddToBothSides....

        public void addObjectToBothSidesOfRelationshipWithKey(DataObject value,
    String relName)
        {
            super.addToManyTarget( relName, value, true);
        }

        public void removeObjectFromBothSidesOfRelationshipWithKey(DataObject
    value, String relName)
        {
            super.removeToManyTarget( relName, value, true );
        }

    BTW: i still dont understand why and when should i say removeToManyTarget(
    relName, value, FALSE ); !!!
    However.

    ============================================================================
    4: Session has the DefaultEditingContext, I think everybody who uses WO, can
    not develop without using the defaultEditiongContext from the Session. so
    here is it for Cayenne :).

        public CayenneCustomSession(DataDomain domain)
        {
            super();

            this.defaultDataContext = domain.createDataContext();
        }

    ============================================================================
    Till now I am very happy using Cayenne. I think I will never go back to EOF.
    Cayenne is really good and has very great level comparing to Tapestry which
    still has not the parellel
    "WebObjects Builder" tool . And the PlugIn Spindle still uses Tapestry 3.
    Cayenne has great GUI and its alomost even better than EOF GUI tool. For
    example Cayenne uses the last JDBC drivers. EOF still needs the old once.

    You will face minor problems, and as a WO developer you will learn Cayenne
    in about 2 or 3 days. depends of course on you WO experiences.

    HTH.

    Maybe Its good idea to have a WebSite on Cayenne WebSite for WO developers
    who want to switch. Should ask Andrus :)

    peaSakoe

    ----- Original Message -----
    From: "Mike Kienenberger" <mkienen..mail.com>
    To: <cayenne-use..ncubator.apache.org>
    Sent: Friday, March 31, 2006 7:41 PM
    Subject: Re: where to put Cayenne.xml

    On 4/1/06, WONDER <mmmmmmmmm5..eb.de> wrote:
    > I use Cayenne in WebObjects 5.2.4
    >
    > In Eclipse, i just add the Directery as Class Folder in the Project, and
    Cayenne.xml is found.
    > One the Server this does not look to work.
    > Where should i put the xml files to be found?

    What about adding the directory to your jar file's classes folder so
    it remains on the classpath? This is how I do it in both Eclipse and
    out of eclipse. Actually, I add the files to the source folder, and
    they're automatically copied over into the classes folder.

    Please keep us up-to-date on using Cayenne with WO5.2.4. I have a
    5.2.4 project I'd like to convert over to using Cayenne instead of EOF
    myself!



    This archive was generated by hypermail 2.0.0 : Fri Mar 31 2006 - 15:47:56 EST