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.util.Validator;
022    import com.liferay.portal.model.ClassName;
023    import com.liferay.portal.model.ModelHintsUtil;
024    import com.liferay.portal.model.impl.ClassNameImpl;
025    import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
026    
027    import java.util.List;
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ClassNameLocalServiceImpl extends ClassNameLocalServiceBaseImpl {
035    
036            public ClassName addClassName(String value) throws SystemException {
037                    ClassName className = classNamePersistence.fetchByValue(value);
038    
039                    if (className == null) {
040                            long classNameId = counterLocalService.increment();
041    
042                            className = classNamePersistence.create(classNameId);
043    
044                            className.setValue(value);
045    
046                            classNamePersistence.update(className, false);
047                    }
048    
049                    return className;
050            }
051    
052            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
053            public void checkClassNames() throws SystemException {
054                    if (_classNames.isEmpty()) {
055                            List<ClassName> classNames = classNamePersistence.findAll();
056    
057                            for (ClassName className : classNames) {
058                                    _classNames.put(className.getValue(), className);
059                            }
060                    }
061    
062                    List<String> models = ModelHintsUtil.getModels();
063    
064                    for (String model : models) {
065                            getClassName(model);
066                    }
067            }
068    
069            public ClassName getClassName(long classNameId)
070                    throws PortalException, SystemException {
071    
072                    return classNamePersistence.findByPrimaryKey(classNameId);
073            }
074    
075            @Transactional(enabled = false)
076            public ClassName getClassName(String value) throws SystemException {
077                    if (Validator.isNull(value)) {
078                            return _nullClassName;
079                    }
080    
081                    // Always cache the class name. This table exists to improve
082                    // performance. Create the class name if one does not exist.
083    
084                    ClassName className = _classNames.get(value);
085    
086                    if (className == null) {
087                            className = classNameLocalService.addClassName(value);
088    
089                            _classNames.put(value, className);
090                    }
091    
092                    return className;
093            }
094    
095            @Transactional(enabled = false)
096            public long getClassNameId(Class<?> classObj) {
097                    return getClassNameId(classObj.getName());
098            }
099    
100            @Transactional(enabled = false)
101            public long getClassNameId(String value) {
102                    try {
103                            ClassName className = getClassName(value);
104    
105                            return className.getClassNameId();
106                    }
107                    catch (Exception e) {
108                            throw new RuntimeException(
109                                    "Unable to get class name from value " + value, e);
110                    }
111            }
112    
113            private static ClassName _nullClassName = new ClassNameImpl();
114            private static Map<String, ClassName> _classNames =
115                    new ConcurrentHashMap<String, ClassName>();
116    
117    }