001    /**
002     * Copyright (c) 2000-2013 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.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.transaction.Propagation;
021    import com.liferay.portal.kernel.transaction.Transactional;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
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            @Override
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            @Override
076            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
077            public void checkResourceCodes() throws SystemException {
078                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
079                            return;
080                    }
081    
082                    if (_resourceCodes.isEmpty()) {
083                            List<ResourceCode> resourceCodes =
084                                    resourceCodePersistence.findAll();
085    
086                            for (ResourceCode resourceCode : resourceCodes) {
087                                    String key = encodeKey(
088                                            resourceCode.getCompanyId(), resourceCode.getName(),
089                                            resourceCode.getScope());
090    
091                                    _resourceCodes.put(key, resourceCode);
092                            }
093                    }
094            }
095    
096            @Override
097            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
098            public void checkResourceCodes(long companyId, String name)
099                    throws SystemException {
100    
101                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
102                            return;
103                    }
104    
105                    getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
106                    getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
107                    getResourceCode(
108                            companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
109                    getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
110            }
111    
112            @Override
113            public ResourceCode getResourceCode(long companyId, String name, int scope)
114                    throws SystemException {
115    
116                    // Always cache the resource code. This table exists to improve
117                    // performance. Create the resource code if one does not exist.
118    
119                    if (Validator.isNull(name)) {
120                            name = StringPool.BLANK;
121                    }
122    
123                    String key = encodeKey(companyId, name, scope);
124    
125                    ResourceCode resourceCode = _resourceCodes.get(key);
126    
127                    if (resourceCode == null) {
128                            resourceCode = resourceCodePersistence.fetchByC_N_S(
129                                    companyId, name, scope);
130    
131                            if (resourceCode == null) {
132                                    resourceCode = resourceCodeLocalService.addResourceCode(
133                                            companyId, name, scope);
134                            }
135    
136                            _resourceCodes.put(key, resourceCode);
137                    }
138    
139                    return resourceCode;
140            }
141    
142            protected String encodeKey(long companyId, String name, int scope) {
143                    StringBundler sb = new StringBundler(5);
144    
145                    sb.append(StringUtil.toHexString(companyId));
146                    sb.append(StringPool.POUND);
147                    sb.append(name);
148                    sb.append(StringPool.POUND);
149                    sb.append(StringUtil.toHexString(scope));
150    
151                    return sb.toString();
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(
155                    ResourceCodeLocalServiceImpl.class);
156    
157            private static Map<String, ResourceCode> _resourceCodes =
158                    new ConcurrentHashMap<String, ResourceCode>();
159    
160    }