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.CacheListener;
018    import com.liferay.portal.kernel.cache.CacheListenerScope;
019    import com.liferay.portal.kernel.cache.PortalCache;
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 leftColumnName, String rightColumnName,
042                    BasePersistence<L> leftBasePersistence,
043                    BasePersistence<R> rightBasePersistence) {
044    
045                    super(
046                            tableName, leftColumnName, rightColumnName, leftBasePersistence,
047                            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.getName(),
057                            leftToRightPortalCache.getPortalCacheManager());
058                    rightToLeftPortalCache = new DummyPortalCache(
059                            rightToLeftPortalCache.getName(),
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                    @Override
108                    public String getName() {
109                            return portalCacheName;
110                    }
111    
112                    @Override
113                    public PortalCacheManager<Long, long[]> getPortalCacheManager() {
114                            return portalCacheManager;
115                    }
116    
117                    @Override
118                    public void put(Long key, long[] value) {
119                    }
120    
121                    @Override
122                    public void put(Long key, long[] value, int timeToLive) {
123                    }
124    
125                    @Override
126                    public void registerCacheListener(
127                            CacheListener<Long, long[]> cacheListener) {
128                    }
129    
130                    @Override
131                    public void registerCacheListener(
132                            CacheListener<Long, long[]> cacheListener,
133                            CacheListenerScope cacheListenerScope) {
134                    }
135    
136                    @Override
137                    public void remove(Long key) {
138                    }
139    
140                    @Override
141                    public void removeAll() {
142                    }
143    
144                    @Override
145                    public void unregisterCacheListener(
146                            CacheListener<Long, long[]> cacheListener) {
147                    }
148    
149                    @Override
150                    public void unregisterCacheListeners() {
151                    }
152    
153                    protected DummyPortalCache(
154                            String portalCacheName,
155                            PortalCacheManager<Long, long[]> portalCacheManager) {
156    
157                            this.portalCacheName = portalCacheName;
158                            this.portalCacheManager = portalCacheManager;
159                    }
160    
161                    protected final PortalCacheManager<Long, long[]> portalCacheManager;
162                    protected final String portalCacheName;
163    
164            }
165    
166    }