1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.tags.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
22  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
23  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
24  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
25  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.FinderPath;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.SQLQuery;
34  import com.liferay.portal.kernel.dao.orm.Session;
35  import com.liferay.portal.kernel.dao.orm.Type;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.OrderByComparator;
40  import com.liferay.portal.kernel.util.SetUtil;
41  import com.liferay.portal.kernel.util.StringBundler;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.model.ModelListener;
46  import com.liferay.portal.service.persistence.BatchSessionUtil;
47  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
48  
49  import com.liferay.portlet.tags.NoSuchEntryException;
50  import com.liferay.portlet.tags.model.TagsEntry;
51  import com.liferay.portlet.tags.model.impl.TagsEntryImpl;
52  import com.liferay.portlet.tags.model.impl.TagsEntryModelImpl;
53  
54  import java.io.Serializable;
55  
56  import java.sql.Types;
57  
58  import java.util.ArrayList;
59  import java.util.Collections;
60  import java.util.List;
61  import java.util.Set;
62  
63  /**
64   * <a href="TagsEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * <p>
67   * ServiceBuilder generated this class. Modifications in this class will be
68   * overwritten the next time is generated.
69   * </p>
70   *
71   * @author    Brian Wing Shun Chan
72   * @see       TagsEntryPersistence
73   * @see       TagsEntryUtil
74   * @generated
75   */
76  public class TagsEntryPersistenceImpl extends BasePersistenceImpl<TagsEntry>
77      implements TagsEntryPersistence {
78      public static final String FINDER_CLASS_NAME_ENTITY = TagsEntryImpl.class.getName();
79      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
80          ".List";
81      public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
82              TagsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
83              "fetchByC_N",
84              new String[] { Long.class.getName(), String.class.getName() });
85      public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
86              TagsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
87              "countByC_N",
88              new String[] { Long.class.getName(), String.class.getName() });
89      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
90              TagsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
91              "findAll", new String[0]);
92      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
93              TagsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
94              "countAll", new String[0]);
95  
96      public void cacheResult(TagsEntry tagsEntry) {
97          EntityCacheUtil.putResult(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
98              TagsEntryImpl.class, tagsEntry.getPrimaryKey(), tagsEntry);
99  
100         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
101             new Object[] { new Long(tagsEntry.getCompanyId()), tagsEntry.getName() },
102             tagsEntry);
103     }
104 
105     public void cacheResult(List<TagsEntry> tagsEntries) {
106         for (TagsEntry tagsEntry : tagsEntries) {
107             if (EntityCacheUtil.getResult(
108                         TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
109                         TagsEntryImpl.class, tagsEntry.getPrimaryKey(), this) == null) {
110                 cacheResult(tagsEntry);
111             }
112         }
113     }
114 
115     public void clearCache() {
116         CacheRegistry.clear(TagsEntryImpl.class.getName());
117         EntityCacheUtil.clearCache(TagsEntryImpl.class.getName());
118         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
119         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
120     }
121 
122     public TagsEntry create(long entryId) {
123         TagsEntry tagsEntry = new TagsEntryImpl();
124 
125         tagsEntry.setNew(true);
126         tagsEntry.setPrimaryKey(entryId);
127 
128         return tagsEntry;
129     }
130 
131     public TagsEntry remove(Serializable primaryKey)
132         throws NoSuchModelException, SystemException {
133         return remove(((Long)primaryKey).longValue());
134     }
135 
136     public TagsEntry remove(long entryId)
137         throws NoSuchEntryException, SystemException {
138         Session session = null;
139 
140         try {
141             session = openSession();
142 
143             TagsEntry tagsEntry = (TagsEntry)session.get(TagsEntryImpl.class,
144                     new Long(entryId));
145 
146             if (tagsEntry == null) {
147                 if (_log.isWarnEnabled()) {
148                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + entryId);
149                 }
150 
151                 throw new NoSuchEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
152                     entryId);
153             }
154 
155             return remove(tagsEntry);
156         }
157         catch (NoSuchEntryException nsee) {
158             throw nsee;
159         }
160         catch (Exception e) {
161             throw processException(e);
162         }
163         finally {
164             closeSession(session);
165         }
166     }
167 
168     public TagsEntry remove(TagsEntry tagsEntry) throws SystemException {
169         for (ModelListener<TagsEntry> listener : listeners) {
170             listener.onBeforeRemove(tagsEntry);
171         }
172 
173         tagsEntry = removeImpl(tagsEntry);
174 
175         for (ModelListener<TagsEntry> listener : listeners) {
176             listener.onAfterRemove(tagsEntry);
177         }
178 
179         return tagsEntry;
180     }
181 
182     protected TagsEntry removeImpl(TagsEntry tagsEntry)
183         throws SystemException {
184         tagsEntry = toUnwrappedModel(tagsEntry);
185 
186         try {
187             clearTagsAssets.clear(tagsEntry.getPrimaryKey());
188         }
189         catch (Exception e) {
190             throw processException(e);
191         }
192         finally {
193             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
194         }
195 
196         Session session = null;
197 
198         try {
199             session = openSession();
200 
201             if (tagsEntry.isCachedModel() || BatchSessionUtil.isEnabled()) {
202                 Object staleObject = session.get(TagsEntryImpl.class,
203                         tagsEntry.getPrimaryKeyObj());
204 
205                 if (staleObject != null) {
206                     session.evict(staleObject);
207                 }
208             }
209 
210             session.delete(tagsEntry);
211 
212             session.flush();
213         }
214         catch (Exception e) {
215             throw processException(e);
216         }
217         finally {
218             closeSession(session);
219         }
220 
221         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
222 
223         TagsEntryModelImpl tagsEntryModelImpl = (TagsEntryModelImpl)tagsEntry;
224 
225         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
226             new Object[] {
227                 new Long(tagsEntryModelImpl.getOriginalCompanyId()),
228                 
229             tagsEntryModelImpl.getOriginalName()
230             });
231 
232         EntityCacheUtil.removeResult(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
233             TagsEntryImpl.class, tagsEntry.getPrimaryKey());
234 
235         return tagsEntry;
236     }
237 
238     /**
239      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
240      */
241     public TagsEntry update(TagsEntry tagsEntry) throws SystemException {
242         if (_log.isWarnEnabled()) {
243             _log.warn(
244                 "Using the deprecated update(TagsEntry tagsEntry) method. Use update(TagsEntry tagsEntry, boolean merge) instead.");
245         }
246 
247         return update(tagsEntry, false);
248     }
249 
250     public TagsEntry updateImpl(
251         com.liferay.portlet.tags.model.TagsEntry tagsEntry, boolean merge)
252         throws SystemException {
253         tagsEntry = toUnwrappedModel(tagsEntry);
254 
255         boolean isNew = tagsEntry.isNew();
256 
257         TagsEntryModelImpl tagsEntryModelImpl = (TagsEntryModelImpl)tagsEntry;
258 
259         Session session = null;
260 
261         try {
262             session = openSession();
263 
264             BatchSessionUtil.update(session, tagsEntry, merge);
265 
266             tagsEntry.setNew(false);
267         }
268         catch (Exception e) {
269             throw processException(e);
270         }
271         finally {
272             closeSession(session);
273         }
274 
275         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
276 
277         EntityCacheUtil.putResult(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
278             TagsEntryImpl.class, tagsEntry.getPrimaryKey(), tagsEntry);
279 
280         if (!isNew &&
281                 ((tagsEntry.getCompanyId() != tagsEntryModelImpl.getOriginalCompanyId()) ||
282                 !Validator.equals(tagsEntry.getName(),
283                     tagsEntryModelImpl.getOriginalName()))) {
284             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
285                 new Object[] {
286                     new Long(tagsEntryModelImpl.getOriginalCompanyId()),
287                     
288                 tagsEntryModelImpl.getOriginalName()
289                 });
290         }
291 
292         if (isNew ||
293                 ((tagsEntry.getCompanyId() != tagsEntryModelImpl.getOriginalCompanyId()) ||
294                 !Validator.equals(tagsEntry.getName(),
295                     tagsEntryModelImpl.getOriginalName()))) {
296             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
297                 new Object[] {
298                     new Long(tagsEntry.getCompanyId()),
299                     
300                 tagsEntry.getName()
301                 }, tagsEntry);
302         }
303 
304         return tagsEntry;
305     }
306 
307     protected TagsEntry toUnwrappedModel(TagsEntry tagsEntry) {
308         if (tagsEntry instanceof TagsEntryImpl) {
309             return tagsEntry;
310         }
311 
312         TagsEntryImpl tagsEntryImpl = new TagsEntryImpl();
313 
314         tagsEntryImpl.setNew(tagsEntry.isNew());
315         tagsEntryImpl.setPrimaryKey(tagsEntry.getPrimaryKey());
316 
317         tagsEntryImpl.setEntryId(tagsEntry.getEntryId());
318         tagsEntryImpl.setCompanyId(tagsEntry.getCompanyId());
319         tagsEntryImpl.setUserId(tagsEntry.getUserId());
320         tagsEntryImpl.setUserName(tagsEntry.getUserName());
321         tagsEntryImpl.setCreateDate(tagsEntry.getCreateDate());
322         tagsEntryImpl.setModifiedDate(tagsEntry.getModifiedDate());
323         tagsEntryImpl.setName(tagsEntry.getName());
324 
325         return tagsEntryImpl;
326     }
327 
328     public TagsEntry findByPrimaryKey(Serializable primaryKey)
329         throws NoSuchModelException, SystemException {
330         return findByPrimaryKey(((Long)primaryKey).longValue());
331     }
332 
333     public TagsEntry findByPrimaryKey(long entryId)
334         throws NoSuchEntryException, SystemException {
335         TagsEntry tagsEntry = fetchByPrimaryKey(entryId);
336 
337         if (tagsEntry == null) {
338             if (_log.isWarnEnabled()) {
339                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + entryId);
340             }
341 
342             throw new NoSuchEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
343                 entryId);
344         }
345 
346         return tagsEntry;
347     }
348 
349     public TagsEntry fetchByPrimaryKey(Serializable primaryKey)
350         throws SystemException {
351         return fetchByPrimaryKey(((Long)primaryKey).longValue());
352     }
353 
354     public TagsEntry fetchByPrimaryKey(long entryId) throws SystemException {
355         TagsEntry tagsEntry = (TagsEntry)EntityCacheUtil.getResult(TagsEntryModelImpl.ENTITY_CACHE_ENABLED,
356                 TagsEntryImpl.class, entryId, this);
357 
358         if (tagsEntry == null) {
359             Session session = null;
360 
361             try {
362                 session = openSession();
363 
364                 tagsEntry = (TagsEntry)session.get(TagsEntryImpl.class,
365                         new Long(entryId));
366             }
367             catch (Exception e) {
368                 throw processException(e);
369             }
370             finally {
371                 if (tagsEntry != null) {
372                     cacheResult(tagsEntry);
373                 }
374 
375                 closeSession(session);
376             }
377         }
378 
379         return tagsEntry;
380     }
381 
382     public TagsEntry findByC_N(long companyId, String name)
383         throws NoSuchEntryException, SystemException {
384         TagsEntry tagsEntry = fetchByC_N(companyId, name);
385 
386         if (tagsEntry == null) {
387             StringBundler msg = new StringBundler(6);
388 
389             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
390 
391             msg.append("companyId=");
392             msg.append(companyId);
393 
394             msg.append(", name=");
395             msg.append(name);
396 
397             msg.append(StringPool.CLOSE_CURLY_BRACE);
398 
399             if (_log.isWarnEnabled()) {
400                 _log.warn(msg.toString());
401             }
402 
403             throw new NoSuchEntryException(msg.toString());
404         }
405 
406         return tagsEntry;
407     }
408 
409     public TagsEntry fetchByC_N(long companyId, String name)
410         throws SystemException {
411         return fetchByC_N(companyId, name, true);
412     }
413 
414     public TagsEntry fetchByC_N(long companyId, String name,
415         boolean retrieveFromCache) throws SystemException {
416         Object[] finderArgs = new Object[] { new Long(companyId), name };
417 
418         Object result = null;
419 
420         if (retrieveFromCache) {
421             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
422                     finderArgs, this);
423         }
424 
425         if (result == null) {
426             Session session = null;
427 
428             try {
429                 session = openSession();
430 
431                 StringBundler query = new StringBundler(4);
432 
433                 query.append(_SQL_SELECT_TAGSENTRY_WHERE);
434 
435                 query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
436 
437                 if (name == null) {
438                     query.append(_FINDER_COLUMN_C_N_NAME_1);
439                 }
440                 else {
441                     if (name.equals(StringPool.BLANK)) {
442                         query.append(_FINDER_COLUMN_C_N_NAME_3);
443                     }
444                     else {
445                         query.append(_FINDER_COLUMN_C_N_NAME_2);
446                     }
447                 }
448 
449                 query.append(TagsEntryModelImpl.ORDER_BY_JPQL);
450 
451                 String sql = query.toString();
452 
453                 Query q = session.createQuery(sql);
454 
455                 QueryPos qPos = QueryPos.getInstance(q);
456 
457                 qPos.add(companyId);
458 
459                 if (name != null) {
460                     qPos.add(name);
461                 }
462 
463                 List<TagsEntry> list = q.list();
464 
465                 result = list;
466 
467                 TagsEntry tagsEntry = null;
468 
469                 if (list.isEmpty()) {
470                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
471                         finderArgs, list);
472                 }
473                 else {
474                     tagsEntry = list.get(0);
475 
476                     cacheResult(tagsEntry);
477 
478                     if ((tagsEntry.getCompanyId() != companyId) ||
479                             (tagsEntry.getName() == null) ||
480                             !tagsEntry.getName().equals(name)) {
481                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
482                             finderArgs, tagsEntry);
483                     }
484                 }
485 
486                 return tagsEntry;
487             }
488             catch (Exception e) {
489                 throw processException(e);
490             }
491             finally {
492                 if (result == null) {
493                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
494                         finderArgs, new ArrayList<TagsEntry>());
495                 }
496 
497                 closeSession(session);
498             }
499         }
500         else {
501             if (result instanceof List<?>) {
502                 return null;
503             }
504             else {
505                 return (TagsEntry)result;
506             }
507         }
508     }
509 
510     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
511         throws SystemException {
512         Session session = null;
513 
514         try {
515             session = openSession();
516 
517             dynamicQuery.compile(session);
518 
519             return dynamicQuery.list();
520         }
521         catch (Exception e) {
522             throw processException(e);
523         }
524         finally {
525             closeSession(session);
526         }
527     }
528 
529     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
530         int start, int end) throws SystemException {
531         Session session = null;
532 
533         try {
534             session = openSession();
535 
536             dynamicQuery.setLimit(start, end);
537 
538             dynamicQuery.compile(session);
539 
540             return dynamicQuery.list();
541         }
542         catch (Exception e) {
543             throw processException(e);
544         }
545         finally {
546             closeSession(session);
547         }
548     }
549 
550     public List<TagsEntry> findAll() throws SystemException {
551         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
552     }
553 
554     public List<TagsEntry> findAll(int start, int end)
555         throws SystemException {
556         return findAll(start, end, null);
557     }
558 
559     public List<TagsEntry> findAll(int start, int end, OrderByComparator obc)
560         throws SystemException {
561         Object[] finderArgs = new Object[] {
562                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
563             };
564 
565         List<TagsEntry> list = (List<TagsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
566                 finderArgs, this);
567 
568         if (list == null) {
569             Session session = null;
570 
571             try {
572                 session = openSession();
573 
574                 StringBundler query = null;
575                 String sql = null;
576 
577                 if (obc != null) {
578                     query = new StringBundler(2 +
579                             (obc.getOrderByFields().length * 3));
580 
581                     query.append(_SQL_SELECT_TAGSENTRY);
582 
583                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
584 
585                     sql = query.toString();
586                 }
587 
588                 else {
589                     sql = _SQL_SELECT_TAGSENTRY.concat(TagsEntryModelImpl.ORDER_BY_JPQL);
590                 }
591 
592                 Query q = session.createQuery(sql);
593 
594                 if (obc == null) {
595                     list = (List<TagsEntry>)QueryUtil.list(q, getDialect(),
596                             start, end, false);
597 
598                     Collections.sort(list);
599                 }
600                 else {
601                     list = (List<TagsEntry>)QueryUtil.list(q, getDialect(),
602                             start, end);
603                 }
604             }
605             catch (Exception e) {
606                 throw processException(e);
607             }
608             finally {
609                 if (list == null) {
610                     list = new ArrayList<TagsEntry>();
611                 }
612 
613                 cacheResult(list);
614 
615                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
616 
617                 closeSession(session);
618             }
619         }
620 
621         return list;
622     }
623 
624     public void removeByC_N(long companyId, String name)
625         throws NoSuchEntryException, SystemException {
626         TagsEntry tagsEntry = findByC_N(companyId, name);
627 
628         remove(tagsEntry);
629     }
630 
631     public void removeAll() throws SystemException {
632         for (TagsEntry tagsEntry : findAll()) {
633             remove(tagsEntry);
634         }
635     }
636 
637     public int countByC_N(long companyId, String name)
638         throws SystemException {
639         Object[] finderArgs = new Object[] { new Long(companyId), name };
640 
641         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
642                 finderArgs, this);
643 
644         if (count == null) {
645             Session session = null;
646 
647             try {
648                 session = openSession();
649 
650                 StringBundler query = new StringBundler(3);
651 
652                 query.append(_SQL_COUNT_TAGSENTRY_WHERE);
653 
654                 query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
655 
656                 if (name == null) {
657                     query.append(_FINDER_COLUMN_C_N_NAME_1);
658                 }
659                 else {
660                     if (name.equals(StringPool.BLANK)) {
661                         query.append(_FINDER_COLUMN_C_N_NAME_3);
662                     }
663                     else {
664                         query.append(_FINDER_COLUMN_C_N_NAME_2);
665                     }
666                 }
667 
668                 String sql = query.toString();
669 
670                 Query q = session.createQuery(sql);
671 
672                 QueryPos qPos = QueryPos.getInstance(q);
673 
674                 qPos.add(companyId);
675 
676                 if (name != null) {
677                     qPos.add(name);
678                 }
679 
680                 count = (Long)q.uniqueResult();
681             }
682             catch (Exception e) {
683                 throw processException(e);
684             }
685             finally {
686                 if (count == null) {
687                     count = Long.valueOf(0);
688                 }
689 
690                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
691                     count);
692 
693                 closeSession(session);
694             }
695         }
696 
697         return count.intValue();
698     }
699 
700     public int countAll() throws SystemException {
701         Object[] finderArgs = new Object[0];
702 
703         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
704                 finderArgs, this);
705 
706         if (count == null) {
707             Session session = null;
708 
709             try {
710                 session = openSession();
711 
712                 Query q = session.createQuery(_SQL_COUNT_TAGSENTRY);
713 
714                 count = (Long)q.uniqueResult();
715             }
716             catch (Exception e) {
717                 throw processException(e);
718             }
719             finally {
720                 if (count == null) {
721                     count = Long.valueOf(0);
722                 }
723 
724                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
725                     count);
726 
727                 closeSession(session);
728             }
729         }
730 
731         return count.intValue();
732     }
733 
734     public List<com.liferay.portlet.tags.model.TagsAsset> getTagsAssets(long pk)
735         throws SystemException {
736         return getTagsAssets(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
737     }
738 
739     public List<com.liferay.portlet.tags.model.TagsAsset> getTagsAssets(
740         long pk, int start, int end) throws SystemException {
741         return getTagsAssets(pk, start, end, null);
742     }
743 
744     public static final FinderPath FINDER_PATH_GET_TAGSASSETS = new FinderPath(com.liferay.portlet.tags.model.impl.TagsAssetModelImpl.ENTITY_CACHE_ENABLED,
745             TagsEntryModelImpl.FINDER_CACHE_ENABLED_TAGSASSETS_TAGSENTRIES,
746             TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME,
747             "getTagsAssets",
748             new String[] {
749                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
750                 "com.liferay.portal.kernel.util.OrderByComparator"
751             });
752 
753     public List<com.liferay.portlet.tags.model.TagsAsset> getTagsAssets(
754         long pk, int start, int end, OrderByComparator obc)
755         throws SystemException {
756         Object[] finderArgs = new Object[] {
757                 new Long(pk), String.valueOf(start), String.valueOf(end),
758                 String.valueOf(obc)
759             };
760 
761         List<com.liferay.portlet.tags.model.TagsAsset> list = (List<com.liferay.portlet.tags.model.TagsAsset>)FinderCacheUtil.getResult(FINDER_PATH_GET_TAGSASSETS,
762                 finderArgs, this);
763 
764         if (list == null) {
765             Session session = null;
766 
767             try {
768                 session = openSession();
769 
770                 String sql = null;
771 
772                 if (obc != null) {
773                     sql = _SQL_GETTAGSASSETS.concat(ORDER_BY_CLAUSE)
774                                             .concat(obc.getOrderBy());
775                 }
776 
777                 sql = _SQL_GETTAGSASSETS;
778 
779                 SQLQuery q = session.createSQLQuery(sql);
780 
781                 q.addEntity("TagsAsset",
782                     com.liferay.portlet.tags.model.impl.TagsAssetImpl.class);
783 
784                 QueryPos qPos = QueryPos.getInstance(q);
785 
786                 qPos.add(pk);
787 
788                 list = (List<com.liferay.portlet.tags.model.TagsAsset>)QueryUtil.list(q,
789                         getDialect(), start, end);
790             }
791             catch (Exception e) {
792                 throw processException(e);
793             }
794             finally {
795                 if (list == null) {
796                     list = new ArrayList<com.liferay.portlet.tags.model.TagsAsset>();
797                 }
798 
799                 tagsAssetPersistence.cacheResult(list);
800 
801                 FinderCacheUtil.putResult(FINDER_PATH_GET_TAGSASSETS,
802                     finderArgs, list);
803 
804                 closeSession(session);
805             }
806         }
807 
808         return list;
809     }
810 
811     public static final FinderPath FINDER_PATH_GET_TAGSASSETS_SIZE = new FinderPath(com.liferay.portlet.tags.model.impl.TagsAssetModelImpl.ENTITY_CACHE_ENABLED,
812             TagsEntryModelImpl.FINDER_CACHE_ENABLED_TAGSASSETS_TAGSENTRIES,
813             TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME,
814             "getTagsAssetsSize", new String[] { Long.class.getName() });
815 
816     public int getTagsAssetsSize(long pk) throws SystemException {
817         Object[] finderArgs = new Object[] { new Long(pk) };
818 
819         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_TAGSASSETS_SIZE,
820                 finderArgs, this);
821 
822         if (count == null) {
823             Session session = null;
824 
825             try {
826                 session = openSession();
827 
828                 SQLQuery q = session.createSQLQuery(_SQL_GETTAGSASSETSSIZE);
829 
830                 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
831 
832                 QueryPos qPos = QueryPos.getInstance(q);
833 
834                 qPos.add(pk);
835 
836                 count = (Long)q.uniqueResult();
837             }
838             catch (Exception e) {
839                 throw processException(e);
840             }
841             finally {
842                 if (count == null) {
843                     count = Long.valueOf(0);
844                 }
845 
846                 FinderCacheUtil.putResult(FINDER_PATH_GET_TAGSASSETS_SIZE,
847                     finderArgs, count);
848 
849                 closeSession(session);
850             }
851         }
852 
853         return count.intValue();
854     }
855 
856     public static final FinderPath FINDER_PATH_CONTAINS_TAGSASSET = new FinderPath(com.liferay.portlet.tags.model.impl.TagsAssetModelImpl.ENTITY_CACHE_ENABLED,
857             TagsEntryModelImpl.FINDER_CACHE_ENABLED_TAGSASSETS_TAGSENTRIES,
858             TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME,
859             "containsTagsAsset",
860             new String[] { Long.class.getName(), Long.class.getName() });
861 
862     public boolean containsTagsAsset(long pk, long tagsAssetPK)
863         throws SystemException {
864         Object[] finderArgs = new Object[] { new Long(pk), new Long(tagsAssetPK) };
865 
866         Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_TAGSASSET,
867                 finderArgs, this);
868 
869         if (value == null) {
870             try {
871                 value = Boolean.valueOf(containsTagsAsset.contains(pk,
872                             tagsAssetPK));
873             }
874             catch (Exception e) {
875                 throw processException(e);
876             }
877             finally {
878                 if (value == null) {
879                     value = Boolean.FALSE;
880                 }
881 
882                 FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_TAGSASSET,
883                     finderArgs, value);
884             }
885         }
886 
887         return value.booleanValue();
888     }
889 
890     public boolean containsTagsAssets(long pk) throws SystemException {
891         if (getTagsAssetsSize(pk) > 0) {
892             return true;
893         }
894         else {
895             return false;
896         }
897     }
898 
899     public void addTagsAsset(long pk, long tagsAssetPK)
900         throws SystemException {
901         try {
902             addTagsAsset.add(pk, tagsAssetPK);
903         }
904         catch (Exception e) {
905             throw processException(e);
906         }
907         finally {
908             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
909         }
910     }
911 
912     public void addTagsAsset(long pk,
913         com.liferay.portlet.tags.model.TagsAsset tagsAsset)
914         throws SystemException {
915         try {
916             addTagsAsset.add(pk, tagsAsset.getPrimaryKey());
917         }
918         catch (Exception e) {
919             throw processException(e);
920         }
921         finally {
922             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
923         }
924     }
925 
926     public void addTagsAssets(long pk, long[] tagsAssetPKs)
927         throws SystemException {
928         try {
929             for (long tagsAssetPK : tagsAssetPKs) {
930                 addTagsAsset.add(pk, tagsAssetPK);
931             }
932         }
933         catch (Exception e) {
934             throw processException(e);
935         }
936         finally {
937             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
938         }
939     }
940 
941     public void addTagsAssets(long pk,
942         List<com.liferay.portlet.tags.model.TagsAsset> tagsAssets)
943         throws SystemException {
944         try {
945             for (com.liferay.portlet.tags.model.TagsAsset tagsAsset : tagsAssets) {
946                 addTagsAsset.add(pk, tagsAsset.getPrimaryKey());
947             }
948         }
949         catch (Exception e) {
950             throw processException(e);
951         }
952         finally {
953             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
954         }
955     }
956 
957     public void clearTagsAssets(long pk) throws SystemException {
958         try {
959             clearTagsAssets.clear(pk);
960         }
961         catch (Exception e) {
962             throw processException(e);
963         }
964         finally {
965             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
966         }
967     }
968 
969     public void removeTagsAsset(long pk, long tagsAssetPK)
970         throws SystemException {
971         try {
972             removeTagsAsset.remove(pk, tagsAssetPK);
973         }
974         catch (Exception e) {
975             throw processException(e);
976         }
977         finally {
978             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
979         }
980     }
981 
982     public void removeTagsAsset(long pk,
983         com.liferay.portlet.tags.model.TagsAsset tagsAsset)
984         throws SystemException {
985         try {
986             removeTagsAsset.remove(pk, tagsAsset.getPrimaryKey());
987         }
988         catch (Exception e) {
989             throw processException(e);
990         }
991         finally {
992             FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
993         }
994     }
995 
996     public void removeTagsAssets(long pk, long[] tagsAssetPKs)
997         throws SystemException {
998         try {
999             for (long tagsAssetPK : tagsAssetPKs) {
1000                removeTagsAsset.remove(pk, tagsAssetPK);
1001            }
1002        }
1003        catch (Exception e) {
1004            throw processException(e);
1005        }
1006        finally {
1007            FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
1008        }
1009    }
1010
1011    public void removeTagsAssets(long pk,
1012        List<com.liferay.portlet.tags.model.TagsAsset> tagsAssets)
1013        throws SystemException {
1014        try {
1015            for (com.liferay.portlet.tags.model.TagsAsset tagsAsset : tagsAssets) {
1016                removeTagsAsset.remove(pk, tagsAsset.getPrimaryKey());
1017            }
1018        }
1019        catch (Exception e) {
1020            throw processException(e);
1021        }
1022        finally {
1023            FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
1024        }
1025    }
1026
1027    public void setTagsAssets(long pk, long[] tagsAssetPKs)
1028        throws SystemException {
1029        try {
1030            Set<Long> tagsAssetPKSet = SetUtil.fromArray(tagsAssetPKs);
1031
1032            List<com.liferay.portlet.tags.model.TagsAsset> tagsAssets = getTagsAssets(pk);
1033
1034            for (com.liferay.portlet.tags.model.TagsAsset tagsAsset : tagsAssets) {
1035                if (!tagsAssetPKSet.contains(tagsAsset.getPrimaryKey())) {
1036                    removeTagsAsset.remove(pk, tagsAsset.getPrimaryKey());
1037                }
1038                else {
1039                    tagsAssetPKSet.remove(tagsAsset.getPrimaryKey());
1040                }
1041            }
1042
1043            for (Long tagsAssetPK : tagsAssetPKSet) {
1044                addTagsAsset.add(pk, tagsAssetPK);
1045            }
1046        }
1047        catch (Exception e) {
1048            throw processException(e);
1049        }
1050        finally {
1051            FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
1052        }
1053    }
1054
1055    public void setTagsAssets(long pk,
1056        List<com.liferay.portlet.tags.model.TagsAsset> tagsAssets)
1057        throws SystemException {
1058        try {
1059            long[] tagsAssetPKs = new long[tagsAssets.size()];
1060
1061            for (int i = 0; i < tagsAssets.size(); i++) {
1062                com.liferay.portlet.tags.model.TagsAsset tagsAsset = tagsAssets.get(i);
1063
1064                tagsAssetPKs[i] = tagsAsset.getPrimaryKey();
1065            }
1066
1067            setTagsAssets(pk, tagsAssetPKs);
1068        }
1069        catch (Exception e) {
1070            throw processException(e);
1071        }
1072        finally {
1073            FinderCacheUtil.clearCache(TagsEntryModelImpl.MAPPING_TABLE_TAGSASSETS_TAGSENTRIES_NAME);
1074        }
1075    }
1076
1077    public void afterPropertiesSet() {
1078        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1079                    com.liferay.portal.util.PropsUtil.get(
1080                        "value.object.listener.com.liferay.portlet.tags.model.TagsEntry")));
1081
1082        if (listenerClassNames.length > 0) {
1083            try {
1084                List<ModelListener<TagsEntry>> listenersList = new ArrayList<ModelListener<TagsEntry>>();
1085
1086                for (String listenerClassName : listenerClassNames) {
1087                    listenersList.add((ModelListener<TagsEntry>)Class.forName(
1088                            listenerClassName).newInstance());
1089                }
1090
1091                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1092            }
1093            catch (Exception e) {
1094                _log.error(e);
1095            }
1096        }
1097
1098        containsTagsAsset = new ContainsTagsAsset(this);
1099
1100        addTagsAsset = new AddTagsAsset(this);
1101        clearTagsAssets = new ClearTagsAssets(this);
1102        removeTagsAsset = new RemoveTagsAsset(this);
1103    }
1104
1105    @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence")
1106    protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
1107    @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsEntryPersistence")
1108    protected com.liferay.portlet.tags.service.persistence.TagsEntryPersistence tagsEntryPersistence;
1109    @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence")
1110    protected com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence tagsPropertyPersistence;
1111    @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsSourcePersistence")
1112    protected com.liferay.portlet.tags.service.persistence.TagsSourcePersistence tagsSourcePersistence;
1113    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1114    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1115    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1116    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1117    @BeanReference(name = "com.liferay.portlet.blogs.service.persistence.BlogsEntryPersistence")
1118    protected com.liferay.portlet.blogs.service.persistence.BlogsEntryPersistence blogsEntryPersistence;
1119    @BeanReference(name = "com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryPersistence")
1120    protected com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryPersistence bookmarksEntryPersistence;
1121    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFolderPersistence")
1122    protected com.liferay.portlet.documentlibrary.service.persistence.DLFolderPersistence dlFolderPersistence;
1123    @BeanReference(name = "com.liferay.portlet.imagegallery.service.persistence.IGImagePersistence")
1124    protected com.liferay.portlet.imagegallery.service.persistence.IGImagePersistence igImagePersistence;
1125    @BeanReference(name = "com.liferay.portlet.journal.service.persistence.JournalArticlePersistence")
1126    protected com.liferay.portlet.journal.service.persistence.JournalArticlePersistence journalArticlePersistence;
1127    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence")
1128    protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
1129    @BeanReference(name = "com.liferay.portlet.softwarecatalog.service.persistence.SCProductEntryPersistence")
1130    protected com.liferay.portlet.softwarecatalog.service.persistence.SCProductEntryPersistence scProductEntryPersistence;
1131    @BeanReference(name = "com.liferay.portlet.wiki.service.persistence.WikiPagePersistence")
1132    protected com.liferay.portlet.wiki.service.persistence.WikiPagePersistence wikiPagePersistence;
1133    protected ContainsTagsAsset containsTagsAsset;
1134    protected AddTagsAsset addTagsAsset;
1135    protected ClearTagsAssets clearTagsAssets;
1136    protected RemoveTagsAsset removeTagsAsset;
1137
1138    protected class ContainsTagsAsset {
1139        protected ContainsTagsAsset(TagsEntryPersistenceImpl persistenceImpl) {
1140            super();
1141
1142            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1143                    _SQL_CONTAINSTAGSASSET,
1144                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1145        }
1146
1147        protected boolean contains(long entryId, long assetId) {
1148            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1149                        new Long(entryId), new Long(assetId)
1150                    });
1151
1152            if (results.size() > 0) {
1153                Integer count = results.get(0);
1154
1155                if (count.intValue() > 0) {
1156                    return true;
1157                }
1158            }
1159
1160            return false;
1161        }
1162
1163        private MappingSqlQuery _mappingSqlQuery;
1164    }
1165
1166    protected class AddTagsAsset {
1167        protected AddTagsAsset(TagsEntryPersistenceImpl persistenceImpl) {
1168            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1169                    "INSERT INTO TagsAssets_TagsEntries (entryId, assetId) VALUES (?, ?)",
1170                    new int[] { Types.BIGINT, Types.BIGINT });
1171            _persistenceImpl = persistenceImpl;
1172        }
1173
1174        protected void add(long entryId, long assetId) {
1175            if (!_persistenceImpl.containsTagsAsset.contains(entryId, assetId)) {
1176                _sqlUpdate.update(new Object[] {
1177                        new Long(entryId), new Long(assetId)
1178                    });
1179            }
1180        }
1181
1182        private SqlUpdate _sqlUpdate;
1183        private TagsEntryPersistenceImpl _persistenceImpl;
1184    }
1185
1186    protected class ClearTagsAssets {
1187        protected ClearTagsAssets(TagsEntryPersistenceImpl persistenceImpl) {
1188            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1189                    "DELETE FROM TagsAssets_TagsEntries WHERE entryId = ?",
1190                    new int[] { Types.BIGINT });
1191        }
1192
1193        protected void clear(long entryId) {
1194            _sqlUpdate.update(new Object[] { new Long(entryId) });
1195        }
1196
1197        private SqlUpdate _sqlUpdate;
1198    }
1199
1200    protected class RemoveTagsAsset {
1201        protected RemoveTagsAsset(TagsEntryPersistenceImpl persistenceImpl) {
1202            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1203                    "DELETE FROM TagsAssets_TagsEntries WHERE entryId = ? AND assetId = ?",
1204                    new int[] { Types.BIGINT, Types.BIGINT });
1205        }
1206
1207        protected void remove(long entryId, long assetId) {
1208            _sqlUpdate.update(new Object[] { new Long(entryId), new Long(
1209                        assetId) });
1210        }
1211
1212        private SqlUpdate _sqlUpdate;
1213    }
1214
1215    private static final String _SQL_SELECT_TAGSENTRY = "SELECT tagsEntry FROM TagsEntry tagsEntry";
1216    private static final String _SQL_SELECT_TAGSENTRY_WHERE = "SELECT tagsEntry FROM TagsEntry tagsEntry WHERE ";
1217    private static final String _SQL_COUNT_TAGSENTRY = "SELECT COUNT(tagsEntry) FROM TagsEntry tagsEntry";
1218    private static final String _SQL_COUNT_TAGSENTRY_WHERE = "SELECT COUNT(tagsEntry) FROM TagsEntry tagsEntry WHERE ";
1219    private static final String _SQL_GETTAGSASSETS = "SELECT {TagsAsset.*} FROM TagsAsset INNER JOIN TagsAssets_TagsEntries ON (TagsAssets_TagsEntries.assetId = TagsAsset.assetId) WHERE (TagsAssets_TagsEntries.entryId = ?)";
1220    private static final String _SQL_GETTAGSASSETSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE entryId = ?";
1221    private static final String _SQL_CONTAINSTAGSASSET = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE entryId = ? AND assetId = ?";
1222    private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "tagsEntry.companyId = ? AND ";
1223    private static final String _FINDER_COLUMN_C_N_NAME_1 = "tagsEntry.name IS NULL";
1224    private static final String _FINDER_COLUMN_C_N_NAME_2 = "tagsEntry.name = ?";
1225    private static final String _FINDER_COLUMN_C_N_NAME_3 = "(tagsEntry.name IS NULL OR tagsEntry.name = ?)";
1226    private static final String _ORDER_BY_ENTITY_ALIAS = "tagsEntry.";
1227    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TagsEntry exists with the primary key ";
1228    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TagsEntry exists with the key {";
1229    private static Log _log = LogFactoryUtil.getLog(TagsEntryPersistenceImpl.class);
1230}