Search in sources :

Example 1 with UOWException

use of org.jaffa.persistence.exceptions.UOWException in project jaffa-framework by jaffa-projects.

the class BusinessEventLogViewerTx method addRelatedDtos.

// .//GEN-END:_buildDto_3_be
// .//GEN-BEGIN:_addRelatedDtos_1_be
private void addRelatedDtos(UOW uow, BusinessEventLogViewerOutDto output, BusinessEventLog businessEventLog) throws UOWException {
    // .//GEN-BEGIN:_addRelatedDtos_Attachment_1_be
    if (businessEventLog.getLogId() != null) {
        Criteria criteria = new Criteria();
        criteria.setTable(AttachmentMeta.getName());
        criteria.addCriteria(AttachmentMeta.SERIALIZED_KEY, businessEventLog.getLogId());
        criteria.addOrderBy("AttachmentId", Criteria.ORDER_BY_ASC);
        // Create a new Criteria, and pass the correct serializedKey
        try {
            criteria = new Criteria();
            criteria.setTable(AttachmentMeta.getName());
            criteria.addCriteria(AttachmentMeta.SERIALIZED_KEY, PersistentHelper.generateSerializedKey(businessEventLog));
            criteria.addOrderBy(AttachmentMeta.ATTACHMENT_ID, Criteria.ORDER_BY_ASC);
        } catch (Exception e) {
            String str = "Error in generating the serialized key";
            log.error(str, e);
            throw new RuntimeException(str, e);
        }
        // .//GEN-LAST:_addRelatedDtos_Attachment_1
        // .//GEN-BEGIN:_addRelatedDtos_Attachment_2_be
        Iterator itr = uow.query(criteria).iterator();
        while (itr.hasNext()) {
            Attachment attachment = (Attachment) itr.next();
            AttachmentDto dto = new AttachmentDto();
            // .//GEN-END:_addRelatedDtos_Attachment_2_be
            // Add custom code before all the setters//GEN-FIRST:_addRelatedDtos_Attachment_2
            // .//GEN-LAST:_addRelatedDtos_Attachment_2
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_AttachmentId_1_be
            dto.setAttachmentId(attachment.getAttachmentId());
            // .//GEN-END:_addRelatedDtos_Attachment_AttachmentId_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_SerializedKey_1_be
            dto.setSerializedKey(attachment.getSerializedKey());
            // .//GEN-END:_addRelatedDtos_Attachment_SerializedKey_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_OriginalFileName_1_be
            dto.setOriginalFileName(attachment.getOriginalFileName());
            // .//GEN-END:_addRelatedDtos_Attachment_OriginalFileName_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_AttachmentType_1_be
            dto.setAttachmentType(attachment.getAttachmentType());
            // .//GEN-END:_addRelatedDtos_Attachment_AttachmentType_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_ContentType_1_be
            dto.setContentType(attachment.getContentType());
            // .//GEN-END:_addRelatedDtos_Attachment_ContentType_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_Description_1_be
            dto.setDescription(attachment.getDescription());
            // .//GEN-END:_addRelatedDtos_Attachment_Description_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_Remarks_1_be
            dto.setRemarks(attachment.getRemarks());
            // .//GEN-END:_addRelatedDtos_Attachment_Remarks_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_SupercededBy_1_be
            dto.setSupercededBy(attachment.getSupercededBy());
            // .//GEN-END:_addRelatedDtos_Attachment_SupercededBy_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_CreatedOn_1_be
            dto.setCreatedOn(attachment.getCreatedOn());
            // .//GEN-END:_addRelatedDtos_Attachment_CreatedOn_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_CreatedBy_1_be
            dto.setCreatedBy(attachment.getCreatedBy());
            // .//GEN-END:_addRelatedDtos_Attachment_CreatedBy_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_LastChangedOn_1_be
            dto.setLastChangedOn(attachment.getLastChangedOn());
            // .//GEN-END:_addRelatedDtos_Attachment_LastChangedOn_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_LastChangedBy_1_be
            dto.setLastChangedBy(attachment.getLastChangedBy());
            // .//GEN-END:_addRelatedDtos_Attachment_LastChangedBy_1_be
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_Data_1_be
            dto.setData(attachment.getData());
            // .//GEN-END:_addRelatedDtos_Attachment_Data_1_be
            // Add custom code to pass values to the dto//GEN-FIRST:_addRelatedDtos_Attachment_3
            // .//GEN-LAST:_addRelatedDtos_Attachment_3
            // .//GEN-BEGIN:_addRelatedDtos_Attachment_3_be
            output.addAttachment(dto);
        }
    }
// .//GEN-END:_addRelatedDtos_Attachment_3_be
// .//GEN-BEGIN:_addRelatedDtos_2_be
}
Also used : AttachmentDto(org.jaffa.modules.messaging.components.businesseventlogviewer.dto.AttachmentDto) Attachment(org.jaffa.components.attachment.domain.Attachment) Criteria(org.jaffa.persistence.Criteria) UOWException(org.jaffa.persistence.exceptions.UOWException) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 2 with UOWException

