1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.annotation.Propagation;
25  import com.liferay.portal.kernel.annotation.Transactional;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.ResourceCode;
30  import com.liferay.portal.model.ResourceConstants;
31  import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.util.List;
35  import java.util.Map;
36  import java.util.concurrent.ConcurrentHashMap;
37  
38  /**
39   * <a href="ResourceCodeLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class ResourceCodeLocalServiceImpl
46      extends ResourceCodeLocalServiceBaseImpl {
47  
48      public ResourceCode addResourceCode(long companyId, String name, int scope)
49          throws SystemException {
50  
51          long codeId = counterLocalService.increment(
52              ResourceCode.class.getName());
53  
54          ResourceCode resourceCode = resourceCodePersistence.create(codeId);
55  
56          resourceCode.setCompanyId(companyId);
57          resourceCode.setName(name);
58          resourceCode.setScope(scope);
59  
60          try {
61              resourceCodePersistence.update(resourceCode, false);
62          }
63          catch (SystemException se) {
64              if (_log.isWarnEnabled()) {
65                  _log.warn(
66                      "Add failed, fetch {companyId=" + companyId + ", name=" +
67                          name + ", scope=" + scope + "}");
68              }
69  
70              resourceCode = resourceCodePersistence.fetchByC_N_S(
71                  companyId, name, scope, false);
72  
73              if (resourceCode == null) {
74                  throw se;
75              }
76          }
77  
78          return resourceCode;
79      }
80  
81      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
82      public void checkResourceCodes() throws SystemException {
83          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
84              return;
85          }
86  
87          if (_resourceCodes.isEmpty()) {
88              List<ResourceCode> resourceCodes =
89                  resourceCodePersistence.findAll();
90  
91              for (ResourceCode resourceCode : resourceCodes) {
92                  String key = encodeKey(
93                      resourceCode.getCompanyId(), resourceCode.getName(),
94                      resourceCode.getScope());
95  
96                  _resourceCodes.put(key, resourceCode);
97              }
98          }
99      }
100 
101     @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
102     public void checkResourceCodes(long companyId, String name)
103         throws SystemException {
104 
105         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
106             return;
107         }
108 
109         getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
110         getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
111         getResourceCode(
112             companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
113         getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
114     }
115 
116     public ResourceCode getResourceCode(long codeId)
117         throws PortalException, SystemException {
118 
119         return resourceCodePersistence.findByPrimaryKey(codeId);
120     }
121 
122     public ResourceCode getResourceCode(long companyId, String name, int scope)
123         throws SystemException {
124 
125         // Always cache the resource code. This table exists to improve
126         // performance. Create the resource code if one does not exist.
127 
128         String key = encodeKey(companyId, name, scope);
129 
130         ResourceCode resourceCode = _resourceCodes.get(key);
131 
132         if (resourceCode == null) {
133             resourceCode = resourceCodePersistence.fetchByC_N_S(
134                 companyId, name, scope);
135 
136             if (resourceCode == null) {
137                 resourceCode = resourceCodeLocalService.addResourceCode(
138                     companyId, name, scope);
139             }
140 
141             _resourceCodes.put(key, resourceCode);
142         }
143 
144         return resourceCode;
145     }
146 
147     protected String encodeKey(long companyId, String name, int scope) {
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append(companyId);
151         sb.append(StringPool.POUND);
152         sb.append(name);
153         sb.append(StringPool.POUND);
154         sb.append(scope);
155 
156         return sb.toString();
157     }
158 
159     private static Log _log =
160         LogFactoryUtil.getLog(ResourceCodeLocalServiceImpl.class);
161 
162     private static Map<String, ResourceCode> _resourceCodes =
163         new ConcurrentHashMap<String, ResourceCode>();
164 
165 }