001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.annotation.Propagation;
018    import com.liferay.portal.kernel.annotation.Transactional;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.ResourceCode;
026    import com.liferay.portal.model.ResourceConstants;
027    import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.util.List;
031    import java.util.Map;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ResourceCodeLocalServiceImpl
038            extends ResourceCodeLocalServiceBaseImpl {
039    
040            public ResourceCode addResourceCode(long companyId, String name, int scope)
041                    throws SystemException {
042    
043                    long codeId = counterLocalService.increment(
044                            ResourceCode.class.getName());
045    
046                    ResourceCode resourceCode = resourceCodePersistence.create(codeId);
047    
048                    resourceCode.setCompanyId(companyId);
049                    resourceCode.setName(name);
050                    resourceCode.setScope(scope);
051    
052                    try {
053                            resourceCodePersistence.update(resourceCode, false);
054                    }
055                    catch (SystemException se) {
056                            if (_log.isWarnEnabled()) {
057                                    _log.warn(
058                                            "Add failed, fetch {companyId=" + companyId + ", name=" +
059                                                    name + ", scope=" + scope + "}");
060                            }
061    
062                            resourceCode = resourceCodePersistence.fetchByC_N_S(
063                                    companyId, name, scope, false);
064    
065                            if (resourceCode == null) {
066                                    throw se;
067                            }
068                    }
069    
070                    return resourceCode;
071            }
072    
073            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
074            public void checkResourceCodes() throws SystemException {
075                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
076                            return;
077                    }
078    
079                    if (_resourceCodes.isEmpty()) {
080                            List<ResourceCode> resourceCodes =
081                                    resourceCodePersistence.findAll();
082    
083                            for (ResourceCode resourceCode : resourceCodes) {
084                                    String key = encodeKey(
085                                            resourceCode.getCompanyId(), resourceCode.getName(),
086                                            resourceCode.getScope());
087    
088                                    _resourceCodes.put(key, resourceCode);
089                            }
090                    }
091            }
092    
093            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
094            public void checkResourceCodes(long companyId, String name)
095                    throws SystemException {
096    
097                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
098                            return;
099                    }
100    
101                    getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
102                    getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
103                    getResourceCode(
104                            companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
105                    getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
106            }
107    
108            public ResourceCode getResourceCode(long codeId)
109                    throws PortalException, SystemException {
110    
111                    return resourceCodePersistence.findByPrimaryKey(codeId);
112            }
113    
114            public ResourceCode getResourceCode(long companyId, String name, int scope)
115                    throws SystemException {
116    
117                    // Always cache the resource code. This table exists to improve
118                    // performance. Create the resource code if one does not exist.
119    
120                    String key = encodeKey(companyId, name, scope);
121    
122                    ResourceCode resourceCode = _resourceCodes.get(key);
123    
124                    if (resourceCode == null) {
125                            resourceCode = resourceCodePersistence.fetchByC_N_S(
126                                    companyId, name, scope);
127    
128                            if (resourceCode == null) {
129                                    resourceCode = resourceCodeLocalService.addResourceCode(
130                                            companyId, name, scope);
131                            }
132    
133                            _resourceCodes.put(key, resourceCode);
134                    }
135    
136                    return resourceCode;
137            }
138    
139            protected String encodeKey(long companyId, String name, int scope) {
140                    StringBundler sb = new StringBundler(5);
141    
142                    sb.append(companyId);
143                    sb.append(StringPool.POUND);
144                    sb.append(name);
145                    sb.append(StringPool.POUND);
146                    sb.append(scope);
147    
148                    return sb.toString();
149            }
150    
151            private static Log _log = LogFactoryUtil.getLog(
152                    ResourceCodeLocalServiceImpl.class);
153    
154            private static Map<String, ResourceCode> _resourceCodes =
155                    new ConcurrentHashMap<String, ResourceCode>();
156    
157    }