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

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

replacing DaishIdentifier?-based classes with simply UUID. tested. Works, excpet that UUID seems not be be serailizable, see e.g. getAllAnnotations in NotebookResource? where I have to use Peter's workoaroun to put UUID in JAXBElement<UUID>

File size: 10.7 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.AnnotationDao;
21import eu.dasish.annotation.backend.dao.NotebookDao;
22import eu.dasish.annotation.schema.Annotations;
23import eu.dasish.annotation.schema.Notebook;
24import eu.dasish.annotation.schema.NotebookInfo;
25import eu.dasish.annotation.schema.ResourceREF;
26import java.sql.ResultSet;
27import java.sql.SQLException;
28import java.util.ArrayList;
29import java.util.GregorianCalendar;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.UUID;
34import javax.sql.DataSource;
35import org.springframework.jdbc.core.RowMapper;
36import javax.xml.datatype.DatatypeConfigurationException;
37import javax.xml.datatype.DatatypeFactory;
38import javax.xml.datatype.XMLGregorianCalendar;
39import org.springframework.beans.factory.annotation.Autowired;
40import org.springframework.dao.DataAccessException;
41import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
42
43/**
44 * Created on : Jun 14, 2013, 3:27:04 PM
45 *
46 * @author Peter Withers <peter.withers@mpi.nl>
47 */
48public class JdbcNotebookDao extends JdbcResourceDao implements NotebookDao {
49
50    @Autowired
51    private AnnotationDao jdbcAnnotationDao;
52
53    public JdbcNotebookDao(DataSource dataSource) {
54        setDataSource(dataSource);       
55        internalIdName = notebook_id;
56        resourceTableName = notebookTableName;
57    }
58
59    @Override
60    public List<NotebookInfo> getNotebookInfos(UUID  userID) {
61        String sql = "SELECT " + notebookTitle + ", " + notebookExternal_id + " FROM " + notebookTableName + ", " + principalTableName + " where " + principalPrincipal_id + " = " + notebookOwner_id + " and " + principalExternal_id + " = ?";
62        return getSimpleJdbcTemplate().query(sql, notebookInfoRowMapper, userID.toString());
63    }
64
65    @Override
66    public List<Notebook> getUsersNotebooks(UUID userID) {
67        String sql = "SELECT " + notebookStar + " FROM " + notebookTableName + ", " + principalTableName + " where " + principal_id + " = " + owner_id + " and " + principalExternal_id + " = ?";
68        return getSimpleJdbcTemplate().query(sql, notebookRowMapper, userID.toString());
69    }
70
71    @Override
72    public UUID addNotebook(UUID userID, String title) {
73        try {
74            final UUID externalIdentifier = UUID.randomUUID();
75            String sql = "INSERT INTO " + notebookTableName + " (" + external_id + ", " + this.title + "," + owner_id + ") VALUES (:notebookId, :title, (SELECT " + principal_id + " FROM " + principalTableName + " WHERE " + principalExternal_id + " = :userID))";
76            Map<String, Object> params = new HashMap<String, Object>();
77            params.put("notebookId", externalIdentifier.toString());
78            params.put("userID", userID.toString());
79            params.put("title", title);
80            final int updatedRowCount = getSimpleJdbcTemplate().update(sql, params);
81            return externalIdentifier;
82        } catch (DataAccessException exception) {
83            throw exception;
84        }
85    }
86    private final RowMapper<NotebookInfo> notebookInfoRowMapper = new RowMapper<NotebookInfo>() {
87        @Override
88        public NotebookInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
89            NotebookInfo notebookInfo = new NotebookInfo();
90            notebookInfo.setRef(rs.getString(external_id)); // todo: what is ref? should it be the external id? Olha: "yes"
91            notebookInfo.setTitle(rs.getString(title));
92//            notebookInfo.setRef(rs.getString("URI"));
93            return notebookInfo;
94        }
95    };
96    private final RowMapper<Notebook> notebookRowMapper = new RowMapper<Notebook>() {
97        @Override
98        public Notebook mapRow(ResultSet rs, int rowNumber) throws SQLException {
99            Notebook notebook = new Notebook();
100//          notebook.setId(rs.getInt("notebook_id"));
101            notebook.setTitle(rs.getString(title));
102            GregorianCalendar calendar = new GregorianCalendar();
103            calendar.setTime(rs.getTimestamp(time_stamp));
104            try {
105                XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
106                notebook.setTimeStamp(gregorianCalendar);
107            } catch (DatatypeConfigurationException exception) {
108                throw new SQLException(exception);
109            }
110//            notebook.setURI(rs.getString("URI_ID"));
111            notebook.setAnnotations(getAnnotations(rs.getInt(notebook_id)));
112            return notebook;
113        }
114    };
115
116    // returns the number of affected annotations
117    @Override
118    public int deleteNotebook(UUID notebookId) {
119        String sql1 = "DELETE FROM " + notebooksAnnotationsTableName + " where " + notebook_id + "= (SELECT " + notebook_id + " FROM " + notebookTableName + " WHERE " + external_id + " = ?)";
120        String sql2 = "DELETE FROM notebook where external_id = ?";
121        int affectedAnnotations = getSimpleJdbcTemplate().update(sql1, notebookId.toString());
122        int affectedNotebooks = getSimpleJdbcTemplate().update(sql2, notebookId.toString());
123        return affectedAnnotations;
124    }
125
126    @Override
127    public int addAnnotation(UUID notebookId, UUID annotationId) {
128        try {
129            SimpleJdbcInsert notebookInsert = new SimpleJdbcInsert(getDataSource()).withTableName(notebooksAnnotationsTableName);
130            Map<String, Object> params = new HashMap<String, Object>();
131            params.put(notebook_id, notebookId);
132            params.put(annotation_id, annotationId);
133            int rowsAffected = notebookInsert.execute(params);
134            return rowsAffected;
135        } catch (DataAccessException exception) {
136            throw exception;
137        }
138    }
139
140    ////////////////////////////////////////////////////////////////////////
141    /**
142     *
143     * @param notebookID
144     * @return the list of annotation-ids belonging to the notebook with
145     * notebookId returns null if notebookId is null or is not in the DB TODO:
146     * do we need to return null here? using an additional check.
147     */
148    @Override
149    public List<Number> getAnnotationIDs(Number notebookID) {
150        StringBuilder sql = new StringBuilder("SELECT DISTINCT ");
151        sql.append(notebooksAnnotationsTableNameAnnotation_id).append("  FROM ").append(notebooksAnnotationsTableName).append(" where ").append(notebook_id).append(" = ?");
152        return getSimpleJdbcTemplate().query(sql.toString(), annotationIDRowMapper, notebookID);
153    }
154    private final RowMapper<Number> annotationIDRowMapper = new RowMapper<Number>() {
155        @Override
156        public Integer mapRow(ResultSet rs, int rowNumber) throws SQLException {
157            Integer annotationId = rs.getInt("annotation_id");
158            return annotationId;
159        }
160    };
161
162    //////////////////////////////////////////////////
163    /**
164     *
165     * @param notebookID
166     * @return the list of annotation-infos of the annotations from notebookID;
167     * if notebook not in the DB or null returns null if the notebook contains
168     * no annotations returns an empty list
169     *
170     *
171     * @Override public List<AnnotationInfo> getAnnotationInfosOfNotebook(Number
172     * notebookID) { return
173     * jdbcAnnotationDao.getAnnotationInfos(getAnnotationIDs(notebookID)); }
174     */
175    //////////////////////////////////////////////
176    /**
177     *
178     * @param notebookID
179     * @return the list of annotation References from the notebookID returns
180     * null if notebookID == null or it does not exists in the DB
181     */
182    @Override
183    public List<ResourceREF> getAnnotationREFsOfNotebook(Number notebookID) {
184        return jdbcAnnotationDao.getAnnotationREFs(getAnnotationIDs(notebookID));
185    }
186
187    ////////////////////////////////////////////////////////////////////////////
188    /**
189     *
190     * @param notebookID
191     * @return the Annotations (as a list of references) from the notebookID *
192     * returns null if notebookID == null, or it does not exists in th DB, or
193     * the list of annotations is empty, or something wrong happened when
194     * extracting annotations from the notebook (according to dasish.xsd if an
195     * Annotation is created then its list of annotations must contain at least
196     * one element!)
197     *
198     */
199    @Override
200    public Annotations getAnnotations(Number notebookID) {
201        if (notebookID == null) {
202            return null;
203        }
204        Annotations result = new Annotations();
205        List<ResourceREF> annotREFs = result.getAnnotation();
206        boolean test = annotREFs.addAll(getAnnotationREFsOfNotebook(notebookID));
207        return (test ? result : null);
208
209    }
210
211    ///////////////////////////////////////////////////
212    // REUSES notebookInfoRowMapper
213    @Override
214    public NotebookInfo getNotebookInfo(Number notebookID) {
215        if (notebookID == null) {
216            return null;
217        }
218        String sql = "SELECT  " + notebookExternal_id + "," + notebookTitle + " FROM " + notebookTableName + " where " + notebook_id + " = ?";
219        List<NotebookInfo> result = getSimpleJdbcTemplate().query(sql, notebookInfoRowMapper, notebookID.toString());
220        if (result == null) {
221            return null;
222        }
223        if (result.isEmpty()) {
224            return null;
225        }
226        return result.get(0);
227    }
228
229   
230    //////////////////////////////////////////////////////////////////
231    @Override
232    public List<UUID> getAnnotationExternalIDs(UUID notebookId) {
233        List<Number> internalIds = getAnnotationIDs(getInternalID(notebookId));
234        if (internalIds == null) {
235            return null;
236        }
237        List<UUID> annotationIds = new ArrayList<UUID>();
238        for (Number internalId : internalIds) {
239            annotationIds.add(jdbcAnnotationDao.getExternalID(internalId));
240        }
241        return annotationIds;
242    }
243
244    ////////////////////////////////////////////////////////////
245    @Override
246    public int removeAnnotation(Number annotationID) {
247        String sqlNotebooks = "DELETE FROM " + notebooksAnnotationsTableName + " where " + annotation_id + " = ?";
248        int affectedNotebooks = getSimpleJdbcTemplate().update(sqlNotebooks, annotationID);
249        return affectedNotebooks;
250    }
251}
Note: See TracBrowser for help on using the repository browser.