source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDao.java @ 3408

Last change on this file since 3408 was 3408, checked in by olhsha, 11 years ago

refactored DAO-s tests are added, but not all working yet. Also: add 8 more tests to dispatcher class

File size: 5.3 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
21import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
22import eu.dasish.annotation.schema.CachedRepresentationInfo;
23import java.sql.ResultSet;
24import java.sql.SQLException;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28import javax.sql.DataSource;
29import org.springframework.jdbc.core.RowMapper;
30
31/**
32 *
33 * @author olhsha
34 */
35public class JdbcCachedRepresentationDao extends JdbcResourceDao implements CachedRepresentationDao {
36   
37   
38    public JdbcCachedRepresentationDao(DataSource dataSource) {
39        setDataSource(dataSource);
40        internalIdName = cached_representation_id;
41        resourceTableName = cachedRepresentationTableName;
42    }
43
44    //////////////////////////////////////////////////////////////////////////////////////////////////////
45    @Override
46    public CachedRepresentationIdentifier getExternalID(Number internalID) {
47        return new CachedRepresentationIdentifier(super.getExternalIdentifier(internalID));
48    }
49   
50     //////////////////////////////////////////////////////////////////////////////////////////////////////
51    @Override
52    public Number getInternalID(CachedRepresentationIdentifier externalID) {
53        return super.getInternalID(externalID);
54    }
55
56
57    ///////////////////////////////////////////////////////////////////////////////////////
58    @Override
59    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
60
61        String sql = "SELECT " + cachedRepresentationStar + " FROM " + cachedRepresentationTableName + " WHERE " + cached_representation_id + "= ?";
62        List<CachedRepresentationInfo> result = getSimpleJdbcTemplate().query(sql, cachedRepresentationRowMapper, internalID);
63
64        if (result == null) {
65            return null;
66        }
67        if (result.isEmpty()) {
68            return null;
69        }
70        return result.get(0);
71    }
72    private final RowMapper<CachedRepresentationInfo> cachedRepresentationRowMapper = new RowMapper<CachedRepresentationInfo>() {
73        @Override
74        public CachedRepresentationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
75            CachedRepresentationInfo result = new CachedRepresentationInfo();
76            result.setMimeType(rs.getString(mime_type));
77            result.setRef(rs.getString(external_id));
78            result.setTool(rs.getString(tool));
79            result.setType(rs.getString(type_));
80            // TODO add "where is the file when the schema" is updated!!!!s
81            return result;
82        }
83    };
84
85    ////////////////////////////////////////////////////////////////////////////
86    @Override
87    public Number addCachedRepresentationInfo(CachedRepresentationInfo cached) {
88
89        CachedRepresentationIdentifier externalIdentifier = new CachedRepresentationIdentifier();
90
91        Map<String, Object> params = new HashMap<String, Object>();
92        params.put("externalId", externalIdentifier.toString());
93        params.put("mime_type", cached.getMimeType());
94        params.put("tool", cached.getTool());
95        params.put("type", cached.getType());
96        String sql = "INSERT INTO " + cachedRepresentationTableName + "(" + external_id + "," + mime_type + "," + tool + "," + type_ + " ) VALUES (:externalId, :mime_type,  :tool, :type)";
97        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
98        return getInternalID(externalIdentifier);
99    }
100
101    //////////////////////////////////////////////////////////////////////////////////////////////
102    @Override
103    public int deleteCachedRepresentationInfo(Number internalID) {
104        if (cachedIsInUse(internalID)){
105           return 0;
106        }
107       
108        String sql = "DELETE FROM " + cachedRepresentationTableName + " where " + cached_representation_id + " = ?";
109        return  getSimpleJdbcTemplate().update(sql, internalID);
110    }
111   
112   
113   
114   
115    //////////////// PRIVATE ////////////////////////
116
117    private boolean cachedIsInUse(Number cachedID) {     
118        String sql = "SELECT " + version_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + cached_representation_id + "= ? LIMIT 1";
119        List<Number> result = getSimpleJdbcTemplate().query(sql, versionIDRowMapper, cachedID);
120        if (result.size() > 0) {
121            return true;
122        }
123        return false;
124    }
125   
126    private final RowMapper<Number> versionIDRowMapper = new RowMapper<Number>() {
127        @Override
128        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
129            return rs.getInt(version_id);
130        }
131    };
132   
133   
134}
Note: See TracBrowser for help on using the repository browser.