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.dynamicdatalists.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.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSetConstants;
030    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordSetImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Marcellus Tavares
038     * @author Connor McKay
039     */
040    public class DDLRecordSetFinderImpl extends BasePersistenceImpl<DDLRecordSet>
041            implements DDLRecordSetFinder {
042    
043            public static final String COUNT_BY_C_G_N_D_S =
044                    DDLRecordSetFinder.class.getName() + ".countByC_G_N_D_S";
045    
046            public static final String FIND_BY_C_G_N_D_S =
047                    DDLRecordSetFinder.class.getName() + ".findByC_G_N_D_S";
048    
049            @Override
050            public int countByKeywords(
051                            long companyId, long groupId, String keywords, int scope)
052                    throws SystemException {
053    
054                    String[] names = null;
055                    String[] descriptions = null;
056                    boolean andOperator = false;
057    
058                    if (Validator.isNotNull(keywords)) {
059                            names = CustomSQLUtil.keywords(keywords);
060                            descriptions = CustomSQLUtil.keywords(keywords, false);
061                    }
062                    else {
063                            andOperator = true;
064                    }
065    
066                    return doCountByC_G_N_D_S(
067                            companyId, groupId, names, descriptions, scope, andOperator);
068            }
069    
070            @Override
071            public int countByC_G_N_D_S(
072                            long companyId, long groupId, String name, String description,
073                            int scope, boolean andOperator)
074                    throws SystemException {
075    
076                    String[] names = CustomSQLUtil.keywords(name);
077                    String[] descriptions = CustomSQLUtil.keywords(description, false);
078    
079                    return doCountByC_G_N_D_S(
080                            companyId, groupId, names, descriptions, scope, andOperator);
081            }
082    
083            @Override
084            public List<DDLRecordSet> findByKeywords(
085                            long companyId, long groupId, String keywords, int scope, int start,
086                            int end, OrderByComparator orderByComparator)
087                    throws SystemException {
088    
089                    String[] names = null;
090                    String[] descriptions = null;
091                    boolean andOperator = false;
092    
093                    if (Validator.isNotNull(keywords)) {
094                            names = CustomSQLUtil.keywords(keywords);
095                            descriptions = CustomSQLUtil.keywords(keywords, false);
096                    }
097                    else {
098                            andOperator = true;
099                    }
100    
101                    return findByC_G_N_D_S(
102                            companyId, groupId, names, descriptions, scope, andOperator, start,
103                            end, orderByComparator);
104            }
105    
106            @Override
107            public List<DDLRecordSet> findByC_G_N_D_S(
108                            long companyId, long groupId, String name, String description,
109                            int scope, boolean andOperator, int start, int end,
110                            OrderByComparator orderByComparator)
111                    throws SystemException {
112    
113                    String[] names = CustomSQLUtil.keywords(name);
114                    String[] descriptions = CustomSQLUtil.keywords(description, false);
115    
116                    return findByC_G_N_D_S(
117                            companyId, groupId, names, descriptions, scope, andOperator, start,
118                            end, orderByComparator);
119            }
120    
121            @Override
122            public List<DDLRecordSet> findByC_G_N_D_S(
123                            long companyId, long groupId, String[] names, String[] descriptions,
124                            int scope, boolean andOperator, int start, int end,
125                            OrderByComparator orderByComparator)
126                    throws SystemException {
127    
128                    return doFindByC_G_N_D_S(
129                            companyId, groupId, names, descriptions, scope, andOperator, start,
130                            end, orderByComparator);
131            }
132    
133            protected int doCountByC_G_N_D_S(
134                            long companyId, long groupId, String[] names, String[] descriptions,
135                            int scope, boolean andOperator)
136                    throws SystemException {
137    
138                    names = CustomSQLUtil.keywords(names);
139                    descriptions = CustomSQLUtil.keywords(descriptions, false);
140    
141                    Session session = null;
142    
143                    try {
144                            session = openSession();
145    
146                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
147    
148                            if (groupId <= 0) {
149                                    sql = StringUtil.replace(
150                                            sql, "(groupId = ?) AND", StringPool.BLANK);
151                            }
152    
153                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
154                                    sql = StringUtil.replace(
155                                            sql, "(scope = ?) AND", StringPool.BLANK);
156                            }
157    
158                            sql = CustomSQLUtil.replaceKeywords(
159                                    sql, "lower(name)", StringPool.LIKE, false, names);
160                            sql = CustomSQLUtil.replaceKeywords(
161                                    sql, "description", StringPool.LIKE, true, descriptions);
162                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
163    
164                            SQLQuery q = session.createSQLQuery(sql);
165    
166                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
167    
168                            QueryPos qPos = QueryPos.getInstance(q);
169    
170                            qPos.add(companyId);
171    
172                            if (groupId > 0) {
173                                    qPos.add(groupId);
174                            }
175    
176                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
177                                    qPos.add(scope);
178                            }
179    
180                            qPos.add(names, 2);
181                            qPos.add(descriptions, 2);
182    
183                            Iterator<Long> itr = q.iterate();
184    
185                            if (itr.hasNext()) {
186                                    Long count = itr.next();
187    
188                                    if (count != null) {
189                                            return count.intValue();
190                                    }
191                            }
192    
193                            return 0;
194                    }
195                    catch (Exception e) {
196                            throw new SystemException(e);
197                    }
198                    finally {
199                            closeSession(session);
200                    }
201            }
202    
203            protected List<DDLRecordSet> doFindByC_G_N_D_S(
204                            long companyId, long groupId, String[] names, String[] descriptions,
205                            int scope, boolean andOperator, int start, int end,
206                            OrderByComparator orderByComparator)
207                    throws SystemException {
208    
209                    names = CustomSQLUtil.keywords(names);
210                    descriptions = CustomSQLUtil.keywords(descriptions, false);
211    
212                    Session session = null;
213    
214                    try {
215                            session = openSession();
216    
217                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
218    
219                            if (groupId <= 0) {
220                                    sql = StringUtil.replace(
221                                            sql, "(groupId = ?) AND", StringPool.BLANK);
222                            }
223    
224                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
225                                    sql = StringUtil.replace(
226                                            sql, "(scope = ?) AND", StringPool.BLANK);
227                            }
228    
229                            sql = CustomSQLUtil.replaceKeywords(
230                                    sql, "lower(name)", StringPool.LIKE, false, names);
231                            sql = CustomSQLUtil.replaceKeywords(
232                                    sql, "description", StringPool.LIKE, true, descriptions);
233                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
234                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
235    
236                            SQLQuery q = session.createSQLQuery(sql);
237    
238                            q.addEntity("DDLRecordSet", DDLRecordSetImpl.class);
239    
240                            QueryPos qPos = QueryPos.getInstance(q);
241    
242                            qPos.add(companyId);
243    
244                            if (groupId > 0) {
245                                    qPos.add(groupId);
246                            }
247    
248                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
249                                    qPos.add(scope);
250                            }
251    
252                            qPos.add(names, 2);
253                            qPos.add(descriptions, 2);
254    
255                            return (List<DDLRecordSet>)QueryUtil.list(
256                                    q, getDialect(), start, end);
257                    }
258                    catch (Exception e) {
259                            throw new SystemException(e);
260                    }
261                    finally {
262                            closeSession(session);
263                    }
264            }
265    
266    }