Re: Using prototypes with more than one EOModel and different databases

Jonathan Rochkind <[email protected]>
Newsgroups gmane.comp.web.webobjects.eof
Message-ID <[email protected]>
What I do:  I have an EOModel in a framework, that will be used by 
different applications. Some applications connect to one db, some to 
another. All individual applications, however, will only be used with one 
db.  [Or at least individual app instances will only be used with one db; 
no app instance needs to switch back and forth; it knows when it launches 
with db it will talk to].

In this situation, it works well for me to change the external types of the 
EOPrototype at run-time, on model load.  Here is some code I use:

1. I have a dictionary in the Application that maps from the prototype 
attribute names to the proper external type:
         prototypeOverrides = new NSMutableDictionary();
         //eg: "integerType" is the name of my prototype attribute.
         //"NUMBER" is the proper external type, in this case for Oracle.
         prototypeOverrides.setObjectForKey("NUMBER", "integerType");
         prototypeOverrides.setObjectForKey("DATE", "datetimeType");
         prototypeOverrides.setObjectForKey("LONG", "longtextType");
         prototypeOverrides.setObjectForKey("VARCHAR2", "varcharType");

2. I register to receive the model added notification in the app constructor:
         Class params[]={com.webobjects.foundation.NSNotification.class};
         NSSelector selector=new 
NSSelector("receiveModelAddedNotification", params);
         NSNotificationCenter.defaultCenter().addObserver(this, selector, 
EOModelGroup.ModelAddedNotification, null);

3. In application, this method receives the notification and acts upon it 
thusly:
    public void receiveModelAddedNotification(NSNotification notification) {
         EOModel model = (EOModel) notification.object();

         // We use EOF prototype feature so we can easily change external
         // types to be appropriate to Oracle.
         EOEntity prototypeEntity = model.entityNamed("EOJDBCPrototypes");
         if (prototypeEntity != null) {
                 java.util.Enumeration eachPrototypeEnumer 
=                prototypeOverrides.allKeys().objectEnumerator();
                     while (eachPrototypeEnumer.hasMoreElements()) {
                         String prototypeName = (String) 
eachPrototypeEnumer.nextElement();
                         String properExternalType =
                            (String) 
prototypeOverrides.objectForKey(prototypeName);
                         EOAttribute prototypeAttribute = 
entity.attributeNamed(prototypeName);
                         prototypeAttribute.setExternalType(properExternalType);
                         }
         }
     }


At 07:40 PM 12/2/2002 -0800, Chuck Hill wrote:
>Hi Stefan,
>
>At 11:34 AM 02/12/2002 +0100, Stefan Apelt wrote:
> >Hi,
> >
> >I   have  two  EOModels  in  a  project  that  are  used  to  maintain
> >user-specific  and  content-specific  data.  In  one EOModel I have an
> >'EOPrototypes' entity that contains all prototypes for both models.
> >
> >Both  models  now  connect  to the same database but in the future, it
> >should  be  possible to connect to two different databases (say, mySQL
> >and  FrontBase).  Has anyone tried this and can tell how to handle the
> >prototypes  thingy  then?  Changing the connection dictionary is not a
> >problem,  it  is  the  external  types (varchar vs. TEXT) that I worry
> >about.
> >
> >I have a few guesses but wanted to ask first before reinventing the
> >wheel:
> >
> >1. It does not work. Stick with one database server. My idea for now.
> >
>Possibly the case.
>
> >2. Have one EOPrototype entity per EOModel. Might not work because of
> >   a naming conflict in the EOModelGroup later.
> >
>I'm certain this will not work.  I've tried it by accident.  ;-)
>
>
> >3. Have separate EOModelGroups, each with one model. Might not work
> >   with WO because it loves its defaultModelGroup best.
> >
>This should work as long as there are no unfortunate references to
>defaultModelGroup.  I don't know if you can make cross model group
>relationships thought.
>
>
> >4. Another cool solution I have not seen yet...
> >
>Some other ideas off the top of my head:
>1. Name the prototypes differently in each model (userLongString,
>contentLongString).  This could be combined with the code to create the
>EOPrototypes Entity on the fly (from Jonathan Rochkind?) from a config file
>that was previously posted to the list.
>
>2. Use public void setPrototype(EOAttribute prototype) on EOAttribute to
>set the prototypes manually.
>
>
>
>Chuck
>
>_______________________________________________
>EOF mailing list
>[email protected]
>http://www.omnigroup.com/mailman/listinfo/eof
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.