001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.dao.shard.ShardUtil;
019 import com.liferay.portal.kernel.language.LanguageUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.LocalizationUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.PropsUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Company;
029 import com.liferay.portal.model.ResourceConstants;
030 import com.liferay.portal.model.ResourcePermission;
031 import com.liferay.portal.util.PortalUtil;
032
033 import java.sql.Connection;
034 import java.sql.PreparedStatement;
035 import java.sql.ResultSet;
036
037 import java.util.HashMap;
038 import java.util.List;
039 import java.util.Locale;
040 import java.util.Map;
041
042
045 public abstract class UpgradeBaseJournal extends UpgradeDynamicDataMapping {
046
047 protected void addResourcePermission(
048 long companyId, String className, long primKey, long roleId,
049 long actionIds)
050 throws Exception {
051
052 Connection con = null;
053 PreparedStatement ps = null;
054
055 try {
056 long resourcePermissionId = increment(
057 ResourcePermission.class.getName());
058
059 con = DataAccess.getUpgradeOptimizedConnection();
060
061 StringBundler sb = new StringBundler(3);
062
063 sb.append("insert into ResourcePermission (resourcePermissionId, ");
064 sb.append("companyId, name, scope, primKey, roleId, ownerId, ");
065 sb.append("actionIds) values (?, ?, ?, ?, ?, ?, ?, ?)");
066
067 String sql = sb.toString();
068
069 ps = con.prepareStatement(sql);
070
071 ps.setLong(1, resourcePermissionId);
072 ps.setLong(2, companyId);
073 ps.setString(3, className);
074 ps.setInt(4, ResourceConstants.SCOPE_INDIVIDUAL);
075 ps.setLong(5, primKey);
076 ps.setLong(6, roleId);
077 ps.setLong(7, 0);
078 ps.setLong(8, actionIds);
079
080 ps.executeUpdate();
081 }
082 catch (Exception e) {
083 if (_log.isWarnEnabled()) {
084 _log.warn("Unable to add resource permission " + className, e);
085 }
086 }
087 finally {
088 DataAccess.cleanUp(con, ps);
089 }
090 }
091
092 protected long getBitwiseValue(
093 Map<String, Long> bitwiseValues, List<String> actionIds) {
094
095 long bitwiseValue = 0;
096
097 for (String actionId : actionIds) {
098 Long actionIdBitwiseValue = bitwiseValues.get(actionId);
099
100 if (actionIdBitwiseValue == null) {
101 continue;
102 }
103
104 bitwiseValue |= actionIdBitwiseValue;
105 }
106
107 return bitwiseValue;
108 }
109
110 protected Map<String, Long> getBitwiseValues(String name) throws Exception {
111 Map<String, Long> bitwiseValues = _bitwiseValues.get(name);
112
113 if (bitwiseValues != null) {
114 return bitwiseValues;
115 }
116
117 Connection con = null;
118 PreparedStatement ps = null;
119 ResultSet rs = null;
120
121 String currentShardName = null;
122
123 try {
124 currentShardName = ShardUtil.setTargetSource(
125 PropsUtil.get(PropsKeys.SHARD_DEFAULT_NAME));
126
127 con = DataAccess.getUpgradeOptimizedConnection();
128
129 ps = con.prepareStatement(
130 "select actionId, bitwiseValue from ResourceAction " +
131 "where name = ?");
132
133 ps.setString(1, name);
134
135 rs = ps.executeQuery();
136
137 bitwiseValues = new HashMap<>();
138
139 while (rs.next()) {
140 String actionId = rs.getString("actionId");
141 long bitwiseValue = rs.getLong("bitwiseValue");
142
143 bitwiseValues.put(actionId, bitwiseValue);
144 }
145
146 _bitwiseValues.put(name, bitwiseValues);
147
148 return bitwiseValues;
149 }
150 finally {
151 if (Validator.isNotNull(currentShardName)) {
152 ShardUtil.setTargetSource(currentShardName);
153 }
154
155 DataAccess.cleanUp(con, ps, rs);
156 }
157 }
158
159 protected long getCompanyGroupId(long companyId) throws Exception {
160 Connection con = null;
161 PreparedStatement ps = null;
162 ResultSet rs = null;
163
164 try {
165 con = DataAccess.getUpgradeOptimizedConnection();
166
167 ps = con.prepareStatement(
168 "select groupId from Group_ where classNameId = ? and " +
169 "classPK = ?");
170
171 ps.setLong(1, PortalUtil.getClassNameId(Company.class.getName()));
172 ps.setLong(2, companyId);
173
174 rs = ps.executeQuery();
175
176 if (rs.next()) {
177 return rs.getLong("groupId");
178 }
179
180 return 0;
181 }
182 finally {
183 DataAccess.cleanUp(con, ps, rs);
184 }
185 }
186
187 protected long getDefaultUserId(long companyId) throws Exception {
188 Connection con = null;
189 PreparedStatement ps = null;
190 ResultSet rs = null;
191
192 try {
193 con = DataAccess.getUpgradeOptimizedConnection();
194
195 ps = con.prepareStatement(
196 "select userId from User_ where companyId = ? and " +
197 "defaultUser = ?"
198 );
199
200 ps.setLong(1, companyId);
201 ps.setBoolean(2, true);
202
203 rs = ps.executeQuery();
204
205 if (rs.next()) {
206 return rs.getLong("userId");
207 }
208
209 return 0;
210 }
211 finally {
212 DataAccess.cleanUp(con, ps, rs);
213 }
214 }
215
216 protected long getRoleId(long companyId, String name) throws Exception {
217 String roleIdsKey = companyId + StringPool.POUND + name;
218
219 Long roleId = _roleIds.get(roleIdsKey);
220
221 if (roleId != null) {
222 return roleId;
223 }
224
225 Connection con = null;
226 PreparedStatement ps = null;
227 ResultSet rs = null;
228
229 try {
230 con = DataAccess.getUpgradeOptimizedConnection();
231
232 ps = con.prepareStatement(
233 "select roleId from Role_ where companyId = ? and name = ?");
234
235 ps.setLong(1, companyId);
236 ps.setString(2, name);
237
238 rs = ps.executeQuery();
239
240 if (rs.next()) {
241 roleId = rs.getLong("roleId");
242 }
243
244 _roleIds.put(roleIdsKey, roleId);
245
246 return roleId;
247 }
248 finally {
249 DataAccess.cleanUp(con, ps, rs);
250 }
251 }
252
253 protected String localize(
254 long groupId, String key, String defaultLanguageId)
255 throws Exception {
256
257 Map<Locale, String> localizationMap = new HashMap<>();
258
259 for (Locale locale : LanguageUtil.getAvailableLocales(groupId)) {
260 localizationMap.put(locale, LanguageUtil.get(locale, key));
261 }
262
263 return LocalizationUtil.updateLocalization(
264 localizationMap, StringPool.BLANK, key, defaultLanguageId);
265 }
266
267 private static final Log _log = LogFactoryUtil.getLog(
268 UpgradeBaseJournal.class);
269
270 private final Map<String, Map<String, Long>> _bitwiseValues =
271 new HashMap<>();
272 private final Map<String, Long> _roleIds = new HashMap<>();
273
274 }