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();
}
}
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;
}
}
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();
}
}
}
Aggregations