001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
028    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordImpl;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.Iterator;
032    import java.util.List;
033    
034    /**
035     * @author Marcellus Tavares
036     */
037    public class DDLRecordFinderImpl extends BasePersistenceImpl<DDLRecord>
038            implements DDLRecordFinder {
039    
040            public static final String COUNT_BY_R_S =
041                    DDLRecordFinder.class.getName() + ".countByR_S";
042    
043            public static final String FIND_BY_R_S =
044                    DDLRecordFinder.class.getName() + ".findByR_S";
045    
046            public int countByR_S(long recordSetId, int status) throws SystemException {
047                    Session session = null;
048    
049                    try {
050                            session = openSession();
051    
052                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
053    
054                            if (status == WorkflowConstants.STATUS_ANY) {
055                                    sql = StringUtil.replace(
056                                            sql, "(DDLRecordVersion.status = ?) AND", "");
057                            }
058    
059                            SQLQuery q = session.createSQLQuery(sql);
060    
061                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
062    
063                            QueryPos qPos = QueryPos.getInstance(q);
064    
065                            if (status != WorkflowConstants.STATUS_ANY) {
066                                    qPos.add(status);
067                            }
068    
069                            qPos.add(recordSetId);
070    
071                            Iterator<Long> itr = q.iterate();
072    
073                            if (itr.hasNext()) {
074                                    Long count = itr.next();
075    
076                                    if (count != null) {
077                                            return count.intValue();
078                                    }
079                            }
080    
081                            return 0;
082                    }
083                    catch (Exception e) {
084                            throw new SystemException(e);
085                    }
086                    finally {
087                            closeSession(session);
088                    }
089            }
090    
091            public List<DDLRecord> findByR_S(
092                            long recordSetId, int status, int start, int end,
093                            OrderByComparator orderByComparator)
094                    throws SystemException {
095    
096                    Session session = null;
097    
098                    try {
099                            session = openSession();
100    
101                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
102    
103                            if (status == WorkflowConstants.STATUS_ANY) {
104                                    sql = StringUtil.replace(
105                                            sql, "(DDLRecordVersion.status = ?) AND", "");
106                            }
107    
108                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
109    
110                            SQLQuery q = session.createSQLQuery(sql);
111    
112                            q.addEntity("DDLRecord", DDLRecordImpl.class);
113    
114                            QueryPos qPos = QueryPos.getInstance(q);
115    
116                            if (status != WorkflowConstants.STATUS_ANY) {
117                                    qPos.add(status);
118                            }
119    
120                            qPos.add(recordSetId);
121    
122                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132    }