Changeset 1633 for ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java
- Timestamp:
- 11/30/11 13:18:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java
r1631 r1633 5 5 package clarin.cmdi.componentregistry.impl.database; 6 6 7 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;7 import clarin.cmdi.componentregistry.model.Comment; 8 8 9 9 import java.util.Date; … … 17 17 import org.slf4j.Logger; 18 18 import org.slf4j.LoggerFactory; 19 import org.springframework.beans.factory.annotation.Autowired; 19 20 import org.springframework.dao.DataAccessException; 20 21 import org.springframework.jdbc.core.simple.ParameterizedRowMapper; 22 import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper; 21 23 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 24 import org.springframework.transaction.PlatformTransactionManager; 25 import org.springframework.transaction.TransactionDefinition; 26 import org.springframework.transaction.TransactionStatus; 22 27 23 28 /** … … 26 31 */ 27 32 public class CommentsDao extends ComponentRegistryDao<Comment> { 33 34 @Autowired 35 private PlatformTransactionManager txManager; 36 @Autowired 37 private TransactionDefinition txDefinition; 28 38 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 private final static String SELECT_BASE = "SELECT " + COLUMN_ID + ", comments, comment_date, user_id, " 40 + "component_description_id, profile_description_id FROM " + TABLE_COMMENTS; 41 42 protected String getTableName() { 39 43 return TABLE_COMMENTS; 40 44 } 41 45 42 46 public List<Comment> getAllComments() throws DataAccessException { 43 return getList(SELECT_BASE); 44 } 45 46 47 48 47 return getList(SELECT_BASE); 48 } 49 49 50 /** 50 51 * … … 67 68 68 69 public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException { 69 String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId;70 return getFirstOrNull( select, commentId);70 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; 71 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 71 72 } 72 73 … … 77 78 */ 78 79 public Comment getSpecifiedCommentFromComponent(String commentId) throws DataAccessException { 79 String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId;80 return getFirstOrNull( select, commentId);80 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; 81 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 81 82 } 82 83 83 84 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); 85 return getList(SELECT_BASE + " WHERE component_description_id = ?", componentId); 86 } 87 88 public Comment getByComment(String aComment) throws DataAccessException { 89 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 90 90 } 91 91 … … 96 96 * @return Principal name of description's owner, if any. Otherwise, null. 97 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 //}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 110 /** 111 111 * … … 121 121 params.put("comments", comment.getComment()); 122 122 params.put("comment_date", extractTimestamp(comment)); 123 params.put("component_description ", comment.getComponentDescriptionId());124 params.put("profile_description ", comment.getProfileDescriptionId());123 params.put("component_description_id", comment.getComponentDescriptionId()); 124 params.put("profile_description_id", comment.getProfileDescriptionId()); 125 125 params.put("user_id", comment.getUserId()); 126 126 return insert.executeAndReturnKey(params); … … 139 139 comment.setId(rs.getString(COLUMN_ID)); 140 140 comment.setComment(rs.getString("comments")); 141 comment.setComponentDescriptionId(rs.getString("component_description_id")); 142 comment.setProfileDescriptionId(rs.getString("profile_description_id")); 143 comment.setUserId(rs.getString("user_id")); 141 144 comment.setCommentDate(commentDate == null ? null : Comment.createNewDate(commentDate.getTime())); 142 145 return comment; … … 163 166 // throw new UnsupportedOperationException("Not yet implemented"); 164 167 // } 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 // } 168 169 public void deleteComment(Comment com, boolean isDeleted) throws DataAccessException { 170 TransactionStatus transaction = getTransaction(); 171 Number dbId = getDbId(Integer.parseInt(com.getId())); 172 StringBuilder delete = new StringBuilder("DELETE ").append(getTableName()); 173 delete.append(" WHERE " + COLUMN_ID + " = ?"); 174 getSimpleJdbcTemplate().update(delete.toString(), dbId); 175 txManager.commit(transaction); 176 } 177 178 public Number getDbId(Number cmdId) { 179 StringBuilder query = new StringBuilder("SELECT " + COLUMN_ID + " FROM ").append(getTableName()); 180 query.append(" WHERE ").append(getDbId(cmdId)).append(" = ?"); 181 return getSimpleJdbcTemplate().queryForInt(query.toString(), cmdId); 182 } 183 184 private Timestamp extractTimestamp(Comment comment) { 185 if (comment.getCommentDate() != null) { 186 try { 187 Date date = Comment.getDate(comment.getCommentDate()); 188 return new Timestamp(date.getTime()); 189 } catch (ParseException ex) { 190 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to date", ex); 191 } catch (IllegalArgumentException ex) { 192 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to timestamp", ex); 193 } 194 } 195 return null; 196 } 197 198 private TransactionStatus getTransaction() { 199 return txManager.getTransaction(txDefinition); 200 } 197 201 }
Note: See TracChangeset
for help on using the changeset viewer.