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.portal.service.persistence.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.model.UserGroupRole;
022    import com.liferay.portal.model.impl.UserGroupRoleImpl;
023    import com.liferay.portal.service.persistence.UserGroupRoleFinder;
024    import com.liferay.util.dao.orm.CustomSQLUtil;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Drew Brokke
031     */
032    public class UserGroupRoleFinderImpl
033            extends UserGroupRoleFinderBaseImpl implements UserGroupRoleFinder {
034    
035            public static final String FIND_BY_GROUP_ROLE_TYPE =
036                    UserGroupRoleFinder.class.getName() + ".findByGroupRoleType";
037    
038            public static final String FIND_BY_USER_USER_GROUP_GROUP_ROLE =
039                    UserGroupRoleFinder.class.getName() + ".findByUserUserGroupGroupRole";
040    
041            @Override
042            public List<UserGroupRole> findByGroupRoleType(long groupId, int roleType) {
043                    Session session = null;
044    
045                    try {
046                            session = openSession();
047    
048                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ROLE_TYPE);
049    
050                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
051    
052                            q.addEntity("UserGroupRole", UserGroupRoleImpl.class);
053    
054                            QueryPos qPos = QueryPos.getInstance(q);
055    
056                            qPos.add(groupId);
057                            qPos.add(roleType);
058    
059                            return q.list(true);
060                    }
061                    catch (Exception e) {
062                            throw new SystemException(e);
063                    }
064                    finally {
065                            closeSession(session);
066                    }
067            }
068    
069            @Override
070            public List<UserGroupRole> findByUserUserGroupGroupRole(
071                    long userId, long groupId) {
072    
073                    Session session = null;
074    
075                    try {
076                            session = openSession();
077    
078                            String sql = CustomSQLUtil.get(FIND_BY_USER_USER_GROUP_GROUP_ROLE);
079    
080                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
081    
082                            q.addEntity("UserGroupRole", UserGroupRoleImpl.class);
083    
084                            QueryPos qPos = QueryPos.getInstance(q);
085    
086                            qPos.add(userId);
087                            qPos.add(groupId);
088    
089                            return q.list(true);
090                    }
091                    catch (Exception e) {
092                            throw new SystemException(e);
093                    }
094                    finally {
095                            closeSession(session);
096                    }
097            }
098    
099    }