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.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.cache.PortalCacheListener;
020    import com.liferay.portal.kernel.cache.PortalCacheListenerScope;
021    import com.liferay.portal.kernel.cache.PortalCacheManager;
022    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
023    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
024    import com.liferay.portal.kernel.dao.jdbc.RowMapper;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.model.BaseModel;
027    import com.liferay.portal.service.persistence.BasePersistence;
028    
029    import java.sql.Types;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class CachelessTableMapperImpl
038                    <L extends BaseModel<L>, R extends BaseModel<R>>
039            extends TableMapperImpl<L, R> {
040    
041            public CachelessTableMapperImpl(
042                    String tableName, String companyColumnName, String leftColumnName,
043                    String rightColumnName, BasePersistence<L> leftBasePersistence,
044                    BasePersistence<R> rightBasePersistence) {
045    
046                    super(
047                            tableName, companyColumnName, leftColumnName, rightColumnName,
048                            leftBasePersistence, rightBasePersistence);
049    
050                    getTableMappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(
051                            leftBasePersistence.getDataSource(),
052                            "SELECT * FROM " + tableName + " WHERE " + companyColumnName +
053                                    " = ? AND " + leftColumnName + " = ? AND " + rightColumnName +
054                                            " = ?",
055                            new int[] {Types.BIGINT, Types.BIGINT, Types.BIGINT},
056                            RowMapper.COUNT);
057    
058                    _leftToRightportalCacheNamePrefix =
059                            TableMapper.class.getName() + "-" + tableName + "-LeftToRight-";
060                    _rightToLeftportalCacheNamePrefix =
061                            TableMapper.class.getName() + "-" + tableName + "-RightToLeft-";
062            }
063    
064            @Override
065            protected boolean containsTableMapping(
066                    long companyId, long leftPrimaryKey, long rightPrimaryKey,
067                    boolean updateCache) {
068    
069                    List<Integer> counts = null;
070    
071                    try {
072                            counts = getTableMappingSqlQuery.execute(
073                                    companyId, leftPrimaryKey, rightPrimaryKey);
074                    }
075                    catch (Exception e) {
076                            throw new SystemException(e);
077                    }
078    
079                    if (counts.isEmpty()) {
080                            return false;
081                    }
082    
083                    int count = counts.get(0);
084    
085                    if (count == 0) {
086                            return false;
087                    }
088    
089                    return true;
090            }
091    
092            @Override
093            protected PortalCache<Long, long[]> getLeftToRightPortalCache(
094                    long companyId) {
095    
096                    return new DummyPortalCache(
097                            _leftToRightportalCacheNamePrefix.concat(
098                                    String.valueOf(companyId)));
099            }
100    
101            @Override
102            protected PortalCache<Long, long[]> getRightToLeftPortalCache(
103                    long companyId) {
104    
105                    return new DummyPortalCache(
106                            _rightToLeftportalCacheNamePrefix.concat(
107                                    String.valueOf(companyId)));
108            }
109    
110            protected final MappingSqlQuery<Integer> getTableMappingSqlQuery;
111    
112            protected static class DummyPortalCache
113                    implements PortalCache<Long, long[]> {
114    
115                    @Override
116                    public long[] get(Long key) {
117                            return null;
118                    }
119    
120                    @Override
121                    public List<Long> getKeys() {
122                            return Collections.emptyList();
123                    }
124    
125                    /**
126                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheName()}
127                     */
128                    @Deprecated
129                    @Override
130                    public String getName() {
131                            return getPortalCacheName();
132                    }
133    
134                    @Override
135                    public PortalCacheManager<Long, long[]> getPortalCacheManager() {
136                            return portalCacheManager;
137                    }
138    
139                    @Override
140                    public String getPortalCacheName() {
141                            return portalCacheName;
142                    }
143    
144                    @Override
145                    public void put(Long key, long[] value) {
146                    }
147    
148                    @Override
149                    public void put(Long key, long[] value, int timeToLive) {
150                    }
151    
152                    @Override
153                    public void registerPortalCacheListener(
154                            PortalCacheListener<Long, long[]> portalCacheListener) {
155                    }
156    
157                    @Override
158                    public void registerPortalCacheListener(
159                            PortalCacheListener<Long, long[]> portalCacheListener,
160                            PortalCacheListenerScope portalCacheListenerScope) {
161                    }
162    
163                    @Override
164                    public void remove(Long key) {
165                    }
166    
167                    @Override
168                    public void removeAll() {
169                    }
170    
171                    @Override
172                    public void unregisterPortalCacheListener(
173                            PortalCacheListener<Long, long[]> portalCacheListener) {
174                    }
175    
176                    @Override
177                    public void unregisterPortalCacheListeners() {
178                    }
179    
180                    protected DummyPortalCache(String portalCacheName) {
181                            this.portalCacheName = portalCacheName;
182                    }
183    
184                    protected final PortalCacheManager<Long, long[]> portalCacheManager =
185                            MultiVMPoolUtil.getPortalCacheManager();
186                    protected final String portalCacheName;
187    
188            }
189    
190            private final String _leftToRightportalCacheNamePrefix;
191            private final String _rightToLeftportalCacheNamePrefix;
192    
193    }