1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.impl.database; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.model.CommentMapping.Comment; |
---|
8 | |
---|
9 | import java.util.Date; |
---|
10 | import java.sql.ResultSet; |
---|
11 | import java.sql.SQLException; |
---|
12 | import java.sql.Timestamp; |
---|
13 | import java.text.ParseException; |
---|
14 | import java.util.HashMap; |
---|
15 | import java.util.List; |
---|
16 | import java.util.Map; |
---|
17 | import org.slf4j.Logger; |
---|
18 | import org.slf4j.LoggerFactory; |
---|
19 | import org.springframework.dao.DataAccessException; |
---|
20 | import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
---|
21 | import org.springframework.jdbc.core.simple.SimpleJdbcInsert; |
---|
22 | |
---|
23 | /** |
---|
24 | * |
---|
25 | * @author jeafer |
---|
26 | */ |
---|
27 | public class CommentsDao extends ComponentRegistryDao<Comment> { |
---|
28 | private final static Logger LOG = LoggerFactory.getLogger(CommentsDao.class); |
---|
29 | private final static String SELECT_BASE = "SELECT " + COLUMN_ID + ", comments, comment_date, user_id FROM " + TABLE_COMMENTS; |
---|
30 | |
---|
31 | // @Autowired |
---|
32 | // private PlatformTransactionManager txManager; |
---|
33 | // @Autowired |
---|
34 | // private TransactionDefinition txDefinition; |
---|
35 | // public List<Comment> getAllComments() throws DataAccessException { |
---|
36 | // return getList(SELECT_BASE); |
---|
37 | // } |
---|
38 | protected String getTableName() { |
---|
39 | return TABLE_COMMENTS; |
---|
40 | } |
---|
41 | |
---|
42 | public List<Comment> getAllComments() throws DataAccessException { |
---|
43 | return getList(SELECT_BASE); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * |
---|
51 | * @param id Database record id (key) |
---|
52 | * @return Comment, if it exists |
---|
53 | * @throws DataAccessException |
---|
54 | */ |
---|
55 | public Comment getById(String id) throws DataAccessException { |
---|
56 | return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", id); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * @param id Database record id (key) |
---|
61 | * Method to retrieve comments from profiles |
---|
62 | * @return Comments, comments_date, user_id and id |
---|
63 | */ |
---|
64 | public List<Comment> getCommentsFromProfile(String profileId) throws DataAccessException { |
---|
65 | return getList(SELECT_BASE + " WHERE profile_description_id = ?", profileId); |
---|
66 | } |
---|
67 | |
---|
68 | public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException { |
---|
69 | String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId; |
---|
70 | return getFirstOrNull(select, commentId); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * @param id Database record id (key) |
---|
75 | * Method to retrieve comments from components |
---|
76 | * @return Comments, comments_date, user_id and id |
---|
77 | */ |
---|
78 | public Comment getSpecifiedCommentFromComponent(String commentId) throws DataAccessException { |
---|
79 | String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId; |
---|
80 | return getFirstOrNull(select, commentId); |
---|
81 | } |
---|
82 | |
---|
83 | public List<Comment> getCommentsFromComponent(String componentId) throws DataAccessException { |
---|
84 | return getList(SELECT_BASE + " WHERE component_description_id =" + componentId); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | public Comment getByComment(String aComment) throws DataAccessException { |
---|
89 | return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * |
---|
94 | * @param id |
---|
95 | * Id of description record |
---|
96 | * @return Principal name of description's owner, if any. Otherwise, null. |
---|
97 | */ |
---|
98 | // public String getOwnerPrincipalName(Number id) { |
---|
99 | // StringBuilder select = new StringBuilder("SELECT principal_name FROM " + TABLE_REGISTRY_USER); |
---|
100 | // select.append(" JOIN ").append(getTableName()); |
---|
101 | // select.append(" ON user_id = " + TABLE_REGISTRY_USER + ".id "); |
---|
102 | // select.append(" WHERE ").append(getTableName()).append(".id = ?"); |
---|
103 | // List<String> owner = getSimpleJdbcTemplate().query(select.toString(), new ParameterizedSingleColumnRowMapper<String>(), id); |
---|
104 | // if (owner.isEmpty()) { |
---|
105 | // return null; |
---|
106 | // } else { |
---|
107 | // return owner.get(0); |
---|
108 | // } |
---|
109 | // } |
---|
110 | /** |
---|
111 | * |
---|
112 | * @param comment |
---|
113 | * @return Record id of the inserted comment |
---|
114 | * @throws DataAccessException |
---|
115 | */ |
---|
116 | public Number insertComment(Comment comment) throws DataAccessException { |
---|
117 | SimpleJdbcInsert insert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_COMMENTS).usingGeneratedKeyColumns( |
---|
118 | COLUMN_ID); |
---|
119 | |
---|
120 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
121 | params.put("comments", comment.getComment()); |
---|
122 | params.put("comment_date", extractTimestamp(comment)); |
---|
123 | params.put("component_description", comment.getComponentDescriptionId()); |
---|
124 | params.put("profile_description", comment.getProfileDescriptionId()); |
---|
125 | params.put("user_id", comment.getUserId()); |
---|
126 | return insert.executeAndReturnKey(params); |
---|
127 | } |
---|
128 | |
---|
129 | @Override |
---|
130 | protected ParameterizedRowMapper<Comment> getRowMapper() { |
---|
131 | return rowMapper; |
---|
132 | } |
---|
133 | private final ParameterizedRowMapper<Comment> rowMapper = new ParameterizedRowMapper<Comment>() { |
---|
134 | |
---|
135 | @Override |
---|
136 | public Comment mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
137 | Comment comment = new Comment(); |
---|
138 | Timestamp commentDate = rs.getTimestamp("comment_date"); |
---|
139 | comment.setId(rs.getString(COLUMN_ID)); |
---|
140 | comment.setComment(rs.getString("comments")); |
---|
141 | comment.setCommentDate(commentDate == null ? null : Comment.createNewDate(commentDate.getTime())); |
---|
142 | return comment; |
---|
143 | } |
---|
144 | }; |
---|
145 | // protected String getTableName() { |
---|
146 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
147 | // } |
---|
148 | // |
---|
149 | // |
---|
150 | // protected String getCMDIdColumn() { |
---|
151 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
152 | // } |
---|
153 | // |
---|
154 | // String getContent(boolean b, String id) { |
---|
155 | // throw new UnsupportedOperationException("Not yet implemented"); |
---|
156 | // } |
---|
157 | // |
---|
158 | // boolean isPublic(String cmdId) { |
---|
159 | // throw new UnsupportedOperationException("Not yet implemented"); |
---|
160 | // } |
---|
161 | // |
---|
162 | // boolean isInUserSpace(String cmdId, Number userId) { |
---|
163 | // throw new UnsupportedOperationException("Not yet implemented"); |
---|
164 | // } |
---|
165 | // void setDeleted(Comment desc, boolean isDeleted) throws DataAccessException { |
---|
166 | // TransactionStatus transaction = getTransaction(); |
---|
167 | // Number dbId = getDbId(desc.getId()); |
---|
168 | // StringBuilder update = new StringBuilder("UPDATE ").append(getTableName()); |
---|
169 | // update.append(" SET is_deleted = ").append(Boolean.toString(isDeleted)).append(" WHERE " + COLUMN_ID + " = ?"); |
---|
170 | // getSimpleJdbcTemplate().update(update.toString(), dbId); |
---|
171 | // txManager.commit(transaction); |
---|
172 | // } |
---|
173 | // |
---|
174 | public Number getDbId(Number cmdId) { |
---|
175 | StringBuilder query = new StringBuilder("SELECT " + COLUMN_ID + " FROM ").append(getTableName()); |
---|
176 | query.append(" WHERE ").append(getDbId(cmdId)).append(" = ?"); |
---|
177 | return getSimpleJdbcTemplate().queryForInt(query.toString(), cmdId); |
---|
178 | } |
---|
179 | |
---|
180 | private Timestamp extractTimestamp(Comment comment) { |
---|
181 | if (comment.getCommentDate() != null) { |
---|
182 | try { |
---|
183 | Date date = Comment.getDate(comment.getCommentDate()); |
---|
184 | return new Timestamp(date.getTime()); |
---|
185 | } catch (ParseException ex) { |
---|
186 | LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to date", ex); |
---|
187 | } catch (IllegalArgumentException ex) { |
---|
188 | LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to timestamp", ex); |
---|
189 | } |
---|
190 | } |
---|
191 | return null; |
---|
192 | } |
---|
193 | // |
---|
194 | // private TransactionStatus getTransaction() { |
---|
195 | // return txManager.getTransaction(txDefinition); |
---|
196 | // } |
---|
197 | } |
---|