Changeset 3380 for DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java
- Timestamp:
- 08/14/13 16:34:29 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java
r3372 r3380 61 61 } 62 62 63 @Override 64 public List<Number> retrieveSourceIDs(Number annotationID) { 65 String sql = "SELECT " + source_id + " FROM " + annotationsSourcesTableName + " WHERE " + annotation_id + "= ?"; 66 List<Number> result = getSimpleJdbcTemplate().query(sql, annotationSourceRowMapper, annotationID); 67 return result; 68 } 69 private final RowMapper<Number> annotationSourceRowMapper = new RowMapper<Number>() { 70 @Override 71 public Number mapRow(ResultSet rs, int rowNumber) throws SQLException { 72 Number result = rs.getInt(source_id); 73 return result; 74 } 75 }; 76 77 /////////////////////////////////////////////////////////////////////////////// 63 /////////////////////////////////////////////////////////////////////////////// 78 64 @Override 79 65 public Source getSource(Number internalID) { 80 66 String sql = "SELECT " + sourceStar + "FROM " + sourceTableName + " WHERE " + source_id + " = ?"; 81 List<Source> result = getSimpleJdbcTemplate().query(sql, SourceRowMapper, internalID);67 List<Source> result = getSimpleJdbcTemplate().query(sql, sourceRowMapper, internalID); 82 68 return result.get(0); 83 69 } 84 private final RowMapper<Source> SourceRowMapper = new RowMapper<Source>() {70 private final RowMapper<Source> sourceRowMapper = new RowMapper<Source>() { 85 71 @Override 86 72 public Source mapRow(ResultSet rs, int rowNumber) throws SQLException { … … 97 83 } 98 84 }; 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 }; 99 103 100 104 /////////////////////////////////////////////////////////////////// … … 140 144 /////////////////////////////////////////////////////////////////// 141 145 @Override 142 public SourceaddSource(Source freshSource) throws SQLException{143 146 public Number addSource(Source freshSource) throws SQLException{ 147 144 148 SourceIdentifier externalIdentifier = new SourceIdentifier(); 149 Number versionID = versionDao.getInternalID(new VersionIdentifier(freshSource.getVersion())); 150 151 if (versionID == null) { 152 System.out.println("Cannot add source because there is no version for it, and no cached representation. Create them and try again."); 153 return -1; 154 } 145 155 146 156 Map<String, Object> params = new HashMap<String, Object>(); 147 157 params.put("externalId", externalIdentifier.toString()); 148 158 params.put("linkUri", freshSource.getLink()); 149 params.put("versionId", freshSource.getVersion());159 params.put("versionId", versionID); 150 160 String sql = "INSERT INTO " + sourceTableName + "(" + external_id + "," + link_uri + "," + version_id + " ) VALUES (:externalId, :linkUri, :versionId)"; 151 161 final int affectedRows = getSimpleJdbcTemplate().update(sql, params); 152 162 163 153 164 Map<String, Object> paramsJoint = new HashMap<String, Object>(); 154 165 paramsJoint.put("sourceId", getInternalID(externalIdentifier)); 155 paramsJoint.put("versionId", freshSource.getVersion()); 156 String sqlSourceVersion = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id + "," + version_id + " ) VALUES (:sourceId, :versionId)"; 157 int affectedRowsJoint = getSimpleJdbcTemplate().update(sqlSourceVersion, paramsJoint); 158 159 if (affectedRows == 1 && affectedRowsJoint == 1) { 160 Source result = makeFreshCopy(freshSource); 161 result.setURI(externalIdentifier.toString()); 162 163 //retrieve taime stamp for the just added annotation 164 XMLGregorianCalendar timeStamp =this.retrieveTimeStamp(getInternalID(new SourceIdentifier(externalIdentifier.toString()))); 165 result.setTimeSatmp(timeStamp); 166 167 return result; 168 } else { 169 throw new SQLException("Cannot add the source"); 170 } 171 166 paramsJoint.put("versionId", versionID); 167 String sqlJoint = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id +"," + version_id + " ) VALUES (:sourceId, :versionId)"; 168 final int affectedJointRows = getSimpleJdbcTemplate().update(sqlJoint, paramsJoint); 169 170 171 return(getInternalID(externalIdentifier)); 172 172 } 173 173 … … 175 175 /////////////////////////////////////////////////////////////////// 176 176 @Override 177 public List<SourceInfo> getSourceInfos(Number annotationID) { 178 List<Number> sources = retrieveSourceIDs(annotationID); 177 public List<SourceInfo> getSourceInfos(List<Number> sources) { 179 178 if (sources == null) { 180 179 return null; … … 228 227 //////////////////////////////////////////////////////////////////////// 229 228 @Override 230 public Map< NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {231 232 Map< NewOrExistingSourceInfo, NewOrExistingSourceInfo> result = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();229 public Map<String, String> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException { 230 231 Map<String, String> result = new HashMap<String, String>(); 233 232 234 233 for (NewOrExistingSourceInfo noeSourceInfo : sources) { 235 234 SourceInfo sourceInfo = noeSourceInfo.getSource(); 236 235 if (sourceInfo != null) { 237 // this is an old source, already exists in the DB 238 result.put(noeSourceInfo, noeSourceInfo); 236 // this is an old source, already exists in the DB 239 237 addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(sourceInfo.getRef()))); 240 238 } else { 241 239 Source newSource = constructNewSource(noeSourceInfo.getNewSource()); 242 Source addedSource = addSource(newSource); 243 int affectedRows = addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(addedSource.getURI()))); 244 245 // create updated source info 246 SourceInfo updatedSourceInfo = new SourceInfo(); 247 updatedSourceInfo.setLink(addedSource.getLink()); 248 updatedSourceInfo.setRef(addedSource.getURI()); 249 updatedSourceInfo.setVersion(addedSource.getVersion()); 250 251 NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo(); 252 updatedInfo.setSource(updatedSourceInfo); 253 result.put(noeSourceInfo, updatedInfo); 240 Number addedSourceID = addSource(newSource);// adding new source 241 int affectedRows = addAnnotationSourcePair(annotationID, addedSourceID); 242 result.put(noeSourceInfo.getNewSource().getId(), getExternalID(addedSourceID).toString()); 254 243 } 255 244
Note: See TracChangeset
for help on using the changeset viewer.