1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
48  public class ResourceCodeLocalServiceImpl
49      extends ResourceCodeLocalServiceBaseImpl {
50  
51      public ResourceCode addResourceCode(long companyId, String name, int scope)
52          throws SystemException {
53  
54          long codeId = counterLocalService.increment(
55              ResourceCode.class.getName());
56  
57          ResourceCode resourceCode = resourceCodePersistence.create(codeId);
58  
59          resourceCode.setCompanyId(companyId);
60          resourceCode.setName(name);
61          resourceCode.setScope(scope);
62  
63          try {
64              resourceCodePersistence.update(resourceCode, false);
65          }
66          catch (SystemException se) {
67              if (_log.isWarnEnabled()) {
68                  _log.warn(
69                      "Add failed, fetch {companyId=" + companyId + ", name=" +
70                          name + ", scope=" + scope + "}");
71              }
72  
73              resourceCode = resourceCodePersistence.fetchByC_N_S(
74                  companyId, name, scope, false);
75  
76              if (resourceCode == null) {
77                  throw se;
78              }
79          }
80  
81          return resourceCode;
82      }
83  
84      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
85      public void checkResourceCodes() throws SystemException {
86          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
87              return;
88          }
89  
90          if (_resourceCodes.isEmpty()) {
91              List<ResourceCode> resourceCodes =
92                  resourceCodePersistence.findAll();
93  
94              for (ResourceCode resourceCode : resourceCodes) {
95                  String key = encodeKey(
96                      resourceCode.getCompanyId(), resourceCode.getName(),
97                      resourceCode.getScope());
98  
99                  _resourceCodes.put(key, resourceCode);
100             }
101         }
102     }
103 
104     @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
105     public void checkResourceCodes(long companyId, String name)
106         throws SystemException {
107 
108         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
109             return;
110         }
111 
112         getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
113         getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
114         getResourceCode(
115             companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
116         getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
117     }
118 
119     public ResourceCode getResourceCode(long codeId)
120         throws PortalException, SystemException {
121 
122         return resourceCodePersistence.findByPrimaryKey(codeId);
123     }
124 
125     public ResourceCode getResourceCode(long companyId, String name, int scope)
126         throws SystemException {
127 
128         // Always cache the resource code. This table exists to improve
129         // performance. Create the resource code if one does not exist.
130 
131         String key = encodeKey(companyId, name, scope);
132 
133         ResourceCode resourceCode = _resourceCodes.get(key);
134 
135         if (resourceCode == null) {
136             resourceCode = resourceCodePersistence.fetchByC_N_S(
137                 companyId, name, scope);
138 
139             if (resourceCode == null) {
140                 resourceCode = resourceCodeLocalService.addResourceCode(
141                     companyId, name, scope);
142             }
143 
144             _resourceCodes.put(key, resourceCode);
145         }
146 
147         return resourceCode;
148     }
149 
150     protected String encodeKey(long companyId, String name, int scope) {
151         StringBuilder sb = new StringBuilder();
152 
153         sb.append(companyId);
154         sb.append(StringPool.POUND);
155         sb.append(name);
156         sb.append(StringPool.POUND);
157         sb.append(scope);
158 
159         return sb.toString();
160     }
161 
162     private static Log _log =
163         LogFactoryUtil.getLog(ResourceCodeLocalServiceImpl.class);
164 
165     private static Map<String, ResourceCode> _resourceCodes =
166         new ConcurrentHashMap<String, ResourceCode>();
167 
168 }