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