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.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 String COUNT_BY_G_C_N =
051                    AssetCategoryFinder.class.getName() + ".countByG_C_N";
052    
053            public static String COUNT_BY_G_N_V =
054                    AssetCategoryFinder.class.getName() + ".countByG_N_V";
055    
056            public static String COUNT_BY_G_N_P =
057                    AssetCategoryFinder.class.getName() + ".countByG_N_P";
058    
059            public static String FIND_BY_ENTRY_ID =
060                    AssetCategoryFinder.class.getName() + ".findByEntryId";
061    
062            public static String FIND_BY_G_N =
063                    AssetCategoryFinder.class.getName() + ".findByG_N";
064    
065            public static String FIND_BY_C_C =
066                    AssetCategoryFinder.class.getName() + ".findByC_C";
067    
068            public static String FIND_BY_G_N_V =
069                    AssetCategoryFinder.class.getName() + ".findByG_N_V";
070    
071            public static 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(
261                                    classNameId, classPK);
262    
263                            if (entry == null) {
264                                    return Collections.emptyList();
265                            }
266    
267                            session = openSession();
268    
269                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
270    
271                            SQLQuery q = session.createSQLQuery(sql);
272    
273                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
274    
275                            QueryPos qPos = QueryPos.getInstance(q);
276    
277                            qPos.add(entry.getEntryId());
278    
279                            return (List<AssetCategory>)QueryUtil.list(
280                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
281                    }
282                    catch (Exception e) {
283                            throw new SystemException(e);
284                    }
285                    finally {
286                            closeSession(session);
287                    }
288            }
289    
290            public List<AssetCategory> findByG_N_V(
291                            long groupId, String name, long vocabularyId, int start, int end,
292                            OrderByComparator obc)
293                    throws SystemException {
294    
295                    return doFindByG_N_V(
296                            groupId, name, vocabularyId, start, end, obc, false);
297            }
298    
299            public List<AssetCategory> findByG_N_P(
300                            long groupId, String name, String[] categoryProperties)
301                    throws SystemException {
302    
303                    return findByG_N_P(
304                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
305                            QueryUtil.ALL_POS);
306            }
307    
308            public List<AssetCategory> findByG_N_P(
309                            long groupId, String name, String[] categoryProperties, int start,
310                            int end)
311                    throws SystemException {
312    
313                    Session session = null;
314    
315                    try {
316                            session = openSession();
317    
318                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
319    
320                            sql = StringUtil.replace(
321                                    sql, "[$JOIN$]", getJoin(categoryProperties));
322    
323                            SQLQuery q = session.createSQLQuery(sql);
324    
325                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
326    
327                            QueryPos qPos = QueryPos.getInstance(q);
328    
329                            setJoin(qPos, categoryProperties);
330    
331                            qPos.add(groupId);
332                            qPos.add(name);
333                            qPos.add(name);
334    
335                            return (List<AssetCategory>)QueryUtil.list(
336                                    q, getDialect(), start, end);
337                    }
338                    catch (Exception e) {
339                            throw new SystemException(e);
340                    }
341                    finally {
342                            closeSession(session);
343                    }
344            }
345    
346            protected int doCountByG_N_V(
347                            long groupId, String name, long vocabularyId,
348                            boolean inlineSQLHelper)
349                    throws SystemException {
350    
351                    Session session = null;
352    
353                    try {
354                            session = openSession();
355    
356                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_V);
357    
358                            if (inlineSQLHelper) {
359                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
360                                            sql, AssetCategory.class.getName(),
361                                            "AssetCategory.categoryId", groupId);
362                            }
363    
364                            SQLQuery q = session.createSQLQuery(sql);
365    
366                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
367    
368                            QueryPos qPos = QueryPos.getInstance(q);
369    
370                            qPos.add(groupId);
371                            qPos.add(name);
372                            qPos.add(name);
373                            qPos.add(vocabularyId);
374    
375                            Iterator<Long> itr = q.iterate();
376    
377                            if (itr.hasNext()) {
378                                    Long count = itr.next();
379    
380                                    if (count != null) {
381                                            return count.intValue();
382                                    }
383                            }
384    
385                            return 0;
386                    }
387                    catch (Exception e) {
388                            throw new SystemException(e);
389                    }
390                    finally {
391                            closeSession(session);
392                    }
393            }
394    
395            protected List<AssetCategory> doFindByG_N_V(
396                            long groupId, String name, long vocabularyId, int start, int end,
397                            OrderByComparator obc, boolean inlineSQLHelper)
398                    throws SystemException {
399    
400                    name = name.trim().toLowerCase();
401    
402                    Session session = null;
403    
404                    try {
405                            session = openSession();
406    
407                            String sql = CustomSQLUtil.get(FIND_BY_G_N_V);
408    
409                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
410    
411                            if (inlineSQLHelper) {
412                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
413                                            sql, AssetVocabulary.class.getName(),
414                                            "AssetCategory.categoryId", groupId);
415                            }
416    
417                            SQLQuery q = session.createSQLQuery(sql);
418    
419                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
420    
421                            QueryPos qPos = QueryPos.getInstance(q);
422    
423                            qPos.add(groupId);
424                            qPos.add(name);
425                            qPos.add(name);
426                            qPos.add(vocabularyId);
427    
428                            return (List<AssetCategory>)QueryUtil.list(
429                                    q, getDialect(), start, end);
430                    }
431                    catch (Exception e) {
432                            throw new SystemException(e);
433                    }
434                    finally {
435                            closeSession(session);
436                    }
437            }
438    
439            protected String getJoin(String[] categoryProperties) {
440                    if (categoryProperties.length == 0) {
441                            return StringPool.BLANK;
442                    }
443                    else {
444                            StringBundler sb = new StringBundler(
445                                    categoryProperties.length * 3 + 2);
446    
447                            sb.append(" INNER JOIN AssetCategoryProperty ON ");
448                            sb.append(" (AssetCategoryProperty.categoryId = ");
449                            sb.append(" AssetCategory.categoryId) AND ");
450    
451                            for (int i = 0; i < categoryProperties.length; i++) {
452                                    sb.append("(AssetCategoryProperty.key_ = ? AND ");
453                                    sb.append("AssetCategoryProperty.value = ?) ");
454    
455                                    if ((i + 1) < categoryProperties.length) {
456                                            sb.append(" AND ");
457                                    }
458                            }
459    
460                            return sb.toString();
461                    }
462            }
463    
464            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
465                    for (int i = 0; i < categoryProperties.length; i++) {
466                            String[] categoryProperty = StringUtil.split(
467                                    categoryProperties[i], CharPool.COLON);
468    
469                            String key = StringPool.BLANK;
470    
471                            if (categoryProperty.length > 0) {
472                                    key = GetterUtil.getString(categoryProperty[0]);
473                            }
474    
475                            String value = StringPool.BLANK;
476    
477                            if (categoryProperty.length > 1) {
478                                    value = GetterUtil.getString(categoryProperty[1]);
479                            }
480    
481                            qPos.add(key);
482                            qPos.add(value);
483                    }
484            }
485    
486    }