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(sql, "(groupId = ?) AND", "");
150                            }
151    
152                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
153                                    sql = StringUtil.replace(sql, "(scope = ?) AND", "");
154                            }
155    
156                            sql = CustomSQLUtil.replaceKeywords(
157                                    sql, "lower(name)", StringPool.LIKE, false, names);
158                            sql = CustomSQLUtil.replaceKeywords(
159                                    sql, "description", StringPool.LIKE, true, descriptions);
160                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
161    
162                            SQLQuery q = session.createSQLQuery(sql);
163    
164                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
165    
166                            QueryPos qPos = QueryPos.getInstance(q);
167    
168                            qPos.add(companyId);
169    
170                            if (groupId > 0) {
171                                    qPos.add(groupId);
172                            }
173    
174                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
175                                    qPos.add(scope);
176                            }
177    
178                            qPos.add(names, 2);
179                            qPos.add(descriptions, 2);
180    
181                            Iterator<Long> itr = q.iterate();
182    
183                            if (itr.hasNext()) {
184                                    Long count = itr.next();
185    
186                                    if (count != null) {
187                                            return count.intValue();
188                                    }
189                            }
190    
191                            return 0;
192                    }
193                    catch (Exception e) {
194                            throw new SystemException(e);
195                    }
196                    finally {
197                            closeSession(session);
198                    }
199            }
200    
201            protected List<DDLRecordSet> doFindByC_G_N_D_S(
202                            long companyId, long groupId, String[] names, String[] descriptions,
203                            int scope, boolean andOperator, int start, int end,
204                            OrderByComparator orderByComparator)
205                    throws SystemException {
206    
207                    names = CustomSQLUtil.keywords(names);
208                    descriptions = CustomSQLUtil.keywords(descriptions, false);
209    
210                    Session session = null;
211    
212                    try {
213                            session = openSession();
214    
215                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
216    
217                            if (groupId <= 0) {
218                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
219                            }
220    
221                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
222                                    sql = StringUtil.replace(sql, "(scope = ?) AND", "");
223                            }
224    
225                            sql = CustomSQLUtil.replaceKeywords(
226                                    sql, "lower(name)", StringPool.LIKE, false, names);
227                            sql = CustomSQLUtil.replaceKeywords(
228                                    sql, "description", StringPool.LIKE, true, descriptions);
229                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
230                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
231    
232                            SQLQuery q = session.createSQLQuery(sql);
233    
234                            q.addEntity("DDLRecordSet", DDLRecordSetImpl.class);
235    
236                            QueryPos qPos = QueryPos.getInstance(q);
237    
238                            qPos.add(companyId);
239    
240                            if (groupId > 0) {
241                                    qPos.add(groupId);
242                            }
243    
244                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
245                                    qPos.add(scope);
246                            }
247    
248                            qPos.add(names, 2);
249                            qPos.add(descriptions, 2);
250    
251                            return (List<DDLRecordSet>)QueryUtil.list(
252                                    q, getDialect(), start, end);
253                    }
254                    catch (Exception e) {
255                            throw new SystemException(e);
256                    }
257                    finally {
258                            closeSession(session);
259                    }
260            }
261    
262    }