001    /**
002     * Copyright (c) 2000-2013 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.FinderCacheUtil;
018    import com.liferay.portal.kernel.dao.orm.FinderPath;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
021    import com.liferay.portal.kernel.dao.orm.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.Session;
023    import com.liferay.portal.kernel.dao.orm.Type;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.util.CharPool;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
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.impl.AssetCategoryImpl;
034    import com.liferay.portlet.asset.model.impl.AssetCategoryModelImpl;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.Collections;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Bruno Farache
044     * @author Jorge Ferrer
045     * @author Shuyang Zhou
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_P =
054                    AssetCategoryFinder.class.getName() + ".countByG_N_P";
055    
056            public static final String FIND_BY_G_L =
057                    AssetCategoryFinder.class.getName() + ".findByG_L";
058    
059            public static final String FIND_BY_G_N =
060                    AssetCategoryFinder.class.getName() + ".findByG_N";
061    
062            public static final String FIND_BY_G_N_P =
063                    AssetCategoryFinder.class.getName() + ".findByG_N_P";
064    
065            public static final FinderPath FINDER_PATH_FIND_BY_G_L = new FinderPath(
066                    AssetCategoryModelImpl.ENTITY_CACHE_ENABLED,
067                    AssetCategoryModelImpl.FINDER_CACHE_ENABLED, List.class,
068                    AssetCategoryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
069                    "findByG_L", new String[] {Long.class.getName()});
070    
071            @Override
072            public int countByG_C_N(long groupId, long classNameId, String name)
073                    throws SystemException {
074    
075                    Session session = null;
076    
077                    try {
078                            session = openSession();
079    
080                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
081    
082                            SQLQuery q = session.createSQLQuery(sql);
083    
084                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
085    
086                            QueryPos qPos = QueryPos.getInstance(q);
087    
088                            qPos.add(groupId);
089                            qPos.add(classNameId);
090                            qPos.add(name);
091                            qPos.add(name);
092    
093                            Iterator<Long> itr = q.iterate();
094    
095                            if (itr.hasNext()) {
096                                    Long count = itr.next();
097    
098                                    if (count != null) {
099                                            return count.intValue();
100                                    }
101                            }
102    
103                            return 0;
104                    }
105                    catch (Exception e) {
106                            throw new SystemException(e);
107                    }
108                    finally {
109                            closeSession(session);
110                    }
111            }
112    
113            @Override
114            public int countByG_N_P(
115                            long groupId, String name, String[] categoryProperties)
116                    throws SystemException {
117    
118                    Session session = null;
119    
120                    try {
121                            session = openSession();
122    
123                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
124    
125                            SQLQuery q = session.createSQLQuery(sql);
126    
127                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
128    
129                            QueryPos qPos = QueryPos.getInstance(q);
130    
131                            setJoin(qPos, categoryProperties);
132    
133                            qPos.add(groupId);
134                            qPos.add(name);
135                            qPos.add(name);
136    
137                            Iterator<Long> itr = q.iterate();
138    
139                            if (itr.hasNext()) {
140                                    Long count = itr.next();
141    
142                                    if (count != null) {
143                                            return count.intValue();
144                                    }
145                            }
146    
147                            return 0;
148                    }
149                    catch (Exception e) {
150                            throw new SystemException(e);
151                    }
152                    finally {
153                            closeSession(session);
154                    }
155            }
156    
157            @Override
158            public List<Long> findByG_L(Long parentCategoryId) throws SystemException {
159                    Object[] finderArgs = new Object[] {parentCategoryId};
160    
161                    List<Long> list = (List<Long>)FinderCacheUtil.getResult(
162                            FINDER_PATH_FIND_BY_G_L, finderArgs, this);
163    
164                    if (list != null) {
165                            return list;
166                    }
167    
168                    AssetCategory parentAssetCategory = AssetCategoryUtil.fetchByPrimaryKey(
169                            parentCategoryId);
170    
171                    if (parentAssetCategory == null) {
172                            return Collections.emptyList();
173                    }
174    
175                    Session session = null;
176    
177                    try {
178                            session = openSession();
179    
180                            String sql = CustomSQLUtil.get(FIND_BY_G_L);
181    
182                            SQLQuery q = session.createSQLQuery(sql);
183    
184                            q.addScalar("categoryId", Type.LONG);
185    
186                            QueryPos qPos = QueryPos.getInstance(q);
187    
188                            qPos.add(parentAssetCategory.getGroupId());
189                            qPos.add(parentAssetCategory.getLeftCategoryId());
190                            qPos.add(parentAssetCategory.getRightCategoryId());
191    
192                            list = q.list();
193                    }
194                    catch (Exception e) {
195                            throw processException(e);
196                    }
197                    finally {
198                            if (list == null) {
199                                    FinderCacheUtil.removeResult(
200                                            FINDER_PATH_FIND_BY_G_L, finderArgs);
201                            }
202                            else {
203                                    FinderCacheUtil.putResult(
204                                            FINDER_PATH_FIND_BY_G_L, finderArgs, list);
205                            }
206    
207                            closeSession(session);
208                    }
209    
210                    return list;
211            }
212    
213            @Override
214            public AssetCategory findByG_N(long groupId, String name)
215                    throws NoSuchCategoryException, SystemException {
216    
217                    name = name.trim().toLowerCase();
218    
219                    Session session = null;
220    
221                    try {
222                            session = openSession();
223    
224                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
225    
226                            SQLQuery q = session.createSQLQuery(sql);
227    
228                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
229    
230                            QueryPos qPos = QueryPos.getInstance(q);
231    
232                            qPos.add(groupId);
233                            qPos.add(name);
234    
235                            List<AssetCategory> categories = q.list();
236    
237                            if (!categories.isEmpty()) {
238                                    return categories.get(0);
239                            }
240                    }
241                    catch (Exception e) {
242                            throw new SystemException(e);
243                    }
244                    finally {
245                            closeSession(session);
246                    }
247    
248                    StringBundler sb = new StringBundler(6);
249    
250                    sb.append("No AssetCategory exists with the key ");
251                    sb.append("{groupId=");
252                    sb.append(groupId);
253                    sb.append(", name=");
254                    sb.append(name);
255                    sb.append("}");
256    
257                    throw new NoSuchCategoryException(sb.toString());
258            }
259    
260            @Override
261            public List<AssetCategory> findByG_N_P(
262                            long groupId, String name, String[] categoryProperties)
263                    throws SystemException {
264    
265                    return findByG_N_P(
266                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
267                            QueryUtil.ALL_POS);
268            }
269    
270            @Override
271            public List<AssetCategory> findByG_N_P(
272                            long groupId, String name, String[] categoryProperties, int start,
273                            int end)
274                    throws SystemException {
275    
276                    Session session = null;
277    
278                    try {
279                            session = openSession();
280    
281                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
282    
283                            sql = StringUtil.replace(
284                                    sql, "[$JOIN$]", getJoin(categoryProperties));
285    
286                            SQLQuery q = session.createSQLQuery(sql);
287    
288                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
289    
290                            QueryPos qPos = QueryPos.getInstance(q);
291    
292                            setJoin(qPos, categoryProperties);
293    
294                            qPos.add(groupId);
295                            qPos.add(name);
296                            qPos.add(name);
297    
298                            return (List<AssetCategory>)QueryUtil.list(
299                                    q, getDialect(), start, end);
300                    }
301                    catch (Exception e) {
302                            throw new SystemException(e);
303                    }
304                    finally {
305                            closeSession(session);
306                    }
307            }
308    
309            protected String getJoin(String[] categoryProperties) {
310                    if (categoryProperties.length == 0) {
311                            return StringPool.BLANK;
312                    }
313                    else {
314                            StringBundler sb = new StringBundler(
315                                    categoryProperties.length * 3 + 2);
316    
317                            sb.append(" INNER JOIN AssetCategoryProperty ON ");
318                            sb.append(" (AssetCategoryProperty.categoryId = ");
319                            sb.append(" AssetCategory.categoryId) AND ");
320    
321                            for (int i = 0; i < categoryProperties.length; i++) {
322                                    sb.append("(AssetCategoryProperty.key_ = ? AND ");
323                                    sb.append("AssetCategoryProperty.value = ?) ");
324    
325                                    if ((i + 1) < categoryProperties.length) {
326                                            sb.append(" AND ");
327                                    }
328                            }
329    
330                            return sb.toString();
331                    }
332            }
333    
334            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
335                    for (int i = 0; i < categoryProperties.length; i++) {
336                            String[] categoryProperty = StringUtil.split(
337                                    categoryProperties[i], CharPool.COLON);
338    
339                            String key = StringPool.BLANK;
340    
341                            if (categoryProperty.length > 0) {
342                                    key = GetterUtil.getString(categoryProperty[0]);
343                            }
344    
345                            String value = StringPool.BLANK;
346    
347                            if (categoryProperty.length > 1) {
348                                    value = GetterUtil.getString(categoryProperty[1]);
349                            }
350    
351                            qPos.add(key);
352                            qPos.add(value);
353                    }
354            }
355    
356    }