001    /**
002     * Copyright (c) 2000-2010 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.social.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
020    import com.liferay.portal.kernel.dao.orm.ProjectionList;
021    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portlet.social.model.SocialEquityUser;
025    import com.liferay.portlet.social.model.SocialEquityValue;
026    import com.liferay.portlet.social.service.base.SocialEquityUserLocalServiceBaseImpl;
027    
028    import java.util.List;
029    
030    /**
031     * @author Zsolt Berentey
032     */
033    public class SocialEquityUserLocalServiceImpl
034            extends SocialEquityUserLocalServiceBaseImpl {
035    
036            public SocialEquityValue getContributionEquity(long userId)
037                    throws SystemException {
038    
039                    ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
040    
041                    projectionList.add(ProjectionFactoryUtil.sum("contributionK"));
042                    projectionList.add(ProjectionFactoryUtil.sum("contributionB"));
043    
044                    return getEquityValue(userId, projectionList);
045            }
046    
047            public SocialEquityValue getParticipationEquity(long userId)
048                    throws SystemException {
049    
050                    ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
051    
052                    projectionList.add(ProjectionFactoryUtil.sum("participationK"));
053                    projectionList.add(ProjectionFactoryUtil.sum("participationB"));
054    
055                    return getEquityValue(userId, projectionList);
056            }
057    
058            public List<SocialEquityUser> getRankedEquityUsers(
059                            long groupId, int start, int end,
060                            OrderByComparator orderByComparator)
061                    throws SystemException {
062    
063                    return socialEquityUserPersistence.findByGroupRanked(
064                            groupId, start, end, orderByComparator);
065            }
066    
067            public int getRankedEquityUsersCount(long groupId) throws SystemException {
068                    return socialEquityUserPersistence.countByGroupRanked(groupId);
069            }
070    
071            protected SocialEquityValue getEquityValue(
072                            long userId, ProjectionList projectionList)
073                    throws SystemException {
074    
075                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
076                            SocialEquityUser.class);
077    
078                    dynamicQuery.setProjection(projectionList);
079    
080                    dynamicQuery.add(RestrictionsFactoryUtil.eq("userId", userId));
081    
082                    List<?> results = dynamicQuery(dynamicQuery);
083    
084                    Object[] values = (Object[])results.get(0);
085    
086                    SocialEquityValue socialEquityValue = null;
087    
088                    if (values[0] != null) {
089                            socialEquityValue = new SocialEquityValue(
090                                    (Double)values[0], (Double)values[1]);
091                    }
092                    else {
093                            socialEquityValue = new SocialEquityValue(0, 0);
094                    }
095    
096                    return socialEquityValue;
097            }
098    
099    }