Ignore:
Timestamp:
11/30/11 13:18:31 (13 years ago)
Author:
jeafer
Message:

Jean-Charles branch commit2,
Changes regarding comment on the ComponentRegistry service.
Delete_comment from RestService? to Database access (CommentsDao?) implemented.
Post_comment from RestService? to Database access (CommentsDao?) implemented. (Not yet tested)
Comment class updated
TestComment? class updated
DataBase? : Table comment updated, Corrections of fkey references
Validate classes implemented to validate comment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java

    r1631 r1633  
    55package clarin.cmdi.componentregistry.impl.database;
    66
    7 import clarin.cmdi.componentregistry.model.CommentMapping.Comment;
     7import clarin.cmdi.componentregistry.model.Comment;
    88
    99import java.util.Date;
     
    1717import org.slf4j.Logger;
    1818import org.slf4j.LoggerFactory;
     19import org.springframework.beans.factory.annotation.Autowired;
    1920import org.springframework.dao.DataAccessException;
    2021import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
     22import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
    2123import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
     24import org.springframework.transaction.PlatformTransactionManager;
     25import org.springframework.transaction.TransactionDefinition;
     26import org.springframework.transaction.TransactionStatus;
    2227
    2328/**
     
    2631 */
    2732public class CommentsDao extends ComponentRegistryDao<Comment> {
     33
     34    @Autowired
     35    private PlatformTransactionManager txManager;
     36    @Autowired
     37    private TransactionDefinition txDefinition;
    2838    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() {
    3943        return TABLE_COMMENTS;
    4044    }
    41        
     45
    4246    public List<Comment> getAllComments() throws DataAccessException {
    43         return getList(SELECT_BASE);
    44     }
    45    
    46    
    47    
    48    
     47        return getList(SELECT_BASE);
     48    }
     49
    4950    /**
    5051     *
     
    6768
    6869    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));
    7172    }
    7273
     
    7778     */
    7879    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));
    8182    }
    8283
    8384    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);
    9090    }
    9191
     
    9696     * @return Principal name of description's owner, if any. Otherwise, null.
    9797     */
    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    }
    110110    /**
    111111     *
     
    121121        params.put("comments", comment.getComment());
    122122        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());
    125125        params.put("user_id", comment.getUserId());
    126126        return insert.executeAndReturnKey(params);
     
    139139            comment.setId(rs.getString(COLUMN_ID));
    140140            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"));
    141144            comment.setCommentDate(commentDate == null ? null : Comment.createNewDate(commentDate.getTime()));
    142145            return comment;
     
    163166//        throw new UnsupportedOperationException("Not yet implemented");
    164167//    }
    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    }
    197201}
Note: See TracChangeset for help on using the changeset viewer.