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