use of org.jaffa.persistence.exceptions.UOWException in project jaffa-framework by jaffa-projects.

the class UserContextWrapper method readUserRoles.

/**
 * Read the roles for the user from the database
 *
 * @param userId the user id.
 * @return the roles for the user.
 * @throws FrameworkException if any internal error occurs.
 */
protected String[] readUserRoles(String userId) throws FrameworkException {
    UOW uow = null;
    List<String> roleList = new ArrayList<String>();
    try {
        uow = new UOW();
        Criteria c = new Criteria();
        c.setTable(UserRoleMeta.getName());
        c.addCriteria(UserRoleMeta.USER_NAME, userId);
        Collection roles = uow.query(c);
        for (Iterator it = roles.iterator(); it.hasNext(); ) {
            UserRole role = (UserRole) it.next();
            roleList.add(role.getRoleName());
        }
    } catch (UOWException e) {
        // Log the error
        log.error("Can't Get The Roles for User - " + userId, e);
    } finally {
        // Attempt to rollback any open transaction
        try {
            if (uow != null) {
                uow.rollback();
            }
        } catch (UOWException e) {
            log.error("Rollback", e);
        }
    }
    return (String[]) roleList.toArray(new String[0]);
}
Also used : UOWException(org.jaffa.persistence.exceptions.UOWException) UserRole(org.jaffa.applications.jaffa.modules.admin.domain.UserRole) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 3 with UOWException

use of org.jaffa.persistence.exceptions.UOWException in project jaffa-framework by jaffa-projects.

the class UpdateInterceptor method invoke.

/**
 *Performs the logic associated with updating Persistent objects in the database.
 * This will update each object in the PersistentTransaction's UPDATE collection in the database, utilising the JdbcBridge.
 * It will then pass on the control to the next Interceptor in the chain.
 * @param pt The PersistentTransaction object, on which the Interceptor is to be executed.
 * @throws UOWException if any error occurs.
 * @return the output from the next Interceptor in the chain.
 */
public Object invoke(PersistentTransaction pt) throws UOWException {
    Collection objects = pt.getUpdates();
    if (objects != null) {
        // updates the objects in the database
        for (Iterator i = objects.iterator(); i.hasNext(); ) {
            IPersistent object = (IPersistent) i.next();
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeUpdate() for the object " + object);
                JdbcBridge.executeUpdate(object, pt.getDataSource());
                pt.getDataSource().clearObjectCache();
            } catch (Exception e) {
                String str = "Error while updating the Persistent object in the database: " + object;
                log.error(str, e);
                throw new UpdateFailedException(null, e);
            }
            i.remove();
        }
    }
    // pass control to the next interceptor in the chain
    if (getNextInterceptor() != null) {
        if (log.isDebugEnabled())
            log.debug("Invoking the next Interceptor in the chain " + getNextInterceptor().getClass().getName());
        return getNextInterceptor().invoke(pt);
    } else {
        if (log.isDebugEnabled())
            log.debug("This is the end of the Interceptor chain");
        return null;
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) UpdateFailedException(org.jaffa.persistence.exceptions.UpdateFailedException) UpdateFailedException(org.jaffa.persistence.exceptions.UpdateFailedException) UOWException(org.jaffa.persistence.exceptions.UOWException)

Example 4 with UOWException

use of org.jaffa.persistence.exceptions.UOWException in project jaffa-framework by jaffa-projects.

the class DeleteInterceptor method invoke.

/**
 *Performs the logic associated with deleting Persistent objects from the database.
 * This will delete each object in the PersistentTransaction's DELETE collection from the database, utilising the JdbcBridge.
 * It will then pass on the control to the next Interceptor in the chain.
 * @param pt The PersistentTransaction object, on which the Interceptor is to be executed.
 * @throws UOWException if any error occurs.
 * @return the output from the next Interceptor in the chain.
 */
