A new way to use CayenneTemplate[How to implement it]

1 message Options
Embed this post
Permalink
gilbertoca

A new way to use CayenneTemplate[How to implement it]

Reply Threaded More More options
Print post
Permalink
Hi all, hope everyone are well!

I'm trying to use the CayenneTemplate class implemented by Malcolm. I
want to use it the following way:
[code]
BaseCayenneTemplate<Veiculo, String> bCT = new
BaseCayenneTemplate<Veiculo, String>(Veiculo.class);

Veiculo entity = new Veiculo();
entity.setPlaca("250-MVU");
entity.setCor("vermelho");
bCT.insert(entity);
Collection<Veiculo> result = bCT.getAll();

[/code]

My implementation isn't complete and maybe doesn't exist any
pattern/optimization on it.
So, I ask you help on it!

Regards,

Gilberto

PS.: Is there the possibility to see this option in click-extras or in
google code?

[BaseCayenneTemplate.java]

public class BaseCayenneTemplate<T, PK extends Serializable> extends
                CayenneTemplate implements IService<T, PK> {
        protected Class<T> classEntity;

        public BaseCayenneTemplate(Class<T> classEntity) {
                super();
                this.classEntity = classEntity;
        }

        public Class<T> getClassEntity() { return classEntity;}

        public Collection<T> getAll() {
                Validate.notNull(value, "Null ClassEntity parameter");
                SelectQuery query = new SelectQuery(getClassEntity());
                return performQuery(query);
        }

        public boolean find(T entity) {
                Validate.notNull(value, "Null Entity parameter");
                Validate.notNull(value, "Null ClassEntity parameter");
                if (performQuery(new SelectQuery(getClassEntity(), entity)) != null){
                       
                }
                return
        }
        public boolean find(PK pk) {
                Validate.notNull(value, "Null PK parameter");
                Validate.notNull(value, "Null ClassEntity parameter");
                if (performQuery(new SelectQuery(getClassEntity(), entity)) != null){
                       
                }
                return
        }

        public void insert(T entity) throws InsertException {
                Validate.notNull(value, "Null Entity parameter");
                if (((Persistent) entity).getObjectContext() == null) {
                        registerNewObject((DataObject) entity);
                }
                commitChanges();
        }

        public void update(T entity) {
                Validate.notNull(value, "Null Entity parameter");
                if (((Persistent) entity).getObjectContext() == null) {
                        registerNewObject((DataObject) entity);
                }
                commitChanges();
        }

        public void delete(T entity) {
                Validate.notNull(value, "Null Entity parameter");
                deleteObject((DataObject) entity);
        }

        public void delete(PK pk) {
                Validate.notNull(value, "Null Pk parameter");
                deleteObject();
        }
       
        public Collection<T> findLike(String likeColumn, String likeValue) {
                Validate.notNull(value, "Null ClassEntity parameter");
                Validate.notNull(value, "Null likeColumn parameter");
                Validate.notNull(value, "Null likeValue parameter");
                Expression qual = ExpressionFactory.likeExp(likeColumn, likeValue);
                return performQuery(new SelectQuery(getClassEntity(), qual));
        }

        public Collection<T> findByNamedQuery(String queryName) {
                SelectQuery query = new SelectQuery(queryName);
                return performQuery(query);
        }
}


[IService.java]

/**
 
 * General interface for use with the persistence mechanism. Establishes essential method signatures for
 * database oparations.
 *
 * @author Gilberto Caetano de Andrade
 */
public interface IService<T, PK extends Serializable> {

    public Class<T> getClassEntity();

    /**
     * Generic method used to get all entities of a particular class. This particular class is
     * defined in the constructor of the implementation of this interface.
     *
     * @return Collection All entities for this particular class
     */
    public Collection<T> getAll();

    /**
     * Generic method to get an entity
     *
     * @param entity
     *                Object to which it returns the search
     * @return true object exists and the entity is filled. False otherwise.
     */
    public boolean find(T entity);

    /**
     * Generic method to check the existence of an entity.
     *
     * @param pk
     *                Object Identification to check the existence of
     * @return true object exists. False otherwise.
     */
    public boolean find(PK pk);
   
    /**
     * Generic method to insert a new entity.
     *
     * @param entity
     *                the new entity.
     * @throws InsertException
     */
    public void insert(T entity) throws InsertException;

    /**
     * Generic method to update an entity.
     *
     * @param entity
     *                the changed entity.
     * @throws ObjectNotExistException
     */
    public void update(T entity);

    /**
     * Generic method to delete an entity. Used with find() method.
     *
     * @param entity
     *                the entity to be deleted.
     * @throws ObjectNotExistException
     */
    public void delete(T entity);

    /**
     * Generic method to delete an entity. Used with find() method.
     *
     * @param p?
     *                the primary key of the entity to be deleted.
     * @throws ObjectNotExistException
     */
    public void delete(PK pk);

    /**
     * Generic method that returns a list of entities for a named query
     *
     * @param queryName
     *                the named query.
     */
    public Collection<T> findByNamedQuery(String queryName);

    /**
     * Generic method that returns a list of entities for LIKE expression
     *
     * @param likeColumn
     *                the field.
     * @param likeValue
     *                the value.
     */
    public Collection<T> findLike(String likeColumn, String likeValue);
}