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.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
030    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSetConstants;
031    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordSetImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Marcellus Tavares
039     * @author Connor McKay
040     */
041    public class DDLRecordSetFinderImpl extends BasePersistenceImpl<DDLRecordSet>
042            implements DDLRecordSetFinder {
043    
044            public static final String COUNT_BY_C_G_N_D_S =
045                    DDLRecordSetFinder.class.getName() + ".countByC_G_N_D_S";
046    
047            public static final String FIND_BY_C_G_N_D_S =
048                    DDLRecordSetFinder.class.getName() + ".findByC_G_N_D_S";
049    
050            @Override
051            public int countByKeywords(
052                            long companyId, long groupId, String keywords, int scope)
053                    throws SystemException {
054    
055                    return doCountByKeywords(companyId, groupId, keywords, scope, false);
056            }
057    
058            @Override
059            public int countByC_G_N_D_S(
060                            long companyId, long groupId, String name, String description,
061                            int scope, boolean andOperator)
062                    throws SystemException {
063    
064                    String[] names = CustomSQLUtil.keywords(name);
065                    String[] descriptions = CustomSQLUtil.keywords(description, false);
066    
067                    return doCountByC_G_N_D_S(
068                            companyId, groupId, names, descriptions, scope, andOperator, false);
069            }
070    
071            @Override
072            public int filterCountByKeywords(
073                            long companyId, long groupId, String keywords, int scope)
074                    throws SystemException {
075    
076                    return doCountByKeywords(companyId, groupId, keywords, scope, true);
077            }
078    
079            @Override
080            public int filterCountByC_G_N_D_S(
081                            long companyId, long groupId, String name, String description,
082                            int scope, boolean andOperator)
083                    throws SystemException {
084    
085                    String[] names = CustomSQLUtil.keywords(name);
086                    String[] descriptions = CustomSQLUtil.keywords(description, false);
087    
088                    return doCountByC_G_N_D_S(
089                            companyId, groupId, names, descriptions, scope, andOperator, true);
090            }
091    
092            @Override
093            public List<DDLRecordSet> filterFindByKeywords(
094                            long companyId, long groupId, String keywords, int scope, int start,
095                            int end, OrderByComparator orderByComparator)
096                    throws SystemException {
097    
098                    String[] names = null;
099                    String[] descriptions = null;
100                    boolean andOperator = false;
101    
102                    if (Validator.isNotNull(keywords)) {
103                            names = CustomSQLUtil.keywords(keywords);
104                            descriptions = CustomSQLUtil.keywords(keywords, false);
105                    }
106                    else {
107                            andOperator = true;
108                    }
109    
110                    return filterFindByC_G_N_D_S(
111                            companyId, groupId, names, descriptions, scope, andOperator, start,
112                            end, orderByComparator);
113            }
114    
115            @Override
116            public List<DDLRecordSet> filterFindByC_G_N_D_S(
117                            long companyId, long groupId, String name, String description,
118                            int scope, boolean andOperator, int start, int end,
119                            OrderByComparator orderByComparator)
120                    throws SystemException {
121    
122                    String[] names = CustomSQLUtil.keywords(name);
123                    String[] descriptions = CustomSQLUtil.keywords(description, false);
124    
125                    return filterFindByC_G_N_D_S(
126                            companyId, groupId, names, descriptions, scope, andOperator, start,
127                            end, orderByComparator);
128            }
129    
130            @Override
131            public List<DDLRecordSet> filterFindByC_G_N_D_S(
132                            long companyId, long groupId, String[] names, String[] descriptions,
133                            int scope, boolean andOperator, int start, int end,
134                            OrderByComparator orderByComparator)
135                    throws SystemException {
136    
137                    return doFindByC_G_N_D_S(
138                            companyId, groupId, names, descriptions, scope, andOperator, start,
139                            end, orderByComparator, true);
140            }
141    
142            @Override
143            public List<DDLRecordSet> findByKeywords(
144                            long companyId, long groupId, String keywords, int scope, int start,
145                            int end, OrderByComparator orderByComparator)
146                    throws SystemException {
147    
148                    String[] names = null;
149                    String[] descriptions = null;
150                    boolean andOperator = false;
151    
152                    if (Validator.isNotNull(keywords)) {
153                            names = CustomSQLUtil.keywords(keywords);
154                            descriptions = CustomSQLUtil.keywords(keywords, false);
155                    }
156                    else {
157                            andOperator = true;
158                    }
159    
160                    return findByC_G_N_D_S(
161                            companyId, groupId, names, descriptions, scope, andOperator, start,
162                            end, orderByComparator);
163            }
164    
165            @Override
166            public List<DDLRecordSet> findByC_G_N_D_S(
167                            long companyId, long groupId, String name, String description,
168                            int scope, boolean andOperator, int start, int end,
169                            OrderByComparator orderByComparator)
170                    throws SystemException {
171    
172                    String[] names = CustomSQLUtil.keywords(name);
173                    String[] descriptions = CustomSQLUtil.keywords(description, false);
174    
175                    return findByC_G_N_D_S(
176                            companyId, groupId, names, descriptions, scope, andOperator, start,
177                            end, orderByComparator);
178            }
179    
180            @Override
181            public List<DDLRecordSet> findByC_G_N_D_S(
182                            long companyId, long groupId, String[] names, String[] descriptions,
183                            int scope, boolean andOperator, int start, int end,
184                            OrderByComparator orderByComparator)
185                    throws SystemException {
186    
187                    return doFindByC_G_N_D_S(
188                            companyId, groupId, names, descriptions, scope, andOperator, start,
189                            end, orderByComparator, false);
190            }
191    
192            protected int doCountByC_G_N_D_S(
193                            long companyId, long groupId, String[] names, String[] descriptions,
194                            int scope, boolean andOperator, boolean inlineSQLHelper)
195                    throws SystemException {
196    
197                    names = CustomSQLUtil.keywords(names);
198                    descriptions = CustomSQLUtil.keywords(descriptions, false);
199    
200                    Session session = null;
201    
202                    try {
203                            session = openSession();
204    
205                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
206    
207                            if (inlineSQLHelper) {
208                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
209                                            sql, DDLRecordSet.class.getName(),
210                                            "DDLRecordSet.recordSetId", groupId);
211                            }
212    
213                            if (groupId <= 0) {
214                                    sql = StringUtil.replace(
215                                            sql, "(DDLRecordSet.groupId = ?) AND", StringPool.BLANK);
216                            }
217    
218                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
219                                    sql = StringUtil.replace(
220                                            sql, "(DDLRecordSet.scope = ?) AND", StringPool.BLANK);
221                            }
222    
223                            sql = CustomSQLUtil.replaceKeywords(
224                                    sql, "lower(DDLRecordSet.name)", StringPool.LIKE, false, names);
225                            sql = CustomSQLUtil.replaceKeywords(
226                                    sql, "DDLRecordSet.description", StringPool.LIKE, true,
227                                    descriptions);
228                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
229    
230                            SQLQuery q = session.createSQLQuery(sql);
231    
232                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
233    
234                            QueryPos qPos = QueryPos.getInstance(q);
235    
236                            qPos.add(companyId);
237    
238                            if (groupId > 0) {
239                                    qPos.add(groupId);
240                            }
241    
242                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
243                                    qPos.add(scope);
244                            }
245    
246                            qPos.add(names, 2);
247                            qPos.add(descriptions, 2);
248    
249                            Iterator<Long> itr = q.iterate();
250    
251                            if (itr.hasNext()) {
252                                    Long count = itr.next();
253    
254                                    if (count != null) {
255                                            return count.intValue();
256                                    }
257                            }
258    
259                            return 0;
260                    }
261                    catch (Exception e) {
262                            throw new SystemException(e);
263                    }
264                    finally {
265                            closeSession(session);
266                    }
267            }
268    
269            protected int doCountByKeywords(
270                            long companyId, long groupId, String keywords, int scope,
271                            boolean inlineSQLHelper)
272                    throws SystemException {
273    
274                    String[] names = null;
275                    String[] descriptions = null;
276                    boolean andOperator = false;
277    
278                    if (Validator.isNotNull(keywords)) {
279                            names = CustomSQLUtil.keywords(keywords);
280                            descriptions = CustomSQLUtil.keywords(keywords, false);
281                    }
282                    else {
283                            andOperator = true;
284                    }
285    
286                    return doCountByC_G_N_D_S(
287                            companyId, groupId, names, descriptions, scope, andOperator,
288                            inlineSQLHelper);
289            }
290    
291            protected List<DDLRecordSet> doFindByC_G_N_D_S(
292                            long companyId, long groupId, String[] names, String[] descriptions,
293                            int scope, boolean andOperator, int start, int end,
294                            OrderByComparator orderByComparator, boolean inlineSQLHelper)
295                    throws SystemException {
296    
297                    names = CustomSQLUtil.keywords(names);
298                    descriptions = CustomSQLUtil.keywords(descriptions, false);
299    
300                    Session session = null;
301    
302                    try {
303                            session = openSession();
304    
305                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
306    
307                            if (inlineSQLHelper) {
308                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
309                                            sql, DDLRecordSet.class.getName(),
310                                            "DDLRecordSet.recordSetId", groupId);
311                            }
312    
313                            if (groupId <= 0) {
314                                    sql = StringUtil.replace(
315                                            sql, "(DDLRecordSet.groupId = ?) AND", StringPool.BLANK);
316                            }
317    
318                            if (scope == DDLRecordSetConstants.SCOPE_ANY) {
319                                    sql = StringUtil.replace(
320                                            sql, "(DDLRecordSet.scope = ?) AND", StringPool.BLANK);
321                            }
322    
323                            sql = CustomSQLUtil.replaceKeywords(
324                                    sql, "lower(DDLRecordSet.name)", StringPool.LIKE, false, names);
325                            sql = CustomSQLUtil.replaceKeywords(
326                                    sql, "DDLRecordSet.description", StringPool.LIKE, true,
327                                    descriptions);
328                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
329                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
330    
331                            SQLQuery q = session.createSQLQuery(sql);
332    
333                            q.addEntity("DDLRecordSet", DDLRecordSetImpl.class);
334    
335                            QueryPos qPos = QueryPos.getInstance(q);
336    
337                            qPos.add(companyId);
338    
339                            if (groupId > 0) {
340                                    qPos.add(groupId);
341                            }
342    
343                            if (scope != DDLRecordSetConstants.SCOPE_ANY) {
344                                    qPos.add(scope);
345                            }
346    
347                            qPos.add(names, 2);
348                            qPos.add(descriptions, 2);
349    
350                            return (List<DDLRecordSet>)QueryUtil.list(
351                                    q, getDialect(), start, end);
352                    }
353                    catch (Exception e) {
354                            throw new SystemException(e);
355                    }
356                    finally {
357                            closeSession(session);
358                    }
359            }
360    
361    }