1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchAccountException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.dao.DynamicQuery;
28 import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.OrderByComparator;
31 import com.liferay.portal.kernel.util.StringMaker;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.Account;
34 import com.liferay.portal.model.ModelListener;
35 import com.liferay.portal.model.impl.AccountImpl;
36 import com.liferay.portal.model.impl.AccountModelImpl;
37 import com.liferay.portal.spring.hibernate.FinderCache;
38 import com.liferay.portal.spring.hibernate.HibernateUtil;
39 import com.liferay.portal.util.PropsUtil;
40
41 import com.liferay.util.dao.hibernate.QueryUtil;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import org.hibernate.Query;
47 import org.hibernate.Session;
48
49 import java.util.Collections;
50 import java.util.Iterator;
51 import java.util.List;
52
53
59 public class AccountPersistenceImpl extends BasePersistence
60 implements AccountPersistence {
61 public Account create(long accountId) {
62 Account account = new AccountImpl();
63
64 account.setNew(true);
65 account.setPrimaryKey(accountId);
66
67 return account;
68 }
69
70 public Account remove(long accountId)
71 throws NoSuchAccountException, SystemException {
72 Session session = null;
73
74 try {
75 session = openSession();
76
77 Account account = (Account)session.get(AccountImpl.class,
78 new Long(accountId));
79
80 if (account == null) {
81 if (_log.isWarnEnabled()) {
82 _log.warn("No Account exists with the primary key " +
83 accountId);
84 }
85
86 throw new NoSuchAccountException(
87 "No Account exists with the primary key " + accountId);
88 }
89
90 return remove(account);
91 }
92 catch (NoSuchAccountException nsee) {
93 throw nsee;
94 }
95 catch (Exception e) {
96 throw HibernateUtil.processException(e);
97 }
98 finally {
99 closeSession(session);
100 }
101 }
102
103 public Account remove(Account account) throws SystemException {
104 ModelListener listener = _getListener();
105
106 if (listener != null) {
107 listener.onBeforeRemove(account);
108 }
109
110 account = removeImpl(account);
111
112 if (listener != null) {
113 listener.onAfterRemove(account);
114 }
115
116 return account;
117 }
118
119 protected Account removeImpl(Account account) throws SystemException {
120 Session session = null;
121
122 try {
123 session = openSession();
124
125 session.delete(account);
126
127 session.flush();
128
129 return account;
130 }
131 catch (Exception e) {
132 throw HibernateUtil.processException(e);
133 }
134 finally {
135 closeSession(session);
136
137 FinderCache.clearCache(Account.class.getName());
138 }
139 }
140
141 public Account update(Account account) throws SystemException {
142 return update(account, false);
143 }
144
145 public Account update(Account account, boolean merge)
146 throws SystemException {
147 ModelListener listener = _getListener();
148
149 boolean isNew = account.isNew();
150
151 if (listener != null) {
152 if (isNew) {
153 listener.onBeforeCreate(account);
154 }
155 else {
156 listener.onBeforeUpdate(account);
157 }
158 }
159
160 account = updateImpl(account, merge);
161
162 if (listener != null) {
163 if (isNew) {
164 listener.onAfterCreate(account);
165 }
166 else {
167 listener.onAfterUpdate(account);
168 }
169 }
170
171 return account;
172 }
173
174 public Account updateImpl(com.liferay.portal.model.Account account,
175 boolean merge) throws SystemException {
176 Session session = null;
177
178 try {
179 session = openSession();
180
181 if (merge) {
182 session.merge(account);
183 }
184 else {
185 if (account.isNew()) {
186 session.save(account);
187 }
188 }
189
190 session.flush();
191
192 account.setNew(false);
193
194 return account;
195 }
196 catch (Exception e) {
197 throw HibernateUtil.processException(e);
198 }
199 finally {
200 closeSession(session);
201
202 FinderCache.clearCache(Account.class.getName());
203 }
204 }
205
206 public Account findByPrimaryKey(long accountId)
207 throws NoSuchAccountException, SystemException {
208 Account account = fetchByPrimaryKey(accountId);
209
210 if (account == null) {
211 if (_log.isWarnEnabled()) {
212 _log.warn("No Account exists with the primary key " +
213 accountId);
214 }
215
216 throw new NoSuchAccountException(
217 "No Account exists with the primary key " + accountId);
218 }
219
220 return account;
221 }
222
223 public Account fetchByPrimaryKey(long accountId) throws SystemException {
224 Session session = null;
225
226 try {
227 session = openSession();
228
229 return (Account)session.get(AccountImpl.class, new Long(accountId));
230 }
231 catch (Exception e) {
232 throw HibernateUtil.processException(e);
233 }
234 finally {
235 closeSession(session);
236 }
237 }
238
239 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
240 throws SystemException {
241 Session session = null;
242
243 try {
244 session = openSession();
245
246 DynamicQuery query = queryInitializer.initialize(session);
247
248 return query.list();
249 }
250 catch (Exception e) {
251 throw HibernateUtil.processException(e);
252 }
253 finally {
254 closeSession(session);
255 }
256 }
257
258 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
259 int begin, int end) throws SystemException {
260 Session session = null;
261
262 try {
263 session = openSession();
264
265 DynamicQuery query = queryInitializer.initialize(session);
266
267 query.setLimit(begin, end);
268
269 return query.list();
270 }
271 catch (Exception e) {
272 throw HibernateUtil.processException(e);
273 }
274 finally {
275 closeSession(session);
276 }
277 }
278
279 public List findAll() throws SystemException {
280 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
281 }
282
283 public List findAll(int begin, int end) throws SystemException {
284 return findAll(begin, end, null);
285 }
286
287 public List findAll(int begin, int end, OrderByComparator obc)
288 throws SystemException {
289 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
290 String finderClassName = Account.class.getName();
291 String finderMethodName = "findAll";
292 String[] finderParams = new String[] {
293 "java.lang.Integer", "java.lang.Integer",
294 "com.liferay.portal.kernel.util.OrderByComparator"
295 };
296 Object[] finderArgs = new Object[] {
297 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
298 };
299
300 Object result = null;
301
302 if (finderClassNameCacheEnabled) {
303 result = FinderCache.getResult(finderClassName, finderMethodName,
304 finderParams, finderArgs, getSessionFactory());
305 }
306
307 if (result == null) {
308 Session session = null;
309
310 try {
311 session = openSession();
312
313 StringMaker query = new StringMaker();
314
315 query.append("FROM com.liferay.portal.model.Account ");
316
317 if (obc != null) {
318 query.append("ORDER BY ");
319 query.append(obc.getOrderBy());
320 }
321
322 Query q = session.createQuery(query.toString());
323
324 List list = QueryUtil.list(q, getDialect(), begin, end);
325
326 if (obc == null) {
327 Collections.sort(list);
328 }
329
330 FinderCache.putResult(finderClassNameCacheEnabled,
331 finderClassName, finderMethodName, finderParams,
332 finderArgs, list);
333
334 return list;
335 }
336 catch (Exception e) {
337 throw HibernateUtil.processException(e);
338 }
339 finally {
340 closeSession(session);
341 }
342 }
343 else {
344 return (List)result;
345 }
346 }
347
348 public void removeAll() throws SystemException {
349 Iterator itr = findAll().iterator();
350
351 while (itr.hasNext()) {
352 remove((Account)itr.next());
353 }
354 }
355
356 public int countAll() throws SystemException {
357 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
358 String finderClassName = Account.class.getName();
359 String finderMethodName = "countAll";
360 String[] finderParams = new String[] { };
361 Object[] finderArgs = new Object[] { };
362
363 Object result = null;
364
365 if (finderClassNameCacheEnabled) {
366 result = FinderCache.getResult(finderClassName, finderMethodName,
367 finderParams, finderArgs, getSessionFactory());
368 }
369
370 if (result == null) {
371 Session session = null;
372
373 try {
374 session = openSession();
375
376 Query q = session.createQuery(
377 "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
378
379 Long count = null;
380
381 Iterator itr = q.list().iterator();
382
383 if (itr.hasNext()) {
384 count = (Long)itr.next();
385 }
386
387 if (count == null) {
388 count = new Long(0);
389 }
390
391 FinderCache.putResult(finderClassNameCacheEnabled,
392 finderClassName, finderMethodName, finderParams,
393 finderArgs, count);
394
395 return count.intValue();
396 }
397 catch (Exception e) {
398 throw HibernateUtil.processException(e);
399 }
400 finally {
401 closeSession(session);
402 }
403 }
404 else {
405 return ((Long)result).intValue();
406 }
407 }
408
409 protected void initDao() {
410 }
411
412 private static ModelListener _getListener() {
413 if (Validator.isNotNull(_LISTENER)) {
414 try {
415 return (ModelListener)Class.forName(_LISTENER).newInstance();
416 }
417 catch (Exception e) {
418 _log.error(e);
419 }
420 }
421
422 return null;
423 }
424
425 private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
426 "value.object.listener.com.liferay.portal.model.Account"));
427 private static Log _log = LogFactory.getLog(AccountPersistenceImpl.class);
428 }