Search in sources :

Example 6 with UOWException

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

the class RequestPasswordComponent method sendEmail.

public void sendEmail() {
    Criteria criteria = new Criteria();
    criteria.setTable(UserMeta.getName());
    if (this.getUserName() != null)
        criteria.addCriteria(UserMeta.USER_NAME, this.getUserName());
    if (this.getEmail() != null)
        criteria.addCriteria(UserMeta.E_MAIL_ADDRESS, this.getEmail());
    if (!this.getSecurityQuestion().equalsIgnoreCase("None"))
        criteria.addCriteria(UserMeta.SECURITY_QUESTION, this.getSecurityQuestion());
    if (this.getSecurityAnswer() != null)
        criteria.addCriteria(UserMeta.SECURITY_ANSWER, this.getSecurityAnswer());
    criteria.addCriteria(UserMeta.STATUS, "A");
    try {
        UOW uow = new UOW();
        if (!uow.query(criteria).isEmpty()) {
            StringBuffer body = new StringBuffer();
            if (uow.query(criteria).size() > 1) {
                body.append("You have " + uow.query(criteria).size() + " Active Accounts");
                int counter = 1;
                for (Iterator itr = uow.query(criteria).iterator(); itr.hasNext(); ) {
                    User ur = (User) itr.next();
                    body.append(", Account " + counter++ + ", User Name is " + ur.getUserName() + ", Security Question is '" + MessageHelper.replaceTokens("[label.Jaffa.User.UserRequest.SecurityQuestion." + ur.getSecurityQuestion() + "]") + "'");
                }
            } else {
                User ur = (User) uow.query(criteria).iterator().next();
                if (this.getUserName() != null) {
                    body.append("You have the following Active Account  ");
                    body.append(", User Name is " + ur.getUserName() + ", Password is " + ur.getPassword() + ".");
                } else if (this.getSecurityQuestion() != null && new Long(this.getSecurityQuestion()).equals(ur.getSecurityQuestion()) && (this.getSecurityAnswer().equals(ur.getSecurityAnswer()))) {
                    body.append("You have the following Active Account  ");
                    body.append(", User Name is " + ur.getUserName() + ", Password is " + ur.getPassword() + ".");
                } else {
                    body.append("You have the following Active Account");
                    body.append(", User Name is " + ur.getUserName() + ", Security Question is '" + MessageHelper.replaceTokens("[label.Jaffa.User.UserRequest.SecurityQuestion." + ur.getSecurityQuestion() + "]") + "'.");
                }
            }
            EmailerBean email = new EmailerBean();
            String[] to = new String[] { this.getEmail() };
            try {
                email.sendMail(to, "Account Information", body.toString());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    } catch (UOWException e) {
        e.printStackTrace();
    }
}
Also used : User(org.jaffa.applications.jaffa.modules.admin.domain.User) UOWException(org.jaffa.persistence.exceptions.UOWException) MessagingException(javax.mail.MessagingException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 7 with UOWException

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

the class AddInterceptor method invoke.

/**
 *Performs the logic associated with adding Persistent objects to the database.
 * This will add each object in the PersistentTransaction's ADD collection to 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.getAdds();
    if (objects != null) {
        // add the objects to the database
        for (Iterator i = objects.iterator(); i.hasNext(); ) {
            IPersistent object = (IPersistent) i.next();
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeAdd() for the object " + object);
                JdbcBridge.executeAdd(object, pt.getDataSource());
                pt.getDataSource().clearObjectCache();
            } catch (SQLIntegrityConstraintViolationException e) {
                if (e.getErrorCode() == PRIMARY_KEY_ERROR_CODE) {
                    String str = "The primary-key is not unique: " + this;
                    log.error(str);
                    String labelToken = null;
                    try {
                        labelToken = PersistentHelper.getLabelToken(object.getClass().getName());
                    } catch (Exception ex) {
                    // don't do anything.. just return the domainClassName
                    }
                    if (labelToken == null)
                        labelToken = MessageHelper.tokenize(object.getClass().getName());
                    throw new PreAddFailedException(null, new DuplicateKeyException(labelToken));
                } else {
                    String str = "Error while adding the Persistent object to the database: " + object;
                    log.error(str, e);
                    // was checked in pre-add previously
                    throw new PreAddFailedException(null, e);
                }
            } catch (Exception e) {
                String str = "Error while adding the Persistent object to the database: " + object;
                log.error(str, e);
                throw new AddFailedException(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) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException) AddFailedException(org.jaffa.persistence.exceptions.AddFailedException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) UOWException(org.jaffa.persistence.exceptions.UOWException) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) AddFailedException(org.jaffa.persistence.exceptions.AddFailedException) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException)

Example 8 with UOWException

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

the class ConnectionPoolTest method testConnectionsExhausted.

/**
 * This test will create the 'maximumConnections as defined in init.xml' number of UOW objects. It will then try to create one more UOW, which is expected to throw an exception.
 */
public void testConnectionsExhausted() {
    UOW[] uows = null;
    UOW oneMoreUow = null;
    try {
        // obtain the maximum number of connections from the pool
        int maximumConnections = ConnectionHelper.getMaximumConnections();
        uows = new UOW[maximumConnections];
        for (int i = 0; i < uows.length; i++) uows[i] = new UOW();
        // now create an additional UOW, which is expected to throw an exception
        try {
            oneMoreUow = new UOW();
            fail("Able to create an UOW even after maximum number of connections were obtained !!");
        } catch (UOWException e) {
        // This is expected.. do nothing
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    } finally {
        try {
            if (oneMoreUow != null)
                oneMoreUow.rollback();
            if (uows != null) {
                for (int i = 0; i < uows.length; i++) {
                    if (uows[i] != null) {
                        uows[i].rollback();
                        uows[i] = null;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
}
Also used : UOWException(org.jaffa.persistence.exceptions.UOWException) UOW(org.jaffa.persistence.UOW) UOWException(org.jaffa.persistence.exceptions.UOWException)

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