Changeset 3144


Ignore:
Timestamp:
07/17/13 09:07:22 (11 years ago)
Author:
olhsha
Message:

DAO-implementation for non-existing notebooks is changed, also check if a notebook exists in the DB is added. Testing dao get-methods for annotations (given a notebook) have been completed.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
2 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

    r3142 r3144  
    4646    }
    4747   
     48    //////////////////////////////////////////
     49    /**
     50     *
     51     * @param notebookID
     52     * @return false if notebookID == null or the notebook with notebookID is not in the DB;
     53     * @return true if the notebook with notebookID in the DB
     54     */
     55    public boolean isNotebookInTheDataBase(Number notebookID){
     56       
     57        if (notebookID == null) {
     58           return false;
     59       }
     60       String sql = "SELECT notebook.notebook_id  FROM notebook where notebook_id = ?";
     61       List<Number> result=getSimpleJdbcTemplate().query(sql, isNotebookInTheDataBaseRowMapper, notebookID.toString());
     62       if (result == null) {
     63           return false;
     64       }
     65       if (result.isEmpty()) {
     66           return false;
     67       }
     68       return true;
     69    }
     70   
     71    private final RowMapper<Number> isNotebookInTheDataBaseRowMapper = new RowMapper<Number>() {       
     72        @Override
     73        public Integer mapRow(ResultSet rs, int rowNumber) throws SQLException {
     74            Integer notebookId = rs.getInt("notebook_id");
     75            return notebookId;
     76        }
     77    };
     78   
    4879    ////////////////////////////////////////////////////////////////////////
    4980    /**
     
    5182     * @param notebookID
    5283     * @return the list of annotation-ids belonging to the notebook with notebookId
    53      * returns null if notebookId is null
    54      * returns empty list if notebookId is not in the DB: getSimpleJdbcTemplate().query is implemented in this way
     84     * returns null if notebookId is null or is not in the DB
    5585     * TODO: do we need to return null here? using an additional check.
    5686     */
    5787     @Override           
    5888    public List<Number> getAnnotationIDs(Number notebookID) {
    59        if (notebookID == null) {
    60            return null;
    61        }
    62        String sql = "SELECT notebooks_annotations.annotation_id  FROM notebooks_annotations where notebook_id = ?";
    63        return getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, notebookID.toString());
     89        if (notebookID == null) {
     90            return null;
     91        }
     92
     93        if (isNotebookInTheDataBase(notebookID)) {
     94            String sql = "SELECT notebooks_annotations.annotation_id  FROM notebooks_annotations where notebook_id = ?";
     95            return getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, notebookID.toString());
     96        } else {
     97            return null;
     98        }
    6499    }
    65100   
     
    78113    * @param annotationIDs is a list of internal annotation identifiers
    79114    * @return the list of the corresponding annotation-infos for the annotation identifiers from the list;
    80     * if the input list is null or empty (zero elements) returns an empty list
     115    * if the input list is null return null
     116    * if the input list is empty (zero elements) returns an empty list
    81117    */
    82118   
     
    115151     * @param notebookID
    116152     * @return the list of annotation-infos of the annotations from notebookID;
     153     * if notebook not in the DB or null returns null
    117154     * if the notebook contains no annotations returns an empty list
    118155     */
     
    162199     * @param notebookID
    163200     * @return the list of annotation References from the notebookID
    164      * returns null if notebookID == null
     201     * returns null if notebookID == null or it does not exists in the DB
    165202     */
    166203   
     
    177214     * @param notebookID
    178215     * @return the Annotations (as a list of references) from the notebookID     *
    179      * returns null if notebookID == null
    180      */
    181 
    182     @Override
    183     public Annotations getAnnotations(Number notebookID) {
    184        
     216     * returns null if notebookID == null, or it does not exists in th DB, or the list of annotations is empty,
     217     * or something wrong happened when extracting annotations from the notebook
     218     * (according to dasish.xsd if an Annotation is created then its list of annotations must contain at least one element!)
     219     *
     220     */
     221
     222    @Override
     223    public Annotations getAnnotations(Number notebookID) {
     224
    185225        if (notebookID == null) {
    186226            return null;
    187227        }
    188        
    189         Annotations result =  new Annotations();
    190         List<ResourceREF> annotREFs = result.getAnnotation(); 
    191         // TODO: what of annotREFS is null???? how to allocated emprey for it???
    192         boolean test = annotREFs.addAll(getAnnotationREFsOfNotebook(notebookID));
    193         return (test ? result : null);       
     228
     229        if (isNotebookInTheDataBase(notebookID)) {
     230            Annotations result = new Annotations();
     231            List<ResourceREF> annotREFs = result.getAnnotation();
     232            // TODO: what of annotREFS is null????
     233            boolean test = annotREFs.addAll(getAnnotationREFsOfNotebook(notebookID));
     234            return (test ? result : null);
     235        } else {
     236            return null;
     237        }
     238
    194239    }
    195240   
     
    230275   
    231276}
     277           
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r3142 r3144  
    100100    public void tearDown() {
    101101    }
     102   
     103   
     104     /**
     105     * Test of getAnnotationIDs method, of class JdbcAnnotationDao.
     106     * boolean isNotebookInTheDataBase(Number notebookID)
     107     */
     108    @Test
     109    public void testIsNotebookInTheDataBase() {
     110        System.out.println("isNotebookInTheDataBase");
     111       
     112        final boolean testOne =  jdbcAnnotationDao.isNotebookInTheDataBase(TestBackendConstants._TEST_NOTEBOOK_1_INT);
     113        assertEquals(true, testOne);
     114       
     115        final boolean testTwo =  jdbcAnnotationDao.isNotebookInTheDataBase(TestBackendConstants._TEST_NOTEBOOK_2_INT);
     116        assertEquals(true, testTwo);
     117       
     118        final boolean testThree =  jdbcAnnotationDao.isNotebookInTheDataBase(TestBackendConstants._TEST_NOTEBOOK_3_INT);
     119        assertEquals(true, testThree);
     120       
     121        final boolean testFour =  jdbcAnnotationDao.isNotebookInTheDataBase(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
     122        assertEquals(false, testFour);
     123       
     124        final boolean testFive =  jdbcAnnotationDao.isNotebookInTheDataBase(null);
     125        assertEquals(false, testFive);
     126    }
    102127
    103128    /**
     
    130155        //test five, the notebook not in the DB
    131156        final List<Number> annotationIDsFive = jdbcAnnotationDao.getAnnotationIDs(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
    132         assertEquals(new ArrayList<Number>(), annotationIDsFive);
     157        assertEquals(null, annotationIDsFive);
    133158       
    134159    }
     
    138163     * List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs)
    139164     */
    140     @Test
    141  
     165    @Test
    142166    public void testGetAnnotationInfos() {
    143167        System.out.println("getAnnotationInfos");
     
    206230        //non-existing notebook
    207231        final List<AnnotationInfo> annotationInfosFour = jdbcAnnotationDao.getAnnotationInfosOfNotebook(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
    208         assertEquals(0, annotationInfosFour.size());
     232        assertEquals(null, annotationInfosFour);
    209233       
    210234       
     
    266290        assertEquals(0, testListThree.size());
    267291       
    268         // test Five, non-existing notebook
     292        // test Four, non-existing notebook
    269293        List<ResourceREF> testListFour = jdbcAnnotationDao.getAnnotationREFsOfNotebook(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
    270         assertEquals(0, testListFour.size());
     294        assertEquals(null, testListFour);
    271295       
    272296        // test Five Null-notebook
     
    277301    /**
    278302     * Test of getAnnotations method, of class JdbcAnnotationDao.
    279      */
    280     @Test   
    281     @Ignore
     303     * Annotations getAnnotations(Number notebookID)
     304     */
     305    @Test     
    282306    public void testGetAnnotations() {
    283307        System.out.println("getAnnotations");
    284         Number notebookID = null;
    285         JdbcAnnotationDao instance = null;
    286         Annotations expResult = null;
    287         Annotations result = instance.getAnnotations(notebookID);
    288         assertEquals(expResult, result);
    289         // TODO review the generated test code and remove the default call to fail.
    290         fail("The test case is a prototype.");
     308       
     309         // test One
     310        Annotations annotations = jdbcAnnotationDao.getAnnotations(TestBackendConstants._TEST_NOTEBOOK_1_INT);
     311        assertEquals(2, annotations.getAnnotation().size());       
     312        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_1_INT), annotations.getAnnotation().get(0).getRef());
     313        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_INT), annotations.getAnnotation().get(1).getRef());
     314       
     315        // test One
     316        Annotations annotationsTwo = jdbcAnnotationDao.getAnnotations(TestBackendConstants._TEST_NOTEBOOK_2_INT);
     317        assertEquals(1, annotationsTwo.getAnnotation().size());       
     318        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_3_INT), annotationsTwo.getAnnotation().get(0).getRef());
     319       
     320        // test Three  "empty" list of annotations
     321        // according to dasish.xsd if an Annotation is created then its list of annotations must contain at least one element!
     322        // therefore: no annotations in the notebook ==> Annotations-pbject must be null :(
     323        Annotations annotationsThree = jdbcAnnotationDao.getAnnotations(TestBackendConstants._TEST_NOTEBOOK_3_INT);
     324        assertEquals(null, annotationsThree);
     325       
     326        // test Five, non-existing notebook
     327        Annotations annotationsFour = jdbcAnnotationDao.getAnnotations(TestBackendConstants._TEST_ANNOT_4_INT_NOT_IN_THE_DB);
     328        assertEquals(null, annotationsFour);
     329       
     330        // test Five Null-notebook
     331        Annotations annotationsFive = jdbcAnnotationDao.getAnnotations(null);
     332        assertEquals(null, annotationsFive);
    291333    }
    292334}
Note: See TracChangeset for help on using the changeset viewer.