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