Changeset 3298


Ignore:
Timestamp:
08/07/13 12:56:03 (11 years ago)
Author:
olhsha
Message:

source Dao is implemented: get, retrieve lists, delete, add

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
9 edited

Legend:

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

    r3297 r3298  
    6666   
    6767   
    68     /**
    69      *
    70      * @return the list of all the internal id-s of the cached representations.
    71      */
    72     public List<Number> cachedRepresentationIDs();
    73    
     68 
    7469 
    7570}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/SourceDao.java

    r3297 r3298  
    6868   
    6969   
    70     /**
    71      *
    72      * @return the list of all internal sourceIDs
    73      */
    74     public List<Number> sourceIDs();
    75    
    76     /**
    77      * removes  all the sources from the DAb which are  not target sources of some annotations
    78      * @return
    79      */
    80     public int purgeAll();
    81    
     70   
    8271    //////////////////////////////////////////////
    8372   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/VersionDao.java

    r3297 r3298  
    6969   
    7070
    71    
    72     /**
    73      *
    74      * @return the list of all the internal-ids of all the versions
    75      */
    76     public List<Number> versionIDs();
     71   
    7772   
    7873}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDao.java

    r3297 r3298  
    120120       
    121121        if (result.isEmpty()) {
    122             // remove the cached representation safely
     122            // rou can remove the cached representation
    123123            String sql = "DELETE FROM " + cachedRepresentationTableName + " where " + cached_representation_id + " = ?";
    124124            return getSimpleJdbcTemplate().update(sql, internalID);
     
    137137    };
    138138
    139     //////////////////////////////////////////////////////////////////////////////////////////////
    140     @Override
    141     public List<Number> cachedRepresentationIDs() {
    142         String sqlCheck = "SELECT " + cached_representation_id + " FROM " + cachedRepresentationTableName;
    143         List<Number> result = getSimpleJdbcTemplate().query(sqlCheck, cachedRepresentationRunnerRowMapper);
    144         return result;
    145     }
    146     private final RowMapper<Number> cachedRepresentationRunnerRowMapper = new RowMapper<Number>() {
    147         @Override
    148         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    149             Number result = rs.getInt(cached_representation_id);
    150             return result;
    151         }
    152     };
    153 
     139   
    154140   
    155141
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java

    r3297 r3298  
    5959    final static protected String time_stamp = "time_stamp";
    6060    final static protected String permission = "permission_";
    61     //final static protected String target_source_id = "target_source_id";
    62     final static protected String link = "link";
     61    final static protected String link_uri = "link_uri";
    6362    final static protected String version = "version";
    6463    final static protected String cached_representation_id = "cached_representation_id";
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java

    r3297 r3298  
    1919
    2020import eu.dasish.annotation.backend.dao.SourceDao;
     21import eu.dasish.annotation.backend.dao.VersionDao;
    2122import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
    2223import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
     
    2728import java.sql.SQLException;
    2829import java.util.ArrayList;
     30import java.util.HashMap;
    2931import java.util.List;
     32import java.util.Map;
    3033import javax.sql.DataSource;
    3134import javax.xml.datatype.DatatypeConfigurationException;
    3235import javax.xml.datatype.DatatypeFactory;
    3336import javax.xml.datatype.XMLGregorianCalendar;
     37import org.springframework.beans.factory.annotation.Autowired;
    3438import org.springframework.jdbc.core.RowMapper;
    3539
     
    3943 */
    4044public class JdbcSourceDao extends JdbcResourceDao implements SourceDao{
     45   
     46    @Autowired
     47    VersionDao versionDao;
    4148   
    4249     public JdbcSourceDao(DataSource dataSource) {
     
    7582     };
    7683   
     84    ///////////////////////////////////////////////////////////////////////////////
     85    @Override
    7786    public Source getSource(Number internalID) {
    7887       String sql = "SELECT "+sourceStar+"FROM "+sourceTableName+" WHERE "+source_id  +" = ?";
     
    8594        public Source mapRow(ResultSet rs, int rowNumber) throws SQLException {           
    8695            try {
    87                 Source result = constructSource(new SourceIdentifier(rs.getString(external_id)), rs.getString(link), rs.getString(version), rs.getString(time_stamp));
     96                Source result = constructSource(new SourceIdentifier(rs.getString(external_id)), rs.getString(link_uri), rs.getString(version), rs.getString(time_stamp));
    8897                return result;
    8998            }
     
    96105    };
    97106   
    98    
     107     ///////////////////////////////////////////////////////////////////
     108    @Override
    99109    public int deleteSource(Number internalID){
    100         String sql = "DELETE FROM " + sourceTableName + " where " + source_id + " = ?";
    101         return (getSimpleJdbcTemplate().update(sql, internalID));
    102     }
    103    
    104    
     110       
     111        // check if there are annotations referring to the source with "internalID", in the table "annotations_sources"
     112        String sqlAnnotationsSources = "SELECT " + annotation_id + " FROM " + annotationsSourcesTableName + " WHERE " + source_id + "= ?";
     113        List<Number> resultAnnotationsSources = getSimpleJdbcTemplate().query(sqlAnnotationsSources, annotationsSourcesRowMapper, internalID);
     114       
     115        if (resultAnnotationsSources.isEmpty()) {
     116
     117            // You can remove the source!
     118
     119            // retrieve the list of versions of the source to be deleted
     120            List<Number> versions = versionDao.retrieveVersionList(internalID);
     121
     122            // remove all the pairs (internalID, version_id) from the joint table       
     123            String sqlSourcesVersions = "DELETE FROM " + sourcesVersionsTableName + " where " + source_id + " = ?";
     124            int affected_sources_versions_rows = getSimpleJdbcTemplate().update(sqlSourcesVersions, internalID);
     125
     126            // the main action: remove the source with internalID from "source" table
     127            String sql = "DELETE FROM " + sourceTableName + " where " + source_id + " = ?";
     128            int affected_source_rows = getSimpleJdbcTemplate().update(sql, internalID);
     129
     130            // remove the versions of "versions" from the DB unless they are still mentioned in "sources_versions"
     131            for (Number versionID : versions) {
     132                versionDao.deleteVersion(versionID);
     133            }
     134
     135            return (affected_source_rows);
     136        } else {
     137            // do not remove
     138            return 0;
     139        }
     140    }
     141   
     142    private final RowMapper<Number> annotationsSourcesRowMapper = new RowMapper<Number>() {
     143        @Override
     144        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     145            Number result = rs.getInt(annotation_id);
     146            return result;
     147        }
     148    };
     149   
     150   
     151     ///////////////////////////////////////////////////////////////////
     152    @Override
    105153    public Source addSource(Source freshSource){
    106         return null;
    107     }
    108    
    109    
    110     public List<Number> sourceIDs(){
    111         return null;
    112     }
    113    
    114    
    115     public int purgeAll(){
    116         return -1;
    117     }
     154       
     155        SourceIdentifier externalIdentifier = new SourceIdentifier();
     156
     157        Map<String, Object> params = new HashMap<String, Object>();
     158        params.put("externalId", externalIdentifier.toString());
     159        params.put("linkUri", freshSource.getLink());
     160        params.put("versionId", freshSource.getVersion());
     161        String sql = "INSERT INTO " + sourceTableName + "(" + external_id + "," + link_uri + "," + version_id + " ) VALUES (:externalId, :linkUri,  :versionId)";
     162        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
     163
     164        Map<String, Object> paramsJoint = new HashMap<String, Object>();
     165        paramsJoint.put("sourceId", getInternalID(externalIdentifier));
     166        paramsJoint.put("versionId", freshSource.getVersion());
     167        String sqlSourceVersion = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id + "," + version_id + " ) VALUES (:sourceId, :versionId)";
     168        int affectedRowsJoint = getSimpleJdbcTemplate().update(sqlSourceVersion, paramsJoint);
     169       
     170        Source result = makeFreshCopy(freshSource);
     171        result.setURI(externalIdentifier.toString());
     172
     173        return result;
     174   
     175    }
     176   
    118177   
    119178    ////////////////////////////////////////////////////////////////
    120    
     179     ///////////////////////////////////////////////////////////////////
     180    @Override
    121181    public List<SourceInfo> getSourceInfos(Number annotationID){
    122182       String sourceIDs = makeListOfValues(retrieveSourceIDs(annotationID));
    123        String sql = "SELECT "+external_id+","+ link +"," + version+"FROM "+sourceTableName+" WHERE "+source_id  +" IN "+sourceIDs;
     183       String sql = "SELECT "+external_id+","+ link_uri +"," + version+"FROM "+sourceTableName+" WHERE "+source_id  +" IN "+sourceIDs;
    124184       List<SourceInfo> result= getSimpleJdbcTemplate().query(sql, SourceInfoRowMapper);       
    125185       return result;
     
    129189        @Override
    130190        public SourceInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
    131           return constructSourceInfo(new SourceIdentifier(rs.getString(external_id)), rs.getString(link), rs.getString(version));
     191          return constructSourceInfo(new SourceIdentifier(rs.getString(external_id)), rs.getString(link_uri), rs.getString(version));
    132192        }
    133193    };
     
    171231        return source;
    172232    }
     233   
     234     private Source makeFreshCopy(Source source) {
     235        Source result = new Source();
     236        result.setLink(source.getLink());
     237        result.setURI(source.getURI());
     238        result.setVersion(source.getVersion());
     239        result.setTimeSatmp(source.getTimeSatmp());
     240        //versions-siblings are mentioned in the table sources_versions
     241        return result;
     242    }
    173243}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcVersionDao.java

    r3297 r3298  
    174174   
    175175
    176     //////////////////////////////////////////////////////
    177     @Override
    178     public List<Number> versionIDs() {
    179         String sqlCheck = "SELECT " + version_id + " FROM " + versionTableName;
    180         List<Number> result = getSimpleJdbcTemplate().query(sqlCheck, versionRunnerRowMapper);
    181         return result;
    182     }
    183     private final RowMapper<Number> versionRunnerRowMapper = new RowMapper<Number>() {
    184         @Override
    185         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    186             Number result = rs.getInt(version_id);
    187             return result;
    188         }
    189     };
    190 
    191    
    192176
    193177    private Version makeFreshCopy(Version version) {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDaoTest.java

    r3297 r3298  
    160160 
    161161   
    162     /**
    163      * test public List<Number> cachedRepresentationIDs()
    164      * public List<Number> cachedRepresentationIDs()
    165      */
    166     @Test 
    167     public void testCachedRepresentationIDs() {
    168         System.out.println(" test cachedRepresentationIDs");
    169         List<Number>  result = jdbcCachedRepresentationDao.cachedRepresentationIDs();
    170         assertEquals(7, result.size());
    171         assertEquals(1, result.get(0));
    172         assertEquals(2, result.get(1));
    173         assertEquals(3, result.get(2));
    174         assertEquals(4, result.get(3));
    175         assertEquals(5, result.get(4));
    176         assertEquals(6, result.get(5));       
    177         assertEquals(7, result.get(6));
    178     }
    179    
     162 
    180163   
    181164}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcVersionDaoTest.java

    r3297 r3298  
    166166
    167167 
    168     /**
    169      * Test of versionIDs method, of class JdbcVersionDao.
    170      */
    171     @Test
    172     public void testVersionIDs() {
    173         System.out.println("versionIDs");
    174         List result = jdbcVersionDao.versionIDs();
    175         assertEquals(6, result.size());
    176         assertEquals(1, result.get(0));
    177         assertEquals(2, result.get(1));
    178         assertEquals(3, result.get(2));
    179         assertEquals(4, result.get(3));
    180         assertEquals(5, result.get(4));
    181         assertEquals(6, result.get(5));
    182     }
    183 
     168 
    184169 
    185170}
Note: See TracChangeset for help on using the changeset viewer.