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.bean.InitializingBean;
28 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30 import com.liferay.portal.kernel.dao.orm.Query;
31 import com.liferay.portal.kernel.dao.orm.QueryUtil;
32 import com.liferay.portal.kernel.dao.orm.Session;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.ListUtil;
35 import com.liferay.portal.kernel.util.OrderByComparator;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.model.Account;
38 import com.liferay.portal.model.ModelListener;
39 import com.liferay.portal.model.impl.AccountImpl;
40 import com.liferay.portal.model.impl.AccountModelImpl;
41 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Iterator;
49 import java.util.List;
50
51
57 public class AccountPersistenceImpl extends BasePersistenceImpl
58 implements AccountPersistence, InitializingBean {
59 public Account create(long accountId) {
60 Account account = new AccountImpl();
61
62 account.setNew(true);
63 account.setPrimaryKey(accountId);
64
65 return account;
66 }
67
68 public Account remove(long accountId)
69 throws NoSuchAccountException, SystemException {
70 Session session = null;
71
72 try {
73 session = openSession();
74
75 Account account = (Account)session.get(AccountImpl.class,
76 new Long(accountId));
77
78 if (account == null) {
79 if (_log.isWarnEnabled()) {
80 _log.warn("No Account exists with the primary key " +
81 accountId);
82 }
83
84 throw new NoSuchAccountException(
85 "No Account exists with the primary key " + accountId);
86 }
87
88 return remove(account);
89 }
90 catch (NoSuchAccountException nsee) {
91 throw nsee;
92 }
93 catch (Exception e) {
94 throw processException(e);
95 }
96 finally {
97 closeSession(session);
98 }
99 }
100
101 public Account remove(Account account) throws SystemException {
102 if (_listeners.length > 0) {
103 for (ModelListener listener : _listeners) {
104 listener.onBeforeRemove(account);
105 }
106 }
107
108 account = removeImpl(account);
109
110 if (_listeners.length > 0) {
111 for (ModelListener listener : _listeners) {
112 listener.onAfterRemove(account);
113 }
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 processException(e);
133 }
134 finally {
135 closeSession(session);
136
137 FinderCacheUtil.clearCache(Account.class.getName());
138 }
139 }
140
141
144 public Account update(Account account) throws SystemException {
145 if (_log.isWarnEnabled()) {
146 _log.warn(
147 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
148 }
149
150 return update(account, false);
151 }
152
153
166 public Account update(Account account, boolean merge)
167 throws SystemException {
168 boolean isNew = account.isNew();
169
170 if (_listeners.length > 0) {
171 for (ModelListener listener : _listeners) {
172 if (isNew) {
173 listener.onBeforeCreate(account);
174 }
175 else {
176 listener.onBeforeUpdate(account);
177 }
178 }
179 }
180
181 account = updateImpl(account, merge);
182
183 if (_listeners.length > 0) {
184 for (ModelListener listener : _listeners) {
185 if (isNew) {
186 listener.onAfterCreate(account);
187 }
188 else {
189 listener.onAfterUpdate(account);
190 }
191 }
192 }
193
194 return account;
195 }
196
197 public Account updateImpl(com.liferay.portal.model.Account account,
198 boolean merge) throws SystemException {
199 Session session = null;
200
201 try {
202 session = openSession();
203
204 if (merge) {
205 session.merge(account);
206 }
207 else {
208 if (account.isNew()) {
209 session.save(account);
210 }
211 }
212
213 session.flush();
214
215 account.setNew(false);
216
217 return account;
218 }
219 catch (Exception e) {
220 throw processException(e);
221 }
222 finally {
223 closeSession(session);
224
225 FinderCacheUtil.clearCache(Account.class.getName());
226 }
227 }
228
229 public Account findByPrimaryKey(long accountId)
230 throws NoSuchAccountException, SystemException {
231 Account account = fetchByPrimaryKey(accountId);
232
233 if (account == null) {
234 if (_log.isWarnEnabled()) {
235 _log.warn("No Account exists with the primary key " +
236 accountId);
237 }
238
239 throw new NoSuchAccountException(
240 "No Account exists with the primary key " + accountId);
241 }
242
243 return account;
244 }
245
246 public Account fetchByPrimaryKey(long accountId) throws SystemException {
247 Session session = null;
248
249 try {
250 session = openSession();
251
252 return (Account)session.get(AccountImpl.class, new Long(accountId));
253 }
254 catch (Exception e) {
255 throw processException(e);
256 }
257 finally {
258 closeSession(session);
259 }
260 }
261
262 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
263 throws SystemException {
264 Session session = null;
265
266 try {
267 session = openSession();
268
269 dynamicQuery.compile(session);
270
271 return dynamicQuery.list();
272 }
273 catch (Exception e) {
274 throw processException(e);
275 }
276 finally {
277 closeSession(session);
278 }
279 }
280
281 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
282 int start, int end) throws SystemException {
283 Session session = null;
284
285 try {
286 session = openSession();
287
288 dynamicQuery.setLimit(start, end);
289
290 dynamicQuery.compile(session);
291
292 return dynamicQuery.list();
293 }
294 catch (Exception e) {
295 throw processException(e);
296 }
297 finally {
298 closeSession(session);
299 }
300 }
301
302 public List<Account> findAll() throws SystemException {
303 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
304 }
305
306 public List<Account> findAll(int start, int end) throws SystemException {
307 return findAll(start, end, null);
308 }
309
310 public List<Account> findAll(int start, int end, OrderByComparator obc)
311 throws SystemException {
312 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
313 String finderClassName = Account.class.getName();
314 String finderMethodName = "findAll";
315 String[] finderParams = new String[] {
316 "java.lang.Integer", "java.lang.Integer",
317 "com.liferay.portal.kernel.util.OrderByComparator"
318 };
319 Object[] finderArgs = new Object[] {
320 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
321 };
322
323 Object result = null;
324
325 if (finderClassNameCacheEnabled) {
326 result = FinderCacheUtil.getResult(finderClassName,
327 finderMethodName, finderParams, finderArgs, this);
328 }
329
330 if (result == null) {
331 Session session = null;
332
333 try {
334 session = openSession();
335
336 StringBuilder query = new StringBuilder();
337
338 query.append("FROM com.liferay.portal.model.Account ");
339
340 if (obc != null) {
341 query.append("ORDER BY ");
342 query.append(obc.getOrderBy());
343 }
344
345 Query q = session.createQuery(query.toString());
346
347 List<Account> list = (List<Account>)QueryUtil.list(q,
348 getDialect(), start, end);
349
350 if (obc == null) {
351 Collections.sort(list);
352 }
353
354 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
355 finderClassName, finderMethodName, finderParams,
356 finderArgs, list);
357
358 return list;
359 }
360 catch (Exception e) {
361 throw processException(e);
362 }
363 finally {
364 closeSession(session);
365 }
366 }
367 else {
368 return (List<Account>)result;
369 }
370 }
371
372 public void removeAll() throws SystemException {
373 for (Account account : findAll()) {
374 remove(account);
375 }
376 }
377
378 public int countAll() throws SystemException {
379 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
380 String finderClassName = Account.class.getName();
381 String finderMethodName = "countAll";
382 String[] finderParams = new String[] { };
383 Object[] finderArgs = new Object[] { };
384
385 Object result = null;
386
387 if (finderClassNameCacheEnabled) {
388 result = FinderCacheUtil.getResult(finderClassName,
389 finderMethodName, finderParams, finderArgs, this);
390 }
391
392 if (result == null) {
393 Session session = null;
394
395 try {
396 session = openSession();
397
398 Query q = session.createQuery(
399 "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
400
401 Long count = null;
402
403 Iterator<Long> itr = q.list().iterator();
404
405 if (itr.hasNext()) {
406 count = itr.next();
407 }
408
409 if (count == null) {
410 count = new Long(0);
411 }
412
413 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
414 finderClassName, finderMethodName, finderParams,
415 finderArgs, count);
416
417 return count.intValue();
418 }
419 catch (Exception e) {
420 throw processException(e);
421 }
422 finally {
423 closeSession(session);
424 }
425 }
426 else {
427 return ((Long)result).intValue();
428 }
429 }
430
431 public void registerListener(ModelListener listener) {
432 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
433
434 listeners.add(listener);
435
436 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
437 }
438
439 public void unregisterListener(ModelListener listener) {
440 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
441
442 listeners.remove(listener);
443
444 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
445 }
446
447 public void afterPropertiesSet() {
448 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
449 com.liferay.portal.util.PropsUtil.get(
450 "value.object.listener.com.liferay.portal.model.Account")));
451
452 if (listenerClassNames.length > 0) {
453 try {
454 List<ModelListener> listeners = new ArrayList<ModelListener>();
455
456 for (String listenerClassName : listenerClassNames) {
457 listeners.add((ModelListener)Class.forName(
458 listenerClassName).newInstance());
459 }
460
461 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
462 }
463 catch (Exception e) {
464 _log.error(e);
465 }
466 }
467 }
468
469 private static Log _log = LogFactory.getLog(AccountPersistenceImpl.class);
470 private ModelListener[] _listeners = new ModelListener[0];
471 }