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.journal.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.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.journal.model.JournalTemplate;
031    import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Bruno Farache
040     * @author Prakash Reddy
041     * @author Connor McKay
042     */
043    public class JournalTemplateFinderImpl
044            extends BasePersistenceImpl<JournalTemplate>
045            implements JournalTemplateFinder {
046    
047            public static final String COUNT_BY_C_G_T_S_N_D =
048                    JournalTemplateFinder.class.getName() + ".countByC_G_T_S_N_D";
049    
050            public static final String FIND_BY_C_G_T_S_N_D =
051                    JournalTemplateFinder.class.getName() + ".findByC_G_T_S_N_D";
052    
053            @Override
054            public int countByKeywords(
055                            long companyId, long[] groupIds, String keywords,
056                            String structureId, String structureIdComparator)
057                    throws SystemException {
058    
059                    String[] templateIds = null;
060                    String[] names = null;
061                    String[] descriptions = null;
062                    boolean andOperator = false;
063    
064                    if (Validator.isNotNull(keywords)) {
065                            templateIds = CustomSQLUtil.keywords(keywords, false);
066                            names = CustomSQLUtil.keywords(keywords);
067                            descriptions = CustomSQLUtil.keywords(keywords);
068                    }
069                    else {
070                            andOperator = true;
071                    }
072    
073                    return doCountByC_G_T_S_N_D(
074                            companyId, groupIds, templateIds, structureId,
075                            structureIdComparator, names, descriptions, andOperator, false);
076            }
077    
078            @Override
079            public int countByC_G_T_S_N_D(
080                            long companyId, long[] groupIds, String templateId,
081                            String structureId, String structureIdComparator, String name,
082                            String description, boolean andOperator)
083                    throws SystemException {
084    
085                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
086                    String[] names = CustomSQLUtil.keywords(name);
087                    String[] descriptions = CustomSQLUtil.keywords(description);
088    
089                    return doCountByC_G_T_S_N_D(
090                            companyId, groupIds, templateIds, structureId,
091                            structureIdComparator, names, descriptions, andOperator, false);
092            }
093    
094            @Override
095            public int filterCountByKeywords(
096                            long companyId, long[] groupIds, String keywords,
097                            String structureId, String structureIdComparator)
098                    throws SystemException {
099    
100                    String[] templateIds = null;
101                    String[] names = null;
102                    String[] descriptions = null;
103                    boolean andOperator = false;
104    
105                    if (Validator.isNotNull(keywords)) {
106                            templateIds = CustomSQLUtil.keywords(keywords, false);
107                            names = CustomSQLUtil.keywords(keywords);
108                            descriptions = CustomSQLUtil.keywords(keywords);
109                    }
110                    else {
111                            andOperator = true;
112                    }
113    
114                    return doCountByC_G_T_S_N_D(
115                            companyId, groupIds, templateIds, structureId,
116                            structureIdComparator, names, descriptions, andOperator, true);
117            }
118    
119            @Override
120            public int filterCountByC_G_T_S_N_D(
121                            long companyId, long[] groupIds, String templateId,
122                            String structureId, String structureIdComparator, String name,
123                            String description, boolean andOperator)
124                    throws SystemException {
125    
126                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
127                    String[] names = CustomSQLUtil.keywords(name);
128                    String[] descriptions = CustomSQLUtil.keywords(description);
129    
130                    return doCountByC_G_T_S_N_D(
131                            companyId, groupIds, templateIds, structureId,
132                            structureIdComparator, names, descriptions, andOperator, true);
133            }
134    
135            @Override
136            public List<JournalTemplate> filterFindByKeywords(
137                            long companyId, long[] groupIds, String keywords,
138                            String structureId, String structureIdComparator, int start,
139                            int end, OrderByComparator obc)
140                    throws SystemException {
141    
142                    String[] templateIds = null;
143                    String[] names = null;
144                    String[] descriptions = null;
145                    boolean andOperator = false;
146    
147                    if (Validator.isNotNull(keywords)) {
148                            templateIds = CustomSQLUtil.keywords(keywords, false);
149                            names = CustomSQLUtil.keywords(keywords);
150                            descriptions = CustomSQLUtil.keywords(keywords);
151                    }
152                    else {
153                            andOperator = true;
154                    }
155    
156                    return doFindByC_G_T_S_N_D(
157                            companyId, groupIds, templateIds, structureId,
158                            structureIdComparator, names, descriptions, andOperator, start, end,
159                            obc, true);
160            }
161    
162            @Override
163            public List<JournalTemplate> filterFindByC_G_T_S_N_D(
164                            long companyId, long[] groupIds, String templateId,
165                            String structureId, String structureIdComparator, String name,
166                            String description, boolean andOperator, int start, int end,
167                            OrderByComparator obc)
168                    throws SystemException {
169    
170                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
171                    String[] names = CustomSQLUtil.keywords(name);
172                    String[] descriptions = CustomSQLUtil.keywords(description);
173    
174                    return doFindByC_G_T_S_N_D(
175                            companyId, groupIds, templateIds, structureId,
176                            structureIdComparator, names, descriptions, andOperator, start, end,
177                            obc, true);
178            }
179    
180            @Override
181            public List<JournalTemplate> findByKeywords(
182                            long companyId, long[] groupIds, String keywords,
183                            String structureId, String structureIdComparator, int start,
184                            int end, OrderByComparator obc)
185                    throws SystemException {
186    
187                    String[] templateIds = null;
188                    String[] names = null;
189                    String[] descriptions = null;
190                    boolean andOperator = false;
191    
192                    if (Validator.isNotNull(keywords)) {
193                            templateIds = CustomSQLUtil.keywords(keywords, false);
194                            names = CustomSQLUtil.keywords(keywords);
195                            descriptions = CustomSQLUtil.keywords(keywords);
196                    }
197                    else {
198                            andOperator = true;
199                    }
200    
201                    return doFindByC_G_T_S_N_D(
202                            companyId, groupIds, templateIds, structureId,
203                            structureIdComparator, names, descriptions, andOperator, start, end,
204                            obc, false);
205            }
206    
207            @Override
208            public List<JournalTemplate> findByC_G_T_S_N_D(
209                            long companyId, long[] groupIds, String templateId,
210                            String structureId, String structureIdComparator, String name,
211                            String description, boolean andOperator, int start, int end,
212                            OrderByComparator obc)
213                    throws SystemException {
214    
215                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
216                    String[] names = CustomSQLUtil.keywords(name);
217                    String[] descriptions = CustomSQLUtil.keywords(description);
218    
219                    return doFindByC_G_T_S_N_D(
220                            companyId, groupIds, templateIds, structureId,
221                            structureIdComparator, names, descriptions, andOperator, start, end,
222                            obc, false);
223            }
224    
225            protected int doCountByC_G_T_S_N_D(
226                            long companyId, long[] groupIds, String[] templateIds,
227                            String structureId, String structureIdComparator, String[] names,
228                            String[] descriptions, boolean andOperator, boolean inlineSQLHelper)
229                    throws SystemException {
230    
231                    templateIds = CustomSQLUtil.keywords(templateIds, false);
232                    names = CustomSQLUtil.keywords(names);
233                    descriptions = CustomSQLUtil.keywords(descriptions);
234    
235                    Session session = null;
236    
237                    try {
238                            session = openSession();
239    
240                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_T_S_N_D);
241    
242                            sql = StringUtil.replace(
243                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
244                            sql = CustomSQLUtil.replaceKeywords(
245                                    sql, "templateId", StringPool.LIKE, false, templateIds);
246    
247                            if (structureIdComparator.equals(StringPool.NOT_LIKE)) {
248                                    sql = replaceStructureIdComparator(sql);
249                            }
250    
251                            sql = CustomSQLUtil.replaceKeywords(
252                                    sql, "lower(name)", StringPool.LIKE, false, names);
253                            sql = CustomSQLUtil.replaceKeywords(
254                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
255    
256                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
257    
258                            if (inlineSQLHelper) {
259                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
260                                            sql, JournalTemplate.class.getName(), "JournalTemplate.id_",
261                                            groupIds);
262    
263                                    sql = StringUtil.replace(
264                                            sql, "(companyId", "(JournalTemplate.companyId");
265    
266                                    sql = StringUtil.replace(sql, "(name", "(JournalTemplate.name");
267                            }
268    
269                            SQLQuery q = session.createSQLQuery(sql);
270    
271                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
272    
273                            QueryPos qPos = QueryPos.getInstance(q);
274    
275                            qPos.add(companyId);
276                            qPos.add(groupIds);
277                            qPos.add(templateIds, 2);
278    
279                            if (structureIdComparator.equals(StringPool.LIKE)) {
280                                    qPos.add(structureId);
281                                    qPos.add(structureId);
282                            }
283    
284                            qPos.add(names, 2);
285                            qPos.add(descriptions, 2);
286    
287                            if (structureIdComparator.equals(StringPool.NOT_LIKE)) {
288                                    if (CustomSQLUtil.isVendorOracle()) {
289                                    }
290                                    else {
291                                            qPos.add(structureId);
292                                    }
293                            }
294    
295                            Iterator<Long> itr = q.iterate();
296    
297                            if (itr.hasNext()) {
298                                    Long count = itr.next();
299    
300                                    if (count != null) {
301                                            return count.intValue();
302                                    }
303                            }
304    
305                            return 0;
306                    }
307                    catch (Exception e) {
308                            throw new SystemException(e);
309                    }
310                    finally {
311                            closeSession(session);
312                    }
313            }
314    
315            protected List<JournalTemplate> doFindByC_G_T_S_N_D(
316                            long companyId, long[] groupIds, String[] templateIds,
317                            String structureId, String structureIdComparator, String[] names,
318                            String[] descriptions, boolean andOperator, int start, int end,
319                            OrderByComparator obc, boolean inlineSQLHelper)
320                    throws SystemException {
321    
322                    templateIds = CustomSQLUtil.keywords(templateIds, false);
323                    names = CustomSQLUtil.keywords(names);
324                    descriptions = CustomSQLUtil.keywords(descriptions);
325    
326                    Session session = null;
327    
328                    try {
329                            session = openSession();
330    
331                            String sql = CustomSQLUtil.get(FIND_BY_C_G_T_S_N_D);
332    
333                            sql = StringUtil.replace(
334                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
335                            sql = CustomSQLUtil.replaceKeywords(
336                                    sql, "templateId", StringPool.LIKE, false, templateIds);
337    
338                            if (structureIdComparator.equals(StringPool.NOT_LIKE)) {
339                                    sql = replaceStructureIdComparator(sql);
340                            }
341    
342                            sql = CustomSQLUtil.replaceKeywords(
343                                    sql, "lower(name)", StringPool.LIKE, false, names);
344                            sql = CustomSQLUtil.replaceKeywords(
345                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
346    
347                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
348                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
349    
350                            if (inlineSQLHelper) {
351                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
352                                            sql, JournalTemplate.class.getName(), "JournalTemplate.id_",
353                                            groupIds);
354    
355                                    sql = StringUtil.replace(
356                                            sql, "(companyId", "(JournalTemplate.companyId");
357    
358                                    sql = StringUtil.replace(sql, "(name", "(JournalTemplate.name");
359                            }
360    
361                            SQLQuery q = session.createSQLQuery(sql);
362    
363                            q.addEntity("JournalTemplate", JournalTemplateImpl.class);
364    
365                            QueryPos qPos = QueryPos.getInstance(q);
366    
367                            qPos.add(companyId);
368                            qPos.add(groupIds);
369                            qPos.add(templateIds, 2);
370    
371                            if (structureIdComparator.equals(StringPool.LIKE)) {
372                                    qPos.add(structureId);
373                                    qPos.add(structureId);
374                            }
375    
376                            qPos.add(names, 2);
377                            qPos.add(descriptions, 2);
378    
379                            if (structureIdComparator.equals(StringPool.NOT_LIKE)) {
380                                    if (CustomSQLUtil.isVendorOracle()) {
381                                    }
382                                    else {
383                                            qPos.add(structureId);
384                                    }
385                            }
386    
387                            return (List<JournalTemplate>)QueryUtil.list(
388                                    q, getDialect(), start, end);
389                    }
390                    catch (Exception e) {
391                            throw new SystemException(e);
392                    }
393                    finally {
394                            closeSession(session);
395                    }
396            }
397    
398            protected String getGroupIds(long[] groupIds) {
399                    if (groupIds.length == 0) {
400                            return StringPool.BLANK;
401                    }
402    
403                    StringBundler sb = new StringBundler(groupIds.length + 2);
404    
405                    sb.append(" (groupId = ? ");
406    
407                    for (int i = 1; i < groupIds.length; i++) {
408                            sb.append(" OR groupId = ? ");
409                    }
410    
411                    sb.append(") AND ");
412    
413                    return sb.toString();
414            }
415    
416            protected String replaceStructureIdComparator(String sql) {
417                    String insertSQL = "structureId NOT LIKE ? AND structureId IS NOT NULL";
418    
419                    if (CustomSQLUtil.isVendorOracle()) {
420                            insertSQL = "structureId IS NOT NULL";
421                    }
422    
423                    insertSQL = " AND (" + insertSQL + ") ";
424    
425                    String removeSQL =
426                            "(structureId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
427    
428                    sql = StringUtil.replace(sql, removeSQL, StringPool.BLANK);
429    
430                    int pos = sql.indexOf("ORDER BY");
431    
432                    if (pos == -1) {
433                            sql = sql + insertSQL;
434                    }
435                    else {
436                            sql = StringUtil.insert(sql, insertSQL, pos);
437                    }
438    
439                    return sql;
440            }
441    
442    }