001    /**
002     * Copyright (c) 2000-2013 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;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.CharPool;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.asset.NoSuchTagException;
033    import com.liferay.portlet.asset.model.AssetTag;
034    import com.liferay.portlet.asset.model.impl.AssetEntryModelImpl;
035    import com.liferay.portlet.asset.model.impl.AssetTagImpl;
036    import com.liferay.portlet.asset.model.impl.AssetTagModelImpl;
037    import com.liferay.util.dao.orm.CustomSQLUtil;
038    
039    import java.util.ArrayList;
040    import java.util.Iterator;
041    import java.util.List;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Bruno Farache
046     */
047    public class AssetTagFinderImpl
048            extends BasePersistenceImpl<AssetTag> implements AssetTagFinder {
049    
050            public static final String COUNT_BY_G_N =
051                    AssetTagFinder.class.getName() + ".countByG_N";
052    
053            public static final String COUNT_BY_G_C_N =
054                    AssetTagFinder.class.getName() + ".countByG_C_N";
055    
056            public static final String COUNT_BY_G_N_P =
057                    AssetTagFinder.class.getName() + ".countByG_N_P";
058    
059            public static final String FIND_BY_G_N =
060                    AssetTagFinder.class.getName() + ".findByG_N";
061    
062            public static final String FIND_BY_G_C_N =
063                    AssetTagFinder.class.getName() + ".findByG_C_N";
064    
065            public static final String FIND_BY_G_N_P =
066                    AssetTagFinder.class.getName() + ".findByG_N_P";
067    
068            public static final String FIND_BY_G_N_S_E =
069                            AssetTagFinder.class.getName() + ".findByG_N_S_E";
070    
071            @Override
072            public int countByG_C_N(long groupId, long classNameId, String name)
073                    throws SystemException {
074    
075                    return doCountByG_C_N(groupId, classNameId, name, false);
076            }
077    
078            @Override
079            public int countByG_N_P(long groupId, String name, String[] tagProperties)
080                    throws SystemException {
081    
082                    return doCountByG_N_P(groupId, name, tagProperties, false);
083            }
084    
085            @Override
086            public int filterCountByG_N(long groupId, String name)
087                    throws SystemException {
088    
089                    return doCountByG_N(groupId, name, true);
090            }
091    
092            @Override
093            public int filterCountByG_C_N(long groupId, long classNameId, String name)
094                    throws SystemException {
095    
096                    return doCountByG_C_N(groupId, classNameId, name, true);
097            }
098    
099            @Override
100            public int filterCountByG_N_P(
101                            long groupId, String name, String[] tagProperties)
102                    throws SystemException {
103    
104                    return doCountByG_N_P(groupId, name, tagProperties, true);
105            }
106    
107            @Override
108            public AssetTag filterFindByG_N(long groupId, String name)
109                    throws NoSuchTagException, SystemException {
110    
111                    return doFindByG_N(groupId, name, true);
112            }
113    
114            @Override
115            public List<AssetTag> filterFindByG_C_N(
116                            long groupId, long classNameId, String name, int start, int end,
117                            OrderByComparator obc)
118                    throws SystemException {
119    
120                    return doFindByG_C_N(groupId, classNameId, name, start, end, obc, true);
121            }
122    
123            @Override
124            public List<AssetTag> filterFindByG_N_P(
125                            long[] groupIds, String name, String[] tagProperties, int start,
126                            int end, OrderByComparator obc)
127                    throws SystemException {
128    
129                    return doFindByG_N_P(
130                            groupIds, name, tagProperties, start, end, obc, true);
131            }
132    
133            @Override
134            public AssetTag findByG_N(long groupId, String name)
135                    throws NoSuchTagException, SystemException {
136    
137                    return doFindByG_N(groupId, name, false);
138            }
139    
140            @Override
141            public List<AssetTag> findByG_C_N(
142                            long groupId, long classNameId, String name, int start, int end,
143                            OrderByComparator obc)
144                    throws SystemException {
145    
146                    return doFindByG_C_N(
147                            groupId, classNameId, name, start, end, obc, false);
148            }
149    
150            @Override
151            public List<AssetTag> findByG_N_P(
152                            long[] groupIds, String name, String[] tagProperties, int start,
153                            int end, OrderByComparator obc)
154                    throws SystemException {
155    
156                    return doFindByG_N_P(
157                            groupIds, name, tagProperties, start, end, obc, false);
158            }
159    
160            @Override
161            public List<AssetTag> findByG_N_S_E(
162                            long groupId, String name, int startPeriod, int endPeriod,
163                            int periodLength)
164                    throws SystemException {
165    
166                    Session session = null;
167    
168                    try {
169                            session = openSession();
170    
171                            String sql = CustomSQLUtil.get(FIND_BY_G_N_S_E);
172                            SQLQuery q = session.createSQLQuery(sql);
173    
174                            QueryPos qPos = QueryPos.getInstance(q);
175    
176                            qPos.add(groupId);
177                            qPos.add(name);
178                            qPos.add(startPeriod);
179                            qPos.add(endPeriod);
180                            qPos.add(periodLength);
181                            qPos.add(endPeriod);
182    
183                            List<AssetTag> assetTags = new ArrayList<AssetTag>();
184    
185                            Iterator<Object[]> itr = q.iterate();
186    
187                            while (itr.hasNext()) {
188                                    Object[] array = itr.next();
189    
190                                    AssetTag assetTag = new AssetTagImpl();
191    
192                                    assetTag.setTagId(GetterUtil.getLong(array[0]));
193                                    assetTag.setName(GetterUtil.getString(array[1]));
194                                    assetTag.setAssetCount(GetterUtil.getInteger(array[2]));
195    
196                                    assetTags.add(assetTag);
197                            }
198    
199                            return assetTags;
200                    }
201                    catch (Exception e) {
202                            throw new SystemException(e);
203                    }
204                    finally {
205                            closeSession(session);
206                    }
207            }
208    
209            protected int doCountByG_N(
210                            long groupId, String name, boolean inlineSQLHelper)
211                    throws SystemException {
212    
213                    Session session = null;
214    
215                    try {
216                            session = openSession();
217    
218                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
219    
220                            if (inlineSQLHelper) {
221                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
222                                            sql, AssetTag.class.getName(), "AssetTag.tagId",
223                                            PortalUtil.getSiteGroupId(groupId));
224                            }
225    
226                            SQLQuery q = session.createSQLQuery(sql);
227    
228                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
229    
230                            QueryPos qPos = QueryPos.getInstance(q);
231    
232                            qPos.add(groupId);
233                            qPos.add(name);
234    
235                            Iterator<Long> itr = q.iterate();
236    
237                            if (itr.hasNext()) {
238                                    Long count = itr.next();
239    
240                                    if (count != null) {
241                                            return count.intValue();
242                                    }
243                            }
244    
245                            return 0;
246                    }
247                    catch (Exception e) {
248                            throw new SystemException(e);
249                    }
250                    finally {
251                            closeSession(session);
252                    }
253            }
254    
255            protected int doCountByG_C_N(
256                            long groupId, long classNameId, String name,
257                            boolean inlineSQLHelper)
258                    throws SystemException {
259    
260                    Session session = null;
261    
262                    try {
263                            session = openSession();
264    
265                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
266    
267                            if (inlineSQLHelper) {
268                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
269                                            sql, AssetTag.class.getName(), "AssetTag.tagId",
270                                            PortalUtil.getSiteGroupId(groupId));
271                            }
272    
273                            SQLQuery q = session.createSQLQuery(sql);
274    
275                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
276    
277                            q.addSynchronizedQuerySpace(AssetEntryModelImpl.TABLE_NAME);
278                            q.addSynchronizedQuerySpace(
279                                    AssetEntryModelImpl.MAPPING_TABLE_ASSETENTRIES_ASSETTAGS_NAME);
280                            q.addSynchronizedQuerySpace(AssetTagModelImpl.TABLE_NAME);
281    
282                            QueryPos qPos = QueryPos.getInstance(q);
283    
284                            qPos.add(groupId);
285                            qPos.add(classNameId);
286                            qPos.add(name);
287                            qPos.add(name);
288    
289                            Iterator<Long> itr = q.iterate();
290    
291                            if (itr.hasNext()) {
292                                    Long count = itr.next();
293    
294                                    if (count != null) {
295                                            return count.intValue();
296                                    }
297                            }
298    
299                            return 0;
300                    }
301                    catch (Exception e) {
302                            throw new SystemException(e);
303                    }
304                    finally {
305                            closeSession(session);
306                    }
307            }
308    
309            protected int doCountByG_N_P(
310                            long groupId, String name, String[] tagProperties,
311                            boolean inlineSQLHelper)
312                    throws SystemException {
313    
314                    Session session = null;
315    
316                    try {
317                            session = openSession();
318    
319                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
320    
321                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(tagProperties));
322    
323                            if (inlineSQLHelper) {
324                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
325                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
326                            }
327    
328                            SQLQuery q = session.createSQLQuery(sql);
329    
330                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
331    
332                            QueryPos qPos = QueryPos.getInstance(q);
333    
334                            setJoin(qPos, tagProperties);
335    
336                            qPos.add(groupId);
337                            qPos.add(name);
338                            qPos.add(name);
339    
340                            Iterator<Long> itr = q.iterate();
341    
342                            if (itr.hasNext()) {
343                                    Long count = itr.next();
344    
345                                    if (count != null) {
346                                            return count.intValue();
347                                    }
348                            }
349    
350                            return 0;
351                    }
352                    catch (Exception e) {
353                            throw new SystemException(e);
354                    }
355                    finally {
356                            closeSession(session);
357                    }
358            }
359    
360            protected AssetTag doFindByG_N(
361                            long groupId, String name, boolean inlineSQLHelper)
362                    throws NoSuchTagException, SystemException {
363    
364                    name = StringUtil.toLowerCase(name.trim());
365    
366                    Session session = null;
367    
368                    try {
369                            session = openSession();
370    
371                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
372    
373                            if (inlineSQLHelper) {
374                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
375                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
376                            }
377    
378                            SQLQuery q = session.createSQLQuery(sql);
379    
380                            q.addEntity("AssetTag", AssetTagImpl.class);
381    
382                            QueryPos qPos = QueryPos.getInstance(q);
383    
384                            qPos.add(groupId);
385                            qPos.add(name);
386    
387                            List<AssetTag> tags = q.list();
388    
389                            if (!tags.isEmpty()) {
390                                    return tags.get(0);
391                            }
392                    }
393                    catch (Exception e) {
394                            throw new SystemException(e);
395                    }
396                    finally {
397                            closeSession(session);
398                    }
399    
400                    StringBundler sb = new StringBundler(6);
401    
402                    sb.append("No AssetTag exists with the key ");
403                    sb.append("{groupId=");
404                    sb.append(groupId);
405                    sb.append(", name=");
406                    sb.append(name);
407                    sb.append("}");
408    
409                    throw new NoSuchTagException(sb.toString());
410            }
411    
412            protected List<AssetTag> doFindByG_C_N(
413                            long groupId, long classNameId, String name, int start, int end,
414                            OrderByComparator obc, boolean inlineSQLHelper)
415                    throws SystemException {
416    
417                    Session session = null;
418    
419                    try {
420                            session = openSession();
421    
422                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N);
423    
424                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
425    
426                            if (inlineSQLHelper) {
427                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
428                                            sql, AssetTag.class.getName(), "AssetTag.tagId",
429                                            PortalUtil.getSiteGroupId(groupId));
430                            }
431    
432                            SQLQuery q = session.createSQLQuery(sql);
433    
434                            q.addEntity("AssetTag", AssetTagImpl.class);
435    
436                            QueryPos qPos = QueryPos.getInstance(q);
437    
438                            qPos.add(groupId);
439                            qPos.add(classNameId);
440                            qPos.add(name);
441                            qPos.add(name);
442    
443                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
444                    }
445                    catch (Exception e) {
446                            throw new SystemException(e);
447                    }
448                    finally {
449                            closeSession(session);
450                    }
451            }
452    
453            protected List<AssetTag> doFindByG_N_P(
454                            long[] groupIds, String name, String[] tagProperties, int start,
455                            int end, OrderByComparator obc, boolean inlineSQLHelper)
456                    throws SystemException {
457    
458                    Session session = null;
459    
460                    try {
461                            session = openSession();
462    
463                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
464    
465                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(tagProperties));
466                            sql = StringUtil.replace(
467                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
468                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
469    
470                            if (inlineSQLHelper) {
471                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
472                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupIds);
473                            }
474    
475                            SQLQuery q = session.createSQLQuery(sql);
476    
477                            q.addEntity("AssetTag", AssetTagImpl.class);
478    
479                            QueryPos qPos = QueryPos.getInstance(q);
480    
481                            setJoin(qPos, tagProperties);
482    
483                            qPos.add(groupIds);
484                            qPos.add(name);
485                            qPos.add(name);
486    
487                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
488                    }
489                    catch (Exception e) {
490                            throw new SystemException(e);
491                    }
492                    finally {
493                            closeSession(session);
494                    }
495            }
496    
497            protected String getGroupIds(long[] groupIds) {
498                    if (groupIds.length == 0) {
499                            return StringPool.BLANK;
500                    }
501    
502                    StringBundler sb = new StringBundler(groupIds.length * 2);
503    
504                    sb.append(StringPool.OPEN_PARENTHESIS);
505    
506                    for (int i = 0; i < groupIds.length; i++) {
507                            sb.append("groupId = ?");
508    
509                            if ((i + 1) < groupIds.length) {
510                                    sb.append(" OR ");
511                            }
512                    }
513    
514                    sb.append(") AND");
515    
516                    return sb.toString();
517            }
518    
519            protected String getJoin(String[] tagProperties) {
520                    if (tagProperties.length == 0) {
521                            return StringPool.BLANK;
522                    }
523    
524                    StringBundler sb = new StringBundler(tagProperties.length * 3 + 1);
525    
526                    sb.append(" INNER JOIN AssetTagProperty ON ");
527                    sb.append(" (AssetTagProperty.tagId = AssetTag.tagId) AND ");
528    
529                    for (int i = 0; i < tagProperties.length; i++) {
530                            sb.append("(AssetTagProperty.key_ = ? AND ");
531                            sb.append("AssetTagProperty.value = ?) ");
532    
533                            if ((i + 1) < tagProperties.length) {
534                                    sb.append(" AND ");
535                            }
536                    }
537    
538                    return sb.toString();
539            }
540    
541            protected void setJoin(QueryPos qPos, String[] tagProperties) {
542                    for (String tagProperty : tagProperties) {
543                            String[] tagPropertyParts = StringUtil.split(
544                                    tagProperty, CharPool.COLON);
545    
546                            String key = StringPool.BLANK;
547    
548                            if (tagPropertyParts.length > 0) {
549                                    key = GetterUtil.getString(tagPropertyParts[0]);
550                            }
551    
552                            String value = StringPool.BLANK;
553    
554                            if (tagPropertyParts.length > 1) {
555                                    value = GetterUtil.getString(tagPropertyParts[1]);
556                            }
557    
558                            qPos.add(key);
559                            qPos.add(value);
560                    }
561            }
562    
563    }