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