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