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.portlet.social.util.test;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.test.util.RandomTestUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.asset.model.AssetEntry;
025    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
026    import com.liferay.portlet.social.model.SocialActivityCounter;
027    import com.liferay.portlet.social.model.SocialActivityCounterConstants;
028    import com.liferay.portlet.social.model.SocialActivityLimit;
029    import com.liferay.portlet.social.service.SocialActivityCounterLocalServiceUtil;
030    import com.liferay.portlet.social.service.SocialActivityLimitLocalServiceUtil;
031    import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
032    
033    /**
034     * @author Zsolt Berentey
035     */
036    public class SocialActivityTestUtil {
037    
038            public static void addActivity(
039                            User user, Group group, AssetEntry assetEntry, int type)
040                    throws Exception {
041    
042                    addActivity(user, group, assetEntry, type, StringPool.BLANK);
043            }
044    
045            public static void addActivity(
046                            User user, Group group, AssetEntry assetEntry, int type,
047                            String extraData)
048                    throws Exception {
049    
050                    SocialActivityLocalServiceUtil.addActivity(
051                            user.getUserId(), group.getGroupId(), assetEntry.getClassName(),
052                            assetEntry.getClassPK(), type, extraData, 0);
053            }
054    
055            public static AssetEntry addAssetEntry(User user, Group group)
056                    throws Exception {
057    
058                    return AssetEntryLocalServiceUtil.updateEntry(
059                            user.getUserId(), group.getGroupId(), RandomTestUtil.randomString(),
060                            RandomTestUtil.randomLong(), null, null);
061            }
062    
063            public static AssetEntry addAssetEntry(
064                            User user, Group group, AssetEntry assetEntry)
065                    throws Exception {
066    
067                    if (assetEntry != null) {
068                            AssetEntryLocalServiceUtil.deleteEntry(assetEntry);
069                    }
070    
071                    return AssetEntryLocalServiceUtil.updateEntry(
072                            user.getUserId(), group.getGroupId(), _TEST_MODEL, 1, null, null);
073            }
074    
075            public static String createExtraDataJSON(String key, String value) {
076                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
077    
078                    extraDataJSONObject.put(key, value);
079    
080                    return extraDataJSONObject.toString();
081            }
082    
083            public static SocialActivityCounter getActivityCounter(
084                            long groupId, String name, Object owner)
085                    throws Exception {
086    
087                    long classNameId = 0;
088                    long classPK = 0;
089                    int ownerType = SocialActivityCounterConstants.TYPE_ACTOR;
090    
091                    if (owner instanceof User) {
092                            classNameId = PortalUtil.getClassNameId(User.class.getName());
093                            classPK = ((User)owner).getUserId();
094                    }
095                    else if (owner instanceof AssetEntry) {
096                            classNameId = ((AssetEntry)owner).getClassNameId();
097                            classPK = ((AssetEntry)owner).getClassPK();
098                            ownerType = SocialActivityCounterConstants.TYPE_ASSET;
099                    }
100    
101                    if (name.equals(SocialActivityCounterConstants.NAME_CONTRIBUTION)) {
102                            ownerType = SocialActivityCounterConstants.TYPE_CREATOR;
103                    }
104    
105                    return
106                            SocialActivityCounterLocalServiceUtil.fetchLatestActivityCounter(
107                                    groupId, classNameId, classPK, name, ownerType);
108            }
109    
110            public static SocialActivityLimit getActivityLimit(
111                            long groupId, User user, AssetEntry assetEntry, int activityType,
112                            String activityCounterName)
113                    throws Exception {
114    
115                    long classPK = assetEntry.getClassPK();
116    
117                    if (activityCounterName.equals(
118                                    SocialActivityCounterConstants.NAME_PARTICIPATION)) {
119    
120                            classPK = 0;
121                    }
122    
123                    return SocialActivityLimitLocalServiceUtil.fetchActivityLimit(
124                            groupId, user.getUserId(), assetEntry.getClassNameId(), classPK,
125                            activityType, activityCounterName);
126            }
127    
128            private static final String _TEST_MODEL = "test-model";
129    
130    }