1
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
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 }