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