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