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