public Object invoke(PersistentTransaction pt) throws UOWException {
    Collection objects = pt.getDeletes();
    if (objects != null) {
        // deletes the objects from the database
        for (Iterator i = objects.iterator(); i.hasNext(); ) {
            IPersistent object = (IPersistent) i.next();
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeDelete() for the object " + object);
                JdbcBridge.executeDelete(object, pt.getDataSource());
                pt.getDataSource().clearObjectCache();
            } catch (Exception e) {
                String str = "Error while deleting the Persistent object from the database: " + object;
                log.error(str, e);
                throw new DeleteFailedException(null, e);
            }
            i.remove();
        }
    }
    // pass control to the next interceptor in the chain
    if (getNextInterceptor() != null) {
        if (log.isDebugEnabled())
            log.debug("Invoking the next Interceptor in the chain " + getNextInterceptor().getClass().getName());
        return getNextInterceptor().invoke(pt);
    } else {
        if (log.isDebugEnabled())
            log.debug("This is the end of the Interceptor chain");
        return null;
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) DeleteFailedException(org.jaffa.persistence.exceptions.DeleteFailedException) DeleteFailedException(org.jaffa.persistence.exceptions.DeleteFailedException) UOWException(org.jaffa.persistence.exceptions.UOWException)

Example 5 with UOWException

use of org.jaffa.persistence.exceptions.UOWException in project jaffa-framework by jaffa-projects.

the class QueryInterceptor method invoke.

/**
 *Performs the logic associated with querying the database. It will perform queries for each Criteria object in the PersistentTransaction's QUERY collection.
 * Ideally there will be just one Criteria object in the collection.
 * Control will be passed to the next interceptor in the chain, if any, else a Collection will be returned containing the Results of the last query.
 * @param pt The PersistentTransaction object, on which the Interceptor is to be executed.
 * @throws UOWException if any error occurs.
 * @return the output from the next Interceptor in the chain, if any, else a Collection will be returned containing the Results of the last query. However, for a Criteria having a StoredProcedure, the output will be null.
 */
public Object invoke(PersistentTransaction pt) throws UOWException {
    Object output = null;
    if (pt.getQuery() != null) {
        // check if the 1st entry in the criteria is a StoredPreocdure
        Criteria criteria = pt.getQuery();
        Object firstCriteriaElement = null;
        Collection criteriaEntries = criteria.getCriteriaEntries();
        if (criteriaEntries != null && criteriaEntries.size() > 0)
            firstCriteriaElement = criteria.getCriteriaEntries().iterator().next();
        if (firstCriteriaElement != null)
            firstCriteriaElement = ((Criteria.CriteriaEntry) firstCriteriaElement).getValue();
        if (firstCriteriaElement != null && firstCriteriaElement instanceof IStoredProcedure) {
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeStoredProcedure()");
                JdbcBridge.executeStoredProcedure((IStoredProcedure) firstCriteriaElement, criteria, pt.getDataSource());
            } catch (Exception e) {
                String str = "Error in executing the StoredProcedure: " + firstCriteriaElement;
                if (log.isDebugEnabled())
                    log.debug(str, e);
                throw new QueryFailedException(null, e);
            }
        } else {
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeQuery()");
                output = JdbcBridge.executeQuery(criteria, pt.getDataSource());
            } catch (Exception e) {
                String str = "Error in executing the query on : " + criteria.getTable();
                if (log.isDebugEnabled())
                    log.debug(str, e);
                throw new QueryFailedException(null, e);
            }
        }
        pt.setQuery(null);
    }
    // pass control to the next interceptor in the chain
    if (getNextInterceptor() != null) {
        if (log.isDebugEnabled())
            log.debug("Invoking the next Interceptor in the chain " + getNextInterceptor().getClass().getName());
        return getNextInterceptor().invoke(pt);
    } else {
        if (log.isDebugEnabled())
            log.debug("This is the end of the Interceptor chain");
        return output;
    }
}
Also used : QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) Criteria(org.jaffa.persistence.Criteria) IStoredProcedure(org.jaffa.persistence.engines.jdbcengine.IStoredProcedure) UOWException(org.jaffa.persistence.exceptions.UOWException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException)

Aggregations

UOWException (org.jaffa.persistence.exceptions.UOWException)8 Criteria (org.jaffa.persistence.Criteria)4 IPersistent (org.jaffa.persistence.IPersistent)3 UOW (org.jaffa.persistence.UOW)3 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 MessagingException (javax.mail.MessagingException)1 User (org.jaffa.applications.jaffa.modules.admin.domain.User)1 UserRole (org.jaffa.applications.jaffa.modules.admin.domain.UserRole)1 Attachment (org.jaffa.components.attachment.domain.Attachment)1 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 AttachmentDto (org.jaffa.modules.messaging.components.businesseventlogviewer.dto.AttachmentDto)1 IStoredProcedure (org.jaffa.persistence.engines.jdbcengine.IStoredProcedure)1 AddFailedException (org.jaffa.persistence.exceptions.AddFailedException)1 DeleteFailedException (org.jaffa.persistence.exceptions.DeleteFailedException)1 PreAddFailedException (org.jaffa.persistence.exceptions.PreAddFailedException)1 QueryFailedException (org.jaffa.persistence.exceptions.QueryFailedException)1