001
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
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 leftColumnName, String rightColumnName,
036 BasePersistence<L> leftPersistence,
037 BasePersistence<R> rightPersistence) {
038
039 TableMapper<?, ?> tableMapper = tableMappers.get(tableName);
040
041 if (tableMapper == null) {
042 TableMapperImpl<L, R> tableMapperImpl = null;
043
044 if (cacheMappingTableNames.contains(tableName)) {
045 tableMapperImpl = new TableMapperImpl<>(
046 tableName, leftColumnName, rightColumnName, leftPersistence,
047 rightPersistence);
048 }
049 else {
050 tableMapperImpl = new CachelessTableMapperImpl<>(
051 tableName, leftColumnName, rightColumnName, leftPersistence,
052 rightPersistence);
053 }
054
055 tableMapperImpl.setReverseTableMapper(
056 new ReverseTableMapper<>(tableMapperImpl));
057
058 tableMapper = tableMapperImpl;
059
060 tableMappers.put(tableName, tableMapper);
061 }
062 else if (!tableMapper.matches(leftColumnName, rightColumnName)) {
063 tableMapper = tableMapper.getReverseTableMapper();
064 }
065
066 return (TableMapper<L, R>)tableMapper;
067 }
068
069 public static void removeTableMapper(String tableName) {
070 TableMapper<?, ?> tableMapper = tableMappers.remove(tableName);
071
072 if (tableMapper != null) {
073 tableMapper.destroy();
074 }
075 }
076
077 protected static final Set<String> cacheMappingTableNames =
078 SetUtil.fromArray(
079 PropsUtil.getArray(
080 PropsKeys.TABLE_MAPPER_CACHE_MAPPING_TABLE_NAMES));
081 protected static final Map<String, TableMapper<?, ?>> tableMappers =
082 new ConcurrentHashMap<>();
083
084 }