Changeset 3167
- Timestamp:
- 07/19/13 09:56:04 (11 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java
r3152 r3167 58 58 */ 59 59 Number getAnnotationID(AnnotationIdentifier externalID) throws SQLException; 60 61 /** 62 * 63 * @param annotationId 64 * @return the amount of deleted sources; removes _aid_ from the DB, together with its tagrget sources to 65 * which no other annotations refers. 66 */ 67 68 public int deleteNotebook(Number annotationId) throws SQLException; 69 60 70 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/NotebookDao.java
r3154 r3167 63 63 64 64 65 65 66 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r3154 r3167 43 43 public class JdbcAnnotationDao extends JdbcResourceDao implements AnnotationDao { 44 44 45 45 46 public JdbcAnnotationDao(DataSource dataSource) { 46 47 setDataSource(dataSource); … … 71 72 72 73 String values = makeListOfValues(annotationIDs); 73 String sql = "SELECT annotation.* FROM annotation WHERE annotation.annotation_idIN "+values;74 String sql = "SELECT "+annotationStar+" FROM "+annotationTableName +" WHERE "+annotationAnnotation_id+" IN "+values; 74 75 return getSimpleJdbcTemplate().query(sql, annotationInfoRowMapper); 75 76 } … … 79 80 public AnnotationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { 80 81 AnnotationInfo annotationInfo = new AnnotationInfo(); 81 annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt( "owner_id"))));82 annotationInfo.setHeadline(rs.getString( "headline"));83 annotationInfo.setTargetSources(getSources(rs.getString( "body_xml")));82 annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt(owner_id)))); 83 annotationInfo.setHeadline(rs.getString(headline)); 84 annotationInfo.setTargetSources(getSources(rs.getString(body_xml))); 84 85 return annotationInfo; 85 86 } … … 109 110 110 111 String values = makeListOfValues(annotationIDs); 111 String sql = "SELECT annotation.annotation_id FROM annotation WHERE annotation.annotation_idIN "+values;112 String sql = "SELECT "+annotationAnnotation_id+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id+" IN "+values; 112 113 return getSimpleJdbcTemplate().query(sql, annotationREFRowMapper); 113 114 } … … 117 118 public ResourceREF mapRow(ResultSet rs, int rowNumber) throws SQLException { 118 119 ResourceREF annotationREF = new ResourceREF(); 119 annotationREF.setRef(Integer.toString(rs.getInt( "annotation_id")));120 annotationREF.setRef(Integer.toString(rs.getInt(annotation_id))); 120 121 return annotationREF; 121 122 } … … 136 137 return null; 137 138 } 138 String sql = "SELECT * FROM annotation WHERE annotation.annotation_id= ?";139 String sql = "SELECT "+annotationStar+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id +"= ?"; 139 140 List<Annotation> result= getSimpleJdbcTemplate().query(sql, annotationRowMapper, annotationID); 140 141 … … 147 148 148 149 if (result.size()>1) { 149 throw new SQLException("There are "+result.size()+" annotations with annotation_id"+annotationID);150 throw new SQLException("There are "+result.size()+" annotations with "+ annotation_id + " "+annotationID); 150 151 } 151 152 return result.get(0); … … 156 157 public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException { 157 158 Annotation result = new Annotation(); 158 result.setHeadline(rs.getString( "headline"));159 result.setHeadline(rs.getString(headline)); 159 160 160 161 ResourceREF ownerREF = new ResourceREF(); 161 ownerREF.setRef(String.valueOf(rs.getInt( "owner_id")));162 ownerREF.setRef(String.valueOf(rs.getInt(owner_id))); 162 163 result.setOwner(ownerREF); 163 164 … … 173 174 // TODO add external reference 174 175 175 result.setBody(convertToAnnotationBody(rs.getString( "body_xml")));176 result.setBody(convertToAnnotationBody(rs.getString(body_xml))); 176 177 return result; 177 178 } … … 198 199 } 199 200 200 String sql = "SELECT annotation.annotation_id FROM annotation WHERE annotation.external_id= ?";201 String sql = "SELECT "+annotationAnnotation_id+" FROM "+annotationTableName+" WHERE "+annotationExternal_id+" = ?"; 201 202 List<Number> result= getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, externalID.toString()); 202 203 if (result == null) { … … 208 209 209 210 if (result.size()>1) { 210 throw new SQLException("There are "+result.size()+" annotations with external_id"+externalID);211 throw new SQLException("There are "+result.size()+" annotations with"+ external_id +" "+externalID); 211 212 } 212 213 return result.get(0); … … 216 217 @Override 217 218 public Number mapRow(ResultSet rs, int rowNumber) throws SQLException { 218 Number result = rs.getInt( "annotation_id");219 Number result = rs.getInt(annotation_id); 219 220 return result; 220 221 } 221 222 }; 223 224 225 public int deleteNotebook(Number annotationId) throws SQLException{ 226 String sqlAnnotation = "DELETE FROM " + annotationTableName + " where "+annotation_id + " = ?"; 227 //String sqSources = "DELETE FROM " + sourceTableName + " where "+ notebook_id +"= ?"; 228 int affectedAnnotations = getSimpleJdbcTemplate().update(sqlAnnotation, annotationId); 229 if (affectedAnnotations>1) { 230 throw new SQLException("There was more than one annotation ("+affectedAnnotations+") with the same ID "+annotationId); 231 } 232 return affectedAnnotations; 233 //TODO implement deleting sources (see the specification document and the interfaces' javadoc 234 } 222 235 223 236 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcNotebookDao.java
r3151 r3167 23 23 import eu.dasish.annotation.backend.identifiers.NotebookIdentifier; 24 24 import eu.dasish.annotation.backend.identifiers.UserIdentifier; 25 import eu.dasish.annotation.schema.AnnotationInfo;26 25 import eu.dasish.annotation.schema.Annotations; 27 26 import eu.dasish.annotation.schema.Notebook; … … 41 40 import org.springframework.beans.factory.annotation.Autowired; 42 41 import org.springframework.dao.DataAccessException; 43 import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;44 42 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 45 43 … … 53 51 @Autowired 54 52 private AnnotationDao jdbcAnnotationDao; 55 final static private String notebookTableName = "notebook"; 56 final static private String notebooksAnnotationsTableName = "notebooks_annotations"; 57 final static private String notebook_id = "notebook_id"; 53 58 54 59 55 public JdbcNotebookDao(DataSource dataSource) { … … 63 59 @Override 64 60 public List<NotebookInfo> getNotebookInfos(UserIdentifier userID) { 65 String sql = "SELECT notebook.title, notebook.external_id FROM notebook, principal where principal.principal_id = notebook.owner_id and principal.external_id= ?";61 String sql = "SELECT "+notebookTitle+", "+notebookExternal_id+" FROM "+notebookTableName+", "+principalTableName+" where "+principalPrincipal_id+" = "+notebookOwner_id+" and "+principalExternal_id+" = ?"; 66 62 return getSimpleJdbcTemplate().query(sql, notebookInfoRowMapper, userID.toString()); 67 63 } … … 69 65 @Override 70 66 public List<Notebook> getUsersNotebooks(UserIdentifier userID) { 71 String sql = "SELECT notebook.* FROM notebook, principal where principal_id = owner_id and principal.external_id= ?";67 String sql = "SELECT "+notebookStar+" FROM "+notebookTableName+", "+principalTableName+" where "+principal_id+" = "+owner_id+" and "+principalExternal_id+" = ?"; 72 68 return getSimpleJdbcTemplate().query(sql, notebookRowMapper, userID.toString()); 73 69 } … … 77 73 try { 78 74 final NotebookIdentifier notebookIdentifier = new NotebookIdentifier(); 79 String sql = "INSERT INTO notebook (external_id, title, owner_id) VALUES (:notebookId, :title, (SELECT principal_id FROM principal WHERE principal.external_id= :userID))";75 String sql = "INSERT INTO "+notebookTableName+" ("+external_id+", "+this.title+","+ owner_id+") VALUES (:notebookId, :title, (SELECT "+principal_id+" FROM "+principalTableName+" WHERE "+principalExternal_id+" = :userID))"; 80 76 Map<String, Object> params = new HashMap<String, Object>(); 81 77 params.put("notebookId", notebookIdentifier.getUUID().toString()); … … 92 88 public NotebookInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { 93 89 NotebookInfo notebookInfo = new NotebookInfo(); 94 notebookInfo.setRef(rs.getString( "external_id")); // todo: what is ref? should it be the external id?95 notebookInfo.setTitle(rs.getString( "title"));90 notebookInfo.setRef(rs.getString(external_id)); // todo: what is ref? should it be the external id? 91 notebookInfo.setTitle(rs.getString(title)); 96 92 // notebookInfo.setRef(rs.getString("URI")); 97 93 return notebookInfo; … … 103 99 Notebook notebook = new Notebook(); 104 100 // notebook.setId(rs.getInt("notebook_id")); 105 notebook.setTitle(rs.getString( "title"));101 notebook.setTitle(rs.getString(title)); 106 102 GregorianCalendar calendar = new GregorianCalendar(); 107 calendar.setTime(rs.getTimestamp( "time_stamp"));103 calendar.setTime(rs.getTimestamp(time_stamp)); 108 104 try { 109 105 XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); … … 113 109 } 114 110 // notebook.setURI(rs.getString("URI_ID")); 115 notebook.setAnnotations(getAnnotations(rs.getInt( "notebook_id")));111 notebook.setAnnotations(getAnnotations(rs.getInt(notebook_id))); 116 112 return notebook; 117 113 } … … 121 117 @Override 122 118 public int deleteNotebook(NotebookIdentifier notebookId) { 123 String sql1 = "DELETE FROM " + notebooksAnnotationsTableName + " where notebook_id = (SELECT notebook_id FROM notebook WHERE external_id= ?)";119 String sql1 = "DELETE FROM " + notebooksAnnotationsTableName + " where "+notebook_id +"= (SELECT "+notebook_id+" FROM "+notebookTableName+" WHERE "+external_id+" = ?)"; 124 120 String sql2 = "DELETE FROM notebook where external_id = ?"; 125 121 int affectedAnnotations = getSimpleJdbcTemplate().update(sql1, notebookId.getUUID().toString()); … … 133 129 SimpleJdbcInsert notebookInsert = new SimpleJdbcInsert(getDataSource()).withTableName(notebooksAnnotationsTableName); 134 130 Map<String, Object> params = new HashMap<String, Object>(); 135 params.put( "notebook_id", notebookId);136 params.put( "annotation_id", annotationId);131 params.put(notebook_id, notebookId); 132 params.put(annotation_id, annotationId); 137 133 int rowsAffected = notebookInsert.execute(params); 138 134 return rowsAffected; … … 157 153 158 154 if (isNotebookInTheDataBase(notebookID)) { 159 String sql = "SELECT notebooks_annotations.annotation_id FROM notebooks_annotations where notebook_id= ?";155 String sql = "SELECT "+notebooksAnnotationsTableNameAnnotation_id+" FROM "+notebooksAnnotationsTableName+" where "+notebook_id+" = ?"; 160 156 return getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, notebookID.toString()); 161 157 } else { -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java
r3146 r3167 30 30 */ 31 31 public class JdbcResourceDao extends SimpleJdbcDaoSupport implements ResourceDao{ 32 33 // base string constants: table Names 34 final static protected String notebookTableName = "notebook"; 35 final static protected String annotationTableName = "annotation"; 36 final static protected String sourceTableName = "source"; 37 final static protected String principalTableName = "principal"; 38 final static protected String notebooksAnnotationsTableName = "notebooks_annotations"; 39 40 41 // base string constants: field Names 42 final static protected String annotation_id = "annotation_id"; 43 final static protected String notebook_id = "notebook_id"; 44 final static protected String source_id = "source_id"; 45 final static protected String external_id = "external_id"; 46 final static protected String owner_id = "owner_id"; 47 final static protected String headline = "headline"; 48 final static protected String body_xml = "body_xml"; 49 final static protected String title="title"; 50 final static protected String principal_id = "principal_id"; 51 final static protected String time_stamp = "time_stamp"; 52 53 // derived string constants: table+field names 54 55 final static protected String annotationStar = annotationTableName+".*"; 56 final static protected String annotationAnnotation_id = annotationTableName+"."+annotation_id; 57 final static protected String annotationExternal_id = annotationTableName+"."+external_id; 58 59 final static protected String notebookStar = notebookTableName+".*"; 60 final static protected String notebookNotebook_id = notebookTableName+"."+notebook_id; 61 final static protected String notebookTitle=notebookTableName+"."+title; 62 final static protected String notebookExternal_id = notebookTableName+"."+external_id; 63 final static protected String notebookOwner_id = notebookTableName+"."+owner_id; 64 65 final static protected String notebooksAnnotationsTableNameAnnotation_id = "notebooks_annotations.annotation_id"; 66 67 final static protected String principalPrincipal_id = principalTableName+"."+principal_id; 68 final static protected String principalExternal_id = principalTableName+"."+external_id; 69 32 70 ////////////////////////////////////////// 33 71 /** … … 43 81 return false; 44 82 } 45 String sql = "SELECT notebook.notebook_id FROM notebook where notebook_id= ?";83 String sql = "SELECT "+notebookNotebook_id+" FROM notebook where "+notebook_id+" = ?"; 46 84 List<Number> result=getSimpleJdbcTemplate().query(sql, isNotebookInTheDataBaseRowMapper, notebookID.toString()); 47 85 if (result == null) { … … 57 95 @Override 58 96 public Integer mapRow(ResultSet rs, int rowNumber) throws SQLException { 59 Integer notebookId = rs.getInt( "notebook_id");97 Integer notebookId = rs.getInt(notebook_id); 60 98 return notebookId; 61 99 }
Note: See TracChangeset
for help on using the changeset viewer.