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