Changeset 3395


Ignore:
Timestamp:
08/15/13 17:00:50 (11 years ago)
Author:
olhsha
Message:

refactored DAO-s and creating an annotation in rest. Still 2 failures and 3 errors.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend
Files:
11 edited

Legend:

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

    r3364 r3395  
    1818package eu.dasish.annotation.backend;
    1919
     20import eu.dasish.annotation.schema.AnnotationBody;
    2021import java.sql.Timestamp;
    2122import java.util.GregorianCalendar;
     23import java.util.Map;
    2224import javax.xml.datatype.DatatypeConfigurationException;
    2325import javax.xml.datatype.DatatypeFactory;
     
    3537            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    3638    }
     39   
     40       
     41    // TODO: change when serialization mechanism for bodies is fixed
     42    public static String serializeBody(AnnotationBody body) {
     43        return body.getAny().get(0).toString();
     44    }
     45
     46    // TODO: change when serialization mechanism for bodies is fixed
     47    public static AnnotationBody deserializeBody(String bodyXml) {
     48        AnnotationBody result = new AnnotationBody();
     49        result.getAny().add(bodyXml);
     50        return result;
     51    }
     52   
     53     
     54    public static String replace(String text, Map<String, String> pairs) {
     55        String result = (new StringBuilder(text)).toString();
     56        for (String tempSource : pairs.keySet()) {
     57            result = result.replaceAll(tempSource, pairs.get(tempSource));
     58        }
     59        return result;
     60    }
     61   
    3762}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java

    r3380 r3395  
    2424import eu.dasish.annotation.schema.AnnotationInfo;
    2525import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
     26import eu.dasish.annotation.schema.NewSourceInfo;
    2627import eu.dasish.annotation.schema.ResourceREF;
    2728import java.sql.SQLException;
     
    121122     * @return the list of annotationdIDs of the annotations that are having target sources from "sourceIDs" list
    122123     */
    123     public List<Number> getAnnotationIDsForSources(List<Number> sourceIDs);
     124    public List<Number> retrieveAnnotationList(List<Number> sourceIDs);
    124125   
    125126   
     
    134135    public List<Number> retrieveSourceIDs(Number annotationID);
    135136   
    136     /**
    137      *
    138      * @param body
    139      * @return serialized body
    140      */
    141     //NOT TESTED, will be changed after serialization is fixed
    142     public String serializeBody(AnnotationBody body);
     137 
    143138   
    144     /**
    145      *
    146      * @param bodyXml
    147      * @return deserialized body
    148      */
    149     // NOT TESTED will be chnaged after serializaion is fixed
    150     public AnnotationBody deserializeBody(String bodyXml);
    151    
    152     /**
    153      *
    154      * @param serializedBody
    155      * @param sourcePairs
    156      * @return replaces temporary source IDs with persistent ones (after they are added)
    157      */   
    158    
    159     //NOT TESTED
    160     public String updateTargetRefsInBody(String serializedBody, Map<String, String> sourceIDPairs);
    161139}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/CachedRepresentationDao.java

    r3380 r3395  
    2020import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
    2121import eu.dasish.annotation.schema.CachedRepresentationInfo;
    22 import java.util.List;
    2322
    2423/**
     
    3433     */
    3534    public CachedRepresentationIdentifier getExternalID(Number internalID);
     35   
     36     
     37    /**
     38     *
     39     * @param externalID
     40     * @return the internal identifier of the resource with the "externalID"
     41     */   
     42    public Number getInternalID(CachedRepresentationIdentifier externalID);
     43   
    3644 
    3745    /**
    3846     *
    3947     * @param internalID
    40      * @return the object "cached representation info"  with the internal id "internalID"
     48     * @return the object "cCachedRepresentationInfo"  with the internal id "internalID"
    4149     */
    4250    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID);
    4351   
    44     /**
    45      *
    46      * @param versionID
    47      * @return The list list of cached representation internal id-s of all the cached representations of the version with versionID
    48      */
    49     public List<Number> retrieveCachedRepresentationList(Number versionID);
    50    
    51      
     52 
    5253    /**
    5354     *
    5455     * @param internalID
    55      * removes the cached representation with internalId from the DB if there is no reference to it in the table "versions_cached_representations"
    56      * @return the amount of removed rows in the table "cached_representation"
     56     * @return  # deleted rows on the table "cached_representation"
    5757     */
    58     public int deleteCachedRepresentationInfo(Number internalID);
     58    public  int deleteCachedRepresentationInfo(Number internalID);
    5959   
    6060    /**
     
    6666   
    6767   
     68      /**
     69     *
     70     * @param versionID
     71     * @param cached
     72     * @return result[0] = the internalId of the added (if it is not yet in th DB) cached representation
     73     * result[1] # added rows to "versions_cached_representations"
     74     */
     75    public Number[] addCachedForVersion(Number versionID, CachedRepresentationInfo cached);
    6876 
     77 
     78      /**
     79     *
     80     * @param versionID
     81     * @param cachedID
     82     * @return result[0] # deleted rows (versionID, cachedID) in the table "versions_cached_representations"
     83     * result[1] # deleted rows in the table "cached_representation"
     84     */
     85    public int[] deleteCachedForVersion(Number versionID,  Number cachedID);
     86   
    6987}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/SourceDao.java

    r3380 r3395  
    2121import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    2222import eu.dasish.annotation.schema.NewOrExistingSourceInfos;
     23import eu.dasish.annotation.schema.NewSourceInfo;
    2324import eu.dasish.annotation.schema.Source;
    2425import eu.dasish.annotation.schema.SourceInfo;
     
    4950    public Source getSource(Number internalID);
    5051   
    51     /**
    52      *
    53      * @param internalID
    54      * @return the  map "colum name" --> vale for the source form the DB
    55      */
    56     public Map<String, Object> getRawSource(Number internalID);
     52   
    5753   
    5854    /**
     
    6258     * @return the amount of affected rows in the "source" table
    6359     */
    64     public int deleteSource(Number internalID);
     60    public int[] deleteSource(Number internalID);
    6561   
    6662    /**
     
    7167     * return -1 id the source cannot be added because its version is not in the DB
    7268     */
    73     public Number addSource(Source freshSource) throws SQLException;
    74    
    75     //////////////////////////////////////////////
     69    public Number addSource(Source freshSource) throws SQLException;   
    7670   
    77     /**
     71   
     72     /**
    7873     *
    79      * @param annotationID
    80      * @return the Information about the target sources to which annotationId refers
     74     * @param sourceID
     75     * @return the list of the internal version id-s for the  target source with the internal Id "sourceID"
    8176     */
     77    public List<Number> retrieveVersionList(Number sourceID);
     78   
     79 
    8280    public List<SourceInfo> getSourceInfos(List<Number> sources);
    8381   
    84    /**
    85     *
    86     * @param sourceInfoList
    87     * @return the list of NewOrExistingSourceo objects in such a way  that an element of the sourceInfoList is injected into an element of the return list
    88     */
    89    //TODO: add non-existing sources!! now is implemented only for existing sources
    90     public NewOrExistingSourceInfos contructNewOrExistingSourceInfo(List<SourceInfo> sourceInfoList);
    91    
    92    
    93    
    94     /**
    95      *
    96      * @param sourceID
    97      * @return delete all the rows in "sources_versions" table with sourceID
    98      */
    99     public int deleteSourceVersionRows(Number sourceID);
    100    
    101    
    102     /**
    103      *
    104      * @param annotationID
    105      * @param sources
    106      * @return the mapping of a (temporary source ID  onto the corresponding same source persisten ID  in the DB.
    107      * The side-effect: the joint table "annotations_target_sources" is extended by the pairs (annotationID, addedSoiurceID).
    108      * Also: calls "addSource" if the source is not yet in the DB.
    109      */
    110     public Map<String, String> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException;       
    111    
     82 
    11283    /**
    11384     *
     
    11687     */
    11788    public List<Number> getSourcesForLink(String link);
     89 
     90   
     91   
     92   public Map<String, String> addTargetSourcesToAnnotation(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException;       
     93   
     94     
    11895}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/VersionDao.java

    r3380 r3395  
    1919
    2020import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
     21import eu.dasish.annotation.schema.CachedRepresentationInfo;
    2122import eu.dasish.annotation.schema.Version;
    2223import java.util.List;
     
    3637    public VersionIdentifier getExternalID(Number internalID);   
    3738   
     39   /**
     40    *
     41    * @param externalID
     42    * @return the internal Id of the Version with the external ID "externalID"
     43    */
     44    public Number getInternalID(VersionIdentifier externalID);
     45   
    3846   
    3947    /**
    4048     *
    4149     * @param internalID
    42      * @return the instance of Version.class  where the version internal Id is "internalID"
     50     * @return the instance of Version.class  with the internal Id equal to "internalID"
    4351     *
    4452     */
    4553    public Version getVersion(Number internalID);
    4654   
    47     /**
    48      *
    49      * @param sourceID
    50      * @return the list of the internal version id-s for the  target source with the internal Id "sourceID"
    51      */
    52     public List<Number> retrieveVersionList(Number sourceID);
    53    
     55   
    5456     
    5557    /** @param versionID
    56      * removes the row of "version" with the internal ID "internalID" if no references to this version from the tables "sources_versions" and "source"
    57      * @return the amount of removed rows
     58     * @return result[0] # deleted rows in the joint table "sources_versions"
     59     * result[1] # deleted rows in the joit table "versions_cached_representations"
     60     * result[2] # deleted rows in "version" table
     61     * result[3] # deleted cached representations (which are not referred by other versions)
    5862     */
    5963   
    60     public int deleteVersion(Number versionID);
     64    public int[] deleteVersion(Number versionID);
    6165   
    6266    /**
     
    6872    public Number addVersion(Version version);
    6973   
    70     /**
     74 
     75 
     76   
     77     /**
    7178     *
    7279     * @param versionID
    73      * @return removes the rows (versionID, some cached representation id) from the joint table "versions_cached_representations"
     80     * @return The list of the cached representation internal id-s of all the cached representations of the version with "versionID"
    7481     */
    75     public int deleteVersionCachedRepresentationRow(Number versionID);
     82    public List<Number> retrieveCachedRepresentationList(Number versionID);
    7683   
    77    
    78     /**
    79      *
    80      * @param sourceID
    81      * @param cachedRepresentationID
    82      * @return
    83      * 1) the amount of rows affected by deleting cached representation "cachedRepresentationID"
    84      * from the table "versions_cached_representations", if the corresponding version is a sibling-version of the source surceID
    85      * 2) the amount of rows affected by SAFE removing cachedRepresentationID from cached_representation table,
    86      * if the first number>0
    87      *
    88      * used to fulfill DELETE api/sources/<sid>/cached/<cid>
    89      */
    90     public  int[] deleteCachedRepresentationForSource(Number sourceID, Number cachedRepresentationID);
    91    
    92     public Number getInternalID(VersionIdentifier externalID);
    9384}
    9485   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r3380 r3395  
    2525import eu.dasish.annotation.backend.dao.UserDao;
    2626import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
     27import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
    2728import eu.dasish.annotation.backend.identifiers.UserIdentifier;
    2829import eu.dasish.annotation.schema.Annotation;
     
    3334import eu.dasish.annotation.schema.NewSourceInfo;
    3435import eu.dasish.annotation.schema.ResourceREF;
     36import eu.dasish.annotation.schema.Source;
    3537import eu.dasish.annotation.schema.SourceInfo;
     38import java.lang.String;
    3639import java.sql.ResultSet;
    3740import java.sql.SQLException;
     
    4447import javax.xml.datatype.DatatypeConfigurationException;
    4548import org.springframework.beans.factory.annotation.Autowired;
    46 import org.springframework.dao.DataAccessException;
    4749import org.springframework.jdbc.core.RowMapper;
    4850
     
    7274    public List<Number> retrieveSourceIDs(Number annotationID) {
    7375        String sql = "SELECT " + source_id + " FROM " + annotationsSourcesTableName + " WHERE " + annotation_id + "= ?";
    74         List<Number> result = getSimpleJdbcTemplate().query(sql, annotationSourceRowMapper, annotationID);
    75         return result;
    76     }
    77     private final RowMapper<Number> annotationSourceRowMapper = new RowMapper<Number>() {
     76        List<Number> result = getSimpleJdbcTemplate().query(sql, sourceIDRowMapper, annotationID);
     77        return result;
     78    }
     79    private final RowMapper<Number> sourceIDRowMapper = new RowMapper<Number>() {
    7880        @Override
    7981        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     
    8385    };
    8486
     87    ////////////////////////////////////////////////////////////////////////
    8588    @Override
    8689    public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UserIdentifier owner, Timestamp after, Timestamp before) {
     
    9295        if (link != null) {
    9396            List<Number> sourceIDs = jdbcSourceDao.getSourcesForLink(link);
    94             List<Number> annotationIDs = getAnnotationIDsForSources(sourceIDs);
     97            List<Number> annotationIDs = retrieveAnnotationList(sourceIDs);
    9598            if (!annotationIDs.isEmpty()) {
    9699                String values = makeListOfValues(annotationIDs);
     
    129132    //////////////////////////////
    130133    @Override
    131     public List<Number> getAnnotationIDsForSources(List<Number> sourceIDs) {
     134    public List<Number> retrieveAnnotationList(List<Number> sourceIDs) {
    132135        if (sourceIDs == null) {
    133136            return null;
     
    230233            result.setHeadline(rs.getString(headline));
    231234
    232             result.setBody(deserializeBody(rs.getString(body_xml)));
     235            result.setBody(Helpers.deserializeBody(rs.getString(body_xml)));
    233236
    234237            List<SourceInfo> sourceInfoList = jdbcSourceDao.getSourceInfos(retrieveSourceIDs(rs.getInt(annotation_id)));
    235             NewOrExistingSourceInfos noeSourceInfos = jdbcSourceDao.contructNewOrExistingSourceInfo(sourceInfoList);
     238            NewOrExistingSourceInfos noeSourceInfos = contructNewOrExistingSourceInfo(sourceInfoList);
    236239            result.setTargetSources(noeSourceInfos);
    237240
     
    267270        result[3] = 0; //removed "target_source" rows
    268271        for (Number sourceID : sourceIDs) {
    269             result[3] = result[3] + jdbcSourceDao.deleteSource(sourceID);
     272            int[] deleteSource = jdbcSourceDao.deleteSource(sourceID);
     273            result[3] = result[3] + deleteSource[1];
    270274        }
    271275
     
    313317        sql.append(annotationTableName).append(" SET ").append(body_xml).append("= ").append(serializedNewBody).append(" WHERE ").append(annotation_id).append("= ?");
    314318        return getSimpleJdbcTemplate().update(sql.toString(), annotationID);
    315     }
    316 
    317     //////////// helpers ///////////////////////
    318     /////////////////////////////////////////////////
    319     // TODO: change when serialization mechanism for bodies is fixed
    320     @Override
    321     public String serializeBody(AnnotationBody body) {
    322         return body.getAny().get(0).toString();
    323     }
    324 
    325     // TODO: change when serialization mechanism for bodies is fixed
    326     @Override
    327     public AnnotationBody deserializeBody(String bodyXml) {
    328         AnnotationBody result = new AnnotationBody();
    329         result.getAny().add(bodyXml);
    330         return result;
    331     }
    332 
    333     ///////////////////////////////////////
    334     @Override
    335     public String updateTargetRefsInBody(String serializedBody, Map<String, String> sourceIDPairs) {
    336         String result = (new StringBuilder(serializedBody)).toString();
    337         for (String tempSource : sourceIDPairs.keySet()) {
    338             result = result.replaceAll(tempSource, sourceIDPairs.get(tempSource));
    339         }
    340         return result;
    341319    }
    342320
     
    357335    public Map<String, Object> getRawAnnotation(Number annotationID) throws SQLException {
    358336
    359 
    360337        if (annotationID == null) {
    361338            return null;
     
    385362        }
    386363    };
     364   
     365   
     366    /////////////// helpers //////////////////
     367   
     368    public NewOrExistingSourceInfos contructNewOrExistingSourceInfo(List<SourceInfo> sourceInfoList) {
     369        List<NewOrExistingSourceInfo> noeSourceInfoList = new ArrayList<NewOrExistingSourceInfo>();
     370        for (SourceInfo sourceInfo : sourceInfoList) {
     371            NewOrExistingSourceInfo noeSourceInfo = new NewOrExistingSourceInfo();
     372            noeSourceInfo.setSource(sourceInfo);
     373            noeSourceInfoList.add(noeSourceInfo);
     374        }
     375        NewOrExistingSourceInfos result = new NewOrExistingSourceInfos();
     376        result.getTarget().addAll(noeSourceInfoList);
     377        return result;
     378    }
     379
    387380}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDao.java

    r3380 r3395  
    1919
    2020import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
     21import eu.dasish.annotation.backend.dao.VersionDao;
    2122import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
    2223import eu.dasish.annotation.schema.CachedRepresentationInfo;
     
    2728import java.util.Map;
    2829import javax.sql.DataSource;
     30import org.springframework.beans.factory.annotation.Autowired;
    2931import org.springframework.jdbc.core.RowMapper;
    3032
     
    3436 */
    3537public class JdbcCachedRepresentationDao extends JdbcResourceDao implements CachedRepresentationDao {
    36 
     38   
     39    @Autowired
     40    VersionDao jdbcVersionDao;
     41   
    3742    public JdbcCachedRepresentationDao(DataSource dataSource) {
    3843        setDataSource(dataSource);
     
    4651        return new CachedRepresentationIdentifier(super.getExternalIdentifier(internalID));
    4752    }
     53   
     54     //////////////////////////////////////////////////////////////////////////////////////////////////////
     55    @Override
     56    public Number getInternalID(CachedRepresentationIdentifier externalID) {
     57        return super.getInternalID(externalID);
     58    }
    4859
     60
     61    ///////////////////////////////////////////////////////////////////////////////////////
    4962    @Override
    5063    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
     
    6578        public CachedRepresentationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
    6679            CachedRepresentationInfo result = new CachedRepresentationInfo();
    67             //external_id, mime_type, tool, type_, where_is_the_file
    6880            result.setMimeType(rs.getString(mime_type));
    6981            result.setRef(rs.getString(external_id));
     
    7688
    7789    ////////////////////////////////////////////////////////////////////////////
    78     @Override
    79     public List<Number> retrieveCachedRepresentationList(Number versionID) {
    80         String sql = "SELECT " + cached_representation_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + version_id + "= ?";
    81         List<Number> result = getSimpleJdbcTemplate().query(sql, internalIDRowMapper, versionID);
    82 
    83         if (result == null) {
    84             return null;
    85         }
    86         if (result.isEmpty()) {
    87             return null;
    88         }
    89         return result;
    90     }
    91 
    92     ;
    93      
    94       ////////////////////////////////////////////////////////////////////////////
    9590    @Override
    9691    public Number addCachedRepresentationInfo(CachedRepresentationInfo cached) {
     
    111106    @Override
    112107    public int deleteCachedRepresentationInfo(Number internalID) {
    113         //check if there are versions referring to the cached with the "internalID"
    114         String sqlCheck = "SELECT " + cached_representation_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + cached_representation_id + "= ?";
    115         List<Number> result = getSimpleJdbcTemplate().query(sqlCheck, cachedRepresentationCheckerRowMapper, internalID);
     108       
     109        // ask the higher level if it can be deleted
     110        if (cachedIsInUse(internalID)){
     111           return 0;
     112        }
     113       
     114        String sql = "DELETE FROM " + cachedRepresentationTableName + " where " + cached_representation_id + " = ?";
     115        return  getSimpleJdbcTemplate().update(sql, internalID);
     116    }
     117   
     118    ////////////////////////////////////////////////
     119    @Override
     120    public Number[] addCachedForVersion(Number versionID, CachedRepresentationInfo cached){
     121        Number[] result = new Number[2];
     122        result[0] =  getInternalID(new CachedRepresentationIdentifier(cached.getRef()));       
     123        if (result[0] == null) {
     124            result[0] = addCachedRepresentationInfo(cached);
     125        }
     126        Map<String, Object> params = new HashMap<String, Object>();
     127        params.put("versionId", versionID);
     128        params.put("cachedId", result[0]);
     129        String sql = "INSERT INTO " + versionsCachedRepresentationsTableName + "(" + version_id + "," + cached_representation_id + " ) VALUES (:versionId, :cachedId)";
     130        result[1] = getSimpleJdbcTemplate().update(sql, params);
     131        return result;   
     132    }
     133   
     134     //////////////////////////////
     135   
     136    @Override
     137    public int[] deleteCachedForVersion(Number versionID,  Number cachedID){
     138         int[] result = new int[2];
     139         Map<String, Object> params = new HashMap<String, Object>();
     140         params.put("versionId", versionID);
     141         params.put("cachedId", cachedID);
     142         String sqlVersionsCachedRepresentations = "DELETE FROM " + versionsCachedRepresentationsTableName + " WHERE " + version_id + " = :versionId AND " + cached_representation_id + "= :cachedId";
     143         result[0] = getSimpleJdbcTemplate().update(sqlVersionsCachedRepresentations, params);
     144         
     145         if (result[0] > 0) {
     146               result[1] = deleteCachedRepresentationInfo(cachedID);
     147         }
     148            else {
     149                result[1]=0;
     150           
     151        }
     152        return result;
     153    }
     154   
     155       //////////////////////////////
    116156
    117         if (result.isEmpty()) {
    118             // you can remove the cached representation
    119             String sql = "DELETE FROM " + cachedRepresentationTableName + " where " + cached_representation_id + " = ?";
    120             return getSimpleJdbcTemplate().update(sql, internalID);
    121         } else {
    122             // do not delete, it is in use!!
    123             return 0;
     157    private boolean cachedIsInUse(Number cachedID) {     
     158        String sql = "SELECT " + version_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + cached_representation_id + "= ? LIMIT 1";
     159        List<Number> result = getSimpleJdbcTemplate().query(sql, versionIDRowMapper, cachedID);
     160        if (result.size() > 0) {
     161            return true;
    124162        }
     163        return false;
    125164    }
    126     private final RowMapper<Number> cachedRepresentationCheckerRowMapper = new RowMapper<Number>() {
     165   
     166    private final RowMapper<Number> versionIDRowMapper = new RowMapper<Number>() {
    127167        @Override
    128168        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    129             Number result = rs.getInt(cached_representation_id);
    130             return result;
     169            return rs.getInt(version_id);
    131170        }
    132171    };
    133 
     172   
     173   
    134174}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java

    r3380 r3395  
    134134        return (sqlResult.get(0));
    135135    }
     136   
    136137    protected final RowMapper<String> externalIDRowMapper = new RowMapper<String>() {
    137138        @Override
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java

    r3380 r3395  
    6161    }
    6262
    63         ///////////////////////////////////////////////////////////////////////////////
     63    ///////////////////////////////////////////////////////////////////////////////
    6464    @Override
    6565    public Source getSource(Number internalID) {
     
    7777                return result;
    7878            } catch (DatatypeConfigurationException e) {
    79                 // TODO: what logger are we going to use
    80                 System.out.println("Cannot construct time stam: probably worng date/time format");
     79                // TODO: which logger are we going to use?
     80                System.out.println("Cannot construct time stamp: probably worng date/time format");
    8181                return null;
    8282            }
    8383        }
    8484    };
    85    
    86         ///////////////////////////////////////////////////////////////////////////////
    87     @Override
    88     public Map<String, Object> getRawSource(Number internalID) {
    89         String sql = "SELECT " + sourceStar + "FROM " + sourceTableName + " WHERE " + source_id + " = ?";
    90         List<Map<String, Object>> result = getSimpleJdbcTemplate().query(sql, rawSourceRowMapper, internalID);
    91         return result.get(0);
    92     }
    93     private final RowMapper<Map<String, Object>> rawSourceRowMapper = new RowMapper<Map<String, Object>>() {
    94         @Override
    95         public Map<String, Object> mapRow(ResultSet rs, int rowNumber) throws SQLException {
    96            Map<String, Object> result = new HashMap<String, Object>();
    97            result.put(external_id, rs.getString(external_id));
    98            result.put(link_uri, rs.getString(link_uri));
    99            result.put(version_id, rs.getInt(version_id));
    100            return result;
    101         }
    102     };
    103 
     85
     86   
    10487    ///////////////////////////////////////////////////////////////////
    10588    @Override
    106     public int deleteSource(Number internalID) {
    107 
    108         // check if there are annotations referring to the source with "internalID", in the table "annotations_sources"
    109         String sqlAnnotationsSources = "SELECT " + annotation_id + " FROM " + annotationsSourcesTableName + " WHERE " + source_id + "= ?";
    110         List<Number> resultAnnotationsSources = getSimpleJdbcTemplate().query(sqlAnnotationsSources, annotationsSourcesRowMapper, internalID);
    111 
    112         if (resultAnnotationsSources.isEmpty()) {
    113 
    114             // You can remove the source!
    115 
    116             // retrieve the list of versions of the source to be deleted
    117             List<Number> versions = versionDao.retrieveVersionList(internalID);
    118 
    119             // remove all the pairs (internalID, version_id) from the joint table
    120             deleteSourceVersionRows(internalID);
    121             // the main action: remove the source with internalID from "source" table
    122             String sql = "DELETE FROM " + sourceTableName + " where " + source_id + " = ?";
    123             int affected_source_rows = getSimpleJdbcTemplate().update(sql, internalID);
    124 
    125             // remove the versions of "versions" from the DB unless they are still mentioned in "sources_versions"
    126             for (Number versionID : versions) {
    127                 versionDao.deleteVersion(versionID);
    128             }
    129 
    130             return (affected_source_rows);
    131         } else {
    132             // do not remove
    133             return 0;
    134         }
    135     }
    136     private final RowMapper<Number> annotationsSourcesRowMapper = new RowMapper<Number>() {
    137         @Override
    138         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    139             Number result = rs.getInt(annotation_id);
     89    public int[] deleteSource(Number internalID) {
     90
     91        int[] result = new int[2];
     92       
     93        if (sourceIsInUse(internalID)){
     94            result[0] =0;
     95            result[1] =0;
    14096            return result;
    14197        }
    142     };
     98
     99        List<Number> versions = retrieveVersionList(internalID);
     100        String sqlSourcesVersions = "DELETE FROM " + sourcesVersionsTableName + " WHERE " + source_id + " = ?";
     101        result[0] = getSimpleJdbcTemplate().update(sqlSourcesVersions, internalID);
     102
     103        String sql = "DELETE FROM " + sourceTableName + " WHERE " + source_id + " = ?";
     104        result[1] = getSimpleJdbcTemplate().update(sql, internalID);
     105
     106        for (Number versionID : versions) {
     107            versionDao.deleteVersion(versionID);
     108        }
     109
     110        return result;
     111
     112    }
    143113
    144114    ///////////////////////////////////////////////////////////////////
    145115    @Override
    146     public Number addSource(Source freshSource) throws SQLException{
    147      
     116    public Number addSource(Source freshSource) throws SQLException {
     117
    148118        SourceIdentifier externalIdentifier = new SourceIdentifier();
    149119        Number versionID = versionDao.getInternalID(new VersionIdentifier(freshSource.getVersion()));
    150        
     120
    151121        if (versionID == null) {
    152122            System.out.println("Cannot add source because there is no version for it, and no cached representation. Create them and try again.");
     
    160130        String sql = "INSERT INTO " + sourceTableName + "(" + external_id + "," + link_uri + "," + version_id + " ) VALUES (:externalId, :linkUri,  :versionId)";
    161131        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
    162        
    163        
     132
     133
    164134        Map<String, Object> paramsJoint = new HashMap<String, Object>();
    165135        paramsJoint.put("sourceId", getInternalID(externalIdentifier));
    166136        paramsJoint.put("versionId", versionID);
    167         String sqlJoint = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id +"," + version_id + " ) VALUES (:sourceId, :versionId)";
     137        String sqlJoint = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id + "," + version_id + " ) VALUES (:sourceId, :versionId)";
    168138        final int affectedJointRows = getSimpleJdbcTemplate().update(sqlJoint, paramsJoint);
    169        
    170        
    171         return(getInternalID(externalIdentifier));
    172     }
    173 
    174     ////////////////////////////////////////////////////////////////
     139
     140
     141        return (getInternalID(externalIdentifier));
     142    }
     143
     144    /////////////////////////////////////////
     145    @Override
     146    public List<Number> retrieveVersionList(Number sourceID) {
     147        String sql = "SELECT " + version_id + " FROM " + sourcesVersionsTableName + " WHERE " + source_id + " = ?";
     148        List<Number> result = getSimpleJdbcTemplate().query(sql, versionsSourcesRunnerRowMapper, sourceID);
     149        return result;
     150    }
     151    private final RowMapper<Number> versionsSourcesRunnerRowMapper = new RowMapper<Number>() {
     152        @Override
     153        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     154            Number result = rs.getInt(version_id);
     155            return result;
     156        }
     157    };
     158
    175159    ///////////////////////////////////////////////////////////////////
    176160    @Override
     
    182166            return new ArrayList<SourceInfo>();
    183167        }
    184        
     168
    185169        String sourceIDs = makeListOfValues(sources);
    186170        String sql = "SELECT " + external_id + "," + link_uri + "," + version_id + " FROM " + sourceTableName + " WHERE " + source_id + " IN " + sourceIDs;
     
    195179    };
    196180
    197     //////////////////////////////////////////
    198     @Override
    199     public NewOrExistingSourceInfos contructNewOrExistingSourceInfo(List<SourceInfo> sourceInfoList) {
    200         List<NewOrExistingSourceInfo> noeSourceInfoList = new ArrayList<NewOrExistingSourceInfo>();
    201         for (SourceInfo sourceInfo : sourceInfoList) {
    202             NewOrExistingSourceInfo noeSourceInfo = new NewOrExistingSourceInfo();
    203             noeSourceInfo.setSource(sourceInfo);
    204             noeSourceInfoList.add(noeSourceInfo);
    205         }
    206         NewOrExistingSourceInfos result = new NewOrExistingSourceInfos();
    207         result.getTarget().addAll(noeSourceInfoList);
    208         return result;
    209     }
     181 
     182    /////////////////////////////////////////////////////
     183    @Override
     184    public List<Number> getSourcesForLink(String link) {
     185        StringBuilder sql = new StringBuilder("SELECT ");
     186        sql.append(source_id).append(" FROM ").append(sourceTableName).append(" WHERE ").append(link_uri).append(" LIKE '%").append(link).append("%'");
     187        List<Number> result = getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper);
     188        return result;
     189    }
     190
     191    ///////////////////////////////////////////////
     192    @Override
     193    public Map<String, String> addTargetSourcesToAnnotation(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {
     194        Map<String, String> result = new HashMap<String, String>();
     195        for (NewOrExistingSourceInfo noesi : sources) {
     196            SourceInfo source = noesi.getSource();
     197            if (source != null) {
     198                int affectedRows = addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(source.getRef())));
     199            } else {
     200                Number newSourceID = addNewSourceToAnnotation(annotationID, noesi.getNewSource());
     201                if (newSourceID.intValue() == -1) {
     202                    result.put(noesi.getNewSource().getId(), null);
     203                } else {
     204                    result.put(noesi.getNewSource().getId(), getExternalID(newSourceID).toString());
     205                    int affectedRows = addAnnotationSourcePair(annotationID, newSourceID);
     206                }
     207            }
     208        }
     209        return result;
     210    }
     211
     212    //////////// helpers ///////////////////////
     213    /////////////////////////////////////////////////
    210214   
     215      //////////////////////////////
     216    private boolean sourceIsInUse(Number sourceID) {
     217        String sql = "SELECT " + annotation_id + " FROM " + annotationsSourcesTableName + " WHERE " + source_id + "= ? LIMIT 1";
     218        List<Number> result = getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, sourceID);
     219        if (result.size() > 0) {
     220            return true;
     221        }
     222        return false;
     223    }
    211224   
     225      private final RowMapper<Number> annotationIDRowMapper = new RowMapper<Number>() {
     226        @Override
     227        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     228            return rs.getInt(annotation_id);
     229        }
     230    };
    212231   
    213 
    214     /////////////////////////////////////////////////
    215     @Override
    216     public int deleteSourceVersionRows(Number sourceID) {
    217         // remove all the pairs (internalID, version_id) from the joint table       
    218         String sqlSourcesVersions = "DELETE FROM " + sourcesVersionsTableName + " where " + source_id + " = ?";
    219         return (getSimpleJdbcTemplate().update(sqlSourcesVersions, sourceID));
    220 
    221     }
    222    
    223    
    224    
    225    
    226    
    227      ////////////////////////////////////////////////////////////////////////
    228     @Override
    229     public Map<String, String> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {
    230 
    231         Map<String, String> result = new HashMap<String, String>();
    232        
    233         for (NewOrExistingSourceInfo noeSourceInfo : sources) {
    234             SourceInfo sourceInfo = noeSourceInfo.getSource();
    235             if (sourceInfo != null) {
    236                 // this is an old source, already exists in the DB             
    237                 addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(sourceInfo.getRef())));
    238             } else {
    239                 Source newSource = constructNewSource(noeSourceInfo.getNewSource());
    240                 Number addedSourceID = addSource(newSource);// adding new source
    241                 int affectedRows = addAnnotationSourcePair(annotationID, addedSourceID);             
    242                 result.put(noeSourceInfo.getNewSource().getId(), getExternalID(addedSourceID).toString());
    243             }
    244            
    245         }
    246         return result;
    247     }
    248 
    249     @Override
    250     public List<Number> getSourcesForLink(String link){
    251       StringBuilder sql = new StringBuilder("SELECT ");
    252       sql.append(source_id).append(" FROM ").append(sourceTableName).append(" WHERE ").append(link_uri).append(" LIKE '%").append(link).append("%'");
    253       List<Number> result = getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper);
    254       return result;
    255     }
    256    
    257  
    258     //////// HELPERS //////////////////////
    259     ////////////////////////////////////////////////////////
    260    
    261  
    262     private int addAnnotationSourcePair(Number annotationID, Number sourceID) throws SQLException{
    263         // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated
     232    private int addAnnotationSourcePair(Number annotationID, Number sourceID) throws SQLException {
    264233        Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>();
    265234        paramsAnnotationsSources.put("annotationId", annotationID);
    266         paramsAnnotationsSources.put("sourceId",  sourceID);
     235        paramsAnnotationsSources.put("sourceId", sourceID);
    267236        String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)";
    268237        int affectedRows = getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources);
    269         if (affectedRows != 1) {
    270             throw (new SQLException("Cannot add annotation properly"));
    271 
    272         }       
    273238        return (affectedRows);
    274239    }
    275    
    276    
    277    
     240    //////////////////////////////////////////////////////////////////////////////////
     241
     242    private Number addNewSourceToAnnotation(Number annotationID, NewSourceInfo source) throws SQLException {
     243        Source newSource = new Source();
     244        newSource.setLink(source.getLink());
     245        newSource.setVersion(source.getVersion());
     246        Number addedSourceID = addSource(newSource);// adding new source
     247        if (addedSourceID.intValue() == -1) {
     248            return -1;
     249        }
     250        int affectedRows = addAnnotationSourcePair(annotationID, addedSourceID);
     251        return addedSourceID;
     252    }
     253
    278254    private SourceInfo constructSourceInfo(SourceIdentifier sourceIdentifier, String link, VersionIdentifier versionIdentifier) {
    279255        SourceInfo sourceInfo = new SourceInfo();
     
    284260    }
    285261
    286       ///////////////////
    287     private Source constructNewSource(NewSourceInfo newSourceInfo) {
    288         Source result = new Source();
    289         result.setLink(newSourceInfo.getLink());
    290         result.setVersion(newSourceInfo.getVersion());
    291         // source's internalID will be generated by the DB
    292         // source's time stamp will be generated by the DB
    293         // source's externalID will be generated by the add-source-dao
    294         return result;
    295     }
    296    
    297    
    298262    private Source constructSource(SourceIdentifier sourceIdentifier, String link, VersionIdentifier version, XMLGregorianCalendar xmlTimeStamp) {
    299263        Source source = new Source();
     
    305269        return source;
    306270    }
    307 
    308     // TODO: make deep copy for source, otherwise testing will be unfair!!
    309     private Source makeFreshCopy(Source source) {
    310         Source result = new Source();
    311         result.setLink(source.getLink());
    312         result.setURI(source.getURI());
    313         result.setVersion(source.getVersion());
    314         result.setTimeSatmp(source.getTimeSatmp());
    315         //versions-siblings are mentioned in the table sources_versions
    316         return result;
    317     }
    318    
    319    
    320271}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcVersionDao.java

    r3380 r3395  
    2020import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
    2121import eu.dasish.annotation.backend.dao.VersionDao;
     22import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
    2223import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
     24import eu.dasish.annotation.schema.CachedRepresentationInfo;
    2325import eu.dasish.annotation.schema.Version;
    2426import java.sql.ResultSet;
     
    5153        return new VersionIdentifier(super.getExternalIdentifier(internalID));
    5254    }
    53    
    54       //////////////////////////////////////////////////////////////////////////////////////////////////////   
     55
     56    //////////////////////////////////////////////////////////////////////////////////////////////////////   
    5557    @Override
    5658    public Number getInternalID(VersionIdentifier externalID) {
    5759        return (super.getInternalID(externalID));
    5860    }
    59    
     61
    6062    ///////////////////////////////////////////////////////////////
    61 
    6263    @Override
    6364    public Version getVersion(Number internalID) {
    6465
    65         String sql = "SELECT " + versionStar + " FROM " + versionTableName + " WHERE " + version_id + "= ?";
     66        String sql = "SELECT " + versionStar + " FROM " + versionTableName + " WHERE " + version_id + "= ? LIMIT 1";
    6667        List<Version> result = getSimpleJdbcTemplate().query(sql, versionRowMapper, internalID);
    6768
     
    8687    };
    8788
    88     /////////////////////////////////////////
     89    ////////////////////////////////////////////////////////////////////////////
    8990    @Override
    90     public List<Number> retrieveVersionList(Number sourceID) {
    91         String sql = "SELECT " + version_id + " FROM " + sourcesVersionsTableName + " WHERE " + source_id + " = ?";
    92         List<Number> result = getSimpleJdbcTemplate().query(sql, versionsSourcesRunnerRowMapper, sourceID);
     91    public List<Number> retrieveCachedRepresentationList(Number versionID) {
     92        String sql = "SELECT " + cached_representation_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + version_id + "= ?";
     93        List<Number> result = getSimpleJdbcTemplate().query(sql, cachedIDRowMapper, versionID);
     94
     95        if (result == null) {
     96            return null;
     97        }
     98
    9399        return result;
    94100    }
    95     private final RowMapper<Number> versionsSourcesRunnerRowMapper = new RowMapper<Number>() {
     101
     102    private final RowMapper<Number> cachedIDRowMapper = new RowMapper<Number>() {
    96103        @Override
    97104        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    98             Number result = rs.getInt(version_id);
     105            return rs.getInt(cached_representation_id);
     106        }
     107    };
     108   
     109    /////////////////////////////////////////
     110    @Override
     111    public int[] deleteVersion(Number internalID) {
     112       
     113        int[] result = new int[3];
     114       
     115        if (versionIsInUse(internalID)) {
     116            // firs delete sources that refer to it!
     117            result[0]=0;
     118            result[1]=0;
     119            result[2]=0;
    99120            return result;
    100121        }
    101     };
     122       
     123        // retrieve the list of cached representations of the version to be deleted;
     124        // they should be deleted if they are not used
     125        // you will use it over the next 2 steps
     126        List<Number> cachedRepresentations = retrieveCachedRepresentationList(internalID);
    102127
    103     /////////////////////////////////////////
    104     @Override
    105     public int deleteVersionCachedRepresentationRow(Number versionID) {
    106         // remove all the pairs (internalID, cached_representation) from the joint table       
     128        // remove all the pairs (internalID, cached_representation) from the joint table "versions_cahched_representations"   
    107129        String sqlVersionsCachedRepresentations = "DELETE FROM " + versionsCachedRepresentationsTableName + " where " + version_id + " = ?";
    108         return (getSimpleJdbcTemplate().update(sqlVersionsCachedRepresentations, versionID));
     130        result[0] = getSimpleJdbcTemplate().update(sqlVersionsCachedRepresentations, internalID);
     131       
     132        // the main action: remove the version with internalID from "version" table
     133        String sql = "DELETE FROM " + versionTableName + " where " + version_id + " = ?";
     134        result[1] = getSimpleJdbcTemplate().update(sql, internalID);
     135
     136        // remove the cached representations of "cachedRepresentations" from the DB unless they are still mentioned in "versions_cached_representations"
     137        result[2]=0;
     138        for (Number cachedID : cachedRepresentations) {
     139             result[2]=result[2]+jdbcCachedRepresentationDao.deleteCachedRepresentationInfo(cachedID);
     140           
     141        }
     142        return result;
     143
    109144    }
    110 
    111     /////////////////////////////////////////
    112     //TODO: refactor ???
    113     @Override
    114     public int deleteVersion(Number internalID) {
    115 
    116         // check if there are sources referring to the version with "internalID", in the table "sources"versions"
    117         String sqlSourcesVersions = "SELECT " + source_id + " FROM " + sourcesVersionsTableName + " WHERE " + version_id + "= ?";
    118         List<Number> resultSourcesVersions = getSimpleJdbcTemplate().query(sqlSourcesVersions, sourcesVersionsRowMapper, internalID);
    119 
    120         // check if there is a source referring to the version "intrenalID", in the table "source"
    121         String sqlSource = "SELECT " + source_id + " FROM " + sourceTableName + " WHERE " + version_id + "= ?";
    122         List<Number> resultSource = getSimpleJdbcTemplate().query(sqlSource, sourceRowMapper, internalID);
    123 
    124 
    125         if (resultSourcesVersions.isEmpty() && resultSource.isEmpty()) {
    126 
    127             // You can remove the version safely!!!
    128 
    129             // retrieve the list of cached representations of the version to be deleted
    130             List<Number> cachedRepresentations = jdbcCachedRepresentationDao.retrieveCachedRepresentationList(internalID);
    131 
    132             // remove all the pairs (internalID, cached_representation) from the joint table       
    133             deleteVersionCachedRepresentationRow(internalID);
    134 
    135             // the main action: remove the version with internalID from "version" table
    136             String sql = "DELETE FROM " + versionTableName + " where " + version_id + " = ?";
    137             int affected_version_rows = getSimpleJdbcTemplate().update(sql, internalID);
    138 
    139             // remove the cached representations of "cachedRepresentations" from the DB unless they are still mentioned in "versions_cached_representations"
    140             for (Number cachedID : cachedRepresentations) {
    141                 jdbcCachedRepresentationDao.deleteCachedRepresentationInfo(cachedID);
    142             }
    143 
    144             return (affected_version_rows);
    145         } else {
    146             // do not remove
    147             return 0;
    148         }
    149     }
    150     private final RowMapper<Number> sourcesVersionsRowMapper = new RowMapper<Number>() {
    151         @Override
    152         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    153             Number result = rs.getInt(source_id);
    154             return result;
    155         }
    156     };
    157     private final RowMapper<Number> sourceRowMapper = new RowMapper<Number>() {
    158         @Override
    159         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    160             Number result = rs.getInt(source_id);
    161             return result;
    162         }
    163     };
    164145
    165146    /////////////////////////////////////////////////
     
    177158        return getInternalID(externalIdentifier);
    178159    }
    179 
    180     @Override
    181     public int[] deleteCachedRepresentationForSource(Number sourceID, Number cachedRepresentationID) {
    182         List<Number> versions = retrieveVersionList(sourceID);
    183         int[] result = new int[2];
    184 
    185         if (versions == null) {
    186             result[0] = 0;
    187             result[1] = 0;
    188             return result;
     160   
     161 
     162 
     163   
     164    //////////////////////////////
     165    private boolean versionIsInUse(Number versionsID) {
     166        String sql = "SELECT " + source_id + " FROM " + sourcesVersionsTableName + " WHERE " + version_id + "= ? LIMIT 1";
     167        List<Number> result = getSimpleJdbcTemplate().query(sql, sourceIDRowMapper, versionsID);
     168        if (result.size() > 0) {
     169            return true;
    189170        }
    190         if (versions.isEmpty()) {
    191             result[0] = 0;
    192             result[1] = 0;
    193             return result;
     171        return false;
     172    }
     173   
     174     private final RowMapper<Number> sourceIDRowMapper = new RowMapper<Number>() {
     175        @Override
     176        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     177            return rs.getInt(source_id);
    194178        }
    195 
    196         String values = makeListOfValues(versions);
    197         StringBuilder sql = new StringBuilder("DELETE FROM ");
    198         sql.append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append(" IN ").append(values);
    199         sql.append(" AND ").append(cached_representation_id).append(" = ?");
    200         result[0] = getSimpleJdbcTemplate().update(sql.toString(), cachedRepresentationID);
    201 
    202         // safe remove from the DB
    203         if (result[0] > 0) {
    204             result[1] = jdbcCachedRepresentationDao.deleteCachedRepresentationInfo(cachedRepresentationID);
    205         } else {
    206             result[1] = 0;
    207         }
    208         return result;
    209     }
    210 
    211    
     179    };
    212180}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r3380 r3395  
    1919
    2020import eu.dasish.annotation.backend.BackendConstants;
     21import eu.dasish.annotation.backend.Helpers;
    2122import eu.dasish.annotation.backend.dao.AnnotationDao;
    2223import eu.dasish.annotation.backend.dao.NotebookDao;
     
    2728import eu.dasish.annotation.backend.identifiers.UserIdentifier;
    2829import eu.dasish.annotation.schema.Annotation;
     30import eu.dasish.annotation.schema.AnnotationBody;
    2931import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    3032import eu.dasish.annotation.schema.ObjectFactory;
     
    115117        }
    116118
     119       
     120        //Add annotation
    117121        Number annotationID = annotationDao.addAnnotation(annotation, userID);
    118122
    119         // adding sources to the DB if necessary and updating the joint table annotations_target_sources
    120         // also, since the source id-s can bementioned in the body, update the body as well
    121         // Note that client provided serialization "NewOrExistingSourceInfo" (of type choice) allows to switch between
    122         // new and existing sources
     123        //Add the sources to the DB
    123124        List<NewOrExistingSourceInfo> sources = annotation.getTargetSources().getTarget();
    124         Map<String, String> sourcePairs = sourceDao.addTargetSources(annotationID, sources);
    125         String body = annotationDao.serializeBody(annotation.getBody());
    126         String newBody = annotationDao.updateTargetRefsInBody(body, sourcePairs);
    127         if (!body.equals(newBody)) {
    128             annotationDao.updateBody(annotationID, newBody);
    129         };
    130 
     125        Map<String, String> sourceIdPairs= sourceDao.addTargetSourcesToAnnotation(annotationID, sources);
     126       
     127        if (sourceIdPairs.containsValue(null)){
     128           // for one of the soirces there was no version and cached representation
     129            // envelope
     130           return (new ObjectFactory().createAnnotation(null));
     131        }
     132        String body = Helpers.serializeBody(annotation.getBody());
     133        String newBody = Helpers.replace(body, sourceIdPairs);
     134        int affectedAnnotRows = annotationDao.updateBody(annotationID, newBody);
     135       
     136        // Add the permission (annotation_id, owner);
    131137        int affectedPermissions = permissionsDao.addAnnotationPrincipalPermission(annotationDao.getExternalID(annotationID), new UserIdentifier(remoteUser), Permission.OWNER);
    132         if (affectedPermissions != 1) {
    133             System.out.println("Cannot update permission table");
    134             return null;
    135         }
     138       
    136139        Annotation newAnnotation = annotationDao.getAnnotation(annotationID);
    137140        return (new ObjectFactory().createAnnotation(newAnnotation));
    138141    }
     142   
     143   
    139144}
Note: See TracChangeset for help on using the changeset viewer.