1717
1818package org .apache .cloudstack .backup .dao ;
1919
20+ import java .sql .PreparedStatement ;
21+ import java .sql .SQLException ;
2022import java .util .Date ;
2123import java .util .List ;
2224
2325import javax .annotation .PostConstruct ;
24- import javax .inject .Inject ;
2526
2627import com .cloud .utils .DateUtil ;
27- import org . apache . cloudstack . api . response . BackupScheduleResponse ;
28- import org . apache . cloudstack . backup . BackupSchedule ;
28+ import com . cloud . utils . db . DB ;
29+ import com . cloud . utils . db . TransactionLegacy ;
2930import org .apache .cloudstack .backup .BackupScheduleVO ;
3031
3132import com .cloud .utils .db .GenericDaoBase ;
3233import com .cloud .utils .db .SearchBuilder ;
3334import com .cloud .utils .db .SearchCriteria ;
34- import com .cloud .vm .VMInstanceVO ;
35- import com .cloud .vm .dao .VMInstanceDao ;
3635
3736public class BackupScheduleDaoImpl extends GenericDaoBase <BackupScheduleVO , Long > implements BackupScheduleDao {
38-
39- @ Inject
40- VMInstanceDao vmInstanceDao ;
41-
4237 private SearchBuilder <BackupScheduleVO > backupScheduleSearch ;
4338 private SearchBuilder <BackupScheduleVO > executableSchedulesSearch ;
4439
@@ -59,13 +54,6 @@ protected void init() {
5954 executableSchedulesSearch .done ();
6055 }
6156
62- @ Override
63- public BackupScheduleVO findByVM (Long vmId ) {
64- SearchCriteria <BackupScheduleVO > sc = backupScheduleSearch .create ();
65- sc .setParameters ("vm_id" , vmId );
66- return findOneBy (sc );
67- }
68-
6957 @ Override
7058 public List <BackupScheduleVO > listByVM (Long vmId ) {
7159 SearchCriteria <BackupScheduleVO > sc = backupScheduleSearch .create ();
@@ -88,21 +76,19 @@ public List<BackupScheduleVO> getSchedulesToExecute(Date currentTimestamp) {
8876 return listBy (sc );
8977 }
9078
79+ @ DB
9180 @ Override
92- public BackupScheduleResponse newBackupScheduleResponse (BackupSchedule schedule ) {
93- VMInstanceVO vm = vmInstanceDao .findByIdIncludingRemoved (schedule .getVmId ());
94- BackupScheduleResponse response = new BackupScheduleResponse ();
95- response .setId (schedule .getUuid ());
96- response .setVmId (vm .getUuid ());
97- response .setVmName (vm .getHostName ());
98- response .setIntervalType (schedule .getScheduleType ());
99- response .setSchedule (schedule .getSchedule ());
100- response .setTimezone (schedule .getTimezone ());
101- response .setMaxBackups (schedule .getMaxBackups ());
102- if (schedule .getQuiesceVM () != null ) {
103- response .setQuiesceVM (schedule .getQuiesceVM ());
81+ public boolean remove (Long id ) {
82+ String sql = "UPDATE backups SET backup_schedule_id = NULL WHERE backup_schedule_id = ?" ;
83+ TransactionLegacy transaction = TransactionLegacy .currentTxn ();
84+ try {
85+ PreparedStatement preparedStatement = transaction .prepareAutoCloseStatement (sql );
86+ preparedStatement .setLong (1 , id );
87+ preparedStatement .executeUpdate ();
88+ return super .remove (id );
89+ } catch (SQLException e ) {
90+ logger .warn ("Unable to clean up backup schedules references from the backups table." , e );
91+ return false ;
10492 }
105- response .setObjectName ("backupschedule" );
106- return response ;
10793 }
10894}
0 commit comments