1818//
1919package org .apache .cloudstack .reservation .dao ;
2020
21+ import java .util .Date ;
2122import java .util .List ;
22- import java .util .stream .Collectors ;
2323
2424import org .apache .cloudstack .context .CallContext ;
2525import org .apache .cloudstack .reservation .ReservationVO ;
@@ -42,6 +42,8 @@ public class ReservationDaoImpl extends GenericDaoBase<ReservationVO, Long> impl
4242 private static final String ACCOUNT_ID = "accountId" ;
4343 private static final String DOMAIN_ID = "domainId" ;
4444 private static final String IDS = "ids" ;
45+ private static final String MS_ID = "managementServerId" ;
46+ private static final String CREATED = "created" ;
4547 private final SearchBuilder <ReservationVO > listResourceByAccountAndTypeSearch ;
4648 private final SearchBuilder <ReservationVO > listAccountAndTypeSearch ;
4749 private final SearchBuilder <ReservationVO > listAccountAndTypeAndNoTagSearch ;
@@ -50,6 +52,7 @@ public class ReservationDaoImpl extends GenericDaoBase<ReservationVO, Long> impl
5052 private final SearchBuilder <ReservationVO > listDomainAndTypeAndNoTagSearch ;
5153 private final SearchBuilder <ReservationVO > listResourceByAccountAndTypeAndNoTagSearch ;
5254 private final SearchBuilder <ReservationVO > listIdsSearch ;
55+ private final SearchBuilder <ReservationVO > listMsIdSearch ;
5356
5457 public ReservationDaoImpl () {
5558
@@ -71,12 +74,14 @@ public ReservationDaoImpl() {
7174 listAccountAndTypeSearch .and (ACCOUNT_ID , listAccountAndTypeSearch .entity ().getAccountId (), SearchCriteria .Op .EQ );
7275 listAccountAndTypeSearch .and (RESOURCE_TYPE , listAccountAndTypeSearch .entity ().getResourceType (), SearchCriteria .Op .EQ );
7376 listAccountAndTypeSearch .and (RESOURCE_TAG , listAccountAndTypeSearch .entity ().getTag (), SearchCriteria .Op .EQ );
77+ listAccountAndTypeSearch .and (CREATED , listAccountAndTypeSearch .entity ().getCreated (), SearchCriteria .Op .LT );
7478 listAccountAndTypeSearch .done ();
7579
7680 listAccountAndTypeAndNoTagSearch = createSearchBuilder ();
7781 listAccountAndTypeAndNoTagSearch .and (ACCOUNT_ID , listAccountAndTypeAndNoTagSearch .entity ().getAccountId (), SearchCriteria .Op .EQ );
7882 listAccountAndTypeAndNoTagSearch .and (RESOURCE_TYPE , listAccountAndTypeAndNoTagSearch .entity ().getResourceType (), SearchCriteria .Op .EQ );
7983 listAccountAndTypeAndNoTagSearch .and (RESOURCE_TAG , listAccountAndTypeAndNoTagSearch .entity ().getTag (), SearchCriteria .Op .NULL );
84+ listAccountAndTypeAndNoTagSearch .and (CREATED , listAccountAndTypeAndNoTagSearch .entity ().getCreated (), SearchCriteria .Op .LT );
8085 listAccountAndTypeAndNoTagSearch .done ();
8186
8287 listDomainAndTypeSearch = createSearchBuilder ();
@@ -94,18 +99,24 @@ public ReservationDaoImpl() {
9499 listIdsSearch = createSearchBuilder ();
95100 listIdsSearch .and (IDS , listIdsSearch .entity ().getId (), SearchCriteria .Op .IN );
96101 listIdsSearch .done ();
102+
103+ listMsIdSearch = createSearchBuilder ();
104+ listMsIdSearch .and (MS_ID , listMsIdSearch .entity ().getManagementServerId (), SearchCriteria .Op .EQ );
105+ listMsIdSearch .done ();
97106 }
98107
99108 @ Override
100109 public long getAccountReservation (Long accountId , Resource .ResourceType resourceType , String tag ) {
101110 long total = 0 ;
102- SearchCriteria <ReservationVO > sc = tag == null ?
103- listAccountAndTypeAndNoTagSearch . create () : listAccountAndTypeSearch . create ();
104- sc . setParameters ( ACCOUNT_ID , accountId );
105- sc . setParameters ( RESOURCE_TYPE , resourceType );
106- if ( tag != null ) {
111+ SearchCriteria <ReservationVO > sc ;
112+ if ( tag == null ) {
113+ sc = listAccountAndTypeAndNoTagSearch . create ( );
114+ } else {
115+ sc = listAccountAndTypeSearch . create ();
107116 sc .setParameters (RESOURCE_TAG , tag );
108117 }
118+ sc .setParameters (ACCOUNT_ID , accountId );
119+ sc .setParameters (RESOURCE_TYPE , resourceType );
109120 List <ReservationVO > reservations = listBy (sc );
110121 for (ReservationVO reservation : reservations ) {
111122 total += reservation .getReservedAmount ();
@@ -116,13 +127,15 @@ public long getAccountReservation(Long accountId, Resource.ResourceType resource
116127 @ Override
117128 public long getDomainReservation (Long domainId , Resource .ResourceType resourceType , String tag ) {
118129 long total = 0 ;
119- SearchCriteria <ReservationVO > sc = tag == null ?
120- listDomainAndTypeAndNoTagSearch . create () : listDomainAndTypeSearch . create ();
121- sc . setParameters ( DOMAIN_ID , domainId );
122- sc . setParameters ( RESOURCE_TYPE , resourceType );
123- if ( tag != null ) {
130+ SearchCriteria <ReservationVO > sc ;
131+ if ( tag == null ) {
132+ sc = listDomainAndTypeAndNoTagSearch . create ( );
133+ } else {
134+ sc = listDomainAndTypeSearch . create ();
124135 sc .setParameters (RESOURCE_TAG , tag );
125136 }
137+ sc .setParameters (DOMAIN_ID , domainId );
138+ sc .setParameters (RESOURCE_TYPE , resourceType );
126139 List <ReservationVO > reservations = listBy (sc );
127140 for (ReservationVO reservation : reservations ) {
128141 total += reservation .getReservedAmount ();
@@ -149,23 +162,17 @@ public void setResourceId(Resource.ResourceType type, Long resourceId) {
149162 }
150163 }
151164
152- @ Override
153- public List <Long > getResourceIds (long accountId , Resource .ResourceType type ) {
154- SearchCriteria <ReservationVO > sc = listResourceByAccountAndTypeSearch .create ();
155- sc .setParameters (ACCOUNT_ID , accountId );
156- sc .setParameters (RESOURCE_TYPE , type );
157- return listBy (sc ).stream ().map (ReservationVO ::getResourceId ).collect (Collectors .toList ());
158- }
159-
160165 @ Override
161166 public List <ReservationVO > getReservationsForAccount (long accountId , Resource .ResourceType type , String tag ) {
162- SearchCriteria <ReservationVO > sc = tag == null ?
163- listResourceByAccountAndTypeAndNoTagSearch . create () : listResourceByAccountAndTypeSearch . create ();
164- sc . setParameters ( ACCOUNT_ID , accountId );
165- sc . setParameters ( RESOURCE_TYPE , type );
166- if ( tag != null ) {
167+ SearchCriteria <ReservationVO > sc ;
168+ if ( tag == null ) {
169+ sc = listResourceByAccountAndTypeAndNoTagSearch . create ( );
170+ } else {
171+ sc = listResourceByAccountAndTypeSearch . create ();
167172 sc .setParameters (RESOURCE_TAG , tag );
168173 }
174+ sc .setParameters (ACCOUNT_ID , accountId );
175+ sc .setParameters (RESOURCE_TYPE , type );
169176 return listBy (sc );
170177 }
171178
@@ -177,4 +184,28 @@ public void removeByIds(List<Long> reservationIds) {
177184 remove (sc );
178185 }
179186 }
187+
188+ @ Override
189+ public int removeByMsId (long managementServerId ) {
190+ SearchCriteria <ReservationVO > sc = listMsIdSearch .create ();
191+ sc .setParameters (MS_ID , managementServerId );
192+ return remove (sc );
193+ }
194+
195+ @ Override
196+ public int removeStaleReservations (Long accountId , Resource .ResourceType resourceType , String tag ,
197+ Date createdBefore ) {
198+ SearchCriteria <ReservationVO > sc ;
199+ if (tag == null ) {
200+ sc = listAccountAndTypeAndNoTagSearch .create ();
201+ } else {
202+ sc = listAccountAndTypeSearch .create ();
203+ sc .setParameters (RESOURCE_TAG , tag );
204+ }
205+ sc .setParameters (ACCOUNT_ID , accountId );
206+ sc .setParameters (RESOURCE_TYPE , resourceType );
207+ sc .setParameters (CREATED , createdBefore );
208+ return remove (sc );
209+ }
210+
180211}
0 commit comments