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