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