001    /**
002     * Copyright (c) 2000-2010 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.journal.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.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.journal.model.JournalStructure;
029    import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
030    import com.liferay.util.dao.orm.CustomSQLUtil;
031    
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class JournalStructureFinderImpl
039            extends BasePersistenceImpl<JournalStructure>
040            implements JournalStructureFinder {
041    
042            public static String COUNT_BY_C_G_S_N_D =
043                    JournalStructureFinder.class.getName() + ".countByC_G_S_N_D";
044    
045            public static String FIND_BY_C_G_S_N_D =
046                    JournalStructureFinder.class.getName() + ".findByC_G_S_N_D";
047    
048            public int countByKeywords(long companyId, long groupId, String keywords)
049                    throws SystemException {
050    
051                    String[] structureIds = null;
052                    String[] names = null;
053                    String[] descriptions = null;
054                    boolean andOperator = false;
055    
056                    if (Validator.isNotNull(keywords)) {
057                            structureIds = CustomSQLUtil.keywords(keywords, false);
058                            names = CustomSQLUtil.keywords(keywords);
059                            descriptions = CustomSQLUtil.keywords(keywords);
060                    }
061                    else {
062                            andOperator = true;
063                    }
064    
065                    return countByC_G_S_N_D(
066                            companyId, groupId, structureIds, names, descriptions, andOperator);
067            }
068    
069            public int countByC_G_S_N_D(
070                            long companyId, long groupId, String structureId, String name,
071                            String description, boolean andOperator)
072                    throws SystemException {
073    
074                    return countByC_G_S_N_D(
075                            companyId, groupId, new String[] {structureId}, new String[] {name},
076                            new String[] {description}, andOperator);
077            }
078    
079            public int countByC_G_S_N_D(
080                            long companyId, long groupId, String[] structureIds, String[] names,
081                            String[] descriptions, boolean andOperator)
082                    throws SystemException {
083    
084                    structureIds = CustomSQLUtil.keywords(structureIds, false);
085                    names = CustomSQLUtil.keywords(names);
086                    descriptions = CustomSQLUtil.keywords(descriptions);
087    
088                    Session session = null;
089    
090                    try {
091                            session = openSession();
092    
093                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_S_N_D);
094    
095                            if (groupId <= 0) {
096                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
097                            }
098    
099                            sql = CustomSQLUtil.replaceKeywords(
100                                    sql, "structureId", StringPool.LIKE, false, structureIds);
101                            sql = CustomSQLUtil.replaceKeywords(
102                                    sql, "lower(name)", StringPool.LIKE, false, names);
103                            sql = CustomSQLUtil.replaceKeywords(
104                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
105    
106                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
107    
108                            SQLQuery q = session.createSQLQuery(sql);
109    
110                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
111    
112                            QueryPos qPos = QueryPos.getInstance(q);
113    
114                            qPos.add(companyId);
115    
116                            if (groupId > 0) {
117                                    qPos.add(groupId);
118                            }
119    
120                            qPos.add(structureIds, 2);
121                            qPos.add(names, 2);
122                            qPos.add(descriptions, 2);
123    
124                            Iterator<Long> itr = q.list().iterator();
125    
126                            if (itr.hasNext()) {
127                                    Long count = itr.next();
128    
129                                    if (count != null) {
130                                            return count.intValue();
131                                    }
132                            }
133    
134                            return 0;
135                    }
136                    catch (Exception e) {
137                            throw new SystemException(e);
138                    }
139                    finally {
140                            closeSession(session);
141                    }
142            }
143    
144            public List<JournalStructure> findByKeywords(
145                            long companyId, long groupId, String keywords, int start, int end,
146                            OrderByComparator obc)
147                    throws SystemException {
148    
149                    String[] structureIds = null;
150                    String[] names = null;
151                    String[] descriptions = null;
152                    boolean andOperator = false;
153    
154                    if (Validator.isNotNull(keywords)) {
155                            structureIds = CustomSQLUtil.keywords(keywords, false);
156                            names = CustomSQLUtil.keywords(keywords);
157                            descriptions = CustomSQLUtil.keywords(keywords);
158                    }
159                    else {
160                            andOperator = true;
161                    }
162    
163                    return findByC_G_S_N_D(
164                            companyId, groupId, structureIds, names, descriptions, andOperator,
165                            start, end, obc);
166            }
167    
168            public List<JournalStructure> findByC_G_S_N_D(
169                            long companyId, long groupId, String structureId, String name,
170                            String description, boolean andOperator, int start, int end,
171                            OrderByComparator obc)
172                    throws SystemException {
173    
174                    return findByC_G_S_N_D(
175                            companyId, groupId, new String[] {structureId}, new String[] {name},
176                            new String[] {description}, andOperator, start, end, obc);
177            }
178    
179            public List<JournalStructure> findByC_G_S_N_D(
180                            long companyId, long groupId, String[] structureIds, String[] names,
181                            String[] descriptions, boolean andOperator, int start, int end,
182                            OrderByComparator obc)
183                    throws SystemException {
184    
185                    structureIds = CustomSQLUtil.keywords(structureIds, false);
186                    names = CustomSQLUtil.keywords(names);
187                    descriptions = CustomSQLUtil.keywords(descriptions);
188    
189                    Session session = null;
190    
191                    try {
192                            session = openSession();
193    
194                            String sql = CustomSQLUtil.get(FIND_BY_C_G_S_N_D);
195    
196                            if (groupId <= 0) {
197                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
198                            }
199    
200                            sql = CustomSQLUtil.replaceKeywords(
201                                    sql, "structureId", StringPool.LIKE, false, structureIds);
202                            sql = CustomSQLUtil.replaceKeywords(
203                                    sql, "lower(name)", StringPool.LIKE, false, names);
204                            sql = CustomSQLUtil.replaceKeywords(
205                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
206    
207                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
208                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
209    
210                            SQLQuery q = session.createSQLQuery(sql);
211    
212                            q.addEntity("JournalStructure", JournalStructureImpl.class);
213    
214                            QueryPos qPos = QueryPos.getInstance(q);
215    
216                            qPos.add(companyId);
217    
218                            if (groupId > 0) {
219                                    qPos.add(groupId);
220                            }
221    
222                            qPos.add(structureIds, 2);
223                            qPos.add(names, 2);
224                            qPos.add(descriptions, 2);
225    
226                            return (List<JournalStructure>)QueryUtil.list(
227                                    q, getDialect(), start, end);
228                    }
229                    catch (Exception e) {
230                            throw new SystemException(e);
231                    }
232                    finally {
233                            closeSession(session);
234                    }
235            }
236    
237    }