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