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.dynamicdatalists.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.ArrayUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
030    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Marcellus Tavares
038     */
039    public class DDLRecordFinderImpl extends BasePersistenceImpl<DDLRecord>
040            implements DDLRecordFinder {
041    
042            public static final String COUNT_BY_R_S =
043                    DDLRecordFinder.class.getName() + ".countByR_S";
044    
045            public static final String COUNT_BY_C_S_S =
046                    DDLRecordFinder.class.getName() + ".countByC_S_S";
047    
048            public static final String FIND_BY_R_S =
049                    DDLRecordFinder.class.getName() + ".findByR_S";
050    
051            public static final String FIND_BY_C_S_S =
052                    DDLRecordFinder.class.getName() + ".findByC_S_S";
053    
054            public int countByR_S(long recordSetId, int status) throws SystemException {
055                    Session session = null;
056    
057                    try {
058                            session = openSession();
059    
060                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
061    
062                            if (status == WorkflowConstants.STATUS_ANY) {
063                                    sql = StringUtil.replace(
064                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
065                            }
066    
067                            SQLQuery q = session.createSQLQuery(sql);
068    
069                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
070    
071                            QueryPos qPos = QueryPos.getInstance(q);
072    
073                            if (status != WorkflowConstants.STATUS_ANY) {
074                                    qPos.add(status);
075                            }
076    
077                            qPos.add(recordSetId);
078    
079                            Iterator<Long> itr = q.iterate();
080    
081                            if (itr.hasNext()) {
082                                    Long count = itr.next();
083    
084                                    if (count != null) {
085                                            return count.intValue();
086                                    }
087                            }
088    
089                            return 0;
090                    }
091                    catch (Exception e) {
092                            throw new SystemException(e);
093                    }
094                    finally {
095                            closeSession(session);
096                    }
097            }
098    
099            public int countByC_S_S(long companyId, int status, int scope)
100                    throws SystemException {
101    
102                    Session session = null;
103    
104                    try {
105                            session = openSession();
106    
107                            String sql = CustomSQLUtil.get(COUNT_BY_C_S_S);
108    
109                            if (status == WorkflowConstants.STATUS_ANY) {
110                                    sql = StringUtil.replace(
111                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
112                            }
113    
114                            SQLQuery q = session.createSQLQuery(sql);
115    
116                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
117    
118                            QueryPos qPos = QueryPos.getInstance(q);
119    
120                            if (status != WorkflowConstants.STATUS_ANY) {
121                                    qPos.add(status);
122                            }
123    
124                            qPos.add(scope);
125                            qPos.add(companyId);
126    
127                            Iterator<Long> itr = q.iterate();
128    
129                            if (itr.hasNext()) {
130                                    Long count = itr.next();
131    
132                                    if (count != null) {
133                                            return count.intValue();
134                                    }
135                            }
136    
137                            return 0;
138                    }
139                    catch (Exception e) {
140                            throw new SystemException(e);
141                    }
142                    finally {
143                            closeSession(session);
144                    }
145            }
146    
147            public List<DDLRecord> findByR_S(
148                            long recordSetId, int status, int start, int end,
149                            OrderByComparator orderByComparator)
150                    throws SystemException {
151    
152                    Session session = null;
153    
154                    try {
155                            session = openSession();
156    
157                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
158    
159                            if (status == WorkflowConstants.STATUS_ANY) {
160                                    sql = StringUtil.replace(
161                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
162                            }
163    
164                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
165    
166                            SQLQuery q = session.createSQLQuery(sql);
167    
168                            q.addEntity("DDLRecord", DDLRecordImpl.class);
169    
170                            QueryPos qPos = QueryPos.getInstance(q);
171    
172                            if (status != WorkflowConstants.STATUS_ANY) {
173                                    qPos.add(status);
174                            }
175    
176                            qPos.add(recordSetId);
177    
178                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
179                    }
180                    catch (Exception e) {
181                            throw new SystemException(e);
182                    }
183                    finally {
184                            closeSession(session);
185                    }
186            }
187    
188            public List<DDLRecord> findByC_S_S(
189                            long companyId, int status, int scope, int start, int end,
190                            OrderByComparator orderByComparator)
191                    throws SystemException {
192    
193                    Session session = null;
194    
195                    try {
196                            session = openSession();
197    
198                            String sql = CustomSQLUtil.get(FIND_BY_C_S_S);
199    
200                            if (status == WorkflowConstants.STATUS_ANY) {
201                                    sql = StringUtil.replace(
202                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
203                            }
204    
205                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
206    
207                            SQLQuery q = session.createSQLQuery(sql);
208    
209                            q.addEntity("DDLRecord", DDLRecordImpl.class);
210    
211                            QueryPos qPos = QueryPos.getInstance(q);
212    
213                            if (status != WorkflowConstants.STATUS_ANY) {
214                                    qPos.add(status);
215                            }
216    
217                            qPos.add(scope);
218                            qPos.add(companyId);
219    
220                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
221                    }
222                    catch (Exception e) {
223                            throw new SystemException(e);
224                    }
225                    finally {
226                            closeSession(session);
227                    }
228            }
229    
230            public Long[] findByC_S_S_MinAndMax(long companyId, int status, int scope)
231                    throws SystemException {
232    
233                    Session session = null;
234    
235                    try {
236                            session = openSession();
237    
238                            String sql = CustomSQLUtil.get(COUNT_BY_C_S_S);
239    
240                            sql = StringUtil.replace(
241                                    sql, "COUNT(DISTINCT DDLRecord.recordId) AS COUNT_VALUE",
242                                    "MIN(DDLRecord.recordId) AS minRecordId, " +
243                                            "MAX(DDLRecord.recordId) AS maxRecordId");
244    
245                            if (status == WorkflowConstants.STATUS_ANY) {
246                                    sql = StringUtil.replace(
247                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
248                            }
249    
250                            SQLQuery q = session.createSQLQuery(sql);
251    
252                            q.addScalar("minRecordId", Type.LONG);
253                            q.addScalar("maxRecordId", Type.LONG);
254    
255                            QueryPos qPos = QueryPos.getInstance(q);
256    
257                            if (status != WorkflowConstants.STATUS_ANY) {
258                                    qPos.add(status);
259                            }
260    
261                            qPos.add(scope);
262                            qPos.add(companyId);
263    
264                            Object[] array = (Object[])q.iterateNext();
265    
266                            if (array == null) {
267                                    return null;
268                            }
269    
270                            return ArrayUtil.toLongArray(array);
271                    }
272                    catch (Exception e) {
273                            throw new SystemException(e);
274                    }
275                    finally {
276                            closeSession(session);
277                    }
278            }
279    
280            public List<DDLRecord> findByC_S_S_MinAndMax(
281                            long companyId, int status, int scope, long minRecordId,
282                            long maxRecordId)
283                    throws SystemException {
284    
285                    Session session = null;
286    
287                    try {
288                            session = openSession();
289    
290                            String sql = CustomSQLUtil.get(FIND_BY_C_S_S);
291    
292                            if (status == WorkflowConstants.STATUS_ANY) {
293                                    sql = StringUtil.replace(
294                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
295                            }
296    
297                            sql = CustomSQLUtil.removeOrderBy(sql);
298    
299                            sql +=
300                                    " AND (DDLRecord.recordId >= ?) AND (DDLRecord.recordId < ?)";
301    
302                            SQLQuery q = session.createSQLQuery(sql);
303    
304                            q.addEntity("DDLRecord", DDLRecordImpl.class);
305    
306                            QueryPos qPos = QueryPos.getInstance(q);
307    
308                            if (status != WorkflowConstants.STATUS_ANY) {
309                                    qPos.add(status);
310                            }
311    
312                            qPos.add(scope);
313                            qPos.add(companyId);
314                            qPos.add(minRecordId);
315                            qPos.add(maxRecordId);
316    
317                            return q.list();
318                    }
319                    catch (Exception e) {
320                            throw new SystemException(e);
321                    }
322                    finally {
323                            closeSession(session);
324                    }
325            }
326    
327    }