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