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