Update of /cvsroot/struts/struts-resume/src/ejb/org/appfuse/persistence In directory sc8-pr-cvs1:/tmp/cvs-serv28776 Added Files: BaseDAOHibernate.java BaseObject.java DAOException.java DAOFactory.java DAOFactoryHibernate.java Education.java Resume.java ResumeDAO.java ResumeDAOHibernate.java Role.java ServiceLocator.java Skill.java SkillGroup.java User.java UserDAO.java UserDAOHibernate.java Log Message: --- NEW FILE: BaseDAOHibernate.java --- package org.appfuse.persistence; import cirrus.hibernate.Session; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Base class for Hibernate DAOs. This class defines common CRUD methods for * child classes to inherit. * * <p> * <a href="BaseDAOHibernate.java.html"><i>View Source</i></a> * </p> */ public class BaseDAOHibernate { //~ Instance fields ======================================================== /** The <code>Log</code> instance for this package. */ private Log log = LogFactory.getLog(BaseDAOHibernate.class); //~ Methods ================================================================ /** * */ protected void removeObject(Class clazz, String id, Object obj) throws DAOException { Session ses = null; try { ses = ServiceLocator.currentSession(); // Object must originate in this session obj = ses.load(clazz, id); ses.delete(obj); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); } ; throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } ; } } /** * */ protected void removeObject(Class clazz, Long id, Object obj) throws DAOException { Session ses = null; try { ses = ServiceLocator.currentSession(); // Object must originate in this session obj = ses.load(clazz, id); ses.delete(obj); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); } ; throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } ; } } /** * */ protected Object retrieveObject(Class clazz, String id) throws DAOException { Object obj = null; Session ses = null; try { ses = ServiceLocator.currentSession(); obj = ses.load(clazz, id); } catch (Exception e) { throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } ; } return obj; } /** * */ protected Object retrieveObject(Class clazz, Long id) throws DAOException { Object obj = null; Session ses = null; try { ses = ServiceLocator.currentSession(); obj = ses.load(clazz, id); } catch (Exception e) { throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } ; } return obj; } /** * */ protected void storeObject(Object obj) throws DAOException { Session ses = null; try { ses = ServiceLocator.currentSession(); ses.saveOrUpdate(obj); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); } ; throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } ; } } } --- NEW FILE: BaseObject.java --- package org.appfuse.persistence; import java.io.Serializable; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Base class for Hibernate objects. This is basically for the toString method * at this time, but may be extended to override equals. * * <p> * <a href="BaseDAOHibernate.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible * @version $Revision: 1.1 $ $Date: 2003/01/08 05:52:35 $ */ public class BaseObject implements Serializable { //~ Methods ================================================================ public String toString() { StringBuffer results = new StringBuffer(); Class clazz = getClass(); results.append(getClass().getName() + "\n"); Field[] fields = clazz.getDeclaredFields(); try { AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { results.append("\t" + fields[i].getName() + "=" + fields[i].get(this) + "\n"); } } catch (Exception e) { // ignored! } return results.toString(); } public boolean equals(Object o) { return EqualsBuilder.reflectionEquals(this, o); } public int hashCode(Object o) { return HashCodeBuilder.reflectionHashCode(this); } } --- NEW FILE: DAOException.java --- package org.appfuse.persistence; /** * A general DAOException that is thrown by all DAO classes. * * <p> * <a href="DAOException.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible */ public class DAOException extends Exception { //~ Constructors =========================================================== /** * Constructor for DAOException. */ public DAOException() { super(); } /** * Constructor for DAOException. * * @param message */ public DAOException(String message) { super(message); } /** * Constructor for DAOException. * * @param message * @param cause */ public DAOException(String message, Throwable cause) { super(message, cause); } /** * Constructor for DAOException. * * @param cause */ public DAOException(Throwable cause) { super(cause); } } --- NEW FILE: DAOFactory.java --- package org.appfuse.persistence; import org.appfuse.common.Constants; /** * This class is responsible for creating DAOs. * * <p> * <a href="DAOException.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible */ public abstract class DAOFactory { //~ Static fields/initializers ============================================= private static DAOFactory factory; //~ Methods ================================================================ // Factory method to create a factory instance. public static DAOFactory createFactory(String daoType) throws DAOException { // Create the factory if it doesn't already exist. if (factory == null) { // Hardcoded selection logic. Can be made more // flexible by "unhardcoding" the selection logic. if (daoType.equalsIgnoreCase(Constants.DAO_TYPE_HIBERNATE)) { factory = DAOFactoryHibernate.createFactory(); } else { // Bad daoType value, throw exception. throw new DAOException("Bad 'daoType' value in web.xml."); } } // Return the factory. return factory; } public abstract UserDAO createUserDAO() throws DAOException; public abstract ResumeDAO createResumeDAO() throws DAOException; } --- NEW FILE: DAOFactoryHibernate.java --- package org.appfuse.persistence; /** * This class creates Hibernate DAOs. * * <p> * <a href="DAOException.java.html"><i>View Source</i></a> * </p> */ public class DAOFactoryHibernate extends DAOFactory { //~ Methods ================================================================ /** * Factory method for creating Hibernate DAOs * * @return new factory */ public static DAOFactoryHibernate createFactory() { DAOFactoryHibernate factory = new DAOFactoryHibernate(); return factory; } public UserDAO createUserDAO() throws DAOException { return new UserDAOHibernate(); } public ResumeDAO createResumeDAO() throws DAOException { return new ResumeDAOHibernate(); } } --- NEW FILE: Education.java --- package org.appfuse.persistence; /** * Education class * * This class is used to represent a User's Education * on their resume * * @author Matt Raible * @created 22 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="education" * */ public class Education { private String id; private String name; private String location; private String resumeId; private String degree; private String gpa; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public String getId() { return id; } /** * Returns the name. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="name" type="string" not-null="true" unique="false" */ public String getName() { return name; } /** * Returns the location. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="location" type="string" not-null="false" unique="false" */ public String getLocation() { return location; } /** * Returns the parent resume. * @return String resumeId * * @hibernate.property not-null="true" */ public String getResumeId() { return resumeId; } /** * Sets the location. * @param location The location to set */ public void setLocation(String location) { this.location = location; } /** * Sets the id. * @param id The id to set */ public void setId(String id) { this.id = id; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Sets the user. * @param user The user to set */ public void setResumeId(String resumeId) { this.resumeId = resumeId; } /** * Returns the degree. * @return String * * @hibernate.property column="degree" */ public String getDegree() { return degree; } /** * Returns the gpa. * @return String * * @hibernate.property column="gpa" */ public String getGpa() { return gpa; } /** * Sets the degree. * @param degree The degree to set */ public void setDegree(String degree) { this.degree = degree; } /** * Sets the gpa. * @param gpa The gpa to set */ public void setGpa(String gpa) { this.gpa = gpa; } } --- NEW FILE: Resume.java --- package org.appfuse.persistence; /** * Resume class * * This class is used to represent a Resume object * * <p> * <a href="Resume.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible * @created 22 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="resume" * */ public class Resume extends BaseObject { private Long id; private String name; private String description; private String objective; private Long userId; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public Long getId() { return id; } /** * Returns the name. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="name" type="string" not-null="true" unique="false" */ public String getName() { return name; } /** * Returns the description. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="description" type="string" not-null="false" unique="false" */ public String getDescription() { return description; } /** * Returns the objective. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="objective" type="string" not-null="false" unique="false" */ public String getObjective() { return objective; } /** * Returns the user. * @return User * * @hibernate.property column="user_id" not-null="true" */ public Long getUserId() { return userId; } /** * Sets the description. * @param description The description to set */ public void setDescription(String description) { this.description = description; } /** * Sets the id. * @param id The id to set */ public void setId(Long id) { this.id = id; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Sets the objective. * @param objective The objective to set */ public void setObjective(String objective) { this.objective = objective; } /** * Sets the user. * @param user The user to set */ public void setUserId(Long userId) { this.userId = userId; } } --- NEW FILE: ResumeDAO.java --- package org.appfuse.persistence; import java.util.List; /** * User Data Access Object (DAO) interface. */ public interface ResumeDAO { //~ Methods ================================================================ /** * Gets resume by name, create new resume if necessary. * * @param userId the user's id * @throws Exception */ public List getResumesByUserId(Long userId) throws DAOException; /** * Gets a resume based on a resume's id * * @param resumeId * @return Resume * @throws Exception */ public Resume getResume(Long resumeId) throws Exception; /** * Saves a resume's information. This method is used for * both adding and for saving a resume object. * * @param resume the resume persistable object * @throws Exception */ public Resume saveResume(Resume resume) throws Exception; /** * Deletes a resume. * * @param resume * @throws Exception */ public void removeResume(Resume resume) throws Exception; } --- NEW FILE: ResumeDAOHibernate.java --- package org.appfuse.persistence; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; import cirrus.hibernate.MappingException; import cirrus.hibernate.Session; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; public class ResumeDAOHibernate extends BaseDAOHibernate implements ResumeDAO { //~ Instance fields ======================================================== private Log log = LogFactory.getLog(ResumeDAOHibernate.class); //~ Methods ================================================================ /** * Get resume by name, create new one if necessary. * * @param userId the current user's id * @return a list of the user's resumes * @throws DAOException */ public List getResumesByUserId(Long userId) throws DAOException { Session ses = null; List resumes = null; try { ses = ServiceLocator.currentSession(); resumes = (List) ses.find("from r in class org.appfuse.persistence.Resume where r.userId=? order by upper(r.name)", userId, Hibernate.LONG); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); } throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } } return resumes; } /** * @see org.appfuse.persistence.ResumeDAO#getResume(String) */ public Resume getResume(Long id) throws DAOException { return (Resume) retrieveObject(org.appfuse.persistence.Resume.class, id); } /** * @see org.appfuse.persistence.ResumeDAO#saveResume(Resume) */ public Resume saveResume(Resume resume) throws DAOException { storeObject(resume); return (Resume) retrieveObject(org.appfuse.persistence.Resume.class, resume.getId()); } /** * @see org.appfuse.persistence.ResumeDAO#removeResume(Resume) */ public void removeResume(Resume resume) throws DAOException { removeObject(org.appfuse.persistence.Resume.class, resume.getId(), resume); } } --- NEW FILE: Role.java --- package org.appfuse.persistence; /** * Role class used to determine roles for a user * * @author Matt Raible * @created 29 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="user_role" * */ public class Role extends BaseObject { // XDoclet's Hibernate module does not generate composite keys // yet, so we have to add an id to this table - it's never used. private Long id; private String username; private String rolename; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public Long getId() { return id; } /** * Sets the id. * @param id The id to set */ public void setId(Long id) { this.id = id; } /** * Returns the username. * @return String * * @struts.validator type="required" msgkey="errors.required" * @struts.validator type="email" msgkey="errors.email" * @hibernate.property column="username" * type="string" not-null="true" unique="false" */ public String getUsername() { return username; } /** * Sets the username. * @param username The username to set */ public void setUsername(String username) { this.username = username; } /** * Returns the rolename. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="role_name" type="string" not-null="true" unique="false" */ public String getRolename() { return rolename; } /** * Sets the rolename. * @param rolename The rolename to set */ public void setRolename(String rolename) { this.rolename = rolename; } } --- NEW FILE: ServiceLocator.java --- package org.appfuse.persistence; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.CommunicationException; import org.appfuse.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import cirrus.hibernate.Datastore; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; import cirrus.hibernate.Session; import cirrus.hibernate.SessionFactory; /** * This class is used to get Hibernate Sessions and may * also contain methods (in the future) to get DBConnections * or Transactions from JNDI. */ public class ServiceLocator { public static final ThreadLocal session = new ThreadLocal(); private static Log log = LogFactory.getLog(ServiceLocator.class); public static Session currentSession() throws HibernateException, SQLException { Session s = (Session) session.get(); if (s == null) { SessionFactory sf = null; // Try to lookup a JNDI Connection try { sf = (SessionFactory) new InitialContext().lookup(Constants.SESSION_FACTORY); } catch (NamingException ne) { if (log.isDebugEnabled()) { log.warn("error communicating with JNDI, assuming testcase"); } Datastore datastore = Hibernate.createDatastore(); /* If you don't want to specify full class names here and in your DAO's, add the following to your hibernate.properties file: hibernate.query.imports=org.appfuse.persistence */ datastore.storeClass(org.appfuse.persistence.Education.class); datastore.storeClass(org.appfuse.persistence.Resume.class); datastore.storeClass(org.appfuse.persistence.Role.class); datastore.storeClass(org.appfuse.persistence.Skill.class); datastore.storeClass(org.appfuse.persistence.SkillGroup.class); datastore.storeClass(org.appfuse.persistence.User.class); sf = datastore.buildSessionFactory(); } s = sf.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException, SQLException { Session s = (Session) session.get(); session.set(null); if (s != null) { s.close(); } } } --- NEW FILE: Skill.java --- package org.appfuse.persistence; /** * Skill class * * This class is used to represent a User's Skill * on their resume * * @author Matt Raible * @created 22 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="skill" * */ public class Skill extends BaseObject { private String id; private String name; private String description; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public String getId() { return id; } /** * Returns the name. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="name" type="string" not-null="true" unique="false" */ public String getName() { return name; } /** * Sets the id. * @param id The id to set */ public void setId(String id) { this.id = id; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Returns the description. * @return String * * @hibernate.property * column="name" type="string" not-null="false" unique="false" */ public String getDescription() { return description; } /** * Sets the description. * @param description The description to set */ public void setDescription(String description) { this.description = description; } } --- NEW FILE: SkillGroup.java --- package org.appfuse.persistence; /** * SkillGroup class * * This class is used to represent a User's SkillGroup * on their resume * * @author Matt Raible * @created 22 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="skill_group" * */ public class SkillGroup extends BaseObject { private String id; private String name; private String description; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public String getId() { return id; } /** * Returns the name. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="name" type="string" not-null="true" unique="false" */ public String getName() { return name; } /** * Sets the id. * @param id The id to set */ public void setId(String id) { this.id = id; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Returns the description. * @return String * * @hibernate.property * column="name" type="string" not-null="false" unique="false" */ public String getDescription() { return description; } /** * Sets the description. * @param description The description to set */ public void setDescription(String description) { this.description = description; } } --- NEW FILE: User.java --- package org.appfuse.persistence; import java.util.List; /** * User class * * This class is used to generate the Struts Validator Form * as well as the Hibernate persistence later. * * <p> * <a href="User.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible * @created 08 December 2002 * * @struts.form include-all="true" * extends="org.appfuse.webapp.form.BaseForm" * * @hibernate.class table="app_user" * */ public class User extends BaseObject { private Long id; private String username; private String password; private String firstName; private String lastName; private String address; private String city; private String province; private String country; private String postalCode; private String phoneNumber; private String email; private String website; private List roles; private List resumes; /** * Returns the id. * @return String * * @hibernate.id column="id" type="long" * generator-class="native" unsaved-value="null" */ public Long getId() { return id; } /** * Returns the username. * @return String * * @struts.validator type="required" msgkey="errors.required" * @struts.validator type="email" msgkey="errors.email" * @hibernate.property * column="username" type="string" not-null="true" unique="true" */ public String getUsername() { return username; } /** * Returns the firstName. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="firstName" type="string" not-null="true" unique="false" */ public String getFirstName() { return firstName; } /** * Returns the lastName. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="lastName" type="string" not-null="true" unique="false" */ public String getLastName() { return lastName; } /** * Returns the address. * @return String * * @hibernate.property * column="address" type="string" not-null="false" unique="false" */ public String getAddress() { return address; } /** * Returns the city. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="city" type="string" not-null="true" unique="false" */ public String getCity() { return city; } /** * Returns the country. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="country" type="string" not-null="true" unique="false" */ public String getCountry() { return country; } /** * Returns the email. This is an optional field for specifying a * different e-mail than the username. * @return String * * * @struts.validator type="email" msgkey="errors.email" * @hibernate.property * column="email" type="string" not-null="false" unique="true" */ public String getEmail() { return email; } /** * Returns the phoneNumber. * @return String * * @struts.validator type="mask" msgkey="errors.phone" * @struts.validator-var name="mask" value="${phone}" * @hibernate.property * column="phoneNumber" type="string" not-null="false" unique="false" */ public String getPhoneNumber() { return phoneNumber; } /** * Returns the postalCode. * @return String * * @struts.validator type="required" msgkey="errors.required" * @struts.validator type="mask" msgkey="errors.zip" * @struts.validator-var name="mask" value="${zip}" * @hibernate.property * column="postalCode" type="string" not-null="true" unique="false" */ public String getPostalCode() { return postalCode; } /** * Returns the province. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="province" type="string" not-null="true" unique="false" */ public String getProvince() { return province; } /** * Returns the website. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="website" type="string" not-null="false" unique="false" */ public String getWebsite() { return website; } /** * Returns the password. * @return String * * @struts.validator type="required" msgkey="errors.required" * @hibernate.property * column="password" type="string" not-null="true" unique="false" */ public String getPassword() { return password; } /** * Returns the user's roles. * @return List * * @hibernate.bag role="roles" table="user_role" readonly="true" * @hibernate.collection-key column="id" * @hibernate.collection-one-to-many class="org.appfuse.persistence.Role" */ public List getRoles() { return roles; } /** * Returns the resumes. * @return List * * @hibernate.bag role="resumes" table="resume" readonly="true" * @hibernate.collection-key column="id" length="32" * @hibernate.collection-one-to-many class="org.appfuse.persistence.Resume" */ public List getResumes() { return resumes; } /** * Adds a role for the user * @param role */ public void addRole(Role role) { role.setUsername(getUsername()); getRoles().add(role); } /** * Adds a resume for the user * @param resume */ public void addResume(Resume resume) { resume.setUserId(getId()); getResumes().add(resume); } /** * Lists the id. * @param id The id to set */ public void setId(Long id) { this.id = id; } /** * Lists the address. * @param address The address to set */ public void setAddress(String address) { this.address = address; } /** * Lists the city. * @param city The city to set */ public void setCity(String city) { this.city = city; } /** * Lists the country. * @param country The country to set */ public void setCountry(String country) { this.country = country; } /** * Lists the email. * @param email The email to set */ public void setEmail(String email) { this.email = email; } /** * Lists the firstName. * @param firstName The firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * Lists the lastName. * @param lastName The lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * Lists the password. * @param password The password to set */ public void setPassword(String password) { this.password = password; } /** * Lists the phoneNumber. * @param phoneNumber The phoneNumber to set */ public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } /** * Lists the postalCode. * @param postalCode The postalCode to set */ public void setPostalCode(String postalCode) { this.postalCode = postalCode; } /** * Lists the province. * @param province The province to set */ public void setProvince(String province) { this.province = province; } /** * Lists the resumes. * @param resumes The resumes to set */ public void setResumes(List resumes) { this.resumes = resumes; } /** * Lists the username. * @param username The username to set */ public void setUsername(String username) { this.username = username; } /** * Lists the website. * @param website The website to set */ public void setWebsite(String website) { this.website = website; } /** * Lists the roles. * @param roles The roles to set */ public void setRoles(List roles) { this.roles = roles; } } --- NEW FILE: UserDAO.java --- package org.appfuse.persistence; /** * User Data Access Object (DAO) interface. * * <p> * <a href="UserDAO.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible */ public interface UserDAO { //~ Methods ================================================================ /** * Gets user by name, create new user if necessary. * * @return DOCUMENT ME! */ public User getUser(String userName) throws DAOException; /** * Saves a user's information * * @param user the user persistable object * * @return DOCUMENT ME! */ public User saveUser(User user) throws Exception; /** * Removes a user from the database by id */ public void removeUser(User user) throws Exception; /** * Adds a user's resume */ public Resume addResume(Resume resume) throws Exception; /** * Deletes a user's resume */ public void removeResume(Resume resume) throws Exception; /** * Adds a role */ public Role addRole(Role role) throws Exception; /** * Removes a role */ public void removeRole(Role role) throws Exception; } --- NEW FILE: UserDAOHibernate.java --- package org.appfuse.persistence; import cirrus.hibernate.Hibernate; import cirrus.hibernate.Session; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; /** * This class interacts with Hibernate's Session object to save and retrieve * Resume objects. * * <p> * <a href="UserDAOHibernate.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible */ public class UserDAOHibernate extends BaseDAOHibernate implements UserDAO { //~ Instance fields ======================================================== private Log log = LogFactory.getLog(UserDAOHibernate.class); //~ Methods ================================================================ /** * Get user by name, create new one if necessary. * * @param username the user's username * @return a populated user object * @throws DAOException if anything goes wrong */ public User getUser(String username) throws DAOException { User user = null; Session ses = null; try { ses = ServiceLocator.currentSession(); List users = (List) ses.find("from user in class org.appfuse.persistence.User where user.username=?", username, Hibernate.STRING); if ((users != null) && (users.size() > 0)) { user = (User) users.get(0); } if (user == null) { log.warn("uh oh, user not found..."); throw new DAOException("User '" + username + "' not found in database!"); } user = (User) ses.load(User.class, user.getId()); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); } throw new DAOException(e); } finally { try { ServiceLocator.closeSession(); } catch (Exception ex) { ex.printStackTrace(); } } return user; } public User saveUser(User user) throws DAOException { storeObject(user); return (User) retrieveObject(org.appfuse.persistence.User.class, user.getId()); } public void removeUser(User user) throws DAOException { removeObject(org.appfuse.persistence.User.class, user.getId(), user); } /** * @see org.appfuse.persistence.UserDAO#addResume(Resume) */ public Resume addResume(Resume resume) throws DAOException { storeObject(resume); return (Resume) retrieveObject(org.appfuse.persistence.Resume.class, resume.getId()); } /** * @see org.appfuse.persistence.UserDAO#removeResume(Resume) */ public void removeResume(Resume resume) throws DAOException { removeObject(org.appfuse.persistence.Resume.class, resume.getId(), resume); } /** * @see org.appfuse.persistence.UserDAO#addRole(Role) */ public Role addRole(Role role) throws Exception { storeObject(role); return (Role) retrieveObject(org.appfuse.persistence.Role.class, role.getId()); } /** * @see org.appfuse.persistence.UserDAO#removeRole(Role) */ public void removeRole(Role role) throws Exception { removeObject(org.appfuse.persistence.Role.class, role.getId(), role); } } |