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.persistence.impl;
016    
017    import com.liferay.portal.model.BaseModel;
018    import com.liferay.portal.service.persistence.BasePersistence;
019    
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class TableMapperFactory {
027    
028            public static
029                    <L extends BaseModel<L>, R extends BaseModel<R>> TableMapper<L, R>
030                            getTableMapper(
031                                    String tableName, String leftColumnName, String rightColumnName,
032                                    BasePersistence<L> leftPersistence,
033                                    BasePersistence<R> rightPersistence) {
034    
035                    TableMapper<?, ?> tableMapper = tableMappers.get(tableName);
036    
037                    if (tableMapper == null) {
038                            TableMapperImpl<L, R> tableMapperImpl =
039                                    new TableMapperImpl<L, R>(
040                                            tableName, leftColumnName, rightColumnName, leftPersistence,
041                                            rightPersistence);
042    
043                            tableMapperImpl.setReverseTableMapper(
044                                    new ReverseTableMapper<R, L>(tableMapperImpl));
045    
046                            tableMapper = tableMapperImpl;
047    
048                            tableMappers.put(tableName, tableMapper);
049                    }
050                    else if (!tableMapper.matches(leftColumnName, rightColumnName)) {
051                            tableMapper = tableMapper.getReverseTableMapper();
052                    }
053    
054                    return (TableMapper<L, R>)tableMapper;
055            }
056    
057            public static void removeTableMapper(String tableName) {
058                    TableMapper<?, ?> tableMapper = tableMappers.remove(tableName);
059    
060                    if (tableMapper != null) {
061                            tableMapper.destroy();
062                    }
063            }
064    
065            protected static Map<String, TableMapper<?, ?>> tableMappers =
066                    new ConcurrentHashMap<String, TableMapper<?, ?>>();
067    
068    }