001 /*
002 * $Id: KatUserDao.java,v 1.4 2005/02/09 08:27:10 mitch Exp $
003 *
004 * Copyright (c) 2004, FullSpan Software (www.fullspan.com)
005 *
006 * Licensed under the BSD License
007 * OSI Certified Open Source Software (www.opensource.org)
008 *
009 * You may not use this file except in compliance with the License. You should
010 * have received a copy of the License with this distribution, or you can find
011 * it at: http://www.fullspan.com/shared/license.html.
012 *
013 * NO WARRANTY - USE AT YOUR OWN RISK. All software and other materials
014 * distributed under the License are provided on an "AS IS" BASIS, WITHOUT
015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 */
017 package com.fullspan.kat.db.dao;
018
019 import java.util.*;
020
021 import net.sf.hibernate.*;
022
023 import com.fullspan.kat.db.*;
024 import com.fullspan.kat.domain.entity.KatUser;
025 import com.fullspan.kat.exception.*;
026 import com.fullspan.util.*;
027
028 /**
029 *
030 * @author Mitch Stuart
031 */
032 public class KatUserDao
033 {
034 // Static variables
035
036 private static final KatUserDao s_theDao;
037
038 // Static initializer
039
040 static
041 {
042 s_theDao = new KatUserDao();
043 }
044
045 // Static methods
046
047 /**
048 * Package private method. Clients should call
049 * KatDao.getUserDao().
050 *
051 */
052 static KatUserDao getDao()
053 {
054 return s_theDao;
055 }
056
057 // Constructor
058
059 /**
060 * Private constructor, clients should call getService.
061 */
062 private KatUserDao()
063 {
064 }
065
066 // Instance methods
067
068 public KatUser get(KatDbSessContext dbCtx, Long id)
069 {
070 return get(dbCtx, id, false, 0);
071 }
072
073 public KatUser getForUpdate(KatDbSessContext dbCtx, Long id,
074 long updateVersion)
075 {
076 return get(dbCtx, id, true, updateVersion);
077 }
078
079 public KatUser get(KatDbSessContext dbCtx, Long id, boolean forUpdate,
080 long updateVersion)
081 {
082 try
083 {
084 if (forUpdate)
085 {
086 KatUser user = (KatUser)
087 (dbCtx.getSession().load(KatUser.class, id, LockMode.UPGRADE));
088
089 if (updateVersion != user.getUpdateVersion())
090 {
091 throw new KatConcurrentModificationException();
092 }
093
094 return user;
095 }
096 else
097 {
098 return (KatUser) (dbCtx.getSession().load(KatUser.class, id));
099 }
100 }
101 catch (HibernateException e)
102 {
103 throw new KatDataAccessException(
104 ExceptionUtil.getExceptionMessage(e), e);
105 }
106 }
107
108 public KatUser getByLoginid(KatDbSessContext dbCtx, String loginid)
109 {
110 try
111 {
112 Query query = dbCtx.getSession().getNamedQuery("GetUserByLoginid");
113 query.setString("loginid", loginid);
114 return (KatUser) query.uniqueResult();
115 }
116 catch (HibernateException e)
117 {
118 throw new KatDataAccessException(
119 ExceptionUtil.getExceptionMessage(e), e);
120 }
121 }
122
123 public List getList(KatDbSessContext dbCtx)
124 {
125 try
126 {
127 Query query = dbCtx.getSession().getNamedQuery("GetUsers");
128 return query.list();
129 }
130 catch (HibernateException e)
131 {
132 throw new KatDataAccessException(
133 ExceptionUtil.getExceptionMessage(e), e);
134 }
135 }
136
137 public Long getUseridByLoginid(KatDbSessContext dbCtx, String loginid)
138 {
139 try
140 {
141 Query query = dbCtx.getSession().getNamedQuery("GetUseridByLoginid");
142 query.setString("loginid", loginid);
143 return (Long) query.uniqueResult();
144 }
145 catch (HibernateException e)
146 {
147 throw new KatDataAccessException(
148 ExceptionUtil.getExceptionMessage(e), e);
149 }
150 }
151
152 public KatUser save(KatDbSessContext dbCtx, KatUser user)
153 {
154 try
155 {
156 if (user.getIsNew())
157 {
158 dbCtx.getSession().save(user);
159 }
160 else
161 {
162 dbCtx.getSession().update(user);
163 // Proactively flush instead of waiting for commit - so we can
164 // report errors on the edit page
165 dbCtx.getSession().flush();
166 }
167
168 return user;
169 }
170 catch (HibernateException e)
171 {
172 KatDuplicateKeyException dupEx = getPossibleDuplicateKeyException(e);
173
174 if (dupEx != null)
175 {
176 throw dupEx;
177 }
178 else
179 {
180 throw new KatDataAccessException(
181 ExceptionUtil.getExceptionMessage(e), e);
182 }
183 }
184 }
185
186 protected KatDuplicateKeyException getPossibleDuplicateKeyException(
187 HibernateException hibEx)
188 {
189 String hibMsg = ExceptionUtil.getExceptionMessage(hibEx);
190
191 if (!hibMsg.startsWith("Duplicate key"))
192 {
193 return null;
194 }
195
196 String fieldName = null;
197
198 if (hibMsg.endsWith("for key 2\""))
199 {
200 fieldName = "loginid";
201 }
202 else if (hibMsg.endsWith("for key 3\""))
203 {
204 fieldName = "email";
205 }
206
207 if (fieldName == null)
208 {
209 return null;
210 }
211 else
212 {
213 return new KatDuplicateKeyException(hibMsg, fieldName);
214 }
215 }
216
217 public void delete(KatDbSessContext dbCtx, Long id)
218 {
219 try
220 {
221 dbCtx.getSession().delete("from KatUser where id = ?",
222 id, Hibernate.LONG);
223 }
224 catch (HibernateException e)
225 {
226 throw new KatDataAccessException(
227 ExceptionUtil.getExceptionMessage(e), e);
228 }
229 }
230
231 }