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