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.documentlibrary.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.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.portlet.documentlibrary.model.DLFileEntryType;
030    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryTypeImpl;
031    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryTypeFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Sergio Gonz??lez
039     * @author Connor McKay
040     * @author Alexander Chow
041     */
042    public class DLFileEntryTypeFinderImpl
043            extends DLFileEntryTypeFinderBaseImpl implements DLFileEntryTypeFinder {
044    
045            public static final String COUNT_BY_C_G_N_D_S =
046                    DLFileEntryTypeFinder.class.getName() + ".countByC_G_N_D_S";
047    
048            public static final String FIND_BY_C_G_N_D_S =
049                    DLFileEntryTypeFinder.class.getName() + ".findByC_G_N_D_S";
050    
051            public static final String JOIN_BY_FILE_ENTRY_TYPE =
052                    DLFileEntryTypeFinder.class.getName() + ".joinByFileEntryType";
053    
054            @Override
055            public int countByKeywords(
056                    long companyId, long[] groupIds, String keywords,
057                    boolean includeBasicFileEntryType) {
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 doCountByC_G_N_D_S(
072                            companyId, groupIds, names, descriptions, andOperator,
073                            includeBasicFileEntryType, false);
074            }
075    
076            @Override
077            public int filterCountByKeywords(
078                    long companyId, long[] groupIds, String keywords,
079                    boolean includeBasicFileEntryType) {
080    
081                    String[] names = null;
082                    String[] descriptions = null;
083                    boolean andOperator = false;
084    
085                    if (Validator.isNotNull(keywords)) {
086                            names = CustomSQLUtil.keywords(keywords);
087                            descriptions = CustomSQLUtil.keywords(keywords, false);
088                    }
089                    else {
090                            andOperator = true;
091                    }
092    
093                    return doCountByC_G_N_D_S(
094                            companyId, groupIds, names, descriptions, andOperator,
095                            includeBasicFileEntryType, true);
096            }
097    
098            @Override
099            public List<DLFileEntryType> filterFindByKeywords(
100                    long companyId, long[] groupIds, String keywords,
101                    boolean includeBasicFileEntryType, int start, int end,
102                    OrderByComparator<DLFileEntryType> orderByComparator) {
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 doFindByC_G_N_D_S(
117                            companyId, groupIds, names, descriptions, andOperator,
118                            includeBasicFileEntryType, start, end, orderByComparator, true);
119            }
120    
121            @Override
122            public List<DLFileEntryType> findByKeywords(
123                    long companyId, long[] groupIds, String keywords,
124                    boolean includeBasicFileEntryType, int start, int end,
125                    OrderByComparator<DLFileEntryType> orderByComparator) {
126    
127                    String[] names = null;
128                    String[] descriptions = null;
129                    boolean andOperator = false;
130    
131                    if (Validator.isNotNull(keywords)) {
132                            names = CustomSQLUtil.keywords(keywords);
133                            descriptions = CustomSQLUtil.keywords(keywords, false);
134                    }
135                    else {
136                            andOperator = true;
137                    }
138    
139                    return doFindByC_G_N_D_S(
140                            companyId, groupIds, names, descriptions, andOperator,
141                            includeBasicFileEntryType, start, end, orderByComparator, false);
142            }
143    
144            protected int doCountByC_G_N_D_S(
145                    long companyId, long[] groupIds, String[] names, String[] descriptions,
146                    boolean andOperator, boolean includeBasicFileEntryType,
147                    boolean inlineSQLHelper) {
148    
149                    names = CustomSQLUtil.keywords(names);
150                    descriptions = CustomSQLUtil.keywords(descriptions, false);
151    
152                    Session session = null;
153    
154                    try {
155                            session = openSession();
156    
157                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
158    
159                            if (inlineSQLHelper) {
160                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
161                                            sql, DLFileEntryType.class.getName(),
162                                            "DLFileEntryType.fileEntryTypeId", groupIds);
163                            }
164    
165                            sql = StringUtil.replace(
166                                    sql, "[$BASIC_DOCUMENT$]",
167                                    getBasicDocumentCount(includeBasicFileEntryType));
168                            sql = StringUtil.replace(
169                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds.length));
170                            sql = CustomSQLUtil.replaceKeywords(
171                                    sql, "lower(DLFileEntryType.name)", StringPool.LIKE, false,
172                                    names);
173                            sql = CustomSQLUtil.replaceKeywords(
174                                    sql, "DLFileEntryType.description", StringPool.LIKE, true,
175                                    descriptions);
176                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
177    
178                            if (includeBasicFileEntryType) {
179                                    sql = sql.concat(StringPool.CLOSE_PARENTHESIS);
180                            }
181    
182                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
183    
184                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
185    
186                            QueryPos qPos = QueryPos.getInstance(q);
187    
188                            if (includeBasicFileEntryType) {
189                                    qPos.add(names, 2);
190                                    qPos.add(descriptions, 2);
191                            }
192    
193                            qPos.add(companyId);
194                            qPos.add(groupIds);
195                            qPos.add(names, 2);
196                            qPos.add(descriptions, 2);
197    
198                            int countValue = 0;
199    
200                            Iterator<Long> itr = q.iterate();
201    
202                            while (itr.hasNext()) {
203                                    Long count = itr.next();
204    
205                                    if (count != null) {
206                                            countValue += count.intValue();
207                                    }
208                            }
209    
210                            return countValue;
211                    }
212                    catch (Exception e) {
213                            throw new SystemException(e);
214                    }
215                    finally {
216                            closeSession(session);
217                    }
218            }
219    
220            protected List<DLFileEntryType> doFindByC_G_N_D_S(
221                    long companyId, long[] groupIds, String[] names, String[] descriptions,
222                    boolean andOperator, boolean includeBasicFileEntryType, int start,
223                    int end, OrderByComparator<DLFileEntryType> orderByComparator,
224                    boolean inlineSQLHelper) {
225    
226                    names = CustomSQLUtil.keywords(names);
227                    descriptions = CustomSQLUtil.keywords(descriptions, false);
228    
229                    Session session = null;
230    
231                    try {
232                            session = openSession();
233    
234                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
235    
236                            if (inlineSQLHelper) {
237                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
238                                            sql, DLFileEntryType.class.getName(),
239                                            "DLFileEntryType.fileEntryTypeId", groupIds);
240                            }
241    
242                            sql = StringUtil.replace(
243                                    sql, "[$BASIC_DOCUMENT$]",
244                                    getBasicDocument(includeBasicFileEntryType));
245                            sql = StringUtil.replace(
246                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds.length));
247                            sql = CustomSQLUtil.replaceKeywords(
248                                    sql, "lower(DLFileEntryType.name)", StringPool.LIKE, false,
249                                    names);
250                            sql = CustomSQLUtil.replaceKeywords(
251                                    sql, "DLFileEntryType.description", StringPool.LIKE, true,
252                                    descriptions);
253                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
254    
255                            if (orderByComparator != null) {
256                                    String orderByFields = StringUtil.merge(
257                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
258    
259                                    sql = StringUtil.replace(
260                                            sql, "DLFileEntryType.name ASC",
261                                            orderByFields.concat(" DESC"));
262                            }
263    
264                            if (includeBasicFileEntryType) {
265                                    sql = sql.concat(StringPool.CLOSE_PARENTHESIS);
266                            }
267    
268                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
269    
270                            q.addEntity("DLFileEntryType", DLFileEntryTypeImpl.class);
271    
272                            QueryPos qPos = QueryPos.getInstance(q);
273    
274                            if (includeBasicFileEntryType) {
275                                    qPos.add(names, 2);
276                                    qPos.add(descriptions, 2);
277                            }
278    
279                            qPos.add(companyId);
280                            qPos.add(groupIds);
281                            qPos.add(names, 2);
282                            qPos.add(descriptions, 2);
283    
284                            return (List<DLFileEntryType>)QueryUtil.list(
285                                    q, getDialect(), start, end);
286                    }
287                    catch (Exception e) {
288                            throw new SystemException(e);
289                    }
290                    finally {
291                            closeSession(session);
292                    }
293            }
294    
295            protected String getBasicDocument(boolean includeBasicFileEntryType) {
296                    if (!includeBasicFileEntryType) {
297                            return StringPool.BLANK;
298                    }
299    
300                    return getBasicDocument(
301                            "(SELECT {DLFileEntryType.*} From DLFileEntryType WHERE ");
302            }
303    
304            protected String getBasicDocument(String prefix) {
305                    StringBundler sb = new StringBundler(9);
306    
307                    sb.append(prefix);
308                    sb.append("((DLFileEntryType.companyId = 0) ");
309                    sb.append("AND (DLFileEntryType.groupId = 0) AND (");
310                    sb.append("(lower(DLFileEntryType.name) LIKE ? ");
311                    sb.append("[$AND_OR_NULL_CHECK$]) ");
312                    sb.append("[$AND_OR_CONNECTOR$] ");
313                    sb.append("(DLFileEntryType.description LIKE ? ");
314                    sb.append("[$AND_OR_NULL_CHECK$]) ");
315                    sb.append("))) UNION ALL (");
316    
317                    return sb.toString();
318            }
319    
320            protected String getBasicDocumentCount(boolean includeBasicFileEntryType) {
321                    if (!includeBasicFileEntryType) {
322                            return StringPool.BLANK;
323                    }
324    
325                    return getBasicDocument(
326                            "(SELECT COUNT(*) AS COUNT_VALUE From DLFileEntryType WHERE ");
327            }
328    
329            protected String getGroupIds(int size) {
330                    if (size == 0) {
331                            return StringPool.BLANK;
332                    }
333    
334                    StringBundler sb = new StringBundler(size + 1);
335    
336                    sb.append(StringPool.OPEN_PARENTHESIS);
337    
338                    for (int i = 0; i < size - 1; i++) {
339                            sb.append("DLFileEntryType.groupId = ? OR ");
340                    }
341    
342                    sb.append("DLFileEntryType.groupId = ?) AND");
343    
344                    return sb.toString();
345            }
346    
347    }