Notification Service: NoticeTemplate DAO
1 November 2008I know that I said that we would be moving on to the Betwixt mapping and associated XSL file for the NoticeTemplate bean, but after further review, I’ve decided to attack the data access object for the NoticeTemplate instead. The NoticeTemplateDao is basically a clone of the LookupTableDao from the Lookup Table project, which was cloned from the DAOs in the Approval Service and Authorization Service, which I originally hijacked from Matt Raible‘s AppFuse project (… or was it AppFuse Light? I can’t even remember now!). Anyway, for those of you who have been following along at home, this should look pretty familiar by now:
package org.restafarian.notify.dao;
import java.util.List;
import org.restafarian.core.dao.Dao;
import org.restafarian.notify.beans.NoticeTemplate;
/**
* <p>This is the NoticeTemplate data access object interface.</p>
*/
public interface NoticeTemplateDao extends Dao {
/**
* <p>Returns all NoticeTemplates in the database.</p>
*
* @return all NoticeTemplates in the database
*/
public List findAll();
/**
* <p>Returns all NoticeTemplates in the database that
* match the specified search criteria.</p>
*
* @param noticeTemplate an example NoticeTemplate
* @return all NoticeTemplates in the database that
* match the specified search criteria
*/
public List findByExample(NoticeTemplate noticeTemplate);
/**
* <p>Returns all NoticeTemplates in the database that
* match the specified search criteria.</p>
*
* @param propertyName the name of the specified property
* @param propertyValue the search value for the specified
* property
* @return all NoticeTemplates in the database that
* match the specified search criteria
*/
public List findByProperty(String propertyName, Object propertyValue);
/**
* <p>Returns the NoticeTemplate with the specified id.</p>
*
* @param id the id of the requested noticeTemplate
* @return the NoticeTemplate with the specified id
*/
public NoticeTemplate findById(int id);
/**
* <p>Returns the NoticeTemplate with the specified name and context.</p>
*
* @param context the context of the requested noticeTemplate
* @param name the name of the requested noticeTemplate
* @return the NoticeTemplate with the specified id
*/
public NoticeTemplate findByContextAndName(String context, String name);
/**
* <p>Saves the NoticeTemplate passed.</p>
*
* @param noticeTemplate the noticeTemplate to save
*/
public void save(NoticeTemplate NoticeTemplate);
/**
* <p>Deletes the NoticeTemplate with the specified id.</p>
*
* @param id the id of the noticeTemplate to delete
*/
public void delete(int id);
}
And of course, the Hibernate implementation of the DAO should also be pretty familiar stuff:
package org.restafarian.notify.dao.hibernate;
import java.util.List;
import org.restafarian.notify.beans.NoticeTemplate;
import org.restafarian.notify.dao.NoticeTemplateDao;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* <p>This is the NoticeTemplate data access object.</p>
*/
public class NoticeTemplateDaoHibernate extends HibernateDaoSupport
implements NoticeTemplateDao {
/**
* <p>Returns all NoticeTemplates in the database.</p>
*
* @return all NoticeTemplates in the database
*/
public List findAll() {
return getHibernateTemplate().find("from NoticeTemplate order by context, name");
}
/**
* <p>Returns all NoticeTemplates in the database that
* match the specified search criteria.</p>
*
* @param noticeTemplate an example NoticeTemplate
* @return all NoticeTemplates in the database that
* match the specified search criteria
*/
public List findByExample(NoticeTemplate noticeTemplate) {
return getHibernateTemplate().findByExample(noticeTemplate);
}
/**
* <p>Returns all NoticeTemplates in the database that
* match the specified search criteria.</p>
*
* @param propertyName the name of the specified property
* @param propertyValue the search value for the specified
* property
* @return all NoticeTemplates in the database that
* match the specified search criteria
*/
public List findByProperty(String propertyName, Object propertyValue) {
StringBuffer buffer = new StringBuffer();
buffer.append("from NoticeTemplate where ");
buffer.append(propertyName);
buffer.append(" = '");
buffer.append(propertyValue);
buffer.append("' order by context, name");
return getHibernateTemplate().find(buffer.toString());
}
/**
* <p>Returns the NoticeTemplate with the specified id.</p>
*
* @param id the id of the requested noticeTemplate
* @return the NoticeTemplate with the specified id
*/
public NoticeTemplate findById(int id) {
NoticeTemplate noticeTemplate = (NoticeTemplate)
getHibernateTemplate().get(NoticeTemplate.class, new Integer(id));
if (noticeTemplate == null) {
throw new ObjectRetrievalFailureException(NoticeTemplate.class, new Integer(id));
}
return noticeTemplate;
}
/**
* <p>Returns the NoticeTemplate with the specified name and context.</p>
*
* @param context the context of the requested noticeTemplate
* @param name the name of the requested noticeTemplate
* @return the NoticeTemplate with the specified id
*/
public NoticeTemplate findByContextAndName(String context, String name) {
NoticeTemplate noticeTemplate = new NoticeTemplate();
noticeTemplate.setName(name);
noticeTemplate.setContext(context);
List noticeTemplates = getHibernateTemplate().findByExample(noticeTemplate);
if (noticeTemplates != null && noticeTemplates.size() > 0) {
noticeTemplate = (NoticeTemplate) noticeTemplates.get(0);
} else {
throw new ObjectRetrievalFailureException(NoticeTemplate.class, context + "/" + name);
}
return noticeTemplate;
}
/**
* <p>Saves the NoticeTemplate passed.</p>
*
* @param noticeTemplate the noticeTemplate to save
*/
public void save(NoticeTemplate noticeTemplate) {
getHibernateTemplate().saveOrUpdate(noticeTemplate);
}
/**
* <p>Deletes the NoticeTemplate with the specified id.</p>
*
* @param id the id of the noticeTemplate to delete
*/
public void delete(int id) {
getHibernateTemplate().delete(findById(id));
}
}
Last, but not least, and theoretically should actually be first if you are truly following TDD to the letter, is the JUnit test that makes sure that it all works:
package org.restafarian.notify.dao;
import java.util.Date;
import java.util.List;
import org.restafarian.notify.beans.NoticeTemplate;
import org.springframework.dao.DataAccessException;
public class NoticeTemplateDaoTest extends BaseDaoTestCase {
private NoticeTemplate noticeTemplate = null;
private NoticeTemplateDao dao = null;
public void setNoticeTemplateDao(NoticeTemplateDao noticeTemplateDao) {
dao = noticeTemplateDao;
}
public void testInsertNoticeTemplate() {
Date rightNow = new Date();
String userId = "NoticeTemplateDaoTest";
noticeTemplate = new NoticeTemplate();
noticeTemplate.setName("testtemplate1");
noticeTemplate.setContext("testtemplates");
noticeTemplate.setDescription("This is a test template.");
noticeTemplate.setContentType("text/plain");
noticeTemplate.setDefaultDeliveryMethod("email");
noticeTemplate.setBody("This is a test template.");
noticeTemplate.setCreationDate(rightNow);
noticeTemplate.setCreatedBy(userId);
noticeTemplate.setLastUpdate(rightNow);
noticeTemplate.setLastUpdateBy(userId);
dao.save(noticeTemplate);
assertTrue(dao.findAll().size() >= 1);
}
public void testInsertAnotherNoticeTemplate() {
Date rightNow = new Date();
String userId = "NoticeTemplateDaoTest";
noticeTemplate = new NoticeTemplate();
noticeTemplate.setName("testtemplate2");
noticeTemplate.setContext("testtemplates");
noticeTemplate.setDescription("This is another test template.");
noticeTemplate.setContentType("text/hmtl");
noticeTemplate.setDefaultDeliveryMethod("email");
noticeTemplate.setBody("<html><body><p>This is a test template.</p><body></html>");
noticeTemplate.setCreationDate(rightNow);
noticeTemplate.setCreatedBy(userId);
noticeTemplate.setLastUpdate(rightNow);
noticeTemplate.setLastUpdateBy(userId);
dao.save(noticeTemplate);
assertTrue(dao.findAll().size() >= 2);
}
public void testFindByExample() throws Exception {
// find testtemplate1
noticeTemplate = new NoticeTemplate();
noticeTemplate.setContext("testtemplates");
noticeTemplate.setName("testtemplate1");
List noticeTemplates = dao.findByExample(noticeTemplate);
assertTrue(noticeTemplates != null && noticeTemplates.size() > 0);
// find testtemplate2
noticeTemplate = new NoticeTemplate();
noticeTemplate.setContext("testtemplates");
noticeTemplate.setName("testtemplate2");
noticeTemplates = dao.findByExample(noticeTemplate);
assertTrue(noticeTemplates != null && noticeTemplates.size() > 0);
}
public void testFindByProperty() throws Exception {
// find testtemplate1
List noticeTemplates = dao.findByProperty("name", "testtemplate1");
assertTrue(noticeTemplates != null && noticeTemplates.size() > 0);
// find testtemplate2
noticeTemplates = dao.findByProperty("name", "testtemplate2");
assertTrue(noticeTemplates != null && noticeTemplates.size() > 0);
}
public void testFindByNameAndContext() throws Exception {
// find testtemplate1
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate1");
assertTrue(noticeTemplate != null);
// find testtemplate2
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate2");
assertTrue(noticeTemplate != null);
}
public void testRemoveNoticeTemplates() throws Exception {
// remove testtemplate1
log.debug("removing noticeTemplate testtemplate1 ...");
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate1");
dao.delete(noticeTemplate.getId());
endTransaction();
// verify removal
try {
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate1");
fail("Deleted NoticeTemplate found in database");
} catch (DataAccessException dae) {
log.debug("Expected exception: " + dae.getMessage());
assertNotNull(dae);
}
// remove testtemplate2
log.debug("removing noticeTemplate testtemplate2 ...");
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate2");
dao.delete(noticeTemplate.getId());
endTransaction();
// verify removal
try {
noticeTemplate = dao.findByContextAndName("testtemplates", "testtemplate2");
fail("Deleted NoticeTemplate found in database");
} catch (DataAccessException dae) {
log.debug("Expected exception: " + dae.getMessage());
assertNotNull(dae);
}
}
}
Of course, you’ll need a database to make this all works (along with some Spring configuration stuff). Here’s my schema for MySQL:
CREATE TABLE example.noticetemplate ( id int(11) NOT NULL auto_increment, context varchar(100) default NULL, name varchar(100) default NULL, description varchar(255) default NULL, contentType varchar(32) default NULL, defaultDeliveryMethod varchar(32) default NULL, titleSubject varchar(255) default NULL, body text default NULL, creationDate datetime default NULL, createdBy varchar(32) default NULL, lastUpdate datetime default NULL, lastUpdateBy varchar(32) default NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
That should just about do it for data access layer. Next time I think we’ll take on the service layer, although for this project, I think I am going rename that as the “manager” layer, since the whole point of this process is to implement a “service”, and I don’t want to create any confusion when we get to that point.
One Response to ' Notification Service: NoticeTemplate DAO '
Leave a reply
You must be logged in to post a comment.






on November 3rd, 2008 at 10:13 am
Stop it with the HibernateDaoSupport!
See: http://raykrueger.blogspot.com/2007/09/best-abstracthibernatedao-ever.html
With the finished product at:
http://raykrueger.blogspot.com/2007/09/adding-generics-to-abstracthibernatedao.html