1
22
23 package com.liferay.portlet.social.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.model.User;
28 import com.liferay.portlet.social.RelationUserIdException;
29 import com.liferay.portlet.social.model.SocialRelation;
30 import com.liferay.portlet.social.model.SocialRelationConstants;
31 import com.liferay.portlet.social.service.base.SocialRelationLocalServiceBaseImpl;
32
33 import java.util.List;
34
35
41 public class SocialRelationLocalServiceImpl
42 extends SocialRelationLocalServiceBaseImpl {
43
44 public SocialRelation addRelation(long userId1, long userId2, int type)
45 throws PortalException, SystemException {
46
47 if (userId1 == userId2) {
48 throw new RelationUserIdException();
49 }
50
51 User user1 = userPersistence.findByPrimaryKey(userId1);
52 User user2 = userPersistence.findByPrimaryKey(userId2);
53
54 if (user1.getCompanyId() != user2.getCompanyId()) {
55 throw new RelationUserIdException();
56 }
57
58 SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
59 userId1, userId2, type);
60
61 if (relation == null) {
62 long relationId = counterLocalService.increment();
63
64 relation = socialRelationPersistence.create(relationId);
65
66 relation.setCompanyId(user1.getCompanyId());
67 relation.setCreateDate(System.currentTimeMillis());
68 relation.setUserId1(userId1);
69 relation.setUserId2(userId2);
70 relation.setType(type);
71
72 socialRelationPersistence.update(relation, false);
73 }
74
75 if (SocialRelationConstants.isTypeBi(type)) {
76 SocialRelation biRelation =
77 socialRelationPersistence.fetchByU1_U2_T(
78 userId2, userId1, type);
79
80 if (biRelation == null) {
81 long biRelationId = counterLocalService.increment();
82
83 biRelation = socialRelationPersistence.create(biRelationId);
84
85 biRelation.setCompanyId(user1.getCompanyId());
86 biRelation.setCreateDate(System.currentTimeMillis());
87 biRelation.setUserId1(userId2);
88 biRelation.setUserId2(userId1);
89 biRelation.setType(type);
90
91 socialRelationPersistence.update(biRelation, false);
92 }
93 }
94
95 return relation;
96 }
97
98 public void deleteRelation(long relationId)
99 throws PortalException, SystemException {
100
101 SocialRelation relation = socialRelationPersistence.findByPrimaryKey(
102 relationId);
103
104 deleteRelation(relation);
105 }
106
107 public void deleteRelation(long userId1, long userId2, int type)
108 throws PortalException, SystemException {
109
110 SocialRelation relation = socialRelationPersistence.findByU1_U2_T(
111 userId1, userId2, type);
112
113 deleteRelation(relation);
114 }
115
116 public void deleteRelation(SocialRelation relation)
117 throws PortalException, SystemException {
118
119 socialRelationPersistence.remove(relation);
120
121 if (SocialRelationConstants.isTypeBi(relation.getType())) {
122 SocialRelation biRelation = socialRelationPersistence.findByU1_U2_T(
123 relation.getUserId2(), relation.getUserId1(),
124 relation.getType());
125
126 socialRelationPersistence.remove(biRelation);
127 }
128 }
129
130 public void deleteRelations(long userId) throws SystemException {
131 socialRelationPersistence.removeByUserId1(userId);
132 socialRelationPersistence.removeByUserId2(userId);
133 }
134
135 public SocialRelation getRelation(long relationId)
136 throws PortalException, SystemException {
137
138 return socialRelationPersistence.findByPrimaryKey(relationId);
139 }
140
141 public SocialRelation getRelation(long userId1, long userId2, int type)
142 throws PortalException, SystemException {
143
144 return socialRelationPersistence.findByU1_U2_T(
145 userId1, userId2, type);
146 }
147
148 public List<SocialRelation> getRelations(
149 long userId, int type, int start, int end)
150 throws SystemException {
151
152 return socialRelationPersistence.findByU1_T(userId, type, start, end);
153 }
154
155 public int getRelationsCount(long userId, int type) throws SystemException {
156 return socialRelationPersistence.countByU1_T(userId, type);
157 }
158
159 public boolean hasRelation(long userId1, long userId2, int type)
160 throws SystemException {
161
162 SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
163 userId1, userId2, type);
164
165 if (relation == null) {
166 return false;
167 }
168 else {
169 return true;
170 }
171 }
172
173 public boolean isRelatable(long userId1, long userId2, int type)
174 throws SystemException {
175
176 if (userId1 == userId2) {
177 return false;
178 }
179
180 User user1 = userPersistence.fetchByPrimaryKey(userId1);
181
182 if ((user1 == null) || user1.isDefaultUser()) {
183 return false;
184 }
185
186 User user2 = userPersistence.fetchByPrimaryKey(userId2);
187
188 if ((user2 == null) || user2.isDefaultUser()) {
189 return false;
190 }
191
192 return !hasRelation(userId1, userId2, type);
193 }
194
195 }