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