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