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