001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.SQLQuery;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.SetUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.CacheModel;
038    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
039    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
040    import com.liferay.portal.service.persistence.impl.TableMapper;
041    import com.liferay.portal.service.persistence.impl.TableMapperFactory;
042    
043    import com.liferay.portlet.asset.NoSuchTagException;
044    import com.liferay.portlet.asset.model.AssetTag;
045    import com.liferay.portlet.asset.model.impl.AssetTagImpl;
046    import com.liferay.portlet.asset.model.impl.AssetTagModelImpl;
047    import com.liferay.portlet.asset.service.persistence.AssetEntryPersistence;
048    import com.liferay.portlet.asset.service.persistence.AssetTagPersistence;
049    
050    import java.io.Serializable;
051    
052    import java.util.Collections;
053    import java.util.HashMap;
054    import java.util.HashSet;
055    import java.util.Iterator;
056    import java.util.List;
057    import java.util.Map;
058    import java.util.Set;
059    
060    /**
061     * The persistence implementation for the asset tag service.
062     *
063     * <p>
064     * Caching information and settings can be found in <code>portal.properties</code>
065     * </p>
066     *
067     * @author Brian Wing Shun Chan
068     * @see AssetTagPersistence
069     * @see AssetTagUtil
070     * @generated
071     */
072    @ProviderType
073    public class AssetTagPersistenceImpl extends BasePersistenceImpl<AssetTag>
074            implements AssetTagPersistence {
075            /*
076             * NOTE FOR DEVELOPERS:
077             *
078             * Never modify or reference this class directly. Always use {@link AssetTagUtil} to access the asset tag persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
079             */
080            public static final String FINDER_CLASS_NAME_ENTITY = AssetTagImpl.class.getName();
081            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
082                    ".List1";
083            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
084                    ".List2";
085            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
086                            AssetTagModelImpl.FINDER_CACHE_ENABLED, AssetTagImpl.class,
087                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
088            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
089                            AssetTagModelImpl.FINDER_CACHE_ENABLED, AssetTagImpl.class,
090                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
091            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
092                            AssetTagModelImpl.FINDER_CACHE_ENABLED, Long.class,
093                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
094            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
095                            AssetTagModelImpl.FINDER_CACHE_ENABLED, AssetTagImpl.class,
096                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByGroupId",
097                            new String[] {
098                                    Long.class.getName(),
099                                    
100                            Integer.class.getName(), Integer.class.getName(),
101                                    OrderByComparator.class.getName()
102                            });
103            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
104                    new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
105                            AssetTagModelImpl.FINDER_CACHE_ENABLED, AssetTagImpl.class,
106                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
107                            new String[] { Long.class.getName() },
108                            AssetTagModelImpl.GROUPID_COLUMN_BITMASK |
109                            AssetTagModelImpl.NAME_COLUMN_BITMASK);
110            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
111                            AssetTagModelImpl.FINDER_CACHE_ENABLED, Long.class,
112                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
113                            new String[] { Long.class.getName() });
114    
115            /**
116             * Returns all the asset tags where groupId = &#63;.
117             *
118             * @param groupId the group ID
119             * @return the matching asset tags
120             */
121            @Override
122            public List<AssetTag> findByGroupId(long groupId) {
123                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
124            }
125    
126            /**
127             * Returns a range of all the asset tags where groupId = &#63;.
128             *
129             * <p>
130             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
131             * </p>
132             *
133             * @param groupId the group ID
134             * @param start the lower bound of the range of asset tags
135             * @param end the upper bound of the range of asset tags (not inclusive)
136             * @return the range of matching asset tags
137             */
138            @Override
139            public List<AssetTag> findByGroupId(long groupId, int start, int end) {
140                    return findByGroupId(groupId, start, end, null);
141            }
142    
143            /**
144             * Returns an ordered range of all the asset tags where groupId = &#63;.
145             *
146             * <p>
147             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
148             * </p>
149             *
150             * @param groupId the group ID
151             * @param start the lower bound of the range of asset tags
152             * @param end the upper bound of the range of asset tags (not inclusive)
153             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
154             * @return the ordered range of matching asset tags
155             */
156            @Override
157            public List<AssetTag> findByGroupId(long groupId, int start, int end,
158                    OrderByComparator<AssetTag> orderByComparator) {
159                    boolean pagination = true;
160                    FinderPath finderPath = null;
161                    Object[] finderArgs = null;
162    
163                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
164                                    (orderByComparator == null)) {
165                            pagination = false;
166                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
167                            finderArgs = new Object[] { groupId };
168                    }
169                    else {
170                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
171                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
172                    }
173    
174                    List<AssetTag> list = (List<AssetTag>)FinderCacheUtil.getResult(finderPath,
175                                    finderArgs, this);
176    
177                    if ((list != null) && !list.isEmpty()) {
178                            for (AssetTag assetTag : list) {
179                                    if ((groupId != assetTag.getGroupId())) {
180                                            list = null;
181    
182                                            break;
183                                    }
184                            }
185                    }
186    
187                    if (list == null) {
188                            StringBundler query = null;
189    
190                            if (orderByComparator != null) {
191                                    query = new StringBundler(3 +
192                                                    (orderByComparator.getOrderByFields().length * 3));
193                            }
194                            else {
195                                    query = new StringBundler(3);
196                            }
197    
198                            query.append(_SQL_SELECT_ASSETTAG_WHERE);
199    
200                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
201    
202                            if (orderByComparator != null) {
203                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
204                                            orderByComparator);
205                            }
206                            else
207                             if (pagination) {
208                                    query.append(AssetTagModelImpl.ORDER_BY_JPQL);
209                            }
210    
211                            String sql = query.toString();
212    
213                            Session session = null;
214    
215                            try {
216                                    session = openSession();
217    
218                                    Query q = session.createQuery(sql);
219    
220                                    QueryPos qPos = QueryPos.getInstance(q);
221    
222                                    qPos.add(groupId);
223    
224                                    if (!pagination) {
225                                            list = (List<AssetTag>)QueryUtil.list(q, getDialect(),
226                                                            start, end, false);
227    
228                                            Collections.sort(list);
229    
230                                            list = Collections.unmodifiableList(list);
231                                    }
232                                    else {
233                                            list = (List<AssetTag>)QueryUtil.list(q, getDialect(),
234                                                            start, end);
235                                    }
236    
237                                    cacheResult(list);
238    
239                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
240                            }
241                            catch (Exception e) {
242                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
243    
244                                    throw processException(e);
245                            }
246                            finally {
247                                    closeSession(session);
248                            }
249                    }
250    
251                    return list;
252            }
253    
254            /**
255             * Returns the first asset tag in the ordered set where groupId = &#63;.
256             *
257             * @param groupId the group ID
258             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
259             * @return the first matching asset tag
260             * @throws com.liferay.portlet.asset.NoSuchTagException if a matching asset tag could not be found
261             */
262            @Override
263            public AssetTag findByGroupId_First(long groupId,
264                    OrderByComparator<AssetTag> orderByComparator)
265                    throws NoSuchTagException {
266                    AssetTag assetTag = fetchByGroupId_First(groupId, orderByComparator);
267    
268                    if (assetTag != null) {
269                            return assetTag;
270                    }
271    
272                    StringBundler msg = new StringBundler(4);
273    
274                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
275    
276                    msg.append("groupId=");
277                    msg.append(groupId);
278    
279                    msg.append(StringPool.CLOSE_CURLY_BRACE);
280    
281                    throw new NoSuchTagException(msg.toString());
282            }
283    
284            /**
285             * Returns the first asset tag in the ordered set where groupId = &#63;.
286             *
287             * @param groupId the group ID
288             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
289             * @return the first matching asset tag, or <code>null</code> if a matching asset tag could not be found
290             */
291            @Override
292            public AssetTag fetchByGroupId_First(long groupId,
293                    OrderByComparator<AssetTag> orderByComparator) {
294                    List<AssetTag> list = findByGroupId(groupId, 0, 1, orderByComparator);
295    
296                    if (!list.isEmpty()) {
297                            return list.get(0);
298                    }
299    
300                    return null;
301            }
302    
303            /**
304             * Returns the last asset tag in the ordered set where groupId = &#63;.
305             *
306             * @param groupId the group ID
307             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
308             * @return the last matching asset tag
309             * @throws com.liferay.portlet.asset.NoSuchTagException if a matching asset tag could not be found
310             */
311            @Override
312            public AssetTag findByGroupId_Last(long groupId,
313                    OrderByComparator<AssetTag> orderByComparator)
314                    throws NoSuchTagException {
315                    AssetTag assetTag = fetchByGroupId_Last(groupId, orderByComparator);
316    
317                    if (assetTag != null) {
318                            return assetTag;
319                    }
320    
321                    StringBundler msg = new StringBundler(4);
322    
323                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
324    
325                    msg.append("groupId=");
326                    msg.append(groupId);
327    
328                    msg.append(StringPool.CLOSE_CURLY_BRACE);
329    
330                    throw new NoSuchTagException(msg.toString());
331            }
332    
333            /**
334             * Returns the last asset tag in the ordered set where groupId = &#63;.
335             *
336             * @param groupId the group ID
337             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
338             * @return the last matching asset tag, or <code>null</code> if a matching asset tag could not be found
339             */
340            @Override
341            public AssetTag fetchByGroupId_Last(long groupId,
342                    OrderByComparator<AssetTag> orderByComparator) {
343                    int count = countByGroupId(groupId);
344    
345                    if (count == 0) {
346                            return null;
347                    }
348    
349                    List<AssetTag> list = findByGroupId(groupId, count - 1, count,
350                                    orderByComparator);
351    
352                    if (!list.isEmpty()) {
353                            return list.get(0);
354                    }
355    
356                    return null;
357            }
358    
359            /**
360             * Returns the asset tags before and after the current asset tag in the ordered set where groupId = &#63;.
361             *
362             * @param tagId the primary key of the current asset tag
363             * @param groupId the group ID
364             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
365             * @return the previous, current, and next asset tag
366             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
367             */
368            @Override
369            public AssetTag[] findByGroupId_PrevAndNext(long tagId, long groupId,
370                    OrderByComparator<AssetTag> orderByComparator)
371                    throws NoSuchTagException {
372                    AssetTag assetTag = findByPrimaryKey(tagId);
373    
374                    Session session = null;
375    
376                    try {
377                            session = openSession();
378    
379                            AssetTag[] array = new AssetTagImpl[3];
380    
381                            array[0] = getByGroupId_PrevAndNext(session, assetTag, groupId,
382                                            orderByComparator, true);
383    
384                            array[1] = assetTag;
385    
386                            array[2] = getByGroupId_PrevAndNext(session, assetTag, groupId,
387                                            orderByComparator, false);
388    
389                            return array;
390                    }
391                    catch (Exception e) {
392                            throw processException(e);
393                    }
394                    finally {
395                            closeSession(session);
396                    }
397            }
398    
399            protected AssetTag getByGroupId_PrevAndNext(Session session,
400                    AssetTag assetTag, long groupId,
401                    OrderByComparator<AssetTag> orderByComparator, boolean previous) {
402                    StringBundler query = null;
403    
404                    if (orderByComparator != null) {
405                            query = new StringBundler(6 +
406                                            (orderByComparator.getOrderByFields().length * 6));
407                    }
408                    else {
409                            query = new StringBundler(3);
410                    }
411    
412                    query.append(_SQL_SELECT_ASSETTAG_WHERE);
413    
414                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
415    
416                    if (orderByComparator != null) {
417                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
418    
419                            if (orderByConditionFields.length > 0) {
420                                    query.append(WHERE_AND);
421                            }
422    
423                            for (int i = 0; i < orderByConditionFields.length; i++) {
424                                    query.append(_ORDER_BY_ENTITY_ALIAS);
425                                    query.append(orderByConditionFields[i]);
426    
427                                    if ((i + 1) < orderByConditionFields.length) {
428                                            if (orderByComparator.isAscending() ^ previous) {
429                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
430                                            }
431                                            else {
432                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
433                                            }
434                                    }
435                                    else {
436                                            if (orderByComparator.isAscending() ^ previous) {
437                                                    query.append(WHERE_GREATER_THAN);
438                                            }
439                                            else {
440                                                    query.append(WHERE_LESSER_THAN);
441                                            }
442                                    }
443                            }
444    
445                            query.append(ORDER_BY_CLAUSE);
446    
447                            String[] orderByFields = orderByComparator.getOrderByFields();
448    
449                            for (int i = 0; i < orderByFields.length; i++) {
450                                    query.append(_ORDER_BY_ENTITY_ALIAS);
451                                    query.append(orderByFields[i]);
452    
453                                    if ((i + 1) < orderByFields.length) {
454                                            if (orderByComparator.isAscending() ^ previous) {
455                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
456                                            }
457                                            else {
458                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
459                                            }
460                                    }
461                                    else {
462                                            if (orderByComparator.isAscending() ^ previous) {
463                                                    query.append(ORDER_BY_ASC);
464                                            }
465                                            else {
466                                                    query.append(ORDER_BY_DESC);
467                                            }
468                                    }
469                            }
470                    }
471                    else {
472                            query.append(AssetTagModelImpl.ORDER_BY_JPQL);
473                    }
474    
475                    String sql = query.toString();
476    
477                    Query q = session.createQuery(sql);
478    
479                    q.setFirstResult(0);
480                    q.setMaxResults(2);
481    
482                    QueryPos qPos = QueryPos.getInstance(q);
483    
484                    qPos.add(groupId);
485    
486                    if (orderByComparator != null) {
487                            Object[] values = orderByComparator.getOrderByConditionValues(assetTag);
488    
489                            for (Object value : values) {
490                                    qPos.add(value);
491                            }
492                    }
493    
494                    List<AssetTag> list = q.list();
495    
496                    if (list.size() == 2) {
497                            return list.get(1);
498                    }
499                    else {
500                            return null;
501                    }
502            }
503    
504            /**
505             * Returns all the asset tags that the user has permission to view where groupId = &#63;.
506             *
507             * @param groupId the group ID
508             * @return the matching asset tags that the user has permission to view
509             */
510            @Override
511            public List<AssetTag> filterFindByGroupId(long groupId) {
512                    return filterFindByGroupId(groupId, QueryUtil.ALL_POS,
513                            QueryUtil.ALL_POS, null);
514            }
515    
516            /**
517             * Returns a range of all the asset tags that the user has permission to view where groupId = &#63;.
518             *
519             * <p>
520             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
521             * </p>
522             *
523             * @param groupId the group ID
524             * @param start the lower bound of the range of asset tags
525             * @param end the upper bound of the range of asset tags (not inclusive)
526             * @return the range of matching asset tags that the user has permission to view
527             */
528            @Override
529            public List<AssetTag> filterFindByGroupId(long groupId, int start, int end) {
530                    return filterFindByGroupId(groupId, start, end, null);
531            }
532    
533            /**
534             * Returns an ordered range of all the asset tags that the user has permissions to view where groupId = &#63;.
535             *
536             * <p>
537             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
538             * </p>
539             *
540             * @param groupId the group ID
541             * @param start the lower bound of the range of asset tags
542             * @param end the upper bound of the range of asset tags (not inclusive)
543             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
544             * @return the ordered range of matching asset tags that the user has permission to view
545             */
546            @Override
547            public List<AssetTag> filterFindByGroupId(long groupId, int start, int end,
548                    OrderByComparator<AssetTag> orderByComparator) {
549                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
550                            return findByGroupId(groupId, start, end, orderByComparator);
551                    }
552    
553                    StringBundler query = null;
554    
555                    if (orderByComparator != null) {
556                            query = new StringBundler(3 +
557                                            (orderByComparator.getOrderByFields().length * 3));
558                    }
559                    else {
560                            query = new StringBundler(3);
561                    }
562    
563                    if (getDB().isSupportsInlineDistinct()) {
564                            query.append(_FILTER_SQL_SELECT_ASSETTAG_WHERE);
565                    }
566                    else {
567                            query.append(_FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_1);
568                    }
569    
570                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
571    
572                    if (!getDB().isSupportsInlineDistinct()) {
573                            query.append(_FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_2);
574                    }
575    
576                    if (orderByComparator != null) {
577                            if (getDB().isSupportsInlineDistinct()) {
578                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
579                                            orderByComparator, true);
580                            }
581                            else {
582                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
583                                            orderByComparator, true);
584                            }
585                    }
586                    else {
587                            if (getDB().isSupportsInlineDistinct()) {
588                                    query.append(AssetTagModelImpl.ORDER_BY_JPQL);
589                            }
590                            else {
591                                    query.append(AssetTagModelImpl.ORDER_BY_SQL);
592                            }
593                    }
594    
595                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
596                                    AssetTag.class.getName(),
597                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
598    
599                    Session session = null;
600    
601                    try {
602                            session = openSession();
603    
604                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
605    
606                            if (getDB().isSupportsInlineDistinct()) {
607                                    q.addEntity(_FILTER_ENTITY_ALIAS, AssetTagImpl.class);
608                            }
609                            else {
610                                    q.addEntity(_FILTER_ENTITY_TABLE, AssetTagImpl.class);
611                            }
612    
613                            QueryPos qPos = QueryPos.getInstance(q);
614    
615                            qPos.add(groupId);
616    
617                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
618                    }
619                    catch (Exception e) {
620                            throw processException(e);
621                    }
622                    finally {
623                            closeSession(session);
624                    }
625            }
626    
627            /**
628             * Returns the asset tags before and after the current asset tag in the ordered set of asset tags that the user has permission to view where groupId = &#63;.
629             *
630             * @param tagId the primary key of the current asset tag
631             * @param groupId the group ID
632             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
633             * @return the previous, current, and next asset tag
634             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
635             */
636            @Override
637            public AssetTag[] filterFindByGroupId_PrevAndNext(long tagId, long groupId,
638                    OrderByComparator<AssetTag> orderByComparator)
639                    throws NoSuchTagException {
640                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
641                            return findByGroupId_PrevAndNext(tagId, groupId, orderByComparator);
642                    }
643    
644                    AssetTag assetTag = findByPrimaryKey(tagId);
645    
646                    Session session = null;
647    
648                    try {
649                            session = openSession();
650    
651                            AssetTag[] array = new AssetTagImpl[3];
652    
653                            array[0] = filterGetByGroupId_PrevAndNext(session, assetTag,
654                                            groupId, orderByComparator, true);
655    
656                            array[1] = assetTag;
657    
658                            array[2] = filterGetByGroupId_PrevAndNext(session, assetTag,
659                                            groupId, orderByComparator, false);
660    
661                            return array;
662                    }
663                    catch (Exception e) {
664                            throw processException(e);
665                    }
666                    finally {
667                            closeSession(session);
668                    }
669            }
670    
671            protected AssetTag filterGetByGroupId_PrevAndNext(Session session,
672                    AssetTag assetTag, long groupId,
673                    OrderByComparator<AssetTag> orderByComparator, boolean previous) {
674                    StringBundler query = null;
675    
676                    if (orderByComparator != null) {
677                            query = new StringBundler(6 +
678                                            (orderByComparator.getOrderByFields().length * 6));
679                    }
680                    else {
681                            query = new StringBundler(3);
682                    }
683    
684                    if (getDB().isSupportsInlineDistinct()) {
685                            query.append(_FILTER_SQL_SELECT_ASSETTAG_WHERE);
686                    }
687                    else {
688                            query.append(_FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_1);
689                    }
690    
691                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
692    
693                    if (!getDB().isSupportsInlineDistinct()) {
694                            query.append(_FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_2);
695                    }
696    
697                    if (orderByComparator != null) {
698                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
699    
700                            if (orderByConditionFields.length > 0) {
701                                    query.append(WHERE_AND);
702                            }
703    
704                            for (int i = 0; i < orderByConditionFields.length; i++) {
705                                    if (getDB().isSupportsInlineDistinct()) {
706                                            query.append(_ORDER_BY_ENTITY_ALIAS);
707                                    }
708                                    else {
709                                            query.append(_ORDER_BY_ENTITY_TABLE);
710                                    }
711    
712                                    query.append(orderByConditionFields[i]);
713    
714                                    if ((i + 1) < orderByConditionFields.length) {
715                                            if (orderByComparator.isAscending() ^ previous) {
716                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
717                                            }
718                                            else {
719                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
720                                            }
721                                    }
722                                    else {
723                                            if (orderByComparator.isAscending() ^ previous) {
724                                                    query.append(WHERE_GREATER_THAN);
725                                            }
726                                            else {
727                                                    query.append(WHERE_LESSER_THAN);
728                                            }
729                                    }
730                            }
731    
732                            query.append(ORDER_BY_CLAUSE);
733    
734                            String[] orderByFields = orderByComparator.getOrderByFields();
735    
736                            for (int i = 0; i < orderByFields.length; i++) {
737                                    if (getDB().isSupportsInlineDistinct()) {
738                                            query.append(_ORDER_BY_ENTITY_ALIAS);
739                                    }
740                                    else {
741                                            query.append(_ORDER_BY_ENTITY_TABLE);
742                                    }
743    
744                                    query.append(orderByFields[i]);
745    
746                                    if ((i + 1) < orderByFields.length) {
747                                            if (orderByComparator.isAscending() ^ previous) {
748                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
749                                            }
750                                            else {
751                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
752                                            }
753                                    }
754                                    else {
755                                            if (orderByComparator.isAscending() ^ previous) {
756                                                    query.append(ORDER_BY_ASC);
757                                            }
758                                            else {
759                                                    query.append(ORDER_BY_DESC);
760                                            }
761                                    }
762                            }
763                    }
764                    else {
765                            if (getDB().isSupportsInlineDistinct()) {
766                                    query.append(AssetTagModelImpl.ORDER_BY_JPQL);
767                            }
768                            else {
769                                    query.append(AssetTagModelImpl.ORDER_BY_SQL);
770                            }
771                    }
772    
773                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
774                                    AssetTag.class.getName(),
775                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
776    
777                    SQLQuery q = session.createSynchronizedSQLQuery(sql);
778    
779                    q.setFirstResult(0);
780                    q.setMaxResults(2);
781    
782                    if (getDB().isSupportsInlineDistinct()) {
783                            q.addEntity(_FILTER_ENTITY_ALIAS, AssetTagImpl.class);
784                    }
785                    else {
786                            q.addEntity(_FILTER_ENTITY_TABLE, AssetTagImpl.class);
787                    }
788    
789                    QueryPos qPos = QueryPos.getInstance(q);
790    
791                    qPos.add(groupId);
792    
793                    if (orderByComparator != null) {
794                            Object[] values = orderByComparator.getOrderByConditionValues(assetTag);
795    
796                            for (Object value : values) {
797                                    qPos.add(value);
798                            }
799                    }
800    
801                    List<AssetTag> list = q.list();
802    
803                    if (list.size() == 2) {
804                            return list.get(1);
805                    }
806                    else {
807                            return null;
808                    }
809            }
810    
811            /**
812             * Removes all the asset tags where groupId = &#63; from the database.
813             *
814             * @param groupId the group ID
815             */
816            @Override
817            public void removeByGroupId(long groupId) {
818                    for (AssetTag assetTag : findByGroupId(groupId, QueryUtil.ALL_POS,
819                                    QueryUtil.ALL_POS, null)) {
820                            remove(assetTag);
821                    }
822            }
823    
824            /**
825             * Returns the number of asset tags where groupId = &#63;.
826             *
827             * @param groupId the group ID
828             * @return the number of matching asset tags
829             */
830            @Override
831            public int countByGroupId(long groupId) {
832                    FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUPID;
833    
834                    Object[] finderArgs = new Object[] { groupId };
835    
836                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
837                                    this);
838    
839                    if (count == null) {
840                            StringBundler query = new StringBundler(2);
841    
842                            query.append(_SQL_COUNT_ASSETTAG_WHERE);
843    
844                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
845    
846                            String sql = query.toString();
847    
848                            Session session = null;
849    
850                            try {
851                                    session = openSession();
852    
853                                    Query q = session.createQuery(sql);
854    
855                                    QueryPos qPos = QueryPos.getInstance(q);
856    
857                                    qPos.add(groupId);
858    
859                                    count = (Long)q.uniqueResult();
860    
861                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
862                            }
863                            catch (Exception e) {
864                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
865    
866                                    throw processException(e);
867                            }
868                            finally {
869                                    closeSession(session);
870                            }
871                    }
872    
873                    return count.intValue();
874            }
875    
876            /**
877             * Returns the number of asset tags that the user has permission to view where groupId = &#63;.
878             *
879             * @param groupId the group ID
880             * @return the number of matching asset tags that the user has permission to view
881             */
882            @Override
883            public int filterCountByGroupId(long groupId) {
884                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
885                            return countByGroupId(groupId);
886                    }
887    
888                    StringBundler query = new StringBundler(2);
889    
890                    query.append(_FILTER_SQL_COUNT_ASSETTAG_WHERE);
891    
892                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
893    
894                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
895                                    AssetTag.class.getName(),
896                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
897    
898                    Session session = null;
899    
900                    try {
901                            session = openSession();
902    
903                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
904    
905                            q.addScalar(COUNT_COLUMN_NAME,
906                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
907    
908                            QueryPos qPos = QueryPos.getInstance(q);
909    
910                            qPos.add(groupId);
911    
912                            Long count = (Long)q.uniqueResult();
913    
914                            return count.intValue();
915                    }
916                    catch (Exception e) {
917                            throw processException(e);
918                    }
919                    finally {
920                            closeSession(session);
921                    }
922            }
923    
924            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "assetTag.groupId = ?";
925            public static final FinderPath FINDER_PATH_FETCH_BY_G_N = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
926                            AssetTagModelImpl.FINDER_CACHE_ENABLED, AssetTagImpl.class,
927                            FINDER_CLASS_NAME_ENTITY, "fetchByG_N",
928                            new String[] { Long.class.getName(), String.class.getName() },
929                            AssetTagModelImpl.GROUPID_COLUMN_BITMASK |
930                            AssetTagModelImpl.NAME_COLUMN_BITMASK);
931            public static final FinderPath FINDER_PATH_COUNT_BY_G_N = new FinderPath(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
932                            AssetTagModelImpl.FINDER_CACHE_ENABLED, Long.class,
933                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_N",
934                            new String[] { Long.class.getName(), String.class.getName() });
935    
936            /**
937             * Returns the asset tag where groupId = &#63; and name = &#63; or throws a {@link com.liferay.portlet.asset.NoSuchTagException} if it could not be found.
938             *
939             * @param groupId the group ID
940             * @param name the name
941             * @return the matching asset tag
942             * @throws com.liferay.portlet.asset.NoSuchTagException if a matching asset tag could not be found
943             */
944            @Override
945            public AssetTag findByG_N(long groupId, String name)
946                    throws NoSuchTagException {
947                    AssetTag assetTag = fetchByG_N(groupId, name);
948    
949                    if (assetTag == null) {
950                            StringBundler msg = new StringBundler(6);
951    
952                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
953    
954                            msg.append("groupId=");
955                            msg.append(groupId);
956    
957                            msg.append(", name=");
958                            msg.append(name);
959    
960                            msg.append(StringPool.CLOSE_CURLY_BRACE);
961    
962                            if (_log.isWarnEnabled()) {
963                                    _log.warn(msg.toString());
964                            }
965    
966                            throw new NoSuchTagException(msg.toString());
967                    }
968    
969                    return assetTag;
970            }
971    
972            /**
973             * Returns the asset tag where groupId = &#63; and name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
974             *
975             * @param groupId the group ID
976             * @param name the name
977             * @return the matching asset tag, or <code>null</code> if a matching asset tag could not be found
978             */
979            @Override
980            public AssetTag fetchByG_N(long groupId, String name) {
981                    return fetchByG_N(groupId, name, true);
982            }
983    
984            /**
985             * Returns the asset tag where groupId = &#63; and name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
986             *
987             * @param groupId the group ID
988             * @param name the name
989             * @param retrieveFromCache whether to use the finder cache
990             * @return the matching asset tag, or <code>null</code> if a matching asset tag could not be found
991             */
992            @Override
993            public AssetTag fetchByG_N(long groupId, String name,
994                    boolean retrieveFromCache) {
995                    Object[] finderArgs = new Object[] { groupId, name };
996    
997                    Object result = null;
998    
999                    if (retrieveFromCache) {
1000                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_N,
1001                                            finderArgs, this);
1002                    }
1003    
1004                    if (result instanceof AssetTag) {
1005                            AssetTag assetTag = (AssetTag)result;
1006    
1007                            if ((groupId != assetTag.getGroupId()) ||
1008                                            !Validator.equals(name, assetTag.getName())) {
1009                                    result = null;
1010                            }
1011                    }
1012    
1013                    if (result == null) {
1014                            StringBundler query = new StringBundler(4);
1015    
1016                            query.append(_SQL_SELECT_ASSETTAG_WHERE);
1017    
1018                            query.append(_FINDER_COLUMN_G_N_GROUPID_2);
1019    
1020                            boolean bindName = false;
1021    
1022                            if (name == null) {
1023                                    query.append(_FINDER_COLUMN_G_N_NAME_1);
1024                            }
1025                            else if (name.equals(StringPool.BLANK)) {
1026                                    query.append(_FINDER_COLUMN_G_N_NAME_3);
1027                            }
1028                            else {
1029                                    bindName = true;
1030    
1031                                    query.append(_FINDER_COLUMN_G_N_NAME_2);
1032                            }
1033    
1034                            String sql = query.toString();
1035    
1036                            Session session = null;
1037    
1038                            try {
1039                                    session = openSession();
1040    
1041                                    Query q = session.createQuery(sql);
1042    
1043                                    QueryPos qPos = QueryPos.getInstance(q);
1044    
1045                                    qPos.add(groupId);
1046    
1047                                    if (bindName) {
1048                                            qPos.add(name);
1049                                    }
1050    
1051                                    List<AssetTag> list = q.list();
1052    
1053                                    if (list.isEmpty()) {
1054                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1055                                                    finderArgs, list);
1056                                    }
1057                                    else {
1058                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
1059                                                    _log.warn(
1060                                                            "AssetTagPersistenceImpl.fetchByG_N(long, String, boolean) with parameters (" +
1061                                                            StringUtil.merge(finderArgs) +
1062                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
1063                                            }
1064    
1065                                            AssetTag assetTag = list.get(0);
1066    
1067                                            result = assetTag;
1068    
1069                                            cacheResult(assetTag);
1070    
1071                                            if ((assetTag.getGroupId() != groupId) ||
1072                                                            (assetTag.getName() == null) ||
1073                                                            !assetTag.getName().equals(name)) {
1074                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1075                                                            finderArgs, assetTag);
1076                                            }
1077                                    }
1078                            }
1079                            catch (Exception e) {
1080                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N,
1081                                            finderArgs);
1082    
1083                                    throw processException(e);
1084                            }
1085                            finally {
1086                                    closeSession(session);
1087                            }
1088                    }
1089    
1090                    if (result instanceof List<?>) {
1091                            return null;
1092                    }
1093                    else {
1094                            return (AssetTag)result;
1095                    }
1096            }
1097    
1098            /**
1099             * Removes the asset tag where groupId = &#63; and name = &#63; from the database.
1100             *
1101             * @param groupId the group ID
1102             * @param name the name
1103             * @return the asset tag that was removed
1104             */
1105            @Override
1106            public AssetTag removeByG_N(long groupId, String name)
1107                    throws NoSuchTagException {
1108                    AssetTag assetTag = findByG_N(groupId, name);
1109    
1110                    return remove(assetTag);
1111            }
1112    
1113            /**
1114             * Returns the number of asset tags where groupId = &#63; and name = &#63;.
1115             *
1116             * @param groupId the group ID
1117             * @param name the name
1118             * @return the number of matching asset tags
1119             */
1120            @Override
1121            public int countByG_N(long groupId, String name) {
1122                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_N;
1123    
1124                    Object[] finderArgs = new Object[] { groupId, name };
1125    
1126                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1127                                    this);
1128    
1129                    if (count == null) {
1130                            StringBundler query = new StringBundler(3);
1131    
1132                            query.append(_SQL_COUNT_ASSETTAG_WHERE);
1133    
1134                            query.append(_FINDER_COLUMN_G_N_GROUPID_2);
1135    
1136                            boolean bindName = false;
1137    
1138                            if (name == null) {
1139                                    query.append(_FINDER_COLUMN_G_N_NAME_1);
1140                            }
1141                            else if (name.equals(StringPool.BLANK)) {
1142                                    query.append(_FINDER_COLUMN_G_N_NAME_3);
1143                            }
1144                            else {
1145                                    bindName = true;
1146    
1147                                    query.append(_FINDER_COLUMN_G_N_NAME_2);
1148                            }
1149    
1150                            String sql = query.toString();
1151    
1152                            Session session = null;
1153    
1154                            try {
1155                                    session = openSession();
1156    
1157                                    Query q = session.createQuery(sql);
1158    
1159                                    QueryPos qPos = QueryPos.getInstance(q);
1160    
1161                                    qPos.add(groupId);
1162    
1163                                    if (bindName) {
1164                                            qPos.add(name);
1165                                    }
1166    
1167                                    count = (Long)q.uniqueResult();
1168    
1169                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1170                            }
1171                            catch (Exception e) {
1172                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1173    
1174                                    throw processException(e);
1175                            }
1176                            finally {
1177                                    closeSession(session);
1178                            }
1179                    }
1180    
1181                    return count.intValue();
1182            }
1183    
1184            private static final String _FINDER_COLUMN_G_N_GROUPID_2 = "assetTag.groupId = ? AND ";
1185            private static final String _FINDER_COLUMN_G_N_NAME_1 = "assetTag.name IS NULL";
1186            private static final String _FINDER_COLUMN_G_N_NAME_2 = "assetTag.name = ?";
1187            private static final String _FINDER_COLUMN_G_N_NAME_3 = "(assetTag.name IS NULL OR assetTag.name = '')";
1188    
1189            public AssetTagPersistenceImpl() {
1190                    setModelClass(AssetTag.class);
1191            }
1192    
1193            /**
1194             * Caches the asset tag in the entity cache if it is enabled.
1195             *
1196             * @param assetTag the asset tag
1197             */
1198            @Override
1199            public void cacheResult(AssetTag assetTag) {
1200                    EntityCacheUtil.putResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1201                            AssetTagImpl.class, assetTag.getPrimaryKey(), assetTag);
1202    
1203                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1204                            new Object[] { assetTag.getGroupId(), assetTag.getName() }, assetTag);
1205    
1206                    assetTag.resetOriginalValues();
1207            }
1208    
1209            /**
1210             * Caches the asset tags in the entity cache if it is enabled.
1211             *
1212             * @param assetTags the asset tags
1213             */
1214            @Override
1215            public void cacheResult(List<AssetTag> assetTags) {
1216                    for (AssetTag assetTag : assetTags) {
1217                            if (EntityCacheUtil.getResult(
1218                                                    AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1219                                                    AssetTagImpl.class, assetTag.getPrimaryKey()) == null) {
1220                                    cacheResult(assetTag);
1221                            }
1222                            else {
1223                                    assetTag.resetOriginalValues();
1224                            }
1225                    }
1226            }
1227    
1228            /**
1229             * Clears the cache for all asset tags.
1230             *
1231             * <p>
1232             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1233             * </p>
1234             */
1235            @Override
1236            public void clearCache() {
1237                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1238                            CacheRegistryUtil.clear(AssetTagImpl.class.getName());
1239                    }
1240    
1241                    EntityCacheUtil.clearCache(AssetTagImpl.class);
1242    
1243                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1244                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1245                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1246            }
1247    
1248            /**
1249             * Clears the cache for the asset tag.
1250             *
1251             * <p>
1252             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1253             * </p>
1254             */
1255            @Override
1256            public void clearCache(AssetTag assetTag) {
1257                    EntityCacheUtil.removeResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1258                            AssetTagImpl.class, assetTag.getPrimaryKey());
1259    
1260                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1261                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1262    
1263                    clearUniqueFindersCache(assetTag);
1264            }
1265    
1266            @Override
1267            public void clearCache(List<AssetTag> assetTags) {
1268                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1269                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1270    
1271                    for (AssetTag assetTag : assetTags) {
1272                            EntityCacheUtil.removeResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1273                                    AssetTagImpl.class, assetTag.getPrimaryKey());
1274    
1275                            clearUniqueFindersCache(assetTag);
1276                    }
1277            }
1278    
1279            protected void cacheUniqueFindersCache(AssetTag assetTag) {
1280                    if (assetTag.isNew()) {
1281                            Object[] args = new Object[] {
1282                                            assetTag.getGroupId(), assetTag.getName()
1283                                    };
1284    
1285                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_N, args,
1286                                    Long.valueOf(1));
1287                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N, args, assetTag);
1288                    }
1289                    else {
1290                            AssetTagModelImpl assetTagModelImpl = (AssetTagModelImpl)assetTag;
1291    
1292                            if ((assetTagModelImpl.getColumnBitmask() &
1293                                            FINDER_PATH_FETCH_BY_G_N.getColumnBitmask()) != 0) {
1294                                    Object[] args = new Object[] {
1295                                                    assetTag.getGroupId(), assetTag.getName()
1296                                            };
1297    
1298                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_N, args,
1299                                            Long.valueOf(1));
1300                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N, args,
1301                                            assetTag);
1302                            }
1303                    }
1304            }
1305    
1306            protected void clearUniqueFindersCache(AssetTag assetTag) {
1307                    AssetTagModelImpl assetTagModelImpl = (AssetTagModelImpl)assetTag;
1308    
1309                    Object[] args = new Object[] { assetTag.getGroupId(), assetTag.getName() };
1310    
1311                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_N, args);
1312                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N, args);
1313    
1314                    if ((assetTagModelImpl.getColumnBitmask() &
1315                                    FINDER_PATH_FETCH_BY_G_N.getColumnBitmask()) != 0) {
1316                            args = new Object[] {
1317                                            assetTagModelImpl.getOriginalGroupId(),
1318                                            assetTagModelImpl.getOriginalName()
1319                                    };
1320    
1321                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_N, args);
1322                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N, args);
1323                    }
1324            }
1325    
1326            /**
1327             * Creates a new asset tag with the primary key. Does not add the asset tag to the database.
1328             *
1329             * @param tagId the primary key for the new asset tag
1330             * @return the new asset tag
1331             */
1332            @Override
1333            public AssetTag create(long tagId) {
1334                    AssetTag assetTag = new AssetTagImpl();
1335    
1336                    assetTag.setNew(true);
1337                    assetTag.setPrimaryKey(tagId);
1338    
1339                    return assetTag;
1340            }
1341    
1342            /**
1343             * Removes the asset tag with the primary key from the database. Also notifies the appropriate model listeners.
1344             *
1345             * @param tagId the primary key of the asset tag
1346             * @return the asset tag that was removed
1347             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
1348             */
1349            @Override
1350            public AssetTag remove(long tagId) throws NoSuchTagException {
1351                    return remove((Serializable)tagId);
1352            }
1353    
1354            /**
1355             * Removes the asset tag with the primary key from the database. Also notifies the appropriate model listeners.
1356             *
1357             * @param primaryKey the primary key of the asset tag
1358             * @return the asset tag that was removed
1359             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
1360             */
1361            @Override
1362            public AssetTag remove(Serializable primaryKey) throws NoSuchTagException {
1363                    Session session = null;
1364    
1365                    try {
1366                            session = openSession();
1367    
1368                            AssetTag assetTag = (AssetTag)session.get(AssetTagImpl.class,
1369                                            primaryKey);
1370    
1371                            if (assetTag == null) {
1372                                    if (_log.isWarnEnabled()) {
1373                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1374                                    }
1375    
1376                                    throw new NoSuchTagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1377                                            primaryKey);
1378                            }
1379    
1380                            return remove(assetTag);
1381                    }
1382                    catch (NoSuchTagException nsee) {
1383                            throw nsee;
1384                    }
1385                    catch (Exception e) {
1386                            throw processException(e);
1387                    }
1388                    finally {
1389                            closeSession(session);
1390                    }
1391            }
1392    
1393            @Override
1394            protected AssetTag removeImpl(AssetTag assetTag) {
1395                    assetTag = toUnwrappedModel(assetTag);
1396    
1397                    assetTagToAssetEntryTableMapper.deleteLeftPrimaryKeyTableMappings(assetTag.getPrimaryKey());
1398    
1399                    Session session = null;
1400    
1401                    try {
1402                            session = openSession();
1403    
1404                            if (!session.contains(assetTag)) {
1405                                    assetTag = (AssetTag)session.get(AssetTagImpl.class,
1406                                                    assetTag.getPrimaryKeyObj());
1407                            }
1408    
1409                            if (assetTag != null) {
1410                                    session.delete(assetTag);
1411                            }
1412                    }
1413                    catch (Exception e) {
1414                            throw processException(e);
1415                    }
1416                    finally {
1417                            closeSession(session);
1418                    }
1419    
1420                    if (assetTag != null) {
1421                            clearCache(assetTag);
1422                    }
1423    
1424                    return assetTag;
1425            }
1426    
1427            @Override
1428            public AssetTag updateImpl(
1429                    com.liferay.portlet.asset.model.AssetTag assetTag) {
1430                    assetTag = toUnwrappedModel(assetTag);
1431    
1432                    boolean isNew = assetTag.isNew();
1433    
1434                    AssetTagModelImpl assetTagModelImpl = (AssetTagModelImpl)assetTag;
1435    
1436                    Session session = null;
1437    
1438                    try {
1439                            session = openSession();
1440    
1441                            if (assetTag.isNew()) {
1442                                    session.save(assetTag);
1443    
1444                                    assetTag.setNew(false);
1445                            }
1446                            else {
1447                                    session.merge(assetTag);
1448                            }
1449                    }
1450                    catch (Exception e) {
1451                            throw processException(e);
1452                    }
1453                    finally {
1454                            closeSession(session);
1455                    }
1456    
1457                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1458    
1459                    if (isNew || !AssetTagModelImpl.COLUMN_BITMASK_ENABLED) {
1460                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1461                    }
1462    
1463                    else {
1464                            if ((assetTagModelImpl.getColumnBitmask() &
1465                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
1466                                    Object[] args = new Object[] {
1467                                                    assetTagModelImpl.getOriginalGroupId()
1468                                            };
1469    
1470                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1471                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1472                                            args);
1473    
1474                                    args = new Object[] { assetTagModelImpl.getGroupId() };
1475    
1476                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1477                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1478                                            args);
1479                            }
1480                    }
1481    
1482                    EntityCacheUtil.putResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1483                            AssetTagImpl.class, assetTag.getPrimaryKey(), assetTag, false);
1484    
1485                    clearUniqueFindersCache(assetTag);
1486                    cacheUniqueFindersCache(assetTag);
1487    
1488                    assetTag.resetOriginalValues();
1489    
1490                    return assetTag;
1491            }
1492    
1493            protected AssetTag toUnwrappedModel(AssetTag assetTag) {
1494                    if (assetTag instanceof AssetTagImpl) {
1495                            return assetTag;
1496                    }
1497    
1498                    AssetTagImpl assetTagImpl = new AssetTagImpl();
1499    
1500                    assetTagImpl.setNew(assetTag.isNew());
1501                    assetTagImpl.setPrimaryKey(assetTag.getPrimaryKey());
1502    
1503                    assetTagImpl.setTagId(assetTag.getTagId());
1504                    assetTagImpl.setGroupId(assetTag.getGroupId());
1505                    assetTagImpl.setCompanyId(assetTag.getCompanyId());
1506                    assetTagImpl.setUserId(assetTag.getUserId());
1507                    assetTagImpl.setUserName(assetTag.getUserName());
1508                    assetTagImpl.setCreateDate(assetTag.getCreateDate());
1509                    assetTagImpl.setModifiedDate(assetTag.getModifiedDate());
1510                    assetTagImpl.setName(assetTag.getName());
1511                    assetTagImpl.setAssetCount(assetTag.getAssetCount());
1512    
1513                    return assetTagImpl;
1514            }
1515    
1516            /**
1517             * Returns the asset tag with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1518             *
1519             * @param primaryKey the primary key of the asset tag
1520             * @return the asset tag
1521             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
1522             */
1523            @Override
1524            public AssetTag findByPrimaryKey(Serializable primaryKey)
1525                    throws NoSuchTagException {
1526                    AssetTag assetTag = fetchByPrimaryKey(primaryKey);
1527    
1528                    if (assetTag == null) {
1529                            if (_log.isWarnEnabled()) {
1530                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1531                            }
1532    
1533                            throw new NoSuchTagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1534                                    primaryKey);
1535                    }
1536    
1537                    return assetTag;
1538            }
1539    
1540            /**
1541             * Returns the asset tag with the primary key or throws a {@link com.liferay.portlet.asset.NoSuchTagException} if it could not be found.
1542             *
1543             * @param tagId the primary key of the asset tag
1544             * @return the asset tag
1545             * @throws com.liferay.portlet.asset.NoSuchTagException if a asset tag with the primary key could not be found
1546             */
1547            @Override
1548            public AssetTag findByPrimaryKey(long tagId) throws NoSuchTagException {
1549                    return findByPrimaryKey((Serializable)tagId);
1550            }
1551    
1552            /**
1553             * Returns the asset tag with the primary key or returns <code>null</code> if it could not be found.
1554             *
1555             * @param primaryKey the primary key of the asset tag
1556             * @return the asset tag, or <code>null</code> if a asset tag with the primary key could not be found
1557             */
1558            @Override
1559            public AssetTag fetchByPrimaryKey(Serializable primaryKey) {
1560                    AssetTag assetTag = (AssetTag)EntityCacheUtil.getResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1561                                    AssetTagImpl.class, primaryKey);
1562    
1563                    if (assetTag == _nullAssetTag) {
1564                            return null;
1565                    }
1566    
1567                    if (assetTag == null) {
1568                            Session session = null;
1569    
1570                            try {
1571                                    session = openSession();
1572    
1573                                    assetTag = (AssetTag)session.get(AssetTagImpl.class, primaryKey);
1574    
1575                                    if (assetTag != null) {
1576                                            cacheResult(assetTag);
1577                                    }
1578                                    else {
1579                                            EntityCacheUtil.putResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1580                                                    AssetTagImpl.class, primaryKey, _nullAssetTag);
1581                                    }
1582                            }
1583                            catch (Exception e) {
1584                                    EntityCacheUtil.removeResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1585                                            AssetTagImpl.class, primaryKey);
1586    
1587                                    throw processException(e);
1588                            }
1589                            finally {
1590                                    closeSession(session);
1591                            }
1592                    }
1593    
1594                    return assetTag;
1595            }
1596    
1597            /**
1598             * Returns the asset tag with the primary key or returns <code>null</code> if it could not be found.
1599             *
1600             * @param tagId the primary key of the asset tag
1601             * @return the asset tag, or <code>null</code> if a asset tag with the primary key could not be found
1602             */
1603            @Override
1604            public AssetTag fetchByPrimaryKey(long tagId) {
1605                    return fetchByPrimaryKey((Serializable)tagId);
1606            }
1607    
1608            @Override
1609            public Map<Serializable, AssetTag> fetchByPrimaryKeys(
1610                    Set<Serializable> primaryKeys) {
1611                    if (primaryKeys.isEmpty()) {
1612                            return Collections.emptyMap();
1613                    }
1614    
1615                    Map<Serializable, AssetTag> map = new HashMap<Serializable, AssetTag>();
1616    
1617                    if (primaryKeys.size() == 1) {
1618                            Iterator<Serializable> iterator = primaryKeys.iterator();
1619    
1620                            Serializable primaryKey = iterator.next();
1621    
1622                            AssetTag assetTag = fetchByPrimaryKey(primaryKey);
1623    
1624                            if (assetTag != null) {
1625                                    map.put(primaryKey, assetTag);
1626                            }
1627    
1628                            return map;
1629                    }
1630    
1631                    Set<Serializable> uncachedPrimaryKeys = null;
1632    
1633                    for (Serializable primaryKey : primaryKeys) {
1634                            AssetTag assetTag = (AssetTag)EntityCacheUtil.getResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1635                                            AssetTagImpl.class, primaryKey);
1636    
1637                            if (assetTag == null) {
1638                                    if (uncachedPrimaryKeys == null) {
1639                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1640                                    }
1641    
1642                                    uncachedPrimaryKeys.add(primaryKey);
1643                            }
1644                            else {
1645                                    map.put(primaryKey, assetTag);
1646                            }
1647                    }
1648    
1649                    if (uncachedPrimaryKeys == null) {
1650                            return map;
1651                    }
1652    
1653                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1654                                    1);
1655    
1656                    query.append(_SQL_SELECT_ASSETTAG_WHERE_PKS_IN);
1657    
1658                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1659                            query.append(String.valueOf(primaryKey));
1660    
1661                            query.append(StringPool.COMMA);
1662                    }
1663    
1664                    query.setIndex(query.index() - 1);
1665    
1666                    query.append(StringPool.CLOSE_PARENTHESIS);
1667    
1668                    String sql = query.toString();
1669    
1670                    Session session = null;
1671    
1672                    try {
1673                            session = openSession();
1674    
1675                            Query q = session.createQuery(sql);
1676    
1677                            for (AssetTag assetTag : (List<AssetTag>)q.list()) {
1678                                    map.put(assetTag.getPrimaryKeyObj(), assetTag);
1679    
1680                                    cacheResult(assetTag);
1681    
1682                                    uncachedPrimaryKeys.remove(assetTag.getPrimaryKeyObj());
1683                            }
1684    
1685                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1686                                    EntityCacheUtil.putResult(AssetTagModelImpl.ENTITY_CACHE_ENABLED,
1687                                            AssetTagImpl.class, primaryKey, _nullAssetTag);
1688                            }
1689                    }
1690                    catch (Exception e) {
1691                            throw processException(e);
1692                    }
1693                    finally {
1694                            closeSession(session);
1695                    }
1696    
1697                    return map;
1698            }
1699    
1700            /**
1701             * Returns all the asset tags.
1702             *
1703             * @return the asset tags
1704             */
1705            @Override
1706            public List<AssetTag> findAll() {
1707                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1708            }
1709    
1710            /**
1711             * Returns a range of all the asset tags.
1712             *
1713             * <p>
1714             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1715             * </p>
1716             *
1717             * @param start the lower bound of the range of asset tags
1718             * @param end the upper bound of the range of asset tags (not inclusive)
1719             * @return the range of asset tags
1720             */
1721            @Override
1722            public List<AssetTag> findAll(int start, int end) {
1723                    return findAll(start, end, null);
1724            }
1725    
1726            /**
1727             * Returns an ordered range of all the asset tags.
1728             *
1729             * <p>
1730             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1731             * </p>
1732             *
1733             * @param start the lower bound of the range of asset tags
1734             * @param end the upper bound of the range of asset tags (not inclusive)
1735             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1736             * @return the ordered range of asset tags
1737             */
1738            @Override
1739            public List<AssetTag> findAll(int start, int end,
1740                    OrderByComparator<AssetTag> orderByComparator) {
1741                    boolean pagination = true;
1742                    FinderPath finderPath = null;
1743                    Object[] finderArgs = null;
1744    
1745                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1746                                    (orderByComparator == null)) {
1747                            pagination = false;
1748                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1749                            finderArgs = FINDER_ARGS_EMPTY;
1750                    }
1751                    else {
1752                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1753                            finderArgs = new Object[] { start, end, orderByComparator };
1754                    }
1755    
1756                    List<AssetTag> list = (List<AssetTag>)FinderCacheUtil.getResult(finderPath,
1757                                    finderArgs, this);
1758    
1759                    if (list == null) {
1760                            StringBundler query = null;
1761                            String sql = null;
1762    
1763                            if (orderByComparator != null) {
1764                                    query = new StringBundler(2 +
1765                                                    (orderByComparator.getOrderByFields().length * 3));
1766    
1767                                    query.append(_SQL_SELECT_ASSETTAG);
1768    
1769                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1770                                            orderByComparator);
1771    
1772                                    sql = query.toString();
1773                            }
1774                            else {
1775                                    sql = _SQL_SELECT_ASSETTAG;
1776    
1777                                    if (pagination) {
1778                                            sql = sql.concat(AssetTagModelImpl.ORDER_BY_JPQL);
1779                                    }
1780                            }
1781    
1782                            Session session = null;
1783    
1784                            try {
1785                                    session = openSession();
1786    
1787                                    Query q = session.createQuery(sql);
1788    
1789                                    if (!pagination) {
1790                                            list = (List<AssetTag>)QueryUtil.list(q, getDialect(),
1791                                                            start, end, false);
1792    
1793                                            Collections.sort(list);
1794    
1795                                            list = Collections.unmodifiableList(list);
1796                                    }
1797                                    else {
1798                                            list = (List<AssetTag>)QueryUtil.list(q, getDialect(),
1799                                                            start, end);
1800                                    }
1801    
1802                                    cacheResult(list);
1803    
1804                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1805                            }
1806                            catch (Exception e) {
1807                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1808    
1809                                    throw processException(e);
1810                            }
1811                            finally {
1812                                    closeSession(session);
1813                            }
1814                    }
1815    
1816                    return list;
1817            }
1818    
1819            /**
1820             * Removes all the asset tags from the database.
1821             *
1822             */
1823            @Override
1824            public void removeAll() {
1825                    for (AssetTag assetTag : findAll()) {
1826                            remove(assetTag);
1827                    }
1828            }
1829    
1830            /**
1831             * Returns the number of asset tags.
1832             *
1833             * @return the number of asset tags
1834             */
1835            @Override
1836            public int countAll() {
1837                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1838                                    FINDER_ARGS_EMPTY, this);
1839    
1840                    if (count == null) {
1841                            Session session = null;
1842    
1843                            try {
1844                                    session = openSession();
1845    
1846                                    Query q = session.createQuery(_SQL_COUNT_ASSETTAG);
1847    
1848                                    count = (Long)q.uniqueResult();
1849    
1850                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1851                                            FINDER_ARGS_EMPTY, count);
1852                            }
1853                            catch (Exception e) {
1854                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1855                                            FINDER_ARGS_EMPTY);
1856    
1857                                    throw processException(e);
1858                            }
1859                            finally {
1860                                    closeSession(session);
1861                            }
1862                    }
1863    
1864                    return count.intValue();
1865            }
1866    
1867            /**
1868             * Returns the primaryKeys of asset entries associated with the asset tag.
1869             *
1870             * @param pk the primary key of the asset tag
1871             * @return long[] of the primaryKeys of asset entries associated with the asset tag
1872             */
1873            @Override
1874            public long[] getAssetEntryPrimaryKeys(long pk) {
1875                    long[] pks = assetTagToAssetEntryTableMapper.getRightPrimaryKeys(pk);
1876    
1877                    return pks.clone();
1878            }
1879    
1880            /**
1881             * Returns all the asset entries associated with the asset tag.
1882             *
1883             * @param pk the primary key of the asset tag
1884             * @return the asset entries associated with the asset tag
1885             */
1886            @Override
1887            public List<com.liferay.portlet.asset.model.AssetEntry> getAssetEntries(
1888                    long pk) {
1889                    return getAssetEntries(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1890            }
1891    
1892            /**
1893             * Returns a range of all the asset entries associated with the asset tag.
1894             *
1895             * <p>
1896             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1897             * </p>
1898             *
1899             * @param pk the primary key of the asset tag
1900             * @param start the lower bound of the range of asset tags
1901             * @param end the upper bound of the range of asset tags (not inclusive)
1902             * @return the range of asset entries associated with the asset tag
1903             */
1904            @Override
1905            public List<com.liferay.portlet.asset.model.AssetEntry> getAssetEntries(
1906                    long pk, int start, int end) {
1907                    return getAssetEntries(pk, start, end, null);
1908            }
1909    
1910            /**
1911             * Returns an ordered range of all the asset entries associated with the asset tag.
1912             *
1913             * <p>
1914             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.asset.model.impl.AssetTagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1915             * </p>
1916             *
1917             * @param pk the primary key of the asset tag
1918             * @param start the lower bound of the range of asset tags
1919             * @param end the upper bound of the range of asset tags (not inclusive)
1920             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1921             * @return the ordered range of asset entries associated with the asset tag
1922             */
1923            @Override
1924            public List<com.liferay.portlet.asset.model.AssetEntry> getAssetEntries(
1925                    long pk, int start, int end,
1926                    OrderByComparator<com.liferay.portlet.asset.model.AssetEntry> orderByComparator) {
1927                    return assetTagToAssetEntryTableMapper.getRightBaseModels(pk, start,
1928                            end, orderByComparator);
1929            }
1930    
1931            /**
1932             * Returns the number of asset entries associated with the asset tag.
1933             *
1934             * @param pk the primary key of the asset tag
1935             * @return the number of asset entries associated with the asset tag
1936             */
1937            @Override
1938            public int getAssetEntriesSize(long pk) {
1939                    long[] pks = assetTagToAssetEntryTableMapper.getRightPrimaryKeys(pk);
1940    
1941                    return pks.length;
1942            }
1943    
1944            /**
1945             * Returns <code>true</code> if the asset entry is associated with the asset tag.
1946             *
1947             * @param pk the primary key of the asset tag
1948             * @param assetEntryPK the primary key of the asset entry
1949             * @return <code>true</code> if the asset entry is associated with the asset tag; <code>false</code> otherwise
1950             */
1951            @Override
1952            public boolean containsAssetEntry(long pk, long assetEntryPK) {
1953                    return assetTagToAssetEntryTableMapper.containsTableMapping(pk,
1954                            assetEntryPK);
1955            }
1956    
1957            /**
1958             * Returns <code>true</code> if the asset tag has any asset entries associated with it.
1959             *
1960             * @param pk the primary key of the asset tag to check for associations with asset entries
1961             * @return <code>true</code> if the asset tag has any asset entries associated with it; <code>false</code> otherwise
1962             */
1963            @Override
1964            public boolean containsAssetEntries(long pk) {
1965                    if (getAssetEntriesSize(pk) > 0) {
1966                            return true;
1967                    }
1968                    else {
1969                            return false;
1970                    }
1971            }
1972    
1973            /**
1974             * Adds an association between the asset tag and the asset entry. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1975             *
1976             * @param pk the primary key of the asset tag
1977             * @param assetEntryPK the primary key of the asset entry
1978             */
1979            @Override
1980            public void addAssetEntry(long pk, long assetEntryPK) {
1981                    assetTagToAssetEntryTableMapper.addTableMapping(pk, assetEntryPK);
1982            }
1983    
1984            /**
1985             * Adds an association between the asset tag and the asset entry. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1986             *
1987             * @param pk the primary key of the asset tag
1988             * @param assetEntry the asset entry
1989             */
1990            @Override
1991            public void addAssetEntry(long pk,
1992                    com.liferay.portlet.asset.model.AssetEntry assetEntry) {
1993                    assetTagToAssetEntryTableMapper.addTableMapping(pk,
1994                            assetEntry.getPrimaryKey());
1995            }
1996    
1997            /**
1998             * Adds an association between the asset tag and the asset entries. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1999             *
2000             * @param pk the primary key of the asset tag
2001             * @param assetEntryPKs the primary keys of the asset entries
2002             */
2003            @Override
2004            public void addAssetEntries(long pk, long[] assetEntryPKs) {
2005                    for (long assetEntryPK : assetEntryPKs) {
2006                            assetTagToAssetEntryTableMapper.addTableMapping(pk, assetEntryPK);
2007                    }
2008            }
2009    
2010            /**
2011             * Adds an association between the asset tag and the asset entries. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2012             *
2013             * @param pk the primary key of the asset tag
2014             * @param assetEntries the asset entries
2015             */
2016            @Override
2017            public void addAssetEntries(long pk,
2018                    List<com.liferay.portlet.asset.model.AssetEntry> assetEntries) {
2019                    for (com.liferay.portlet.asset.model.AssetEntry assetEntry : assetEntries) {
2020                            assetTagToAssetEntryTableMapper.addTableMapping(pk,
2021                                    assetEntry.getPrimaryKey());
2022                    }
2023            }
2024    
2025            /**
2026             * Clears all associations between the asset tag and its asset entries. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2027             *
2028             * @param pk the primary key of the asset tag to clear the associated asset entries from
2029             */
2030            @Override
2031            public void clearAssetEntries(long pk) {
2032                    assetTagToAssetEntryTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
2033            }
2034    
2035            /**
2036             * Removes the association between the asset tag and the asset entry. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2037             *
2038             * @param pk the primary key of the asset tag
2039             * @param assetEntryPK the primary key of the asset entry
2040             */
2041            @Override
2042            public void removeAssetEntry(long pk, long assetEntryPK) {
2043                    assetTagToAssetEntryTableMapper.deleteTableMapping(pk, assetEntryPK);
2044            }
2045    
2046            /**
2047             * Removes the association between the asset tag and the asset entry. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2048             *
2049             * @param pk the primary key of the asset tag
2050             * @param assetEntry the asset entry
2051             */
2052            @Override
2053            public void removeAssetEntry(long pk,
2054                    com.liferay.portlet.asset.model.AssetEntry assetEntry) {
2055                    assetTagToAssetEntryTableMapper.deleteTableMapping(pk,
2056                            assetEntry.getPrimaryKey());
2057            }
2058    
2059            /**
2060             * Removes the association between the asset tag and the asset entries. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2061             *
2062             * @param pk the primary key of the asset tag
2063             * @param assetEntryPKs the primary keys of the asset entries
2064             */
2065            @Override
2066            public void removeAssetEntries(long pk, long[] assetEntryPKs) {
2067                    for (long assetEntryPK : assetEntryPKs) {
2068                            assetTagToAssetEntryTableMapper.deleteTableMapping(pk, assetEntryPK);
2069                    }
2070            }
2071    
2072            /**
2073             * Removes the association between the asset tag and the asset entries. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2074             *
2075             * @param pk the primary key of the asset tag
2076             * @param assetEntries the asset entries
2077             */
2078            @Override
2079            public void removeAssetEntries(long pk,
2080                    List<com.liferay.portlet.asset.model.AssetEntry> assetEntries) {
2081                    for (com.liferay.portlet.asset.model.AssetEntry assetEntry : assetEntries) {
2082                            assetTagToAssetEntryTableMapper.deleteTableMapping(pk,
2083                                    assetEntry.getPrimaryKey());
2084                    }
2085            }
2086    
2087            /**
2088             * Sets the asset entries associated with the asset tag, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2089             *
2090             * @param pk the primary key of the asset tag
2091             * @param assetEntryPKs the primary keys of the asset entries to be associated with the asset tag
2092             */
2093            @Override
2094            public void setAssetEntries(long pk, long[] assetEntryPKs) {
2095                    Set<Long> newAssetEntryPKsSet = SetUtil.fromArray(assetEntryPKs);
2096                    Set<Long> oldAssetEntryPKsSet = SetUtil.fromArray(assetTagToAssetEntryTableMapper.getRightPrimaryKeys(
2097                                            pk));
2098    
2099                    Set<Long> removeAssetEntryPKsSet = new HashSet<Long>(oldAssetEntryPKsSet);
2100    
2101                    removeAssetEntryPKsSet.removeAll(newAssetEntryPKsSet);
2102    
2103                    for (long removeAssetEntryPK : removeAssetEntryPKsSet) {
2104                            assetTagToAssetEntryTableMapper.deleteTableMapping(pk,
2105                                    removeAssetEntryPK);
2106                    }
2107    
2108                    newAssetEntryPKsSet.removeAll(oldAssetEntryPKsSet);
2109    
2110                    for (long newAssetEntryPK : newAssetEntryPKsSet) {
2111                            assetTagToAssetEntryTableMapper.addTableMapping(pk, newAssetEntryPK);
2112                    }
2113            }
2114    
2115            /**
2116             * Sets the asset entries associated with the asset tag, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2117             *
2118             * @param pk the primary key of the asset tag
2119             * @param assetEntries the asset entries to be associated with the asset tag
2120             */
2121            @Override
2122            public void setAssetEntries(long pk,
2123                    List<com.liferay.portlet.asset.model.AssetEntry> assetEntries) {
2124                    try {
2125                            long[] assetEntryPKs = new long[assetEntries.size()];
2126    
2127                            for (int i = 0; i < assetEntries.size(); i++) {
2128                                    com.liferay.portlet.asset.model.AssetEntry assetEntry = assetEntries.get(i);
2129    
2130                                    assetEntryPKs[i] = assetEntry.getPrimaryKey();
2131                            }
2132    
2133                            setAssetEntries(pk, assetEntryPKs);
2134                    }
2135                    catch (Exception e) {
2136                            throw processException(e);
2137                    }
2138            }
2139    
2140            /**
2141             * Initializes the asset tag persistence.
2142             */
2143            public void afterPropertiesSet() {
2144                    assetTagToAssetEntryTableMapper = TableMapperFactory.getTableMapper("AssetEntries_AssetTags",
2145                                    "tagId", "entryId", this, assetEntryPersistence);
2146            }
2147    
2148            public void destroy() {
2149                    EntityCacheUtil.removeCache(AssetTagImpl.class.getName());
2150                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2151                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2152                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2153    
2154                    TableMapperFactory.removeTableMapper("AssetEntries_AssetTags");
2155            }
2156    
2157            @BeanReference(type = AssetEntryPersistence.class)
2158            protected AssetEntryPersistence assetEntryPersistence;
2159            protected TableMapper<AssetTag, com.liferay.portlet.asset.model.AssetEntry> assetTagToAssetEntryTableMapper;
2160            private static final String _SQL_SELECT_ASSETTAG = "SELECT assetTag FROM AssetTag assetTag";
2161            private static final String _SQL_SELECT_ASSETTAG_WHERE_PKS_IN = "SELECT assetTag FROM AssetTag assetTag WHERE tagId IN (";
2162            private static final String _SQL_SELECT_ASSETTAG_WHERE = "SELECT assetTag FROM AssetTag assetTag WHERE ";
2163            private static final String _SQL_COUNT_ASSETTAG = "SELECT COUNT(assetTag) FROM AssetTag assetTag";
2164            private static final String _SQL_COUNT_ASSETTAG_WHERE = "SELECT COUNT(assetTag) FROM AssetTag assetTag WHERE ";
2165            private static final String _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN = "assetTag.tagId";
2166            private static final String _FILTER_SQL_SELECT_ASSETTAG_WHERE = "SELECT DISTINCT {assetTag.*} FROM AssetTag assetTag WHERE ";
2167            private static final String _FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_1 =
2168                    "SELECT {AssetTag.*} FROM (SELECT DISTINCT assetTag.tagId FROM AssetTag assetTag WHERE ";
2169            private static final String _FILTER_SQL_SELECT_ASSETTAG_NO_INLINE_DISTINCT_WHERE_2 =
2170                    ") TEMP_TABLE INNER JOIN AssetTag ON TEMP_TABLE.tagId = AssetTag.tagId";
2171            private static final String _FILTER_SQL_COUNT_ASSETTAG_WHERE = "SELECT COUNT(DISTINCT assetTag.tagId) AS COUNT_VALUE FROM AssetTag assetTag WHERE ";
2172            private static final String _FILTER_ENTITY_ALIAS = "assetTag";
2173            private static final String _FILTER_ENTITY_TABLE = "AssetTag";
2174            private static final String _ORDER_BY_ENTITY_ALIAS = "assetTag.";
2175            private static final String _ORDER_BY_ENTITY_TABLE = "AssetTag.";
2176            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AssetTag exists with the primary key ";
2177            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AssetTag exists with the key {";
2178            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
2179            private static final Log _log = LogFactoryUtil.getLog(AssetTagPersistenceImpl.class);
2180            private static final AssetTag _nullAssetTag = new AssetTagImpl() {
2181                            @Override
2182                            public Object clone() {
2183                                    return this;
2184                            }
2185    
2186                            @Override
2187                            public CacheModel<AssetTag> toCacheModel() {
2188                                    return _nullAssetTagCacheModel;
2189                            }
2190                    };
2191    
2192            private static final CacheModel<AssetTag> _nullAssetTagCacheModel = new CacheModel<AssetTag>() {
2193                            @Override
2194                            public AssetTag toEntityModel() {
2195                                    return _nullAssetTag;
2196                            }
2197                    };
2198    }