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