001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.kernel.util.Validator;
026    import com.liferay.portal.model.ResourceCode;
027    import com.liferay.portal.model.ResourceConstants;
028    import com.liferay.portal.service.base.ResourceCodeLocalServiceBaseImpl;
029    import com.liferay.portal.util.PropsValues;
030    
031    import java.util.List;
032    import java.util.Map;
033    import java.util.concurrent.ConcurrentHashMap;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ResourceCodeLocalServiceImpl
039            extends ResourceCodeLocalServiceBaseImpl {
040    
041            public ResourceCode addResourceCode(long companyId, String name, int scope)
042                    throws SystemException {
043    
044                    long codeId = counterLocalService.increment(
045                            ResourceCode.class.getName());
046    
047                    ResourceCode resourceCode = resourceCodePersistence.create(codeId);
048    
049                    resourceCode.setCompanyId(companyId);
050                    resourceCode.setName(name);
051                    resourceCode.setScope(scope);
052    
053                    try {
054                            resourceCodePersistence.update(resourceCode, false);
055                    }
056                    catch (SystemException se) {
057                            if (_log.isWarnEnabled()) {
058                                    _log.warn(
059                                            "Add failed, fetch {companyId=" + companyId + ", name=" +
060                                                    name + ", scope=" + scope + "}");
061                            }
062    
063                            resourceCode = resourceCodePersistence.fetchByC_N_S(
064                                    companyId, name, scope, false);
065    
066                            if (resourceCode == null) {
067                                    throw se;
068                            }
069                    }
070    
071                    return resourceCode;
072            }
073    
074            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
075            public void checkResourceCodes() throws SystemException {
076                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
077                            return;
078                    }
079    
080                    if (_resourceCodes.isEmpty()) {
081                            List<ResourceCode> resourceCodes =
082                                    resourceCodePersistence.findAll();
083    
084                            for (ResourceCode resourceCode : resourceCodes) {
085                                    String key = encodeKey(
086                                            resourceCode.getCompanyId(), resourceCode.getName(),
087                                            resourceCode.getScope());
088    
089                                    _resourceCodes.put(key, resourceCode);
090                            }
091                    }
092            }
093    
094            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
095            public void checkResourceCodes(long companyId, String name)
096                    throws SystemException {
097    
098                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
099                            return;
100                    }
101    
102                    getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
103                    getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
104                    getResourceCode(
105                            companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
106                    getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
107            }
108    
109            public ResourceCode getResourceCode(long codeId)
110                    throws PortalException, SystemException {
111    
112                    return resourceCodePersistence.findByPrimaryKey(codeId);
113            }
114    
115            public ResourceCode getResourceCode(long companyId, String name, int scope)
116                    throws SystemException {
117    
118                    // Always cache the resource code. This table exists to improve
119                    // performance. Create the resource code if one does not exist.
120    
121                    if (Validator.isNull(name)) {
122                            name = StringPool.BLANK;
123                    }
124    
125                    String key = encodeKey(companyId, name, scope);
126    
127                    ResourceCode resourceCode = _resourceCodes.get(key);
128    
129                    if (resourceCode == null) {
130                            resourceCode = resourceCodePersistence.fetchByC_N_S(
131                                    companyId, name, scope);
132    
133                            if (resourceCode == null) {
134                                    resourceCode = resourceCodeLocalService.addResourceCode(
135                                            companyId, name, scope);
136                            }
137    
138                            _resourceCodes.put(key, resourceCode);
139                    }
140    
141                    return resourceCode;
142            }
143    
144            protected String encodeKey(long companyId, String name, int scope) {
145                    StringBundler sb = new StringBundler(5);
146    
147                    sb.append(companyId);
148                    sb.append(StringPool.POUND);
149                    sb.append(name);
150                    sb.append(StringPool.POUND);
151                    sb.append(scope);
152    
153                    return sb.toString();
154            }
155    
156            private static Log _log = LogFactoryUtil.getLog(
157                    ResourceCodeLocalServiceImpl.class);
158    
159            private static Map<String, ResourceCode> _resourceCodes =
160                    new ConcurrentHashMap<String, ResourceCode>();
161    
162    }