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.JournalTemplate;
029    import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
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     * @author Bruno Farache
038     * @author Prakash Reddy
039     */
040    public class JournalTemplateFinderImpl
041            extends BasePersistenceImpl<JournalTemplate>
042            implements JournalTemplateFinder {
043    
044            public static String COUNT_BY_C_G_T_S_N_D =
045                    JournalTemplateFinder.class.getName() + ".countByC_G_T_S_N_D";
046    
047            public static String FIND_BY_C_G_T_S_N_D =
048                    JournalTemplateFinder.class.getName() + ".findByC_G_T_S_N_D";
049    
050            public int countByKeywords(
051                            long companyId, long groupId, String keywords, String structureId,
052                            String structureIdComparator)
053                    throws SystemException {
054    
055                    String[] templateIds = null;
056                    String[] names = null;
057                    String[] descriptions = null;
058                    boolean andOperator = false;
059    
060                    if (Validator.isNotNull(keywords)) {
061                            templateIds = CustomSQLUtil.keywords(keywords, false);
062                            names = CustomSQLUtil.keywords(keywords);
063                            descriptions = CustomSQLUtil.keywords(keywords);
064                    }
065                    else {
066                            andOperator = true;
067                    }
068    
069                    return countByC_G_T_S_N_D(
070                            companyId, groupId, templateIds, structureId, structureIdComparator,
071                            names, descriptions, andOperator);
072            }
073    
074            public int countByC_G_T_S_N_D(
075                            long companyId, long groupId, String templateId, String structureId,
076                            String structureIdComparator, String name, String description,
077                            boolean andOperator)
078                    throws SystemException {
079    
080                    return countByC_G_T_S_N_D(
081                            companyId, groupId, new String[] {templateId}, structureId,
082                            structureIdComparator, new String[] {name},
083                            new String[] {description}, andOperator);
084            }
085    
086            public int countByC_G_T_S_N_D(
087                            long companyId, long groupId, String[] templateIds,
088                            String structureId, String structureIdComparator, String[] names,
089                            String[] descriptions, boolean andOperator)
090                    throws SystemException {
091    
092                    templateIds = CustomSQLUtil.keywords(templateIds, false);
093                    names = CustomSQLUtil.keywords(names);
094                    descriptions = CustomSQLUtil.keywords(descriptions);
095    
096                    Session session = null;
097    
098                    try {
099                            session = openSession();
100    
101                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_T_S_N_D);
102    
103                            if (groupId <= 0) {
104                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
105                            }
106    
107                            sql = CustomSQLUtil.replaceKeywords(
108                                    sql, "templateId", StringPool.LIKE, false, templateIds);
109    
110                            if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
111                                    sql = replaceStructureIdComparator(sql);
112                            }
113    
114                            sql = CustomSQLUtil.replaceKeywords(
115                                    sql, "lower(name)", StringPool.LIKE, false, names);
116                            sql = CustomSQLUtil.replaceKeywords(
117                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
118    
119                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
120    
121                            SQLQuery q = session.createSQLQuery(sql);
122    
123                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
124    
125                            QueryPos qPos = QueryPos.getInstance(q);
126    
127                            qPos.add(companyId);
128    
129                            if (groupId > 0) {
130                                    qPos.add(groupId);
131                            }
132    
133                            qPos.add(templateIds, 2);
134    
135                            if (structureIdComparator.equals(StringPool.EQUAL)) {
136                                    qPos.add(structureId);
137                                    qPos.add(structureId);
138                            }
139    
140                            qPos.add(names, 2);
141                            qPos.add(descriptions, 2);
142    
143                            if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
144                                    if (CustomSQLUtil.isVendorOracle()) {
145                                    }
146                                    else {
147                                            qPos.add(structureId);
148                                    }
149                            }
150    
151                            Iterator<Long> itr = q.list().iterator();
152    
153                            if (itr.hasNext()) {
154                                    Long count = itr.next();
155    
156                                    if (count != null) {
157                                            return count.intValue();
158                                    }
159                            }
160    
161                            return 0;
162                    }
163                    catch (Exception e) {
164                            throw new SystemException(e);
165                    }
166                    finally {
167                            closeSession(session);
168                    }
169            }
170    
171            public List<JournalTemplate> findByKeywords(
172                            long companyId, long groupId, String keywords, String structureId,
173                            String structureIdComparator, int start, int end,
174                            OrderByComparator obc)
175                    throws SystemException {
176    
177                    String[] templateIds = null;
178                    String[] names = null;
179                    String[] descriptions = null;
180                    boolean andOperator = false;
181    
182                    if (Validator.isNotNull(keywords)) {
183                            templateIds = CustomSQLUtil.keywords(keywords, false);
184                            names = CustomSQLUtil.keywords(keywords);
185                            descriptions = CustomSQLUtil.keywords(keywords);
186                    }
187                    else {
188                            andOperator = true;
189                    }
190    
191                    return findByC_G_T_S_N_D(
192                            companyId, groupId, templateIds, structureId, structureIdComparator,
193                            names, descriptions, andOperator, start, end, obc);
194            }
195    
196            public List<JournalTemplate> findByC_G_T_S_N_D(
197                            long companyId, long groupId, String templateId, String structureId,
198                            String structureIdComparator, String name, String description,
199                            boolean andOperator, int start, int end, OrderByComparator obc)
200                    throws SystemException {
201    
202                    return findByC_G_T_S_N_D(
203                            companyId, groupId, new String[] {templateId}, structureId,
204                            structureIdComparator, new String[] {name},
205                            new String[] {description}, andOperator, start, end, obc);
206            }
207    
208            public List<JournalTemplate> findByC_G_T_S_N_D(
209                            long companyId, long groupId, String[] templateIds,
210                            String structureId, String structureIdComparator, String[] names,
211                            String[] descriptions, boolean andOperator, int start, int end,
212                            OrderByComparator obc)
213                    throws SystemException {
214    
215                    templateIds = CustomSQLUtil.keywords(templateIds, false);
216                    names = CustomSQLUtil.keywords(names);
217                    descriptions = CustomSQLUtil.keywords(descriptions);
218    
219                    Session session = null;
220    
221                    try {
222                            session = openSession();
223    
224                            String sql = CustomSQLUtil.get(FIND_BY_C_G_T_S_N_D);
225    
226                            if (groupId <= 0) {
227                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
228                            }
229    
230                            sql = CustomSQLUtil.replaceKeywords(
231                                    sql, "templateId", StringPool.LIKE, false, templateIds);
232    
233                            if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
234                                    sql = replaceStructureIdComparator(sql);
235                            }
236    
237                            sql = CustomSQLUtil.replaceKeywords(
238                                    sql, "lower(name)", StringPool.LIKE, false, names);
239                            sql = CustomSQLUtil.replaceKeywords(
240                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
241    
242                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
243                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
244    
245                            SQLQuery q = session.createSQLQuery(sql);
246    
247                            q.addEntity("JournalTemplate", JournalTemplateImpl.class);
248    
249                            QueryPos qPos = QueryPos.getInstance(q);
250    
251                            qPos.add(companyId);
252    
253                            if (groupId > 0) {
254                                    qPos.add(groupId);
255                            }
256    
257                            qPos.add(templateIds, 2);
258    
259                            if (structureIdComparator.equals(StringPool.EQUAL)) {
260                                    qPos.add(structureId);
261                                    qPos.add(structureId);
262                            }
263    
264                            qPos.add(names, 2);
265                            qPos.add(descriptions, 2);
266    
267                            if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
268                                    if (CustomSQLUtil.isVendorOracle()) {
269                                    }
270                                    else {
271                                            qPos.add(structureId);
272                                    }
273                            }
274    
275                            return (List<JournalTemplate>)QueryUtil.list(
276                                    q, getDialect(), start, end);
277                    }
278                    catch (Exception e) {
279                            throw new SystemException(e);
280                    }
281                    finally {
282                            closeSession(session);
283                    }
284            }
285    
286            protected String replaceStructureIdComparator(String sql) {
287                    String insertSQL = "structureId != ? AND structureId IS NOT NULL";
288    
289                    if (CustomSQLUtil.isVendorOracle()) {
290                            insertSQL = "structureId IS NOT NULL";
291                    }
292    
293                    insertSQL = " AND (" + insertSQL + ") ";
294    
295                    String removeSQL =
296                            "(structureId = ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
297    
298                    sql = StringUtil.replace(sql, removeSQL, StringPool.BLANK);
299    
300                    int pos = sql.indexOf("ORDER BY");
301    
302                    if (pos == -1) {
303                            sql = sql + insertSQL;
304                    }
305                    else {
306                            sql = StringUtil.insert(sql, insertSQL, pos);
307                    }
308    
309                    return sql;
310            }
311    
312    }