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 if (SocialRelationConstants.isTypeBi(relation.getType())) {
105 SocialRelation biRelation = socialRelationPersistence.findByU1_U2_T(
106 relation.getUserId2(), relation.getUserId1(),
107 relation.getType());
108
109 socialRelationPersistence.remove(biRelation);
110 }
111
112 socialRelationPersistence.remove(relation);
113 }
114
115 public void deleteRelation(long userId1, long userId2, int type)
116 throws PortalException, SystemException {
117
118 SocialRelation relation = socialRelationPersistence.findByU1_U2_T(
119 userId1, userId2, type);
120
121 deleteRelation(relation.getRelationId());
122 }
123
124 public void deleteRelations(long userId) throws SystemException {
125 socialRelationPersistence.removeByUserId1(userId);
126 socialRelationPersistence.removeByUserId2(userId);
127 }
128
129 public SocialRelation getRelation(long relationId)
130 throws PortalException, SystemException {
131
132 return socialRelationPersistence.findByPrimaryKey(relationId);
133 }
134
135 public SocialRelation getRelation(long userId1, long userId2, int type)
136 throws PortalException, SystemException {
137
138 return socialRelationPersistence.findByU1_U2_T(
139 userId1, userId2, type);
140 }
141
142 public List<SocialRelation> getRelations(
143 long userId, int type, int start, int end)
144 throws SystemException {
145
146 return socialRelationPersistence.findByU1_T(userId, type, start, end);
147 }
148
149 public int getRelationsCount(long userId, int type) throws SystemException {
150 return socialRelationPersistence.countByU1_T(userId, type);
151 }
152
153 public boolean hasRelation(long userId1, long userId2, int type)
154 throws SystemException {
155
156 SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
157 userId1, userId2, type);
158
159 if (relation == null) {
160 return false;
161 }
162 else {
163 return true;
164 }
165 }
166
167 public boolean isRelatable(long userId1, long userId2, int type)
168 throws SystemException {
169
170 if (userId1 == userId2) {
171 return false;
172 }
173
174 User user1 = userPersistence.fetchByPrimaryKey(userId1);
175
176 if ((user1 == null) || user1.isDefaultUser()) {
177 return false;
178 }
179
180 User user2 = userPersistence.fetchByPrimaryKey(userId2);
181
182 if ((user2 == null) || user2.isDefaultUser()) {
183 return false;
184 }
185
186 return !hasRelation(userId1, userId2, type);
187 }
188
189 }