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