1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.Propagation;
20  import com.liferay.portal.kernel.annotation.Transactional;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.ResourceCode;
28  import com.liferay.portal.model.ResourceConstants;
29  import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
30  import com.liferay.portal.util.PropsValues;
31  
32  import java.util.List;
33  import java.util.Map;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  /**
37   * <a href="ResourceCodeLocalServiceImpl.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class ResourceCodeLocalServiceImpl
43      extends ResourceCodeLocalServiceBaseImpl {
44  
45      public ResourceCode addResourceCode(long companyId, String name, int scope)
46          throws SystemException {
47  
48          long codeId = counterLocalService.increment(
49              ResourceCode.class.getName());
50  
51          ResourceCode resourceCode = resourceCodePersistence.create(codeId);
52  
53          resourceCode.setCompanyId(companyId);
54          resourceCode.setName(name);
55          resourceCode.setScope(scope);
56  
57          try {
58              resourceCodePersistence.update(resourceCode, false);
59          }
60          catch (SystemException se) {
61              if (_log.isWarnEnabled()) {
62                  _log.warn(
63                      "Add failed, fetch {companyId=" + companyId + ", name=" +
64                          name + ", scope=" + scope + "}");
65              }
66  
67              resourceCode = resourceCodePersistence.fetchByC_N_S(
68                  companyId, name, scope, false);
69  
70              if (resourceCode == null) {
71                  throw se;
72              }
73          }
74  
75          return resourceCode;
76      }
77  
78      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
79      public void checkResourceCodes() throws SystemException {
80          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
81              return;
82          }
83  
84          if (_resourceCodes.isEmpty()) {
85              List<ResourceCode> resourceCodes =
86                  resourceCodePersistence.findAll();
87  
88              for (ResourceCode resourceCode : resourceCodes) {
89                  String key = encodeKey(
90                      resourceCode.getCompanyId(), resourceCode.getName(),
91                      resourceCode.getScope());
92  
93                  _resourceCodes.put(key, resourceCode);
94              }
95          }
96      }
97  
98      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
99      public void checkResourceCodes(long companyId, String name)
100         throws SystemException {
101 
102         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
103             return;
104         }
105 
106         getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
107         getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
108         getResourceCode(
109             companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
110         getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
111     }
112 
113     public ResourceCode getResourceCode(long codeId)
114         throws PortalException, SystemException {
115 
116         return resourceCodePersistence.findByPrimaryKey(codeId);
117     }
118 
119     public ResourceCode getResourceCode(long companyId, String name, int scope)
120         throws SystemException {
121 
122         // Always cache the resource code. This table exists to improve
123         // performance. Create the resource code if one does not exist.
124 
125         if (Validator.isNull(name)) {
126             name = StringPool.BLANK;
127         }
128 
129         String key = encodeKey(companyId, name, scope);
130 
131         ResourceCode resourceCode = _resourceCodes.get(key);
132 
133         if (resourceCode == null) {
134             resourceCode = resourceCodePersistence.fetchByC_N_S(
135                 companyId, name, scope);
136 
137             if (resourceCode == null) {
138                 resourceCode = resourceCodeLocalService.addResourceCode(
139                     companyId, name, scope);
140             }
141 
142             _resourceCodes.put(key, resourceCode);
143         }
144 
145         return resourceCode;
146     }
147 
148     protected String encodeKey(long companyId, String name, int scope) {
149         StringBundler sb = new StringBundler(5);
150 
151         sb.append(StringUtil.toHexString(companyId));
152         sb.append(StringPool.POUND);
153         sb.append(name);
154         sb.append(StringPool.POUND);
155         sb.append(StringUtil.toHexString(scope));
156 
157         return sb.toString();
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(
161         ResourceCodeLocalServiceImpl.class);
162 
163     private static Map<String, ResourceCode> _resourceCodes =
164         new ConcurrentHashMap<String, ResourceCode>();
165 
166 }