Changeset 3345


Ignore:
Timestamp:
08/09/13 12:55:17 (11 years ago)
Author:
olhsha
Message:

adding annotation refactored, so adding the sources and updating the corresponding joint table went to sourceDao. Still needs testing

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

Legend:

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

    r3334 r3345  
    6464    public int deleteAnnotation(Number annotationId) throws SQLException;
    6565   
    66     /**
    67      *
    68      * @param annotationIdentifier
    69      * @param sourceInfo
    70      * Only the table "annotations_target_sources" is affected
    71      * @return the amount of affected rows in the table "annotations_target_sources". Must be 1 if everything went well.
    72      */
    73     public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier);
    7466   
    7567   
     
    8274 
    8375   
    84     /**
    85      *
    86      * @param annotation
    87      * @param sources
    88      * @param clear
    89      * @return annotation which target sources replaced (if clear == true) or extended (if clear -- false) by "sources"
    90      * side-effects: updates the annotation_target_source table in the DB
    91      * adds a source if it is not in the DB
    92      * in the body (both, annotation-class, and in the table "annotation" replaced temporary sourceIDs of new sources
    93      * with persistent once once they are added to the DB
    94      */
    95     public Annotation updateSourceInfo(Annotation annotation, List<NewOrExistingSourceInfo> sources, boolean clear);
    96            
    97    
     76   
    9877   
    9978    public List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs);   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/SourceDao.java

    r3334 r3345  
    2424import eu.dasish.annotation.schema.SourceInfo;
    2525import java.util.List;
     26import java.util.Map;
    2627
    2728/**
     
    9394     */
    9495    public int deleteSourceVersionRows(Number sourceID);
     96   
     97   
     98    /**
     99     *
     100     * @param annotationID
     101     * @param sources
     102     * @return the mapping of a (possible new, freshly added) source onto the corresponding same source in the DB.
     103     * The difference between the argument/key source and the value source is just their internal and external identifiers.
     104     * The already existing source is mapped onto itself.
     105     * The side-effect: the joint table "annotations_target_sources" is extended by the pairs (annotationID, addedSoiurceID).
     106     */
     107    public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources);       
     108   
    95109       
    96110}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r3334 r3345  
    2525import eu.dasish.annotation.backend.dao.UserDao;
    2626import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
    27 import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
    2827import eu.dasish.annotation.schema.Annotation;
    2928import eu.dasish.annotation.schema.AnnotationBody;
     
    3332import eu.dasish.annotation.schema.NewSourceInfo;
    3433import eu.dasish.annotation.schema.ResourceREF;
    35 import eu.dasish.annotation.schema.Source;
    3634import eu.dasish.annotation.schema.SourceInfo;
    3735import java.sql.ResultSet;
    3836import java.sql.SQLException;
    3937import java.util.ArrayList;
    40 import java.util.Date;
    4138import java.util.HashMap;
    4239import java.util.List;
     
    208205    }
    209206
    210     ////////////////////////////////////////////////////////////////////////
    211     @Override
    212     public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier) {
    213         // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated
    214         Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>();
    215         paramsAnnotationsSources.put("annotationId", getInternalID(annotationIdentifier));
    216         paramsAnnotationsSources.put("sourceId", jdbcSourceDao.getInternalID(sourceIdentifier));
    217         String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)";
    218         return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources));
    219     }
    220 
    221207    // TODO: so far URI in the xml is the same as the external_id in the DB!!
    222208    // Change it when the decision is taken!!!
     
    247233            final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
    248234
    249             result.setTimeStamp(retrieveTimeStamp(getInternalID(annotationIdentifier)));
    250 
     235            Number internalID = getInternalID(annotationIdentifier);
     236
     237
     238            //retrieve taime stamp for the just added annotation
     239            result.setTimeStamp(retrieveTimeStamp(internalID));
     240
     241            // place new target sources in the DB, when necessary, update the corresponding target source info for the result
     242            // the joint annotations_target_sources" tabel is updated.
    251243            List<NewOrExistingSourceInfo> sources = result.getTargetSources().getTarget();
    252             result = updateSourceInfo(result, sources, true);
    253 
     244            Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> sourcePairs = jdbcSourceDao.addTargetSources(internalID, sources);
     245            sources.clear();
     246            sources.addAll(sourcePairs.values());
     247
     248            //replace the temporary sourceId-references in the body with the persistent externalId
     249            String body = annotation.getBody().getAny().get(0).toString();
     250            String newBody =  updateTargetRefsInBody(body, sourcePairs);
     251            List<Object> bodyXML = result.getBody().getAny();
     252            bodyXML.clear();
     253            bodyXML.add(newBody);
     254            String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + internalID;
     255            int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML);
     256           
     257           
    254258            return result;
    255259        } catch (DataAccessException exception) {
     
    258262    }
    259263
    260     ////////////////////////////////////////////////////////////////////////
    261     @Override
    262     public Annotation updateSourceInfo(Annotation annotation, List<NewOrExistingSourceInfo> sources, boolean clear) {
    263 
    264         AnnotationIdentifier annotationIdentifier = new AnnotationIdentifier(annotation.getURI());
    265         List<NewOrExistingSourceInfo> sourcesUpdated = new ArrayList<NewOrExistingSourceInfo>();
    266         String bodyXML = annotation.getBody().getAny().get(0).toString();
    267 
    268         for (NewOrExistingSourceInfo noeSourceInfo : sources) {
    269             SourceInfo sourceInfo = noeSourceInfo.getSource();
    270             if (sourceInfo != null) {
    271                 // this is an old source, already exists in the DB
    272                 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(sourceInfo.getRef()));
    273                 sourcesUpdated.add(noeSourceInfo);
    274             } else {
    275                 Source newSource = constructNewSource(noeSourceInfo.getNewSource());
    276                 Source addedSource = jdbcSourceDao.addSource(newSource);
    277                 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(addedSource.getURI()));
    278 
    279                 //  create updated source info
    280                 SourceInfo updatedSourceInfo = new SourceInfo();
    281                 updatedSourceInfo.setLink(addedSource.getLink());
    282                 updatedSourceInfo.setRef(addedSource.getURI());
    283                 updatedSourceInfo.setVersion(addedSource.getVersion());
    284 
    285                 NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo();
    286                 updatedInfo.setSource(updatedSourceInfo);
    287                 sourcesUpdated.add(updatedInfo);
    288 
    289                 bodyXML = bodyXML.replaceAll(noeSourceInfo.getNewSource().getId(), addedSource.getURI());
    290 
    291             }
    292         }
    293 
    294         List<NewOrExistingSourceInfo> targets = annotation.getTargetSources().getTarget();
    295         if (clear) {
    296             targets.clear();
    297         }
    298        
    299         targets.addAll(sourcesUpdated);
    300        
    301         /// time to update body
    302         Number annotationInternalId = getInternalID(annotationIdentifier);
    303         String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + annotationInternalId;
    304         int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML);
    305 
    306         List<Object> body = annotation.getBody().getAny();
    307         body.clear();
    308         body.add(bodyXML);
    309         //bodu=y is updated
    310        
    311         return annotation;
    312     }
    313 
    314264    //////////////////////////////////////////////////
    315265    @Override
     
    320270    //////////// helpers ///////////////////////
    321271    /////////////////////////////////////////////////
     272   
     273    private String updateTargetRefsInBody(String body, Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> sourcePairs){
     274        String result = body;
     275        for (NewOrExistingSourceInfo tempSource : sourcePairs.keySet()) {
     276                NewSourceInfo newSource = tempSource.getNewSource();
     277                if (newSource != null) {
     278                    result=result.replaceAll(newSource.getId(),sourcePairs.get(tempSource).getSource().getRef() );
     279                }
     280            }
     281        return result;
     282    }
     283   
    322284    private XMLGregorianCalendar retrieveTimeStamp(Number internalID) {
    323285        String sqlTime = "SELECT " + time_stamp + " FROM " + annotationTableName + " WHERE " + annotation_id + "= ?";
     
    366328        return result;
    367329    }
    368 
    369     ///////////////////
    370     private Source constructNewSource(NewSourceInfo newSourceInfo) {
    371         Source result = new Source();
    372         result.setLink(newSourceInfo.getLink());
    373         result.setVersion(newSourceInfo.getVersion());
    374         // source's internalID will be generated by the DB
    375         // source's time stamp will be generated by the DB
    376         // source's externalID will be generated by the add-source-dao
    377         return result;
    378     }
    379330}
     331//List<NewOrExistingSourceInfo> targets = annotation.getTargetSources().getTarget();
     332//        if (clear) {
     333//            targets.clear();
     334//        }
     335//       
     336//        targets.addAll(sourcesUpdated);
     337//       
     338//        /// time to update body
     339//        Number annotationInternalId = getInternalID(annotationIdentifier);
     340//        String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + annotationInternalId;
     341//        int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML);
     342//
     343//        List<Object> body = annotation.getBody().getAny();
     344//        body.clear();
     345//        body.add(bodyXML);
     346        //body is updated
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java

    r3334 r3345  
    2525import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    2626import eu.dasish.annotation.schema.NewOrExistingSourceInfos;
     27import eu.dasish.annotation.schema.NewSourceInfo;
    2728import eu.dasish.annotation.schema.Source;
    2829import eu.dasish.annotation.schema.SourceInfo;
     
    211212    }
    212213   
    213  
     214   
     215   
    214216
    215217    /////////////////////////////////////////////////
     
    221223
    222224    }
     225   
     226   
     227
     228   
     229   
     230     ////////////////////////////////////////////////////////////////////////
     231    @Override
     232    public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) {
     233
     234        Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> result = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
     235       
     236        for (NewOrExistingSourceInfo noeSourceInfo : sources) {
     237            SourceInfo sourceInfo = noeSourceInfo.getSource();
     238            if (sourceInfo != null) {
     239                // this is an old source, already exists in the DB
     240                result.put(noeSourceInfo, noeSourceInfo);               
     241                addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(sourceInfo.getRef())));
     242            } else {
     243                Source newSource = constructNewSource(noeSourceInfo.getNewSource());
     244                Source addedSource = addSource(newSource);
     245                addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(addedSource.getURI())));
     246                //  create updated source info
     247                SourceInfo updatedSourceInfo = new SourceInfo();
     248                updatedSourceInfo.setLink(addedSource.getLink());
     249                updatedSourceInfo.setRef(addedSource.getURI());
     250                updatedSourceInfo.setVersion(addedSource.getVersion());
     251
     252                NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo();
     253                updatedInfo.setSource(updatedSourceInfo);
     254                result.put(noeSourceInfo, updatedInfo);
     255            }
     256           
     257        }
     258       
     259       
     260        return result;
     261    }
     262
     263   
     264   
    223265
    224266    //////// HELPERS //////////////////////
    225267    ////////////////////////////////////////////////////////
     268   
     269 
     270    private int addAnnotationSourcePair(Number annotationID, Number sourceID) {
     271        // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated
     272        Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>();
     273        paramsAnnotationsSources.put("annotationId", annotationID);
     274        paramsAnnotationsSources.put("sourceId",  sourceID);
     275        String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)";
     276        return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources));
     277    }
     278   
     279   
     280   
    226281    private SourceInfo constructSourceInfo(SourceIdentifier sourceIdentifier, String link, VersionIdentifier versionIdentifier) {
    227282        SourceInfo sourceInfo = new SourceInfo();
     
    232287    }
    233288
     289      ///////////////////
     290    private Source constructNewSource(NewSourceInfo newSourceInfo) {
     291        Source result = new Source();
     292        result.setLink(newSourceInfo.getLink());
     293        result.setVersion(newSourceInfo.getVersion());
     294        // source's internalID will be generated by the DB
     295        // source's time stamp will be generated by the DB
     296        // source's externalID will be generated by the add-source-dao
     297        return result;
     298    }
     299   
     300   
    234301    private Source constructSource(SourceIdentifier sourceIdentifier, String link, VersionIdentifier version, XMLGregorianCalendar xmlTimeStamp) {
    235302        Source source = new Source();
     
    251318        return result;
    252319    }
     320   
     321   
    253322}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r3334 r3345  
    251251     */
    252252    @Test
     253    @Ignore
    253254    public void testAddAnnotationExistingSource() throws SQLException{
    254255        System.out.println("test_addAnnotation with an existing source");
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDaoTest.java

    r3300 r3345  
    2222import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
    2323import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
     24import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    2425import eu.dasish.annotation.schema.NewOrExistingSourceInfos;
    2526import eu.dasish.annotation.schema.Source;
     
    2728import java.util.ArrayList;
    2829import java.util.List;
     30import java.util.Map;
    2931import org.jmock.Expectations;
    3032import org.jmock.Mockery;
     
    235237       
    236238    }
     239   
     240   
     241    /**
     242     * Test of addTargetSources method, of class JdbcSourceDao.
     243     * public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources)
     244     */
     245    @Test
     246    public void testAddTargetSources()
     247    {
     248       
     249    }
    237250}
Note: See TracChangeset for help on using the changeset viewer.