hi,
sorry for this noisy mail, but can any guru here tell me if my persistence
service is well done ??
thanks a lot in advance



----------------------------------------------------------------------------
----------------------------
package services.persistence;

import java.util.Collection;

import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import services.exception.ExceptionService;

public class PersistenceService implements IPersistenceService {

        private Log log = LogFactory.getLog(PersistenceService.class);

        private PersistenceBroker getBroker() throws Exception {
                PersistenceBroker broker = null;
                try {
                        broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
                } catch (Throwable t) {
                        log.error(t);
        
ExceptionService.getInstance().throwException(ExceptionService.FATAL,
ExceptionService.OJB_BROKERINSTANCIATION);
                }
                return broker;
        }

        public void storeObject(Object object) throws Exception {
                PersistenceBroker broker = getBroker();
                try {
                        broker.beginTransaction();
                        broker.store(object);
                        broker.commitTransaction();
                } catch (Exception e1) {
                        try {
                                broker.abortTransaction();
                        } catch (Exception e2) {
                                log.error(e2);
        
ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME,
ExceptionService.OJB_TRANSACTION);
                        }
                        log.error(e1);
        
ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME,
ExceptionService.OJB_STORE);
                }
        }

        public void deleteObject(Object object) throws Exception {
                PersistenceBroker broker = getBroker();
                try {
                        broker.beginTransaction();
                        broker.delete(object);
                        broker.commitTransaction();
                } catch (Exception e1) {
                        try {
                                broker.abortTransaction();
                        } catch (Exception e2) {
                                log.error(e2);
        
ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME,
ExceptionService.OJB_TRANSACTION);
                        }
                        log.error(e1);
        
ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME,
ExceptionService.OJB_STORE);
                }
        }

        public Object getObjectByCriteria(Class clazz, Criteria criteria) {
                Object object = null;
                Query query = new QueryByCriteria(clazz, criteria);
                try {
                        object = getBroker().getObjectByQuery(query);
                } catch (Throwable t) {
                        log.error(t);
        
ExceptionService.getInstance().addError(ExceptionService.MESSAGENAME,
ExceptionService.OJB_GETCOLLECTION);
                }
                return object;
        }

        public Object getObjectById(Class clazz, int id) {
                Criteria criteria = new Criteria();
                criteria.addEqualTo("id", String.valueOf(id));
                return getObjectByCriteria(clazz, criteria);
        }

        public Collection getCollectionByCriteria(Class clazz, Criteria
criteria) {
                Collection collection = null;
                Query query = new QueryByCriteria(clazz, criteria);
                try {
                        collection =
getBroker().getCollectionByQuery(query);
                } catch (Throwable t) {
                        log.error(t);
        
ExceptionService.getInstance().addError(ExceptionService.MESSAGENAME,
ExceptionService.OJB_GETCOLLECTION);
                }
                return collection;

        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to