001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.transaction.Propagation;
021 import com.liferay.portal.kernel.transaction.Transactional;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.ResourceCode;
027 import com.liferay.portal.model.ResourceConstants;
028 import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
029 import com.liferay.portal.util.PropsValues;
030
031 import java.util.List;
032 import java.util.Map;
033 import java.util.concurrent.ConcurrentHashMap;
034
035
038 public class ResourceCodeLocalServiceImpl
039 extends ResourceCodeLocalServiceBaseImpl {
040
041 public ResourceCode addResourceCode(long companyId, String name, int scope)
042 throws SystemException {
043
044 long codeId = counterLocalService.increment(
045 ResourceCode.class.getName());
046
047 ResourceCode resourceCode = resourceCodePersistence.create(codeId);
048
049 resourceCode.setCompanyId(companyId);
050 resourceCode.setName(name);
051 resourceCode.setScope(scope);
052
053 try {
054 resourceCodePersistence.update(resourceCode, false);
055 }
056 catch (SystemException se) {
057 if (_log.isWarnEnabled()) {
058 _log.warn(
059 "Add failed, fetch {companyId=" + companyId + ", name=" +
060 name + ", scope=" + scope + "}");
061 }
062
063 resourceCode = resourceCodePersistence.fetchByC_N_S(
064 companyId, name, scope, false);
065
066 if (resourceCode == null) {
067 throw se;
068 }
069 }
070
071 return resourceCode;
072 }
073
074 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
075 public void checkResourceCodes() throws SystemException {
076 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
077 return;
078 }
079
080 if (_resourceCodes.isEmpty()) {
081 List<ResourceCode> resourceCodes =
082 resourceCodePersistence.findAll();
083
084 for (ResourceCode resourceCode : resourceCodes) {
085 String key = encodeKey(
086 resourceCode.getCompanyId(), resourceCode.getName(),
087 resourceCode.getScope());
088
089 _resourceCodes.put(key, resourceCode);
090 }
091 }
092 }
093
094 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
095 public void checkResourceCodes(long companyId, String name)
096 throws SystemException {
097
098 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
099 return;
100 }
101
102 getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
103 getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
104 getResourceCode(
105 companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
106 getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
107 }
108
109 public ResourceCode getResourceCode(long companyId, String name, int scope)
110 throws SystemException {
111
112
113
114
115 if (Validator.isNull(name)) {
116 name = StringPool.BLANK;
117 }
118
119 String key = encodeKey(companyId, name, scope);
120
121 ResourceCode resourceCode = _resourceCodes.get(key);
122
123 if (resourceCode == null) {
124 resourceCode = resourceCodePersistence.fetchByC_N_S(
125 companyId, name, scope);
126
127 if (resourceCode == null) {
128 resourceCode = resourceCodeLocalService.addResourceCode(
129 companyId, name, scope);
130 }
131
132 _resourceCodes.put(key, resourceCode);
133 }
134
135 return resourceCode;
136 }
137
138 protected String encodeKey(long companyId, String name, int scope) {
139 StringBundler sb = new StringBundler(5);
140
141 sb.append(StringUtil.toHexString(companyId));
142 sb.append(StringPool.POUND);
143 sb.append(name);
144 sb.append(StringPool.POUND);
145 sb.append(StringUtil.toHexString(scope));
146
147 return sb.toString();
148 }
149
150 private static Log _log = LogFactoryUtil.getLog(
151 ResourceCodeLocalServiceImpl.class);
152
153 private static Map<String, ResourceCode> _resourceCodes =
154 new ConcurrentHashMap<String, ResourceCode>();
155
156 }