1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.social.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.User;
25  import com.liferay.portlet.social.RelationUserIdException;
26  import com.liferay.portlet.social.model.SocialRelation;
27  import com.liferay.portlet.social.model.SocialRelationConstants;
28  import com.liferay.portlet.social.service.base.SocialRelationLocalServiceBaseImpl;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * <a href="SocialRelationLocalServiceImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class SocialRelationLocalServiceImpl
41      extends SocialRelationLocalServiceBaseImpl {
42  
43      public SocialRelation addRelation(long userId1, long userId2, int type)
44          throws PortalException, SystemException {
45  
46          if (userId1 == userId2) {
47              throw new RelationUserIdException();
48          }
49  
50          User user1 = userPersistence.findByPrimaryKey(userId1);
51          User user2 = userPersistence.findByPrimaryKey(userId2);
52  
53          if (user1.getCompanyId() != user2.getCompanyId()) {
54              throw new RelationUserIdException();
55          }
56  
57          SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
58              userId1, userId2, type);
59  
60          if (relation == null) {
61              long relationId = counterLocalService.increment();
62  
63              relation = socialRelationPersistence.create(relationId);
64  
65              relation.setCompanyId(user1.getCompanyId());
66              relation.setCreateDate(new Date());
67              relation.setUserId1(userId1);
68              relation.setUserId2(userId2);
69              relation.setType(type);
70  
71              socialRelationPersistence.update(relation, false);
72          }
73  
74          if (SocialRelationConstants.isTypeBi(type)) {
75              SocialRelation biRelation =
76                  socialRelationPersistence.fetchByU1_U2_T(
77                      userId2, userId1, type);
78  
79              if (biRelation == null) {
80                  long biRelationId = counterLocalService.increment();
81  
82                  biRelation = socialRelationPersistence.create(biRelationId);
83  
84                  biRelation.setCompanyId(user1.getCompanyId());
85                  biRelation.setCreateDate(new Date());
86                  biRelation.setUserId1(userId2);
87                  biRelation.setUserId2(userId1);
88                  biRelation.setType(type);
89  
90                  socialRelationPersistence.update(biRelation, false);
91              }
92          }
93  
94          return relation;
95      }
96  
97      public void deleteRelation(long relationId)
98          throws PortalException, SystemException {
99  
100         SocialRelation relation = socialRelationPersistence.findByPrimaryKey(
101             relationId);
102 
103         if (SocialRelationConstants.isTypeBi(relation.getType())) {
104             SocialRelation biRelation = socialRelationPersistence.findByU1_U2_T(
105                 relation.getUserId2(), relation.getUserId1(),
106                 relation.getType());
107 
108             socialRelationPersistence.remove(biRelation);
109         }
110 
111         socialRelationPersistence.remove(relation);
112     }
113 
114     public void deleteRelation(long userId1, long userId2, int type)
115         throws PortalException, SystemException {
116 
117         SocialRelation relation = socialRelationPersistence.findByU1_U2_T(
118             userId1, userId2, type);
119 
120         deleteRelation(relation.getRelationId());
121     }
122 
123     public void deleteRelations(long userId) throws SystemException {
124         socialRelationPersistence.removeByUserId1(userId);
125         socialRelationPersistence.removeByUserId2(userId);
126     }
127 
128     public SocialRelation getRelation(long relationId)
129         throws PortalException, SystemException {
130 
131         return socialRelationPersistence.findByPrimaryKey(relationId);
132     }
133 
134     public SocialRelation getRelation(long userId1, long userId2, int type)
135         throws PortalException, SystemException {
136 
137         return socialRelationPersistence.findByU1_U2_T(
138             userId1, userId2, type);
139     }
140 
141     public List<SocialRelation> getRelations(
142             long userId, int type, int start, int end)
143         throws SystemException {
144 
145         return socialRelationPersistence.findByU1_T(userId, type, start, end);
146     }
147 
148     public int getRelationsCount(long userId, int type) throws SystemException {
149         return socialRelationPersistence.countByU1_T(userId, type);
150     }
151 
152     public boolean hasRelation(long userId1, long userId2, int type)
153         throws SystemException {
154 
155         SocialRelation relation = socialRelationPersistence.fetchByU1_U2_T(
156             userId1, userId2, type);
157 
158         if (relation == null) {
159             return false;
160         }
161         else {
162             return true;
163         }
164     }
165 
166     public boolean isRelatable(long userId1, long userId2, int type)
167         throws SystemException {
168 
169         if (userId1 == userId2) {
170             return false;
171         }
172 
173         User user1 = userPersistence.fetchByPrimaryKey(userId1);
174 
175         if ((user1 == null) || user1.isDefaultUser()) {
176             return false;
177         }
178 
179         User user2 = userPersistence.fetchByPrimaryKey(userId2);
180 
181         if ((user2 == null) || user2.isDefaultUser()) {
182             return false;
183         }
184 
185         return !hasRelation(userId1, userId2, type);
186     }
187 
188 }