Changeset 3152


Ignore:
Timestamp:
07/17/13 16:01:11 (11 years ago)
Author:
olhsha
Message:

adding getAnnotation(Number internal_id) and getAnnotationID(AnnotationIdentifier? externalID) to Dao and testing methods

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

Legend:

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

    r3151 r3152  
    1818package eu.dasish.annotation.backend.dao;
    1919
     20import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
     21import eu.dasish.annotation.schema.Annotation;
    2022import eu.dasish.annotation.schema.AnnotationInfo;
    21 import eu.dasish.annotation.schema.Annotations;
    2223import eu.dasish.annotation.schema.ResourceREF;
     24import java.sql.SQLException;
    2325import java.util.List;
    2426
     
    4143    List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs);   
    4244   
    43    
     45    /**
     46     *
     47     * @param annotationID
     48     * @return annotation which has an annotation ID "annotationID"
     49     * if externalID is null or such annotation does not exist in the DB returns null;
     50     */
     51    Annotation getAnnotation(Number annotationID) throws SQLException;
     52   
     53    /**
     54     *
     55     * @param externalID
     56     * @return the internal annotationId for the annotation with the external Id "extrnalID"
     57     * if annotationID is null or such annotation does not exist in the DB returns null;
     58     */
     59    Number getAnnotationID(AnnotationIdentifier externalID) throws SQLException;
    4460}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r3151 r3152  
    1919
    2020import eu.dasish.annotation.backend.dao.AnnotationDao;
     21import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
     22import eu.dasish.annotation.schema.Annotation;
     23import eu.dasish.annotation.schema.AnnotationBody;
    2124import eu.dasish.annotation.schema.AnnotationInfo;
    22 import eu.dasish.annotation.schema.Annotations;
    2325import eu.dasish.annotation.schema.ResourceREF;
    2426import eu.dasish.annotation.schema.Sources;
     
    2931import javax.sql.DataSource;
    3032import org.springframework.jdbc.core.RowMapper;
    31 import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
    3233
    3334/**
     
    121122    };
    122123   
    123    
    124      
    125    
    126    
     124   //////////////////////////////////////////////
     125   /* CREATE TABLE annotation (
     126    annotation_id SERIAL UNIQUE NOT NULL,
     127    external_id UUID UNIQUE NOT NULL,
     128    time_stamp timestamp with time zone default now(),
     129    owner_id integer,
     130    headline text,
     131    body_xml xml */
     132
     133   @Override
     134    public Annotation getAnnotation(Number annotationID) throws SQLException{
     135        if (annotationID == null) {
     136            return null;
     137        }
     138       String sql = "SELECT * FROM annotation WHERE annotation.annotation_id  = ?";
     139       List<Annotation> result= getSimpleJdbcTemplate().query(sql, annotationRowMapper, annotationID);
     140       
     141       if (result == null) {
     142           return null;
     143       }
     144       if (result.isEmpty()) {
     145           return null;
     146       }
     147       
     148        if (result.size()>1) {
     149           throw new SQLException("There are "+result.size()+" annotations with annotation_id "+annotationID);
     150       }
     151       return result.get(0);
     152   }
     153     
     154      private final RowMapper<Annotation> annotationRowMapper = new RowMapper<Annotation>() {       
     155        @Override
     156        public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException {
     157           Annotation result = new Annotation();
     158           result.setHeadline(rs.getString("headline"));
     159           
     160           ResourceREF ownerREF = new ResourceREF();
     161           ownerREF.setRef(String.valueOf(rs.getInt("owner_id")));
     162           result.setOwner(ownerREF);
     163           
     164           /*TODO
     165            * Add permissions also to the database
     166            * ResourceREF permissionsREF = new ResourceREF();
     167           permissionsREF.setRef(String.valueOf(rs.getInt("permissions")));
     168           result.setPermissions(permissionsREF);*/
     169           
     170           
     171           // TODO: add source, also to the database
     172           
     173           result.setBody(convertToAnnotationBody(rs.getString("body_xml")));
     174           return result;
     175        }
     176    };
     177   
     178   // TODO: fill in the stub, when the annotation body is elaborated
     179   private AnnotationBody convertToAnnotationBody(String input){
     180     if (input == null) {
     181         return null;
     182     } 
     183     
     184     AnnotationBody result = new AnnotationBody();
     185     List<Object> element =result.getAny();
     186     element.add(input);
     187     return result;
     188   } 
     189     
     190     
     191   //////////////////////////////////////////////////
     192     @Override
     193    public Number getAnnotationID(AnnotationIdentifier externalID) throws SQLException{
     194        if (externalID == null) {
     195            return null;
     196        }
     197       
     198       String sql = "SELECT annotation.annotation_id FROM annotation WHERE annotation.external_id  = ?";
     199       List<Number> result= getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, externalID.toString());
     200       if (result == null) {
     201           return null;
     202       }
     203       if (result.isEmpty()) {
     204           return null;
     205       }
     206       
     207       if (result.size()>1) {
     208           throw new SQLException("There are "+result.size()+" annotations with external_id "+externalID);
     209       }
     210       return result.get(0);
     211   }
     212     
     213      private final RowMapper<Number> annotationIDRowMapper = new RowMapper<Number>() {       
     214        @Override
     215        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     216           Number result = rs.getInt("annotation_id");
     217           return result;
     218        }
     219    };
     220     
     221   
     222    //////////// helpers ///////////////////////
    127223   
    128224    /////////////////////////////////////////////////
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestBackendConstants.java

    r3142 r3152  
    4444    public static final String _TEST_ANNOT_2_EXT = "00000000-0000-0000-0000-000000000022";
    4545    public static final String _TEST_ANNOT_3_EXT = "00000000-0000-0000-0000-000000000023";
     46    public static final String _TEST_ANNOT_4_EXT_NOT_IN_THE_DB = "00000000-0000-0000-0000-000000000024";
    4647   
    4748    public static final int _TEST_ANNOT_1_INT = 21;
     
    5859    public static final int _TEST_ANNOT_3_OWNER = 113;
    5960   
     61    public static final String _TEST_ANNOT_1_BODY = "<html><body>some html 1</body></html>";
     62   
    6063}
    6164
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r3151 r3152  
    1919
    2020import eu.dasish.annotation.backend.TestBackendConstants;
     21import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
     22import eu.dasish.annotation.schema.Annotation;
    2123import eu.dasish.annotation.schema.AnnotationInfo;
    2224import eu.dasish.annotation.schema.ResourceREF;
     25import java.sql.SQLException;
    2326import java.util.ArrayList;
    2427import java.util.List;
     
    112115       
    113116    }
    114 
     117    /**
     118     *
     119     * Test of getAnnotationID method, of class JdbcAnnotationDao.
     120     * Integer getAnnotationID(AnnotationIdentifier externalID)
     121     */
     122    @Test
     123    public void getAnnotationID() throws SQLException{
     124       System.out.println("getAnnotationID");
     125       
     126       final Number annotaionId = jdbcAnnotationDao.getAnnotationID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_1_EXT));
     127       assertEquals(TestBackendConstants._TEST_ANNOT_1_INT, annotaionId.intValue());
     128       
     129       final Number annotaionIdNE = jdbcAnnotationDao.getAnnotationID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_4_EXT_NOT_IN_THE_DB));
     130       assertEquals(null, annotaionIdNE);   
     131     
     132       final Number annotaionIdNull = jdbcAnnotationDao.getAnnotationID(null);
     133       assertEquals(null, annotaionIdNull);
     134    }
     135   
     136     /**
     137     *
     138     * Test of getAnnotation method, of class JdbcAnnotationDao.
     139     * Annotation getAnnotation(Number annotationlID)
     140     */
     141    @Test
     142    public void getAnnotation() throws SQLException{
     143       System.out.println("getAnnotation");
     144       
     145       final Annotation annotaion = jdbcAnnotationDao.getAnnotation(TestBackendConstants._TEST_ANNOT_1_INT);
     146       assertEquals(TestBackendConstants._TEST_ANNOT_1_HEADLINE, annotaion.getHeadline());
     147       assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_1_OWNER), annotaion.getOwner().getRef());
     148       assertEquals(TestBackendConstants._TEST_ANNOT_1_BODY, annotaion.getBody().getAny().get(0)); // when the body is elaborated it will be changed
     149       
     150       final Annotation annotaionNE = jdbcAnnotationDao.getAnnotation(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
     151       assertEquals(null, annotaionNE);
     152       
     153       final Annotation annotaionNull = jdbcAnnotationDao.getAnnotation(null);
     154       assertEquals(null, annotaionNull);
     155    }
    115156   
    116157}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/resources/test-data/InsertTestData.sql

    r3142 r3152  
    3636INSERT INTO notebook (notebook_id, title, owner_id, external_id) VALUES (13, 'Notebook 13', 113, '00000000-0000-0000-0000-000000000013');
    3737
    38 INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (21, 111, 'Sagrada Famiglia','<html><body>some html</body></html>', '00000000-0000-0000-0000-000000000021');
    39 INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (22, 112, 'Gaudi','<html><body>some html</body></html>', '00000000-0000-0000-0000-000000000022');
    40 INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (23, 113, 'Art Nuveau','<html><body>some html</body></html>', '00000000-0000-0000-0000-000000000023');
     38INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (21, 111, 'Sagrada Famiglia','<html><body>some html 1</body></html>', '00000000-0000-0000-0000-000000000021');
     39INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (22, 112, 'Gaudi','<html><body>some html 2 </body></html>', '00000000-0000-0000-0000-000000000022');
     40INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (23, 113, 'Art Nuveau','<html><body>some html 3</body></html>', '00000000-0000-0000-0000-000000000023');
    4141
    4242INSERT INTO notebooks_annotations (notebook_id,annotation_id) VALUES (11,21);
Note: See TracChangeset for help on using the changeset viewer.