001    /**
002     * Copyright (c) 2000-present 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.persistence.impl;
016    
017    import com.liferay.portal.kernel.util.PropsKeys;
018    import com.liferay.portal.kernel.util.PropsUtil;
019    import com.liferay.portal.kernel.util.SetUtil;
020    import com.liferay.portal.model.BaseModel;
021    import com.liferay.portal.service.persistence.BasePersistence;
022    
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class TableMapperFactory {
031    
032            public static
033                    <L extends BaseModel<L>, R extends BaseModel<R>> TableMapper<L, R>
034                            getTableMapper(
035                                    String tableName, String companyColumnName,
036                                    String leftColumnName, String rightColumnName,
037                                    BasePersistence<L> leftPersistence,
038                                    BasePersistence<R> rightPersistence) {
039    
040                    TableMapper<?, ?> tableMapper = tableMappers.get(tableName);
041    
042                    if (tableMapper == null) {
043                            TableMapperImpl<L, R> tableMapperImpl = null;
044    
045                            if (cacheMappingTableNames.contains(tableName)) {
046                                    tableMapperImpl = new TableMapperImpl<>(
047                                            tableName, companyColumnName, leftColumnName,
048                                            rightColumnName, leftPersistence, rightPersistence);
049                            }
050                            else {
051                                    tableMapperImpl = new CachelessTableMapperImpl<>(
052                                            tableName, companyColumnName, leftColumnName,
053                                            rightColumnName, leftPersistence, rightPersistence);
054                            }
055    
056                            tableMapperImpl.setReverseTableMapper(
057                                    new ReverseTableMapper<>(tableMapperImpl));
058    
059                            tableMapper = tableMapperImpl;
060    
061                            tableMappers.put(tableName, tableMapper);
062                    }
063                    else if (!tableMapper.matches(leftColumnName, rightColumnName)) {
064                            tableMapper = tableMapper.getReverseTableMapper();
065                    }
066    
067                    return (TableMapper<L, R>)tableMapper;
068            }
069    
070            public static void removeTableMapper(String tableName) {
071                    TableMapper<?, ?> tableMapper = tableMappers.remove(tableName);
072    
073                    if (tableMapper != null) {
074                            tableMapper.destroy();
075                    }
076            }
077    
078            protected static final Set<String> cacheMappingTableNames =
079                    SetUtil.fromArray(
080                            PropsUtil.getArray(
081                                    PropsKeys.TABLE_MAPPER_CACHE_MAPPING_TABLE_NAMES));
082            protected static final Map<String, TableMapper<?, ?>> tableMappers =
083                    new ConcurrentHashMap<>();
084    
085    }