Hibernate definitions. Note that this can be used to generate the database schema in any of the supported databases automatically.
\n<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE hibernate-mapping PUBLIC\n "-//Hibernate/Hibernate Mapping DTD//EN"\n "[link|http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd|http://hibernate.sou...e-mapping-2.0.dtd]">\n\n<!-- $Id: iwethey.hbm.xml 17 2004-04-28 22:13:29Z anderson $ -->\n<hibernate-mapping auto-import="true">\n\n\t<class name="org.iwethey.domain.User" table="iwethey_user" lazy="true">\n\t\t<cache usage="read-write"/>\n\n\t\t<id name="id" column="user_id" unsaved-value="0">\n\t\t\t<generator class="sequence">\n\t\t\t\t<param name="sequence">seq_user</param>\n\t\t\t</generator>\n\t\t</id>\n\t\n\t\t<property name="nickname"/>\n\t\t<property name="encryptedPassword" column="password"/>\n\t\t<property name="created" type="timestamp" insert="false"/> <!-- update="false"/> -->\n\t\t<property name="lastPresent" column="last_present" type="timestamp" insert="false"/>\n\n\t\t<map name="properties" table="user_property" cascade="all" lazy="true">\n\t\t\t<cache usage="read-write"/>\n\t\t\t<key column="user_id"/>\n\t\t\t<index column="name" type="string"/>\n\t\t\t<element column="value" type="string"/>\n\t\t</map>\n\t</class>\n</hibernate-mapping>\n
Data interface class. Exists for the purpose of allowing swappable data sources (note the "implements UserManager"). Not strictly necessary as a separate object/interface if you are using only a single data source.
\npublic class HibUserManager extends HibernateSupportExtensions implements UserManager\n{\n\tpublic List getUserList()\n\t{\n\t\treturn getHibernateTemplate().loadAll(User.class);\n\t}\n\n\tpublic User getUserByNickname(String nickname)\n\t{\n\t\tList l = getHibernateTemplate().find("from User user where user.nickname = ?", nickname);\n\t\tif (l.size() == 1)\n\t\t\treturn (User) l.get(0);\n\t\treturn null;\n\t}\n\n\tpublic User getUserById(int id)\n\t{\n\t\ttry\n\t\t\t{\n\t\t\t\treturn (User) getHibernateTemplate().get(User.class, new Integer(id));\n\t\t\t}\n\t\tcatch (HibernateObjectRetrievalFailureException e)\n\t\t\t{\n\t\t\t\treturn null;\n\t\t\t}\n\t}\n\n\tpublic boolean checkLogin(User user)\n\t{\n\t\tList l = getHibernateTemplate().find(\n\t\t\t"from User user where user.nickname = ? and user.encryptedPassword = ?",\n\t\t\tnew Object[] { user.getNickname(), user.getEncryptedPassword() }\n\t\t\t);\n\t\treturn l.size() == 1;\n\t}\n\n\tpublic void saveUser(User user)\n\t{\n\t\tgetHibernateTemplate().saveOrUpdate(user);\n\t}\n\n\tpublic void saveUserAttributes(User user)\n\t\t{\n\t\t\tif (!user.isAnonymous())\n\t\t\t\tgetHibernateTemplate().saveOrUpdate(user);\n\t\t}\n\n\tpublic void removeUser(User user)\n\t\t{\n\t\t\tif (!user.isAnonymous())\n\t\t\t\tgetHibernateTemplate().delete(user);\n\t\t}\n\n\tpublic int getUserCount()\n\t\t{\n\t\t\treturn single("select count(*) from User user").intValue();\n\t\t}\n\n\tpublic int getActiveUserCount()\n\t\t{\n\t\t\treturn single("select count(*) from User user where (CURRENT_TIMESTAMP - last_present) < interval '10 minutes'").intValue();\n\t\t}\n\n\tpublic boolean getExists(String nickname)\n\t\t{\n\t\t\treturn single("select count(*) from User user where nickname = ?", nickname).intValue() > 0;\n\t\t}\n\n\tpublic boolean getExists(User user)\n\t\t{\n\t\t\treturn getExists(user.getNickname());\n\t\t}\n}\n
User object. Serves as a form backing object as well as a data object. Supports an arbitrary number of properties, any type that can be represented as a string. I've omitted some convenience methods (getPropertyAsBoolean, etc.) that aren't necessary for the purpose of proving the point. This class can also be used in anonymous mode before the user has logged in, allowing preference settings that persist for the session only. Note that other OO languages do not require nearly as much boiler-plate set/get cruft as Java.
\npublic class User implements Serializable\n{\n\tprivate int mId = 0;\n\tprivate String mNickname = null;\n\tprivate String mUnencryptedPassword = null;\n\tprivate String mEncryptedPassword = null;\n\tprivate String mPasswordCheck = null;\n\tprivate Date mCreated = null;\n\tprivate Date mLastPresent = null;\n\tprivate Map mProperties = null;\n\n\tpublic User() { }\n\n\tpublic User(User user)\n\t\t{\n\t\t\tsetFromUser(user);\n\t\t}\n\n\tpublic User(String nickname)\n\t\t{\n\t\t\tsetNickname(nickname);\n\t\t}\n\n\tpublic User(String nickname, String password)\n\t\t{\n\t\t\tthis(nickname);\n\t\t\tsetUnencryptedPassword(password);\n\t\t}\n\n\tpublic void setId(int id) { mId = id; }\n\tpublic int getId() { return mId; }\n\n\tpublic void setNickname(String nickname) { mNickname = nickname; }\n\tpublic String getNickname() { return mNickname; }\n\n\tpublic void setUnencryptedPassword(String password)\n\t\t{\n\t\t\tmUnencryptedPassword = password;\n\t\t\tif (password != null && !password.equals(""))\n\t\t\t\tmEncryptedPassword = encrypt(password);\n\t\t}\n\tpublic String getUnencryptedPassword() { return mUnencryptedPassword; }\n\n\tpublic void setEncryptedPassword(String password) { mEncryptedPassword = password; }\n\tpublic String getEncryptedPassword() { return mEncryptedPassword; }\n\n\tpublic void setPasswordCheck(String passwordCheck) { mPasswordCheck = passwordCheck; }\n\tpublic String getPasswordCheck() { return mPasswordCheck; }\n\n\tpublic void setCreated(Date created) { mCreated = created; }\n\tpublic Date getCreated() { return mCreated; }\n\n\tpublic void setLastPresent(Date lastPresent) { mLastPresent = lastPresent; }\n\tpublic Date getLastPresent() { return mLastPresent; }\n\n\tpublic void setProperty(String name, String value)\n\t\t{\n\t\t\tgetProperties().put(name, value);\n\t\t}\n\n\tpublic Map getProperties()\n\t\t{\n\t\t\tif (mProperties == null)\n\t\t\t\tmProperties = new HashMap();\n\n\t\t\treturn mProperties;\n\t\t}\n\n\tpublic void setProperties(Map props)\n\t\t{\n\t\t\tmProperties = props;\n\t\t}\n\n\tpublic String getProperty(String name, String defaultValue)\n\t\t{\n\t\t\tString val = (String) getProperties().get(name);\n\n\t\t\tif (val == null)\n\t\t\t\treturn defaultValue;\n\n\t\t\treturn val;\n\t\t}\n\n\tpublic void setFromUser(User user)\n\t\t{\n\t\t\tassert user != null;\n\n\t\t\tsetId(user.getId());\n\t\t\tsetNickname(user.getNickname());\n\t\t\tsetUnencryptedPassword(user.getUnencryptedPassword());\n\t\t\tsetEncryptedPassword(user.getEncryptedPassword());\n\t\t\tsetPasswordCheck(user.getPasswordCheck());\n\t\t\tsetCreated(user.getCreated());\n\t\t\tsetLastPresent(user.getLastPresent());\n\t\t\tsetProperties(user.getProperties());\n\t\t}\n\n\tpublic Object clone()\n\t\t{\n\t\t\treturn new User(this);\n\t\t}\n\n\tprivate static final char mHexChars[] =\n\t{\n\t\t'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'\n\t};\n\n\tpublic String encrypt(String buffer)\n\t\t{\n\t\t\tMessageDigest crypt = null;\n\n\t\t\ttry\n\t\t\t\t{\n\t\t\t\t\tcrypt = MessageDigest.getInstance("MD5");\n\t\t\t\t}\n\t\t\tcatch (NoSuchAlgorithmException e1)\n\t\t\t\t{\n\t\t\t\t\tLogFactory.getLog(getClass()).error("No MD5 implementation!");\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\tcrypt.update(buffer.getBytes());\n\n\t\t\tbyte[] bytes = crypt.digest();\n\n\t\t\tStringBuffer hex = new StringBuffer(2 * bytes.length);\n \n\t\t\tfor (int i = 0; i < bytes.length; i++)\n\t\t\t\t{\n\t\t\t\t\tbyte bch = bytes[i];\n\n\t\t\t\t\thex.append(mHexChars[(bch & 0xF0) >> 4]);\n\t\t\t\t\thex.append(mHexChars[bch & 0x0F]);\n\t\t\t\t}\n\t\t\n\t\t\treturn hex.toString();\n\t\t}\n\n\tpublic boolean isAnonymous()\n\t\t{\n\t\t\treturn mId == 0;\n\t\t}\n}\n
Omitted: Spring bean factory XML, as this is application dependent and not actually necessary to use the code. It adds things like declarative transaction boundaries, cache controls, and the like.
This is working code. If you don't believe me, go [link|http://lord.gregfolkert.net:8180/iwethey/main.iwt|here]. Note that there are few interface bugs in that version, since fixed.