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.asset.kernel.exception.NoSuchCategoryException;
018    import com.liferay.asset.kernel.model.AssetCategory;
019    import com.liferay.asset.kernel.model.AssetCategoryConstants;
020    import com.liferay.asset.kernel.service.persistence.AssetCategoryFinder;
021    import com.liferay.portal.kernel.dao.orm.QueryPos;
022    import com.liferay.portal.kernel.dao.orm.QueryUtil;
023    import com.liferay.portal.kernel.dao.orm.SQLQuery;
024    import com.liferay.portal.kernel.dao.orm.Session;
025    import com.liferay.portal.kernel.dao.orm.Type;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.util.CharPool;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
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(5);
178    
179                    sb.append("No AssetCategory exists with the key {groupId=");
180                    sb.append(groupId);
181                    sb.append(", name=");
182                    sb.append(name);
183                    sb.append("}");
184    
185                    throw new NoSuchCategoryException(sb.toString());
186            }
187    
188            @Override
189            public List<AssetCategory> findByG_N_P(
190                    long groupId, String name, String[] categoryProperties) {
191    
192                    return findByG_N_P(
193                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
194                            QueryUtil.ALL_POS);
195            }
196    
197            @Override
198            public List<AssetCategory> findByG_N_P(
199                    long groupId, String name, String[] categoryProperties, int start,
200                    int end) {
201    
202                    Session session = null;
203    
204                    try {
205                            session = openSession();
206    
207                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
208    
209                            sql = StringUtil.replace(
210                                    sql, "[$JOIN$]", getJoin(categoryProperties));
211    
212                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
213    
214                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
215    
216                            QueryPos qPos = QueryPos.getInstance(q);
217    
218                            setJoin(qPos, categoryProperties);
219    
220                            qPos.add(groupId);
221                            qPos.add(name);
222                            qPos.add(name);
223    
224                            return (List<AssetCategory>)QueryUtil.list(
225                                    q, getDialect(), start, end);
226                    }
227                    catch (Exception e) {
228                            throw new SystemException(e);
229                    }
230                    finally {
231                            closeSession(session);
232                    }
233            }
234    
235            protected String getJoin(String[] categoryProperties) {
236                    if (categoryProperties.length == 0) {
237                            return StringPool.BLANK;
238                    }
239    
240                    StringBundler sb = new StringBundler(categoryProperties.length * 3 + 2);
241    
242                    sb.append(" INNER JOIN AssetCategoryProperty ON ");
243                    sb.append("(AssetCategoryProperty.categoryId = ");
244                    sb.append("AssetCategory.categoryId) AND ");
245    
246                    for (int i = 0; i < categoryProperties.length; i++) {
247                            sb.append("(AssetCategoryProperty.key_ = ? AND ");
248                            sb.append("AssetCategoryProperty.value = ?) ");
249    
250                            if ((i + 1) < categoryProperties.length) {
251                                    sb.append(" AND ");
252                            }
253                    }
254    
255                    return sb.toString();
256            }
257    
258            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
259                    for (int i = 0; i < categoryProperties.length; i++) {
260                            String[] categoryProperty = StringUtil.split(
261                                    categoryProperties[i],
262                                    AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);
263    
264                            if (categoryProperty.length <= 1) {
265                                    categoryProperty = StringUtil.split(
266                                            categoryProperties[i], CharPool.COLON);
267                            }
268    
269                            String key = StringPool.BLANK;
270    
271                            if (categoryProperty.length > 0) {
272                                    key = GetterUtil.getString(categoryProperty[0]);
273                            }
274    
275                            String value = StringPool.BLANK;
276    
277                            if (categoryProperty.length > 1) {
278                                    value = GetterUtil.getString(categoryProperty[1]);
279                            }
280    
281                            qPos.add(key);
282                            qPos.add(value);
283                    }
284            }
285    
286    }