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