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