1   /**
2    * Copyright (c) 2000-2007 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.journal.lar;
24  
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.service.persistence.GroupUtil;
27  import com.liferay.portlet.journal.model.JournalArticle;
28  
29  import java.lang.ref.SoftReference;
30  
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * <a href="JournalCreationStrategyImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * <p>
40   * Provides the strategy for creating new content when new Journal content is
41   * imported into a layout set from a LAR. The default strategy implemented by
42   * this class is to return the first user in the database that is a member of
43   * the specified group as the author Id. If the group contains no users, the
44   * original author will remain unchanged.
45   * </p>
46   *
47   * <p>
48   * Content will be added as is (i.e. no transformations).
49   * </p>
50   *
51   * @author Joel Kozikowski
52   *
53   * @see com.liferay.portlet.journal.lar.JournalContentPortletDataHandlerImpl
54   *
55   */
56  public class JournalCreationStrategyImpl implements JournalCreationStrategy {
57  
58      public long getAuthorUserId(
59              long companyId, long groupId, Object journalObj)
60          throws Exception {
61  
62          User user = getFirstUser(groupId);
63  
64          if (user != null) {
65               return user.getUserId();
66          }
67          else {
68               return 0;
69          }
70      }
71  
72      public String getAuthorUserName(
73              long companyId, long groupId, Object journalObj)
74          throws Exception {
75  
76          User user = getFirstUser(groupId);
77  
78          if (user != null) {
79               return user.getFullName();
80          }
81          else {
82               return null;
83          }
84      }
85  
86      public String getApprovalUserName(
87              long companyId, long groupId, Object journalObj)
88          throws Exception {
89  
90          User user = getFirstUser(groupId);
91  
92          if (user != null) {
93               return user.getFullName();
94          }
95          else {
96               return null;
97          }
98      }
99  
100     public long getApprovalUserId(
101             long companyId, long groupId, Object journalObj)
102         throws Exception {
103 
104         User user = getFirstUser(groupId);
105 
106         if (user != null) {
107              return user.getUserId();
108         }
109         else {
110              return 0;
111         }
112     }
113 
114     public String getTransformedContent(
115             long companyId, long groupId, JournalArticle newArticle)
116         throws Exception {
117 
118         return null;
119     }
120 
121     public boolean addCommunityPermissions(
122             long companyId, long groupId, Object journalObj)
123         throws Exception {
124 
125         return true;
126     }
127 
128     public boolean addGuestPermissions(
129             long companyId, long groupId, Object journalObj)
130         throws Exception {
131 
132         return true;
133     }
134 
135     protected synchronized User getFirstUser(long groupId) throws Exception {
136         User firstUser = null;
137 
138         if ((groupId == _lastGroupId) &&
139             (_lastUserFound != null) && (_lastUserFound.get() != null)) {
140 
141             firstUser = (User)_lastUserFound.get();
142 
143             if (_log.isDebugEnabled()) {
144                 _log.debug(
145                     "Returning cached first user " + firstUser.getUserId() +
146                         " for group id " + groupId);
147             }
148         }
149         else {
150             List userList = GroupUtil.getUsers(groupId, 0, 1);
151 
152             if (userList.size() > 0) {
153                 firstUser = (User)userList.get(0);
154 
155                 if (_log.isDebugEnabled()) {
156                     _log.debug(
157                         "Caching first user " + firstUser.getUserId() +
158                             " for group id " + groupId);
159                 }
160 
161                 _lastGroupId = groupId;
162                 _lastUserFound = new SoftReference(firstUser);
163             }
164             else {
165                 if (_log.isDebugEnabled()) {
166                     _log.debug("No users found for group id " + groupId);
167                 }
168             }
169         }
170 
171         return firstUser;
172     }
173 
174     private static Log _log =
175         LogFactory.getLog(JournalCreationStrategyImpl.class);
176 
177     private static long _lastGroupId = 0;
178     private static SoftReference _lastUserFound = null;
179 
180 }