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