001    /**
002     * Copyright (c) 2000-present 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.impl;
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.portlet.dynamicdatalists.service.persistence.DDLRecordFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Marcellus Tavares
039     */
040    public class DDLRecordFinderImpl extends BasePersistenceImpl<DDLRecord>
041            implements DDLRecordFinder {
042    
043            public static final String COUNT_BY_R_S =
044                    DDLRecordFinder.class.getName() + ".countByR_S";
045    
046            public static final String COUNT_BY_C_S_S =
047                    DDLRecordFinder.class.getName() + ".countByC_S_S";
048    
049            public static final String FIND_BY_R_S =
050                    DDLRecordFinder.class.getName() + ".findByR_S";
051    
052            public static final String FIND_BY_C_S_S =
053                    DDLRecordFinder.class.getName() + ".findByC_S_S";
054    
055            @Override
056            public int countByR_S(long recordSetId, int status) {
057                    Session session = null;
058    
059                    try {
060                            session = openSession();
061    
062                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
063    
064                            if (status == WorkflowConstants.STATUS_ANY) {
065                                    sql = StringUtil.replace(
066                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
067                            }
068    
069                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
070    
071                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
072    
073                            QueryPos qPos = QueryPos.getInstance(q);
074    
075                            if (status != WorkflowConstants.STATUS_ANY) {
076                                    qPos.add(status);
077                            }
078    
079                            qPos.add(recordSetId);
080    
081                            Iterator<Long> itr = q.iterate();
082    
083                            if (itr.hasNext()) {
084                                    Long count = itr.next();
085    
086                                    if (count != null) {
087                                            return count.intValue();
088                                    }
089                            }
090    
091                            return 0;
092                    }
093                    catch (Exception e) {
094                            throw new SystemException(e);
095                    }
096                    finally {
097                            closeSession(session);
098                    }
099            }
100    
101            @Override
102            public int countByC_S_S(long companyId, int status, int scope) {
103                    Session session = null;
104    
105                    try {
106                            session = openSession();
107    
108                            String sql = CustomSQLUtil.get(COUNT_BY_C_S_S);
109    
110                            if (status == WorkflowConstants.STATUS_ANY) {
111                                    sql = StringUtil.replace(
112                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
113                            }
114    
115                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
116    
117                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
118    
119                            QueryPos qPos = QueryPos.getInstance(q);
120    
121                            if (status != WorkflowConstants.STATUS_ANY) {
122                                    qPos.add(status);
123                            }
124    
125                            qPos.add(scope);
126                            qPos.add(companyId);
127    
128                            Iterator<Long> itr = q.iterate();
129    
130                            if (itr.hasNext()) {
131                                    Long count = itr.next();
132    
133                                    if (count != null) {
134                                            return count.intValue();
135                                    }
136                            }
137    
138                            return 0;
139                    }
140                    catch (Exception e) {
141                            throw new SystemException(e);
142                    }
143                    finally {
144                            closeSession(session);
145                    }
146            }
147    
148            @Override
149            public List<DDLRecord> findByR_S(
150                    long recordSetId, int status, int start, int end,
151                    OrderByComparator<DDLRecord> orderByComparator) {
152    
153                    Session session = null;
154    
155                    try {
156                            session = openSession();
157    
158                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
159    
160                            if (status == WorkflowConstants.STATUS_ANY) {
161                                    sql = StringUtil.replace(
162                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
163                            }
164    
165                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
166    
167                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
168    
169                            q.addEntity("DDLRecord", DDLRecordImpl.class);
170    
171                            QueryPos qPos = QueryPos.getInstance(q);
172    
173                            if (status != WorkflowConstants.STATUS_ANY) {
174                                    qPos.add(status);
175                            }
176    
177                            qPos.add(recordSetId);
178    
179                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
180                    }
181                    catch (Exception e) {
182                            throw new SystemException(e);
183                    }
184                    finally {
185                            closeSession(session);
186                    }
187            }
188    
189            @Override
190            public List<DDLRecord> findByC_S_S(
191                    long companyId, int status, int scope, int start, int end,
192                    OrderByComparator<DDLRecord> orderByComparator) {
193    
194                    Session session = null;
195    
196                    try {
197                            session = openSession();
198    
199                            String sql = CustomSQLUtil.get(FIND_BY_C_S_S);
200    
201                            if (status == WorkflowConstants.STATUS_ANY) {
202                                    sql = StringUtil.replace(
203                                            sql, "(DDLRecordVersion.status = ?) AND", StringPool.BLANK);
204                            }
205    
206                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
207    
208                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
209    
210                            q.addEntity("DDLRecord", DDLRecordImpl.class);
211    
212                            QueryPos qPos = QueryPos.getInstance(q);
213    
214                            if (status != WorkflowConstants.STATUS_ANY) {
215                                    qPos.add(status);
216                            }
217    
218                            qPos.add(scope);
219                            qPos.add(companyId);
220    
221                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
222                    }
223                    catch (Exception e) {
224                            throw new SystemException(e);
225                    }
226                    finally {
227                            closeSession(session);
228                    }
229            }
230    
231            @Override
232            public Long[] findByC_S_S_MinAndMax(long companyId, int status, int scope) {
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.createSynchronizedSQLQuery(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            @Override
281            public List<DDLRecord> findByC_S_S_MinAndMax(
282                    long companyId, int status, int scope, long minRecordId,
283                    long maxRecordId) {
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.createSynchronizedSQLQuery(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    }