1
22
23 package com.liferay.portlet.tags.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.bean.InitializingBean;
27 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29 import com.liferay.portal.kernel.dao.orm.Query;
30 import com.liferay.portal.kernel.dao.orm.QueryUtil;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.util.GetterUtil;
33 import com.liferay.portal.kernel.util.ListUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringUtil;
36 import com.liferay.portal.model.ModelListener;
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, InitializingBean {
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 session.delete(tagsSource);
128
129 session.flush();
130
131 return tagsSource;
132 }
133 catch (Exception e) {
134 throw processException(e);
135 }
136 finally {
137 closeSession(session);
138
139 FinderCacheUtil.clearCache(TagsSource.class.getName());
140 }
141 }
142
143
146 public TagsSource update(TagsSource tagsSource) throws SystemException {
147 if (_log.isWarnEnabled()) {
148 _log.warn(
149 "Using the deprecated update(TagsSource tagsSource) method. Use update(TagsSource tagsSource, boolean merge) instead.");
150 }
151
152 return update(tagsSource, false);
153 }
154
155
168 public TagsSource update(TagsSource tagsSource, boolean merge)
169 throws SystemException {
170 boolean isNew = tagsSource.isNew();
171
172 if (_listeners.length > 0) {
173 for (ModelListener listener : _listeners) {
174 if (isNew) {
175 listener.onBeforeCreate(tagsSource);
176 }
177 else {
178 listener.onBeforeUpdate(tagsSource);
179 }
180 }
181 }
182
183 tagsSource = updateImpl(tagsSource, merge);
184
185 if (_listeners.length > 0) {
186 for (ModelListener listener : _listeners) {
187 if (isNew) {
188 listener.onAfterCreate(tagsSource);
189 }
190 else {
191 listener.onAfterUpdate(tagsSource);
192 }
193 }
194 }
195
196 return tagsSource;
197 }
198
199 public TagsSource updateImpl(
200 com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
201 throws SystemException {
202 Session session = null;
203
204 try {
205 session = openSession();
206
207 if (merge) {
208 session.merge(tagsSource);
209 }
210 else {
211 if (tagsSource.isNew()) {
212 session.save(tagsSource);
213 }
214 }
215
216 session.flush();
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 = (List<TagsSource>)QueryUtil.list(q,
354 getDialect(), start, end);
355
356 if (obc == null) {
357 Collections.sort(list);
358 }
359
360 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
361 finderClassName, finderMethodName, finderParams,
362 finderArgs, list);
363
364 return list;
365 }
366 catch (Exception e) {
367 throw processException(e);
368 }
369 finally {
370 closeSession(session);
371 }
372 }
373 else {
374 return (List<TagsSource>)result;
375 }
376 }
377
378 public void removeAll() throws SystemException {
379 for (TagsSource tagsSource : findAll()) {
380 remove(tagsSource);
381 }
382 }
383
384 public int countAll() throws SystemException {
385 boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
386 String finderClassName = TagsSource.class.getName();
387 String finderMethodName = "countAll";
388 String[] finderParams = new String[] { };
389 Object[] finderArgs = new Object[] { };
390
391 Object result = null;
392
393 if (finderClassNameCacheEnabled) {
394 result = FinderCacheUtil.getResult(finderClassName,
395 finderMethodName, finderParams, finderArgs, this);
396 }
397
398 if (result == null) {
399 Session session = null;
400
401 try {
402 session = openSession();
403
404 Query q = session.createQuery(
405 "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
406
407 Long count = null;
408
409 Iterator<Long> itr = q.list().iterator();
410
411 if (itr.hasNext()) {
412 count = itr.next();
413 }
414
415 if (count == null) {
416 count = new Long(0);
417 }
418
419 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
420 finderClassName, finderMethodName, finderParams,
421 finderArgs, count);
422
423 return count.intValue();
424 }
425 catch (Exception e) {
426 throw processException(e);
427 }
428 finally {
429 closeSession(session);
430 }
431 }
432 else {
433 return ((Long)result).intValue();
434 }
435 }
436
437 public void registerListener(ModelListener listener) {
438 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
439
440 listeners.add(listener);
441
442 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
443 }
444
445 public void unregisterListener(ModelListener listener) {
446 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
447
448 listeners.remove(listener);
449
450 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
451 }
452
453 public void afterPropertiesSet() {
454 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
455 com.liferay.portal.util.PropsUtil.get(
456 "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
457
458 if (listenerClassNames.length > 0) {
459 try {
460 List<ModelListener> listeners = new ArrayList<ModelListener>();
461
462 for (String listenerClassName : listenerClassNames) {
463 listeners.add((ModelListener)Class.forName(
464 listenerClassName).newInstance());
465 }
466
467 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
468 }
469 catch (Exception e) {
470 _log.error(e);
471 }
472 }
473 }
474
475 private static Log _log = LogFactory.getLog(TagsSourcePersistenceImpl.class);
476 private ModelListener[] _listeners = new ModelListener[0];
477 }