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