📄 dao.java
字号:
studyTime = timestamp.getTime(); study.setStudyTime(studyTime); } } catch (SQLException e) { e.printStackTrace(); } finally { excHandle(rs, stmt); } } /** * METHODS - SELECT: Expanded version of getStudiesToBackup(). Also selects studySize and studyTime */ public Vector<Study> getStudiesToBackupExpanded() { String query = "SELECT study_size.*" + " FROM(" + " SELECT temp1.stuinsuid" + " FROM(" + " SELECT stuinsuid" + " FROM img_studylevel" + " EXCEPT" + " SELECT stuinsuid" + " FROM img_imagelevel" + " JOIN img_serieslevel" + " ON img_imagelevel.serinsuid = img_serieslevel.serinsuid" + " WHERE img_imagelevel.insertdatetime > (NOW() - INTERVAL '1 HOUR')" + " ) AS temp1" + " JOIN(" + " SELECT DISTINCT ON (img_serieslevel.stuinsuid) stuinsuid" + " FROM img_imagelevel" + " JOIN img_serieslevel" + " ON img_imagelevel.serinsuid = img_serieslevel.serinsuid" + " EXCEPT" + " SELECT study_unique_id" + " FROM img_backup_register" + " ) AS temp2" + " ON temp1.stuinsuid = temp2.stuinsuid" + " ) AS backup_studies" + " JOIN(" + " SELECT img_studylevel.stuinsuid, study_size, insertdatetime" + " FROM img_studylevel" + " JOIN(" + " SELECT stuinsuid, SUM(size_bytes) AS study_size" + " FROM (" + " SELECT stuinsuid, img_imagelevel.size_bytes" + " FROM img_imagelevel" + " JOIN img_serieslevel" + " ON img_imagelevel.serinsuid = img_serieslevel.serinsuid" + " ) AS serie_image" + " GROUP BY stuinsuid" + " ) AS size" + " ON img_studylevel.stuinsuid = size.stuinsuid" + " ) AS study_size" + " ON backup_studies.stuinsuid = study_size.stuinsuid;"; String studyUniqueId; int studySize; long studyTime; Timestamp timestamp; Vector<Study> result = new Vector<Study>(); ResultSet rs = null; Statement stmt = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery(query); while (rs.next()){ studyUniqueId = rs.getString("stuinsuid"); studySize = rs.getInt("study_size"); timestamp = rs.getTimestamp("insertdatetime"); studyTime = timestamp.getTime(); result.addElement(new Study(studyUniqueId, studySize, studyTime)); } result.trimToSize(); } catch (SQLException e) { e.printStackTrace(); } finally { excHandle(rs, stmt); } return result; } public int setBackupRegister(long lastIndex, Vector<Study> studies) { Vector<String> studyUniqueIds = new Vector<String>(); for (int i=0; i<studies.size(); i++) { studyUniqueIds.addElement(studies.elementAt(i).getStudyUniqueId()); } String query = ""; for (int i=0; i<studyUniqueIds.size(); i++) { query += "INSERT INTO img_backup_register (backup_id, study_unique_id)" + " VALUES ('" + lastIndex + "'," + " '" + studyUniqueIds.elementAt(i) + "');"; } int success = -1; Statement stmt = null; try { stmt = connection.createStatement(); success = stmt.executeUpdate(query); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // ignore } stmt = null; } } return success; } public int insertBackupIso(BackupIso backupIso) { Vector<Study> studies = backupIso.getStudies(); String label = backupIso.getLabel(); String mediumType = backupIso.getMediumType(); String description = backupIso.getDescription(); String location = backupIso.getLocation(); int dbIndex = 0; int dataOk = 0; boolean success = false; if (studies.isEmpty()) { dataOk = 1; } if ((label.equals("")) || (mediumType.equals("")) || (description.equals("")) || (location.equals(""))) { dataOk = 2; } if (dataOk == 0) { String queryInsert = "INSERT INTO img_backup (label, medium_type, description, location)" + " VALUES ('" + label + "'," + " '" + mediumType + "'," + " '" + description + "'," + " '" + location + "');"; String querySelect = "SELECT max(backup_id) AS last_index" + " FROM img_backup;"; int lastIndex = 0; int insertSuccess = -1; ResultSet rs = null; Statement stmt = null; try { stmt = connection.createStatement(); insertSuccess = stmt.executeUpdate(queryInsert); System.out.println("Insert action turned: " + insertSuccess); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } } if (insertSuccess != -1) { try{ stmt = connection.createStatement(); rs = stmt.executeQuery(querySelect); while (rs.next()){ lastIndex = rs.getInt("last_index"); } dbIndex = lastIndex; System.out.println("Select action turned: " + lastIndex); } catch (SQLException e) { e.printStackTrace(); } finally { excHandle(rs, stmt); } } if (lastIndex != 0) { int result = this.setBackupRegister(lastIndex, studies); if (result != -1) { success = true; } //TODO: throw exception if success = false } } else { if (dataOk == 1) { // TODO: Throw exception System.out.println("The ISO-file contains no studies to be backed up!!"); } else { // TODO: Throw exception System.out.println("Some of the ISO-informations were not set!!"); } } return dbIndex; } public int updateBackupIso(BackupIso backupIso) { String label = backupIso.getLabel(); String mediumType = backupIso.getMediumType(); String description = backupIso.getDescription(); String location = backupIso.getLocation(); String operator = backupIso.getOperator(); int updateIndex = backupIso.getBackupUniqueId(); int insertStatus = -1; if(updateIndex == 0) { return insertStatus; } String queryUpdate = "UPDATE img_backup" + " SET label = '" + label + "', " + " medium_type = '" + mediumType + "', " + " description = '" + description + "', " + " location = '" + location + "', " + " operator = '" + operator + "', " + " timestamp = NOW()" + " WHERE backup_id = " + updateIndex + ";"; Statement stmt = null; try { stmt = connection.createStatement(); insertStatus = stmt.executeUpdate(queryUpdate); System.out.println("Update action turned: " + insertStatus); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } } return insertStatus; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -