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