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.model.ResourceCode;
27  import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
28  
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * <a href="ResourceCodeLocalServiceImpl.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class ResourceCodeLocalServiceImpl
40      extends ResourceCodeLocalServiceBaseImpl {
41  
42      @Transactional(propagation = Propagation.REQUIRES_NEW)
43      public ResourceCode getResourceCode(long codeId)
44          throws PortalException, SystemException {
45  
46          return resourceCodePersistence.findByPrimaryKey(codeId);
47      }
48  
49      @Transactional(propagation = Propagation.REQUIRES_NEW)
50      public ResourceCode getResourceCode(long companyId, String name, int scope)
51          throws SystemException {
52  
53          // Always cache the resource code. This table exists to improve
54          // performance. Create the resource code if one does not exist.
55  
56          String key = encodeKey(companyId, name, scope);
57  
58          ResourceCode resourceCode = _resourceCodes.get(key);
59  
60          if (resourceCode == null) {
61              resourceCode = resourceCodePersistence.fetchByC_N_S(
62                  companyId, name, scope);
63  
64              if (resourceCode == null) {
65                  long codeId = counterLocalService.increment(
66                      ResourceCode.class.getName());
67  
68                  resourceCode = resourceCodePersistence.create(codeId);
69  
70                  resourceCode.setCompanyId(companyId);
71                  resourceCode.setName(name);
72                  resourceCode.setScope(scope);
73  
74                  resourceCodePersistence.update(resourceCode, false);
75              }
76  
77              _resourceCodes.put(key, resourceCode);
78          }
79  
80          return resourceCode;
81      }
82  
83      protected String encodeKey(long companyId, String name, int scope) {
84          StringBuilder sb = new StringBuilder();
85  
86          sb.append(companyId);
87          sb.append(name);
88          sb.append(scope);
89  
90          return sb.toString();
91      }
92  
93      private static Map<String, ResourceCode> _resourceCodes =
94          new ConcurrentHashMap<String, ResourceCode>();
95  
96  }