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