001    /**
002     * Copyright (c) 2000-2011 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.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    }