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