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 String COUNT_BY_G_N =
050                    AssetTagFinder.class.getName() + ".countByG_N";
051    
052            public static String COUNT_BY_G_C_N =
053                    AssetTagFinder.class.getName() + ".countByG_C_N";
054    
055            public static String COUNT_BY_G_N_P =
056                    AssetTagFinder.class.getName() + ".countByG_N_P";
057    
058            public static String FIND_BY_ENTRY_ID =
059                    AssetTagFinder.class.getName() + ".findByEntryId";
060    
061            public static String FIND_BY_G_N =
062                    AssetTagFinder.class.getName() + ".findByG_N";
063    
064            public static String FIND_BY_C_C =
065                    AssetTagFinder.class.getName() + ".findByC_C";
066    
067            public static String FIND_BY_G_C_N =
068                    AssetTagFinder.class.getName() + ".findByG_C_N";
069    
070            public static String FIND_BY_G_N_P =
071                    AssetTagFinder.class.getName() + ".findByG_N_P";
072    
073            public static 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 String getJoin(String[] tagProperties) {
267                    if (tagProperties.length == 0) {
268                            return StringPool.BLANK;
269                    }
270                    else {
271                            StringBundler sb = new StringBundler(tagProperties.length * 3 + 1);
272    
273                            sb.append(" INNER JOIN AssetTagProperty ON ");
274                            sb.append(" (AssetTagProperty.tagId = AssetTag.tagId) AND ");
275    
276                            for (int i = 0; i < tagProperties.length; i++) {
277                                    sb.append("(AssetTagProperty.key_ = ? AND ");
278                                    sb.append("AssetTagProperty.value = ?) ");
279    
280                                    if ((i + 1) < tagProperties.length) {
281                                            sb.append(" AND ");
282                                    }
283                            }
284    
285                            return sb.toString();
286                    }
287            }
288    
289            protected int doCountByG_N(
290                            long groupId, String name, boolean inlineSQLHelper)
291                    throws SystemException {
292    
293                    Session session = null;
294    
295                    try {
296                            session = openSession();
297    
298                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
299    
300                            if (inlineSQLHelper) {
301                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
302                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
303                            }
304    
305                            SQLQuery q = session.createSQLQuery(sql);
306    
307                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
308    
309                            QueryPos qPos = QueryPos.getInstance(q);
310    
311                            qPos.add(groupId);
312                            qPos.add(name);
313    
314                            Iterator<Long> itr = q.iterate();
315    
316                            if (itr.hasNext()) {
317                                    Long count = itr.next();
318    
319                                    if (count != null) {
320                                            return count.intValue();
321                                    }
322                            }
323    
324                            return 0;
325                    }
326                    catch (Exception e) {
327                            throw new SystemException(e);
328                    }
329                    finally {
330                            closeSession(session);
331                    }
332            }
333    
334            protected int doCountByG_C_N(
335                            long groupId, long classNameId, String name,
336                            boolean inlineSQLHelper)
337                    throws SystemException {
338    
339                    Session session = null;
340    
341                    try {
342                            session = openSession();
343    
344                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
345    
346                            if (inlineSQLHelper) {
347                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
348                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
349                            }
350    
351                            SQLQuery q = session.createSQLQuery(sql);
352    
353                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
354    
355                            QueryPos qPos = QueryPos.getInstance(q);
356    
357                            qPos.add(groupId);
358                            qPos.add(classNameId);
359                            qPos.add(name);
360                            qPos.add(name);
361    
362                            Iterator<Long> itr = q.iterate();
363    
364                            if (itr.hasNext()) {
365                                    Long count = itr.next();
366    
367                                    if (count != null) {
368                                            return count.intValue();
369                                    }
370                            }
371    
372                            return 0;
373                    }
374                    catch (Exception e) {
375                            throw new SystemException(e);
376                    }
377                    finally {
378                            closeSession(session);
379                    }
380            }
381    
382            protected int doCountByG_N_P(
383                            long groupId, String name, String[] tagProperties,
384                            boolean inlineSQLHelper)
385                    throws SystemException {
386    
387                    Session session = null;
388    
389                    try {
390                            session = openSession();
391    
392                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
393    
394                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(tagProperties));
395    
396                            if (inlineSQLHelper) {
397                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
398                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
399                            }
400    
401                            SQLQuery q = session.createSQLQuery(sql);
402    
403                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
404    
405                            QueryPos qPos = QueryPos.getInstance(q);
406    
407                            setJoin(qPos, tagProperties);
408    
409                            qPos.add(groupId);
410                            qPos.add(name);
411                            qPos.add(name);
412    
413                            Iterator<Long> itr = q.iterate();
414    
415                            if (itr.hasNext()) {
416                                    Long count = itr.next();
417    
418                                    if (count != null) {
419                                            return count.intValue();
420                                    }
421                            }
422    
423                            return 0;
424                    }
425                    catch (Exception e) {
426                            throw new SystemException(e);
427                    }
428                    finally {
429                            closeSession(session);
430                    }
431            }
432    
433            protected AssetTag doFindByG_N(
434                            long groupId, String name, boolean inlineSQLHelper)
435                    throws NoSuchTagException, SystemException {
436    
437                    name = name.trim().toLowerCase();
438    
439                    Session session = null;
440    
441                    try {
442                            session = openSession();
443    
444                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
445    
446                            if (inlineSQLHelper) {
447                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
448                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
449                            }
450    
451                            SQLQuery q = session.createSQLQuery(sql);
452    
453                            q.addEntity("AssetTag", AssetTagImpl.class);
454    
455                            QueryPos qPos = QueryPos.getInstance(q);
456    
457                            qPos.add(groupId);
458                            qPos.add(name);
459    
460                            List<AssetTag> tags = q.list();
461    
462                            if (!tags.isEmpty()) {
463                                    return tags.get(0);
464                            }
465                    }
466                    catch (Exception e) {
467                            throw new SystemException(e);
468                    }
469                    finally {
470                            closeSession(session);
471                    }
472    
473                    StringBundler sb = new StringBundler(6);
474    
475                    sb.append("No AssetTag exists with the key ");
476                    sb.append("{groupId=");
477                    sb.append(groupId);
478                    sb.append(", name=");
479                    sb.append(name);
480                    sb.append("}");
481    
482                    throw new NoSuchTagException(sb.toString());
483            }
484    
485            protected List<AssetTag> doFindByG_C_N(
486                            long groupId, long classNameId, String name, int start, int end,
487                            OrderByComparator obc, boolean inlineSQLHelper)
488                    throws SystemException {
489    
490                    Session session = null;
491    
492                    try {
493                            session = openSession();
494    
495                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N);
496    
497                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
498    
499                            if (inlineSQLHelper) {
500                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
501                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
502                            }
503    
504                            SQLQuery q = session.createSQLQuery(sql);
505    
506                            q.addEntity("AssetTag", AssetTagImpl.class);
507    
508                            QueryPos qPos = QueryPos.getInstance(q);
509    
510                            qPos.add(groupId);
511                            qPos.add(classNameId);
512                            qPos.add(name);
513                            qPos.add(name);
514    
515                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
516                    }
517                    catch (Exception e) {
518                            throw new SystemException(e);
519                    }
520                    finally {
521                            closeSession(session);
522                    }
523            }
524    
525            protected List<AssetTag> doFindByG_N_P(
526                            long groupId, String name, String[] tagProperties, int start,
527                            int end, OrderByComparator obc, boolean inlineSQLHelper)
528                    throws SystemException {
529    
530                    Session session = null;
531    
532                    try {
533                            session = openSession();
534    
535                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
536    
537                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(tagProperties));
538                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
539    
540                            if (inlineSQLHelper) {
541                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
542                                            sql, AssetTag.class.getName(), "AssetTag.tagId", groupId);
543                            }
544    
545                            SQLQuery q = session.createSQLQuery(sql);
546    
547                            q.addEntity("AssetTag", AssetTagImpl.class);
548    
549                            QueryPos qPos = QueryPos.getInstance(q);
550    
551                            setJoin(qPos, tagProperties);
552    
553                            qPos.add(groupId);
554                            qPos.add(name);
555                            qPos.add(name);
556    
557                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
558                    }
559                    catch (Exception e) {
560                            throw new SystemException(e);
561                    }
562                    finally {
563                            closeSession(session);
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    }