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.NoSuchCategoryException;
032    import com.liferay.portlet.asset.model.AssetCategory;
033    import com.liferay.portlet.asset.model.AssetEntry;
034    import com.liferay.portlet.asset.model.AssetVocabulary;
035    import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
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     * @author Jorge Ferrer
046     */
047    public class AssetCategoryFinderImpl
048            extends BasePersistenceImpl<AssetCategory> implements AssetCategoryFinder {
049    
050            public static final String COUNT_BY_G_C_N =
051                    AssetCategoryFinder.class.getName() + ".countByG_C_N";
052    
053            public static final String COUNT_BY_G_N_V =
054                    AssetCategoryFinder.class.getName() + ".countByG_N_V";
055    
056            public static final String COUNT_BY_G_N_P =
057                    AssetCategoryFinder.class.getName() + ".countByG_N_P";
058    
059            public static final String FIND_BY_ENTRY_ID =
060                    AssetCategoryFinder.class.getName() + ".findByEntryId";
061    
062            public static final String FIND_BY_G_N =
063                    AssetCategoryFinder.class.getName() + ".findByG_N";
064    
065            public static final String FIND_BY_C_C =
066                    AssetCategoryFinder.class.getName() + ".findByC_C";
067    
068            public static final String FIND_BY_G_N_V =
069                    AssetCategoryFinder.class.getName() + ".findByG_N_V";
070    
071            public static final String FIND_BY_G_N_P =
072                    AssetCategoryFinder.class.getName() + ".findByG_N_P";
073    
074            public int countByG_C_N(long groupId, long classNameId, String name)
075                    throws SystemException {
076    
077                    Session session = null;
078    
079                    try {
080                            session = openSession();
081    
082                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
083    
084                            SQLQuery q = session.createSQLQuery(sql);
085    
086                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
087    
088                            QueryPos qPos = QueryPos.getInstance(q);
089    
090                            qPos.add(groupId);
091                            qPos.add(classNameId);
092                            qPos.add(name);
093                            qPos.add(name);
094    
095                            Iterator<Long> itr = q.iterate();
096    
097                            if (itr.hasNext()) {
098                                    Long count = itr.next();
099    
100                                    if (count != null) {
101                                            return count.intValue();
102                                    }
103                            }
104    
105                            return 0;
106                    }
107                    catch (Exception e) {
108                            throw new SystemException(e);
109                    }
110                    finally {
111                            closeSession(session);
112                    }
113            }
114    
115            public int countByG_N_V(long groupId, String name, long vocabularyId)
116                    throws SystemException {
117    
118                    return doCountByG_N_V(groupId, name, vocabularyId, false);
119            }
120    
121            public int countByG_N_P(
122                            long groupId, String name, String[] categoryProperties)
123                    throws SystemException {
124    
125                    Session session = null;
126    
127                    try {
128                            session = openSession();
129    
130                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
131    
132                            SQLQuery q = session.createSQLQuery(sql);
133    
134                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
135    
136                            QueryPos qPos = QueryPos.getInstance(q);
137    
138                            setJoin(qPos, categoryProperties);
139    
140                            qPos.add(groupId);
141                            qPos.add(name);
142                            qPos.add(name);
143    
144                            Iterator<Long> itr = q.iterate();
145    
146                            if (itr.hasNext()) {
147                                    Long count = itr.next();
148    
149                                    if (count != null) {
150                                            return count.intValue();
151                                    }
152                            }
153    
154                            return 0;
155                    }
156                    catch (Exception e) {
157                            throw new SystemException(e);
158                    }
159                    finally {
160                            closeSession(session);
161                    }
162            }
163    
164            public int filterCountByG_N_V(long groupId, String name, long vocabularyId)
165                    throws SystemException {
166    
167                    return doCountByG_N_V(groupId, name, vocabularyId, true);
168            }
169    
170            public List<AssetCategory> filterFindByG_N_V(
171                            long groupId, String name, long vocabularyId, int start, int end,
172                            OrderByComparator obc)
173                    throws SystemException {
174    
175                    return doFindByG_N_V(
176                            groupId, name, vocabularyId, start, end, obc, true);
177            }
178    
179            public List<AssetCategory> findByEntryId(long entryId)
180                    throws SystemException {
181    
182                    Session session = null;
183    
184                    try {
185                            session = openSession();
186    
187                            String sql = CustomSQLUtil.get(FIND_BY_ENTRY_ID);
188    
189                            SQLQuery q = session.createSQLQuery(sql);
190    
191                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
192    
193                            QueryPos qPos = QueryPos.getInstance(q);
194    
195                            qPos.add(entryId);
196    
197                            return (List<AssetCategory>)QueryUtil.list(
198                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
199                    }
200                    catch (Exception e) {
201                            throw new SystemException(e);
202                    }
203                    finally {
204                            closeSession(session);
205                    }
206            }
207    
208            public AssetCategory findByG_N(long groupId, String name)
209                    throws NoSuchCategoryException, SystemException {
210    
211                    name = name.trim().toLowerCase();
212    
213                    Session session = null;
214    
215                    try {
216                            session = openSession();
217    
218                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
219    
220                            SQLQuery q = session.createSQLQuery(sql);
221    
222                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
223    
224                            QueryPos qPos = QueryPos.getInstance(q);
225    
226                            qPos.add(groupId);
227                            qPos.add(name);
228    
229                            List<AssetCategory> categories = q.list();
230    
231                            if (!categories.isEmpty()) {
232                                    return categories.get(0);
233                            }
234                    }
235                    catch (Exception e) {
236                            throw new SystemException(e);
237                    }
238                    finally {
239                            closeSession(session);
240                    }
241    
242                    StringBundler sb = new StringBundler(6);
243    
244                    sb.append("No AssetCategory exists with the key ");
245                    sb.append("{groupId=");
246                    sb.append(groupId);
247                    sb.append(", name=");
248                    sb.append(name);
249                    sb.append("}");
250    
251                    throw new NoSuchCategoryException(sb.toString());
252            }
253    
254            public List<AssetCategory> findByC_C(long classNameId, long classPK)
255                    throws SystemException {
256    
257                    Session session = null;
258    
259                    try {
260                            AssetEntry entry = AssetEntryUtil.fetchByC_C(classNameId, classPK);
261    
262                            if (entry == null) {
263                                    return Collections.emptyList();
264                            }
265    
266                            session = openSession();
267    
268                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
269    
270                            SQLQuery q = session.createSQLQuery(sql);
271    
272                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
273    
274                            QueryPos qPos = QueryPos.getInstance(q);
275    
276                            qPos.add(entry.getEntryId());
277    
278                            return (List<AssetCategory>)QueryUtil.list(
279                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
280                    }
281                    catch (Exception e) {
282                            throw new SystemException(e);
283                    }
284                    finally {
285                            closeSession(session);
286                    }
287            }
288    
289            public List<AssetCategory> findByG_N_V(
290                            long groupId, String name, long vocabularyId, int start, int end,
291                            OrderByComparator obc)
292                    throws SystemException {
293    
294                    return doFindByG_N_V(
295                            groupId, name, vocabularyId, start, end, obc, false);
296            }
297    
298            public List<AssetCategory> findByG_N_P(
299                            long groupId, String name, String[] categoryProperties)
300                    throws SystemException {
301    
302                    return findByG_N_P(
303                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
304                            QueryUtil.ALL_POS);
305            }
306    
307            public List<AssetCategory> findByG_N_P(
308                            long groupId, String name, String[] categoryProperties, int start,
309                            int end)
310                    throws SystemException {
311    
312                    Session session = null;
313    
314                    try {
315                            session = openSession();
316    
317                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
318    
319                            sql = StringUtil.replace(
320                                    sql, "[$JOIN$]", getJoin(categoryProperties));
321    
322                            SQLQuery q = session.createSQLQuery(sql);
323    
324                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
325    
326                            QueryPos qPos = QueryPos.getInstance(q);
327    
328                            setJoin(qPos, categoryProperties);
329    
330                            qPos.add(groupId);
331                            qPos.add(name);
332                            qPos.add(name);
333    
334                            return (List<AssetCategory>)QueryUtil.list(
335                                    q, getDialect(), start, end);
336                    }
337                    catch (Exception e) {
338                            throw new SystemException(e);
339                    }
340                    finally {
341                            closeSession(session);
342                    }
343            }
344    
345            protected int doCountByG_N_V(
346                            long groupId, String name, long vocabularyId,
347                            boolean inlineSQLHelper)
348                    throws SystemException {
349    
350                    Session session = null;
351    
352                    try {
353                            session = openSession();
354    
355                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_V);
356    
357                            if (inlineSQLHelper) {
358                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
359                                            sql, AssetCategory.class.getName(),
360                                            "AssetCategory.categoryId", groupId);
361                            }
362    
363                            SQLQuery q = session.createSQLQuery(sql);
364    
365                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
366    
367                            QueryPos qPos = QueryPos.getInstance(q);
368    
369                            qPos.add(groupId);
370                            qPos.add(name);
371                            qPos.add(name);
372                            qPos.add(vocabularyId);
373    
374                            Iterator<Long> itr = q.iterate();
375    
376                            if (itr.hasNext()) {
377                                    Long count = itr.next();
378    
379                                    if (count != null) {
380                                            return count.intValue();
381                                    }
382                            }
383    
384                            return 0;
385                    }
386                    catch (Exception e) {
387                            throw new SystemException(e);
388                    }
389                    finally {
390                            closeSession(session);
391                    }
392            }
393    
394            protected List<AssetCategory> doFindByG_N_V(
395                            long groupId, String name, long vocabularyId, int start, int end,
396                            OrderByComparator obc, boolean inlineSQLHelper)
397                    throws SystemException {
398    
399                    name = name.trim().toLowerCase();
400    
401                    Session session = null;
402    
403                    try {
404                            session = openSession();
405    
406                            String sql = CustomSQLUtil.get(FIND_BY_G_N_V);
407    
408                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
409    
410                            if (inlineSQLHelper) {
411                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
412                                            sql, AssetVocabulary.class.getName(),
413                                            "AssetCategory.categoryId", groupId);
414                            }
415    
416                            SQLQuery q = session.createSQLQuery(sql);
417    
418                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
419    
420                            QueryPos qPos = QueryPos.getInstance(q);
421    
422                            qPos.add(groupId);
423                            qPos.add(name);
424                            qPos.add(name);
425                            qPos.add(vocabularyId);
426    
427                            return (List<AssetCategory>)QueryUtil.list(
428                                    q, getDialect(), start, end);
429                    }
430                    catch (Exception e) {
431                            throw new SystemException(e);
432                    }
433                    finally {
434                            closeSession(session);
435                    }
436            }
437    
438            protected String getJoin(String[] categoryProperties) {
439                    if (categoryProperties.length == 0) {
440                            return StringPool.BLANK;
441                    }
442                    else {
443                            StringBundler sb = new StringBundler(
444                                    categoryProperties.length * 3 + 2);
445    
446                            sb.append(" INNER JOIN AssetCategoryProperty ON ");
447                            sb.append(" (AssetCategoryProperty.categoryId = ");
448                            sb.append(" AssetCategory.categoryId) AND ");
449    
450                            for (int i = 0; i < categoryProperties.length; i++) {
451                                    sb.append("(AssetCategoryProperty.key_ = ? AND ");
452                                    sb.append("AssetCategoryProperty.value = ?) ");
453    
454                                    if ((i + 1) < categoryProperties.length) {
455                                            sb.append(" AND ");
456                                    }
457                            }
458    
459                            return sb.toString();
460                    }
461            }
462    
463            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
464                    for (int i = 0; i < categoryProperties.length; i++) {
465                            String[] categoryProperty = StringUtil.split(
466                                    categoryProperties[i], CharPool.COLON);
467    
468                            String key = StringPool.BLANK;
469    
470                            if (categoryProperty.length > 0) {
471                                    key = GetterUtil.getString(categoryProperty[0]);
472                            }
473    
474                            String value = StringPool.BLANK;
475    
476                            if (categoryProperty.length > 1) {
477                                    value = GetterUtil.getString(categoryProperty[1]);
478                            }
479    
480                            qPos.add(key);
481                            qPos.add(value);
482                    }
483            }
484    
485    }