Ignore:
Timestamp:
08/14/13 16:34:29 (11 years ago)
Author:
olhsha
Message:

"put updated body" is implemented and tested. Big refactring: simplifying DAO's and pushing their composition to rest methods. add-methods in DAO return now not the classes but internalID-s of the added resources. Still 2 test errors and 2 test failures.

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/JdbcSourceDao.java

    r3372 r3380  
    6161    }
    6262
    63     @Override
    64     public List<Number> retrieveSourceIDs(Number annotationID) {
    65         String sql = "SELECT " + source_id + " FROM " + annotationsSourcesTableName + " WHERE " + annotation_id + "= ?";
    66         List<Number> result = getSimpleJdbcTemplate().query(sql, annotationSourceRowMapper, annotationID);       
    67         return result;
    68     }
    69     private final RowMapper<Number> annotationSourceRowMapper = new RowMapper<Number>() {
    70         @Override
    71         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    72             Number result = rs.getInt(source_id);
    73             return result;
    74         }
    75     };
    76 
    77     ///////////////////////////////////////////////////////////////////////////////
     63        ///////////////////////////////////////////////////////////////////////////////
    7864    @Override
    7965    public Source getSource(Number internalID) {
    8066        String sql = "SELECT " + sourceStar + "FROM " + sourceTableName + " WHERE " + source_id + " = ?";
    81         List<Source> result = getSimpleJdbcTemplate().query(sql, SourceRowMapper, internalID);
     67        List<Source> result = getSimpleJdbcTemplate().query(sql, sourceRowMapper, internalID);
    8268        return result.get(0);
    8369    }
    84     private final RowMapper<Source> SourceRowMapper = new RowMapper<Source>() {
     70    private final RowMapper<Source> sourceRowMapper = new RowMapper<Source>() {
    8571        @Override
    8672        public Source mapRow(ResultSet rs, int rowNumber) throws SQLException {
     
    9783        }
    9884    };
     85   
     86        ///////////////////////////////////////////////////////////////////////////////
     87    @Override
     88    public Map<String, Object> getRawSource(Number internalID) {
     89        String sql = "SELECT " + sourceStar + "FROM " + sourceTableName + " WHERE " + source_id + " = ?";
     90        List<Map<String, Object>> result = getSimpleJdbcTemplate().query(sql, rawSourceRowMapper, internalID);
     91        return result.get(0);
     92    }
     93    private final RowMapper<Map<String, Object>> rawSourceRowMapper = new RowMapper<Map<String, Object>>() {
     94        @Override
     95        public Map<String, Object> mapRow(ResultSet rs, int rowNumber) throws SQLException {
     96           Map<String, Object> result = new HashMap<String, Object>();
     97           result.put(external_id, rs.getString(external_id));
     98           result.put(link_uri, rs.getString(link_uri));
     99           result.put(version_id, rs.getInt(version_id));
     100           return result;
     101        }
     102    };
    99103
    100104    ///////////////////////////////////////////////////////////////////
     
    140144    ///////////////////////////////////////////////////////////////////
    141145    @Override
    142     public Source addSource(Source freshSource) throws SQLException{
    143 
     146    public Number addSource(Source freshSource) throws SQLException{
     147     
    144148        SourceIdentifier externalIdentifier = new SourceIdentifier();
     149        Number versionID = versionDao.getInternalID(new VersionIdentifier(freshSource.getVersion()));
     150       
     151        if (versionID == null) {
     152            System.out.println("Cannot add source because there is no version for it, and no cached representation. Create them and try again.");
     153            return -1;
     154        }
    145155
    146156        Map<String, Object> params = new HashMap<String, Object>();
    147157        params.put("externalId", externalIdentifier.toString());
    148158        params.put("linkUri", freshSource.getLink());
    149         params.put("versionId", freshSource.getVersion());
     159        params.put("versionId", versionID);
    150160        String sql = "INSERT INTO " + sourceTableName + "(" + external_id + "," + link_uri + "," + version_id + " ) VALUES (:externalId, :linkUri,  :versionId)";
    151161        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
    152 
     162       
     163       
    153164        Map<String, Object> paramsJoint = new HashMap<String, Object>();
    154165        paramsJoint.put("sourceId", getInternalID(externalIdentifier));
    155         paramsJoint.put("versionId", freshSource.getVersion());
    156         String sqlSourceVersion = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id + "," + version_id + " ) VALUES (:sourceId, :versionId)";
    157         int affectedRowsJoint = getSimpleJdbcTemplate().update(sqlSourceVersion, paramsJoint);
    158        
    159         if (affectedRows == 1 && affectedRowsJoint == 1) {
    160             Source result = makeFreshCopy(freshSource);
    161             result.setURI(externalIdentifier.toString());
    162            
    163             //retrieve taime stamp for the just added annotation
    164             XMLGregorianCalendar timeStamp =this.retrieveTimeStamp(getInternalID(new SourceIdentifier(externalIdentifier.toString())));
    165             result.setTimeSatmp(timeStamp);
    166            
    167             return result;
    168         } else {
    169             throw new SQLException("Cannot add the source");
    170         }
    171          
     166        paramsJoint.put("versionId", versionID);
     167        String sqlJoint = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id +"," + version_id + " ) VALUES (:sourceId, :versionId)";
     168        final int affectedJointRows = getSimpleJdbcTemplate().update(sqlJoint, paramsJoint);
     169       
     170       
     171        return(getInternalID(externalIdentifier));
    172172    }
    173173
     
    175175    ///////////////////////////////////////////////////////////////////
    176176    @Override
    177     public List<SourceInfo> getSourceInfos(Number annotationID) {
    178         List<Number> sources = retrieveSourceIDs(annotationID);
     177    public List<SourceInfo> getSourceInfos(List<Number> sources) {
    179178        if (sources == null) {
    180179            return null;
     
    228227     ////////////////////////////////////////////////////////////////////////
    229228    @Override
    230     public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {
    231 
    232         Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> result = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
     229    public Map<String, String> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {
     230
     231        Map<String, String> result = new HashMap<String, String>();
    233232       
    234233        for (NewOrExistingSourceInfo noeSourceInfo : sources) {
    235234            SourceInfo sourceInfo = noeSourceInfo.getSource();
    236235            if (sourceInfo != null) {
    237                 // this is an old source, already exists in the DB
    238                 result.put(noeSourceInfo, noeSourceInfo);               
     236                // this is an old source, already exists in the DB             
    239237                addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(sourceInfo.getRef())));
    240238            } else {
    241239                Source newSource = constructNewSource(noeSourceInfo.getNewSource());
    242                 Source addedSource = addSource(newSource);
    243                 int affectedRows = addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(addedSource.getURI())));
    244                
    245                 //  create updated source info
    246                 SourceInfo updatedSourceInfo = new SourceInfo();
    247                 updatedSourceInfo.setLink(addedSource.getLink());
    248                 updatedSourceInfo.setRef(addedSource.getURI());
    249                 updatedSourceInfo.setVersion(addedSource.getVersion());
    250 
    251                 NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo();
    252                 updatedInfo.setSource(updatedSourceInfo);
    253                 result.put(noeSourceInfo, updatedInfo);
     240                Number addedSourceID = addSource(newSource);// adding new source
     241                int affectedRows = addAnnotationSourcePair(annotationID, addedSourceID);             
     242                result.put(noeSourceInfo.getNewSource().getId(), getExternalID(addedSourceID).toString());
    254243            }
    255244           
Note: See TracChangeset for help on using the changeset viewer.