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