Changeset 3370


Ignore:
Timestamp:
08/13/13 14:03:21 (11 years ago)
Author:
olhsha
Message:

filtered getting annotation is implemented and tested (except filtering by the logged-in user access mode)

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

    r3369 r3370  
    7171    @Override
    7272    public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UserIdentifier owner, Timestamp after, Timestamp before) {
    73 
    7473       
    75         String table;
    76        
     74        StringBuilder sql = new StringBuilder("SELECT DISTINCT ");
     75        sql.append(annotation_id).append(" FROM ").append(annotationTableName).append(" WHERE TRUE ");       
     76        Map<String, Object> params = new HashMap<String, Object>();
     77       
    7778         if (link != null) {
    7879            List<Number> sourceIDs = jdbcSourceDao.getSourcesForLink(link);
     
    8081            if (!annotationIDs.isEmpty()) {
    8182                String values = makeListOfValues(annotationIDs);
    82                 table = "( SELECT DISTINCT "+annotationStar + "WHERE "+ annotation_id +"IN " + values + ") ";
     83                sql.append(" AND ").append(annotation_id).append(" IN ").append(values);
    8384            }
    8485            else{
     
    8687            }
    8788        }
    88          else{
    89              table = annotationTableName;
    90          }
    9189         
    92        
    93         StringBuilder sql = new StringBuilder("SELECT ");
    94         sql.append(annotation_id).append(" FROM ").append(table).append(" WHERE TRUE ");
    95        
    96         Map<String, Object> params = new HashMap<String, Object>();
    97         //TODO: optimizie String builder
    98 
    9990
    10091        if (owner != null) {
    101             sql.append(" AND ").append(principal_id).append(" = :owner ");
    102             params.put("owner", owner.toString());
     92            Number ownerID = jdbcUserDao.getInternalID(owner);
     93            sql.append(" AND ").append(owner_id).append(" = :owner ");
     94            params.put("owner", ownerID);
    10395        }
    10496
     
    114106
    115107        if (text != null) {
    116             sql .append(" AND ").append(body_xml).append("  LIKE '% :text %'");
    117             params.put("text", text);
    118         }
     108            sql.append(" AND ").append(body_xml).append("  LIKE '%").append(text).append("%'");
     109        }
     110       
    119111       
    120112        List<Number> result = getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, params);
     
    174166     * from the input list if the input list is null or empty (zero elements)
    175167     * returns an empty list
     168     * there may be annotationIDs which are not in the DB (so that's why we need this method).
    176169     */
     170   
    177171    @Override
    178172    public List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs) {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java

    r3361 r3370  
    106106            return null;
    107107        }
    108        String sql = "SELECT "+internalIdName+" FROM "+resourceTableName+" WHERE "+external_id  +"= ?";
     108       String sql = "SELECT "+internalIdName+" FROM "+resourceTableName+" WHERE "+external_id  +"= ? LIMIT 1";
    109109       List<Number> sqlResult= getSimpleJdbcTemplate().query(sql, internalIDRowMapper, externalId.toString());
    110110       
     
    137137            return null;
    138138        }
    139         String sql = "SELECT " + external_id + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ?";
     139        String sql = "SELECT " + external_id + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ? LIMIT 1";
    140140        List<String> sqlResult = getSimpleJdbcTemplate().query(sql, externalIDRowMapper, internalId);
    141141
     
    162162    /////////////////////////////////////////////////////
    163163   protected XMLGregorianCalendar retrieveTimeStamp(Number internalID) {
    164         String sqlTime = "SELECT " + time_stamp + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ?";
     164        String sqlTime = "SELECT " + time_stamp + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ? LIMIT 1";
    165165        List<XMLGregorianCalendar> timeStamp = getSimpleJdbcTemplate().query(sqlTime, timeStampRowMapper, internalID);
    166166        if (timeStamp.isEmpty()) {
     
    195195           return false;
    196196       }
    197        String sql = "SELECT "+notebookNotebook_id+"  FROM notebook where "+notebook_id+" = ?";
     197       String sql = "SELECT "+notebookNotebook_id+"  FROM notebook where "+notebook_id+" = ? LIMIT 1";
    198198       List<Number> result=getSimpleJdbcTemplate().query(sql, isNotebookInTheDataBaseRowMapper, notebookID.toString());
    199199       if (result == null) {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcUserDao.java

    r3309 r3370  
    2020import eu.dasish.annotation.backend.dao.UserDao;
    2121import eu.dasish.annotation.backend.identifiers.UserIdentifier;
    22 import java.sql.ResultSet;
    23 import java.sql.SQLException;
    24 import java.util.List;
    2522import javax.sql.DataSource;
    26 import org.springframework.jdbc.core.RowMapper;
    2723
    2824/**
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r3369 r3370  
    2323import eu.dasish.annotation.backend.dao.PermissionsDao;
    2424import eu.dasish.annotation.backend.dao.SourceDao;
     25import eu.dasish.annotation.backend.dao.UserDao;
    2526import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
    2627import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
     28import eu.dasish.annotation.backend.identifiers.UserIdentifier;
    2729import eu.dasish.annotation.schema.Annotation;
    2830import eu.dasish.annotation.schema.AnnotationInfo;
     
    3335import eu.dasish.annotation.schema.SourceInfo;
    3436import java.sql.SQLException;
     37import java.sql.Timestamp;
    3538import java.util.ArrayList;
    3639import java.util.HashMap;
     
    4043import org.jmock.Mockery;
    4144import static org.junit.Assert.*;
    42 import org.junit.Ignore;
    4345import org.junit.Test;
    4446import org.junit.runner.RunWith;
     
    6466    @Autowired
    6567    private SourceDao sourceDao;
     68    @Autowired
     69    private UserDao userDao;   
    6670    @Autowired
    6771    private Mockery mockery;
     
    376380        List<Number> result = jdbcAnnotationDao.getAnnotationIDsForSources(sources);
    377381        assertEquals (2, result.size());
    378         //assertEquals(2, result.get(0));
    379         //assertEquals(3, result.get(1));
     382        assertEquals(2, result.get(0));
     383        assertEquals(3, result.get(1));
    380384    }
    381385   
    382386    @Test   
    383387    public void testGetExternalID() {
    384         System.out.println("getAnnotationID");
     388        System.out.println("getExternalID");
    385389
    386390        final AnnotationIdentifier externalId = jdbcAnnotationDao.getExternalID(2);
     
    392396
    393397    }
     398   
     399   
     400    /** test
     401     * public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UserIdentifier owner, Timestamp after, Timestamp before) {
     402  **/
     403   
     404    @Test   
     405    public void testGetFilteredAnnotationIDs(){
     406        System.out.println(" test getFilteredAnnotationIDs");
     407       
     408       
     409        //////////////////////////////////////////
     410        // TEST 1
     411        final String link = "nl.wikipedia.org";
     412        final List<Number> sourceIDs = new ArrayList<Number>();
     413        sourceIDs.add(1);
     414        sourceIDs.add(2);
     415       
     416       
     417        mockery.checking(new Expectations() {
     418            {
     419                oneOf(sourceDao).getSourcesForLink(link);
     420                will(returnValue(sourceIDs));
     421            }
     422        });
     423        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(link, null, null, null, null, null, null);       
     424        assertEquals(2, result_1.size());
     425        assertEquals(2, result_1.get(0));
     426        assertEquals(3, result_1.get(1));
     427       
     428        ///////////////////////////////////////////////
     429        // TEST 2       
     430        mockery.checking(new Expectations() {
     431            {
     432                oneOf(sourceDao).getSourcesForLink(link);
     433                will(returnValue(sourceIDs));
     434            }
     435        });
     436        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(link, "some html", null, null, null, null, null);       
     437        assertEquals(2, result_2.size());
     438        assertEquals(2, result_2.get(0));
     439        assertEquals(3, result_2.get(1));
     440       
     441        ///////////////////////////////////////////////
     442        // TEST 3
     443        final UserIdentifier owner = new UserIdentifier("00000000-0000-0000-0000-000000000111");
     444       
     445       
     446         mockery.checking(new Expectations() {
     447            {
     448                oneOf(sourceDao).getSourcesForLink(link);
     449                will(returnValue(sourceIDs));
     450               
     451                oneOf(userDao).getInternalID(owner);
     452                will(returnValue(3));
     453            }
     454        });
     455       
     456        List<Number> result_3 = jdbcAnnotationDao.getFilteredAnnotationIDs(link, "some html", null, null, owner, null, null);       
     457        assertEquals(1, result_3.size());
     458        assertEquals(2, result_3.get(0));
     459       
     460         ///////////////////////////////////////////////
     461        // TEST 4
     462       
     463         mockery.checking(new Expectations() {
     464            {
     465                oneOf(sourceDao).getSourcesForLink(link);
     466                will(returnValue(sourceIDs));
     467               
     468                oneOf(userDao).getInternalID(owner);
     469                will(returnValue(3));
     470            }
     471        });
     472        Timestamp after = new Timestamp(0);
     473        Timestamp before = new Timestamp(System.currentTimeMillis()); 
     474        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(link, "some html", null, null, owner, after, before);       
     475        assertEquals(1, result_4.size());
     476        assertEquals(2, result_4.get(0));
     477       
     478          ///////////////////////////////////////////////
     479        // TEST 5
     480       
     481         mockery.checking(new Expectations() {
     482            {
     483                oneOf(sourceDao).getSourcesForLink(link);
     484                will(returnValue(sourceIDs));
     485               
     486                oneOf(userDao).getInternalID(owner);
     487                will(returnValue(3));
     488            }
     489        });
     490        Timestamp after_1 = new Timestamp(System.currentTimeMillis());       
     491        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(link, "some html", null, null, owner, after_1, null);       
     492        assertEquals(0, result_5.size());
     493       
     494       
     495    }
     496   
    394497    //////////// helpers //////////////////////
    395498}
Note: See TracChangeset for help on using the changeset viewer.