Ignore:
Timestamp:
07/19/13 09:56:04 (11 years ago)
Author:
olhsha
Message:

completed Peter's work on refactroing sql requests vi using constants for field and table names. Now, if a table of field name is changed, you need to update the code only in one place: the corresponding string constant in JdbcReourceDao?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r3154 r3167  
    4343public class JdbcAnnotationDao extends JdbcResourceDao implements AnnotationDao {
    4444
     45   
    4546    public JdbcAnnotationDao(DataSource dataSource) {
    4647        setDataSource(dataSource);
     
    7172               
    7273        String values = makeListOfValues(annotationIDs);
    73         String sql = "SELECT annotation.* FROM annotation WHERE annotation.annotation_id  IN "+values;
     74        String sql = "SELECT "+annotationStar+" FROM "+annotationTableName +" WHERE "+annotationAnnotation_id+"  IN "+values;
    7475        return getSimpleJdbcTemplate().query(sql, annotationInfoRowMapper);
    7576    }
     
    7980        public AnnotationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
    8081           AnnotationInfo annotationInfo = new AnnotationInfo();
    81            annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt("owner_id"))));
    82            annotationInfo.setHeadline(rs.getString("headline"));
    83            annotationInfo.setTargetSources(getSources(rs.getString("body_xml")));
     82           annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt(owner_id))));
     83           annotationInfo.setHeadline(rs.getString(headline));
     84           annotationInfo.setTargetSources(getSources(rs.getString(body_xml)));
    8485           return annotationInfo;
    8586        }
     
    109110       
    110111        String values = makeListOfValues(annotationIDs);
    111         String sql = "SELECT annotation.annotation_id FROM annotation WHERE annotation.annotation_id  IN "+values;
     112        String sql = "SELECT "+annotationAnnotation_id+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id+"  IN "+values;
    112113        return getSimpleJdbcTemplate().query(sql, annotationREFRowMapper);
    113114    }
     
    117118        public ResourceREF mapRow(ResultSet rs, int rowNumber) throws SQLException {
    118119           ResourceREF annotationREF = new ResourceREF();
    119            annotationREF.setRef(Integer.toString(rs.getInt("annotation_id")));
     120           annotationREF.setRef(Integer.toString(rs.getInt(annotation_id)));
    120121           return annotationREF;
    121122        }
     
    136137            return null;
    137138        }
    138        String sql = "SELECT * FROM annotation WHERE annotation.annotation_id  = ?";
     139       String sql = "SELECT "+annotationStar+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id  +"= ?";
    139140       List<Annotation> result= getSimpleJdbcTemplate().query(sql, annotationRowMapper, annotationID);
    140141       
     
    147148       
    148149        if (result.size()>1) {
    149            throw new SQLException("There are "+result.size()+" annotations with annotation_id "+annotationID);
     150           throw new SQLException("There are "+result.size()+" annotations with "+ annotation_id + " "+annotationID);
    150151       }
    151152       return result.get(0);
     
    156157        public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException {
    157158           Annotation result = new Annotation();
    158            result.setHeadline(rs.getString("headline"));
     159           result.setHeadline(rs.getString(headline));
    159160           
    160161           ResourceREF ownerREF = new ResourceREF();
    161            ownerREF.setRef(String.valueOf(rs.getInt("owner_id")));
     162           ownerREF.setRef(String.valueOf(rs.getInt(owner_id)));
    162163           result.setOwner(ownerREF);
    163164           
     
    173174           // TODO add external reference
    174175           
    175            result.setBody(convertToAnnotationBody(rs.getString("body_xml")));
     176           result.setBody(convertToAnnotationBody(rs.getString(body_xml)));
    176177           return result;
    177178        }
     
    198199        }
    199200       
    200        String sql = "SELECT annotation.annotation_id FROM annotation WHERE annotation.external_id  = ?";
     201       String sql = "SELECT "+annotationAnnotation_id+" FROM "+annotationTableName+" WHERE "+annotationExternal_id+"  = ?";
    201202       List<Number> result= getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, externalID.toString());
    202203       if (result == null) {
     
    208209       
    209210       if (result.size()>1) {
    210            throw new SQLException("There are "+result.size()+" annotations with external_id "+externalID);
     211           throw new SQLException("There are "+result.size()+" annotations with"+ external_id +" "+externalID);
    211212       }
    212213       return result.get(0);
     
    216217        @Override
    217218        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    218            Number result = rs.getInt("annotation_id");
     219           Number result = rs.getInt(annotation_id);
    219220           return result;
    220221        }
    221222    };
     223     
     224     
     225     public int deleteNotebook(Number annotationId) throws SQLException{
     226        String sqlAnnotation = "DELETE FROM " + annotationTableName + " where "+annotation_id + " = ?";
     227        //String sqSources = "DELETE FROM " + sourceTableName + " where "+ notebook_id +"= ?";
     228        int affectedAnnotations = getSimpleJdbcTemplate().update(sqlAnnotation, annotationId);
     229        if (affectedAnnotations>1) {
     230            throw new SQLException("There was more than one annotation ("+affectedAnnotations+") with the same ID "+annotationId);
     231        }
     232        return affectedAnnotations;
     233        //TODO implement deleting sources (see the specification document and the interfaces' javadoc
     234    }
    222235     
    223236   
Note: See TracChangeset for help on using the changeset viewer.