001
014
015 package com.liferay.portlet.social.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.model.User;
020 import com.liferay.portlet.social.RelationUserIdException;
021 import com.liferay.portlet.social.model.SocialRelation;
022 import com.liferay.portlet.social.model.SocialRelationConstants;
023 import com.liferay.portlet.social.service.base.SocialRelationLocalServiceBaseImpl;
024
025 import java.util.List;
026
027
030 public class SocialRelationLocalServiceImpl
031 extends SocialRelationLocalServiceBaseImpl {
032
033 public SocialRelation addRelation(long userId1, long userId2, int type)
034 throws PortalException, SystemException {
035
036 if (userId1 == userId2) {
037 throw new RelationUserIdException();
038 }
039
040 User user1 = userPersistence.findByPrimaryKey(userId1);
041 User user2 = userPersistence.findByPrimaryKey(userId2);
042
043 if (user1.getCompanyId() != user2.getCompanyId()) {
044 throw new RelationUserIdException();
045 }
046
047 SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
048 userId1, userId2, type);
049
050 if (relation == null) {
051 long relationId = counterLocalService.increment();
052
053 relation = socialRelationPersistence.create(relationId);
054
055 relation.setCompanyId(user1.getCompanyId());
056 relation.setCreateDate(System.currentTimeMillis());
057 relation.setUserId1(userId1);
058 relation.setUserId2(userId2);
059 relation.setType(type);
060
061 socialRelationPersistence.update(relation, false);
062 }
063
064 if (SocialRelationConstants.isTypeBi(type)) {
065 SocialRelation biRelation =
066 socialRelationPersistence.fetchByU1_U2_T(
067 userId2, userId1, type);
068
069 if (biRelation == null) {
070 long biRelationId = counterLocalService.increment();
071
072 biRelation = socialRelationPersistence.create(biRelationId);
073
074 biRelation.setCompanyId(user1.getCompanyId());
075 biRelation.setCreateDate(System.currentTimeMillis());
076 biRelation.setUserId1(userId2);
077 biRelation.setUserId2(userId1);
078 biRelation.setType(type);
079
080 socialRelationPersistence.update(biRelation, false);
081 }
082 }
083
084 return relation;
085 }
086
087 public void deleteRelation(long relationId)
088 throws PortalException, SystemException {
089
090 SocialRelation relation = socialRelationPersistence.findByPrimaryKey(
091 relationId);
092
093 deleteRelation(relation);
094 }
095
096 public void deleteRelation(long userId1, long userId2, int type)
097 throws PortalException, SystemException {
098
099 SocialRelation relation = socialRelationPersistence.findByU1_U2_T(
100 userId1, userId2, type);
101
102 deleteRelation(relation);
103 }
104
105 public void deleteRelation(SocialRelation relation)
106 throws PortalException, SystemException {
107
108 socialRelationPersistence.remove(relation);
109
110 if (SocialRelationConstants.isTypeBi(relation.getType())) {
111 SocialRelation biRelation = socialRelationPersistence.findByU1_U2_T(
112 relation.getUserId2(), relation.getUserId1(),
113 relation.getType());
114
115 socialRelationPersistence.remove(biRelation);
116 }
117 }
118
119 public void deleteRelations(long userId) throws SystemException {
120 socialRelationPersistence.removeByUserId1(userId);
121 socialRelationPersistence.removeByUserId2(userId);
122 }
123
124 public SocialRelation getRelation(long relationId)
125 throws PortalException, SystemException {
126
127 return socialRelationPersistence.findByPrimaryKey(relationId);
128 }
129
130 public SocialRelation getRelation(long userId1, long userId2, int type)
131 throws PortalException, SystemException {
132
133 return socialRelationPersistence.findByU1_U2_T(
134 userId1, userId2, type);
135 }
136
137 public List<SocialRelation> getRelations(
138 long userId, int type, int start, int end)
139 throws SystemException {
140
141 return socialRelationPersistence.findByU1_T(userId, type, start, end);
142 }
143
144 public int getRelationsCount(long userId, int type) throws SystemException {
145 return socialRelationPersistence.countByU1_T(userId, type);
146 }
147
148 public boolean hasRelation(long userId1, long userId2, int type)
149 throws SystemException {
150
151 SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
152 userId1, userId2, type);
153
154 if (relation == null) {
155 return false;
156 }
157 else {
158 return true;
159 }
160 }
161
162 public boolean isRelatable(long userId1, long userId2, int type)
163 throws SystemException {
164
165 if (userId1 == userId2) {
166 return false;
167 }
168
169 User user1 = userPersistence.fetchByPrimaryKey(userId1);
170
171 if ((user1 == null) || user1.isDefaultUser()) {
172 return false;
173 }
174
175 User user2 = userPersistence.fetchByPrimaryKey(userId2);
176
177 if ((user2 == null) || user2.isDefaultUser()) {
178 return false;
179 }
180
181 return !hasRelation(userId1, userId2, type);
182 }
183
184 }