1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
36   * <a href="SocialRelationLocalServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
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 }