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