CayenneObjectManager - alternative API for using Apache Cayenne

From: David Marko (dmark..iscali.cz)
Date: Sun Feb 24 2008 - 05:13:48 EST

  • Next message: Aristedes Maniatis: "Re: CayenneObjectManager - alternative API for using Apache Cayenne"

    Hello,
    a few months ago a wrote about creating alternative approach for using Apache
    Cayenne. Actualy I use Cayenne with Click Framework and following scenario came
    from practice on several projects. The main idea is to use Cayenne as very
    stable background(engine) but alter API to something closer to ActiveRecord like
    approach. The result is CayenneObjectManager that implements alternative API
    that, as result, generates standard Cayenne queries.

    CayenneObjectManager API assumes Java5 as generics are used extensively and is
    tested with latest Apache Cayenne M3. Entire package is rather small and is
    intended to be extended by other methods. I'm not very experienced JAVA
    developer so one thing I would like to ask is, if someone can review the concept
    and correct some potencial mistakes or contribute with some ideas etc. I'm open
    for correction.

    Thanks,
    David
    See attached file with source at https://issues.apache.org/cayenne/browse/CAY-988

    For quick example just image you have a simple table with Role names and imagine
    how simply you can work with this:
    // Load role object by its name
    COManager manager=new COManagerImpl();
    // init is based on usage scenario. This can be used in standalone app. for e.g.
    web app usage, the context is inititalized automaticly from
    DataContext.getThreadDataContext();
    co_manager.setContext(DataContext.createDataContext());
    Role
    role_admin=co_manager.find(Role.class).byProperty("name").EQUALS("admin").first();

    // all roles from allowed list as key-pair for checkbox field
    Map<String,String>
    roles=co_manager.find(Role.class).byProperty(Role.NAME_PROPERTY).IN(allowed_roles).to_pairs("name",
    "id");

    CayenneObjectsManager - alternative API for working with Apache Cayenne subsystem
    // Available use cases
    // find one item by ID
    co_manager.find(Class).byId(id)

    // find all items by property and given condition
    co_manager.find(Class).byProperty("property").EQUALS(value)

    // find first item by property and given condition
    co_manager.find(Class).byProperty("property").BETWEEN(value1,value2).first()
    co_manager.find(Class).byProperty("property").LIKE(value).first()
    co_manager.find(Class).byProperty("property").IN(range).first()

    // find all items
    co_manager.find(Class).all()

    // find all items by based on CayenneObjectsQuery
    co_manager.find(Class).allByQuery(COQuery query)

    // find all firts item by based on CayenneObjectsQuery
    co_manager.find(Class).allByQuery(COQuery query).firts()

    //find all selected items as key-pairs available e.g. for checkbox field
    co_manager.find(Class).byProperty("property").EQUAL(value1).to_pairs(key,value)

    // aggregate functions
    co_manager.aggregate(Class).count("property")
    co_manager.aggregate(Class).count("property", COQuery query)
    co_manager.aggregate(Class).sum("property")
    co_manager.aggregate(Class).sum("property", COQuery query)
    co_manager.aggregate(Class).avg("property")
    co_manager.aggregate(Class).avg("property", COQuery query)
    co_manager.aggregate(Class).count("property",
    Query.where("year>$year").addParam("year", 2003));

    // common actions
    co_manager.create(Class)
    co_manager.save()



    This archive was generated by hypermail 2.0.0 : Sun Feb 24 2008 - 05:14:24 EST