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.dao.jdbc.MappingSqlQuery;
021    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
022    import com.liferay.portal.kernel.dao.jdbc.RowMapper;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.model.BaseModel;
025    import com.liferay.portal.service.persistence.BasePersistence;
026    
027    import java.sql.Types;
028    
029    import java.util.Collection;
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 =
050                            MappingSqlQueryFactoryUtil.getMappingSqlQuery(
051                                    leftBasePersistence.getDataSource(),
052                                    "SELECT * FROM " + tableName + " WHERE " + leftColumnName +
053                                            " = ? AND " + rightColumnName + " = ?",
054                                    new int[] {Types.BIGINT, Types.BIGINT}, RowMapper.COUNT);
055    
056                    leftToRightPortalCache = new DummyPortalCache(
057                            leftToRightPortalCache.getName());
058                    rightToLeftPortalCache = new DummyPortalCache(
059                            rightToLeftPortalCache.getName());
060    
061                    destroy();
062            }
063    
064            @Override
065            protected boolean containsTableMapping(
066                    long leftPrimaryKey, long rightPrimaryKey, boolean updateCache)
067            throws SystemException {
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 void destroy() {
099                    }
100    
101                    @Override
102                    public Collection<long[]> get(Collection<Long> keys) {
103                            return Collections.emptyList();
104                    }
105    
106                    @Override
107                    public long[] get(Long key) {
108                            return null;
109                    }
110    
111                    @Override
112                    public List<Long> getKeys() {
113                            return Collections.emptyList();
114                    }
115    
116                    @Override
117                    public String getName() {
118                            return name;
119                    }
120    
121                    @Override
122                    public void put(Long key, long[] value) {
123                    }
124    
125                    @Override
126                    public void put(Long key, long[] value, int timeToLive) {
127                    }
128    
129                    @Override
130                    public void registerCacheListener(
131                            CacheListener<Long, long[]> cacheListener) {
132                    }
133    
134                    @Override
135                    public void registerCacheListener(
136                            CacheListener<Long, long[]> cacheListener,
137                            CacheListenerScope cacheListenerScope) {
138                    }
139    
140                    @Override
141                    public void remove(Long key) {
142                    }
143    
144                    @Override
145                    public void removeAll() {
146                    }
147    
148                    @Override
149                    public void unregisterCacheListener(
150                            CacheListener<Long, long[]> cacheListener) {
151                    }
152    
153                    @Override
154                    public void unregisterCacheListeners() {
155                    }
156    
157                    protected DummyPortalCache(String name) {
158                            this.name = name;
159                    }
160    
161                    protected final String name;
162    
163            }
164    
165    }