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