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.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 = null;
61  
62          try {
63              relation = socialRelationFinder.findByU_U_T(
64                  userId1, userId2, type);
65          }
66          catch (NoSuchRelationException nsre) {
67              long relationId = counterLocalService.increment();
68  
69              relation = socialRelationPersistence.create(relationId);
70  
71              relation.setCompanyId(user1.getCompanyId());
72              relation.setCreateDate(new Date());
73              relation.setUserId1(userId1);
74              relation.setUserId2(userId2);
75              relation.setType(type);
76  
77              socialRelationPersistence.update(relation, false);
78          }
79  
80          return relation;
81      }
82  
83      public void deleteRelation(long relationId)
84          throws PortalException, SystemException {
85  
86          socialRelationPersistence.remove(relationId);
87      }
88  
89      public void deleteRelations(long userId) throws SystemException {
90          socialRelationPersistence.removeByUserId1(userId);
91          socialRelationPersistence.removeByUserId2(userId);
92      }
93  
94      public List<SocialRelation> getRelations(
95              long userId, int type, int begin, int end)
96          throws SystemException {
97  
98          return socialRelationFinder.findByU_T(userId, type, begin, end);
99      }
100 
101 }