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