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