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