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, "[$GROUP_ID$]", getGroupIds(groupIds));
167                            sql = CustomSQLUtil.replaceKeywords(
168                                    sql, "lower(name)", StringPool.LIKE, false, names);
169                            sql = CustomSQLUtil.replaceKeywords(
170                                    sql, "description", StringPool.LIKE, true, descriptions);
171                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
172    
173                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
174    
175                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
176    
177                            QueryPos qPos = QueryPos.getInstance(q);
178    
179                            qPos.add(companyId);
180                            qPos.add(groupIds);
181                            qPos.add(names, 2);
182                            qPos.add(descriptions, 2);
183    
184                            int countValue = 0;
185    
186                            if (includeBasicFileEntryType) {
187                                    countValue = 1;
188                            }
189    
190                            Iterator<Long> itr = q.iterate();
191    
192                            if (itr.hasNext()) {
193                                    Long count = itr.next();
194    
195                                    if (count != null) {
196                                            return countValue + count.intValue();
197                                    }
198                            }
199    
200                            return countValue;
201                    }
202                    catch (Exception e) {
203                            throw new SystemException(e);
204                    }
205                    finally {
206                            closeSession(session);
207                    }
208            }
209    
210            protected List<DLFileEntryType> doFindByC_G_N_D_S(
211                    long companyId, long[] groupIds, String[] names, String[] descriptions,
212                    boolean andOperator, boolean includeBasicFileEntryType, int start,
213                    int end, OrderByComparator<DLFileEntryType> orderByComparator,
214                    boolean inlineSQLHelper) {
215    
216                    names = CustomSQLUtil.keywords(names);
217                    descriptions = CustomSQLUtil.keywords(descriptions, false);
218    
219                    Session session = null;
220    
221                    try {
222                            session = openSession();
223    
224                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
225    
226                            if (inlineSQLHelper) {
227                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
228                                            sql, DLFileEntryType.class.getName(),
229                                            "DLFileEntryType.fileEntryTypeId", groupIds);
230                            }
231    
232                            sql = StringUtil.replace(
233                                    sql, "[$BASIC_DOCUMENT$]",
234                                    getBasicDocument(includeBasicFileEntryType));
235                            sql = StringUtil.replace(
236                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
237                            sql = CustomSQLUtil.replaceKeywords(
238                                    sql, "lower(name)", StringPool.LIKE, false, names);
239                            sql = CustomSQLUtil.replaceKeywords(
240                                    sql, "description", StringPool.LIKE, true, descriptions);
241                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
242    
243                            if (orderByComparator != null) {
244                                    String orderByFields = StringUtil.merge(
245                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
246    
247                                    sql = StringUtil.replace(
248                                            sql, "name ASC", orderByFields.concat(" DESC"));
249                            }
250    
251                            if (includeBasicFileEntryType) {
252                                    sql = sql.concat(StringPool.CLOSE_PARENTHESIS);
253                            }
254    
255                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
256    
257                            q.addEntity("DLFileEntryType", DLFileEntryTypeImpl.class);
258    
259                            QueryPos qPos = QueryPos.getInstance(q);
260    
261                            if (includeBasicFileEntryType) {
262                                    qPos.add(names, 2);
263                                    qPos.add(descriptions, 2);
264                            }
265    
266                            qPos.add(companyId);
267                            qPos.add(groupIds);
268                            qPos.add(names, 2);
269                            qPos.add(descriptions, 2);
270    
271                            return (List<DLFileEntryType>)QueryUtil.list(
272                                    q, getDialect(), start, end);
273                    }
274                    catch (Exception e) {
275                            throw new SystemException(e);
276                    }
277                    finally {
278                            closeSession(session);
279                    }
280            }
281    
282            protected String getBasicDocument(boolean includeBasicFileEntryType) {
283                    if (!includeBasicFileEntryType) {
284                            return StringPool.BLANK;
285                    }
286    
287                    StringBundler sb = new StringBundler(6);
288    
289                    sb.append("(SELECT {DLFileEntryType.*} From DLFileEntryType WHERE ");
290                    sb.append("((companyId = 0) AND (groupId = 0) AND (");
291                    sb.append("(lower(name) LIKE ? [$AND_OR_NULL_CHECK$]) ");
292                    sb.append("[$AND_OR_CONNECTOR$] ");
293                    sb.append("(description LIKE ? [$AND_OR_NULL_CHECK$]) ");
294                    sb.append("))) UNION ALL (");
295    
296                    return sb.toString();
297            }
298    
299            protected String getGroupIds(long[] groupIds) {
300                    if (groupIds.length == 0) {
301                            return StringPool.BLANK;
302                    }
303    
304                    StringBundler sb = new StringBundler(groupIds.length * 2);
305    
306                    sb.append(StringPool.OPEN_PARENTHESIS);
307    
308                    for (int i = 0; i < groupIds.length; i++) {
309                            sb.append("groupId = ?");
310    
311                            if ((i + 1) < groupIds.length) {
312                                    sb.append(" OR ");
313                            }
314                    }
315    
316                    sb.append(") AND");
317    
318                    return sb.toString();
319            }
320    
321    }