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