33import de .rwth .idsg .steve .SteveException ;
44import de .rwth .idsg .steve .repository .ReservationRepository ;
55import de .rwth .idsg .steve .repository .ReservationStatus ;
6+ import de .rwth .idsg .steve .repository .dto .InsertReservationParams ;
67import de .rwth .idsg .steve .repository .dto .Reservation ;
78import de .rwth .idsg .steve .utils .CustomDSL ;
89import de .rwth .idsg .steve .utils .DateTimeUtils ;
910import de .rwth .idsg .steve .web .dto .ReservationQueryForm ;
1011import lombok .extern .slf4j .Slf4j ;
1112import org .joda .time .DateTime ;
1213import org .jooq .DSLContext ;
13- import org .jooq .Record9 ;
14+ import org .jooq .Record1 ;
15+ import org .jooq .Record10 ;
1416import org .jooq .RecordMapper ;
17+ import org .jooq .SelectConditionStep ;
1518import org .jooq .SelectQuery ;
1619import org .jooq .exception .DataAccessException ;
20+ import org .jooq .impl .DSL ;
1721import org .springframework .beans .factory .annotation .Autowired ;
1822import org .springframework .stereotype .Repository ;
1923
2024import java .util .List ;
2125
2226import static jooq .steve .db .tables .ChargeBox .CHARGE_BOX ;
27+ import static jooq .steve .db .tables .Connector .CONNECTOR ;
2328import static jooq .steve .db .tables .OcppTag .OCPP_TAG ;
2429import static jooq .steve .db .tables .Reservation .RESERVATION ;
2530
@@ -39,7 +44,9 @@ public List<Reservation> getReservations(ReservationQueryForm form) {
3944 SelectQuery selectQuery = ctx .selectQuery ();
4045 selectQuery .addFrom (RESERVATION );
4146 selectQuery .addJoin (OCPP_TAG , OCPP_TAG .ID_TAG .eq (RESERVATION .ID_TAG ));
42- selectQuery .addJoin (CHARGE_BOX , CHARGE_BOX .CHARGE_BOX_ID .eq (RESERVATION .CHARGE_BOX_ID ));
47+ selectQuery .addJoin (CONNECTOR , CONNECTOR .CONNECTOR_PK .eq (RESERVATION .CONNECTOR_PK ));
48+ selectQuery .addJoin (CHARGE_BOX , CONNECTOR .CHARGE_BOX_ID .eq (CHARGE_BOX .CHARGE_BOX_ID ));
49+
4350 selectQuery .addSelect (
4451 RESERVATION .RESERVATION_PK ,
4552 RESERVATION .TRANSACTION_PK ,
@@ -49,11 +56,12 @@ public List<Reservation> getReservations(ReservationQueryForm form) {
4956 CHARGE_BOX .CHARGE_BOX_ID ,
5057 RESERVATION .START_DATETIME ,
5158 RESERVATION .EXPIRY_DATETIME ,
52- RESERVATION .STATUS
59+ RESERVATION .STATUS ,
60+ CONNECTOR .CONNECTOR_ID
5361 );
5462
5563 if (form .isChargeBoxIdSet ()) {
56- selectQuery .addConditions (RESERVATION .CHARGE_BOX_ID .eq (form .getChargeBoxId ()));
64+ selectQuery .addConditions (CHARGE_BOX .CHARGE_BOX_ID .eq (form .getChargeBoxId ()));
5765 }
5866
5967 if (form .isOcppIdTagSet ()) {
@@ -76,24 +84,31 @@ public List<Reservation> getReservations(ReservationQueryForm form) {
7684 public List <Integer > getActiveReservationIds (String chargeBoxId ) {
7785 return ctx .select (RESERVATION .RESERVATION_PK )
7886 .from (RESERVATION )
79- .where (RESERVATION .CHARGE_BOX_ID .equal (chargeBoxId ))
80- .and (RESERVATION .EXPIRY_DATETIME .greaterThan (CustomDSL .utcTimestamp ()))
81- .and (RESERVATION .STATUS .equal (ReservationStatus .ACCEPTED .name ()))
87+ .where (RESERVATION .CONNECTOR_PK .in (DSL .select (CONNECTOR .CONNECTOR_PK )
88+ .from (CONNECTOR )
89+ .where (CONNECTOR .CHARGE_BOX_ID .equal (chargeBoxId ))))
90+ .and (RESERVATION .EXPIRY_DATETIME .greaterThan (CustomDSL .utcTimestamp ()))
91+ .and (RESERVATION .STATUS .equal (ReservationStatus .ACCEPTED .name ()))
8292 .fetch (RESERVATION .RESERVATION_PK );
8393 }
8494
8595 @ Override
86- public int insert (String idTag , String chargeBoxId , DateTime startTimestamp , DateTime expiryTimestamp ) {
96+ public int insert (InsertReservationParams params ) {
8797 // Check overlapping
8898 //isOverlapping(startTimestamp, expiryTimestamp, chargeBoxId);
8999
90- int reservationId = ctx .insertInto (RESERVATION ,
91- RESERVATION .ID_TAG , RESERVATION .CHARGE_BOX_ID ,
92- RESERVATION .START_DATETIME , RESERVATION .EXPIRY_DATETIME ,
93- RESERVATION .STATUS )
94- .values (idTag , chargeBoxId ,
95- startTimestamp , expiryTimestamp ,
96- ReservationStatus .WAITING .name ())
100+ SelectConditionStep <Record1 <Integer >> connectorPkQuery =
101+ DSL .select (CONNECTOR .CONNECTOR_PK )
102+ .from (CONNECTOR )
103+ .where (CONNECTOR .CHARGE_BOX_ID .equal (params .getChargeBoxId ()))
104+ .and (CONNECTOR .CONNECTOR_ID .equal (params .getConnectorId ()));
105+
106+ int reservationId = ctx .insertInto (RESERVATION )
107+ .set (RESERVATION .CONNECTOR_PK , connectorPkQuery )
108+ .set (RESERVATION .ID_TAG , params .getIdTag ())
109+ .set (RESERVATION .START_DATETIME , params .getStartTimestamp ())
110+ .set (RESERVATION .EXPIRY_DATETIME , params .getExpiryTimestamp ())
111+ .set (RESERVATION .STATUS , ReservationStatus .WAITING .name ())
97112 .returning (RESERVATION .RESERVATION_PK )
98113 .fetchOne ()
99114 .getReservationPk ();
@@ -135,9 +150,11 @@ public void used(int reservationId, int transactionId) {
135150 // -------------------------------------------------------------------------
136151
137152 private static class ReservationMapper implements
138- RecordMapper <Record9 <Integer , Integer , Integer , Integer , String , String , DateTime , DateTime , String >, Reservation > {
153+ RecordMapper <Record10 <Integer , Integer , Integer , Integer , String ,
154+ String , DateTime , DateTime , String , Integer >, Reservation > {
139155 @ Override
140- public Reservation map (Record9 <Integer , Integer , Integer , Integer , String , String , DateTime , DateTime , String > r ) {
156+ public Reservation map (Record10 <Integer , Integer , Integer , Integer , String ,
157+ String , DateTime , DateTime , String , Integer > r ) {
141158 return Reservation .builder ()
142159 .id (r .value1 ())
143160 .transactionId (r .value2 ())
@@ -148,6 +165,7 @@ public Reservation map(Record9<Integer, Integer, Integer, Integer, String, Strin
148165 .startDatetime (DateTimeUtils .humanize (r .value7 ()))
149166 .expiryDatetime (DateTimeUtils .humanize (r .value8 ()))
150167 .status (r .value9 ())
168+ .connectorId (r .value10 ())
151169 .build ();
152170 }
153171 }
@@ -184,21 +202,21 @@ private void processType(SelectQuery selectQuery, ReservationQueryForm form) {
184202 /**
185203 * Throws exception, if there are rows whose date/time ranges overlap with the input
186204 */
187- private void isOverlapping (DateTime start , DateTime stop , String chargeBoxId ) {
188- try {
189- int count = ctx .selectOne ()
190- .from (RESERVATION )
191- .where (RESERVATION .EXPIRY_DATETIME .greaterOrEqual (start ))
192- .and (RESERVATION .START_DATETIME .lessOrEqual (stop ))
193- .and (RESERVATION .CHARGE_BOX_ID .equal (chargeBoxId ))
194- .execute ();
195-
196- if (count != 1 ) {
197- throw new SteveException ("The desired reservation overlaps with another reservation" );
198- }
199-
200- } catch (DataAccessException e ) {
201- log .error ("Exception occurred" , e );
202- }
203- }
205+ // private void isOverlapping(DateTime start, DateTime stop, String chargeBoxId) {
206+ // try {
207+ // int count = ctx.selectOne()
208+ // .from(RESERVATION)
209+ // .where(RESERVATION.EXPIRY_DATETIME.greaterOrEqual(start))
210+ // .and(RESERVATION.START_DATETIME.lessOrEqual(stop))
211+ // .and(RESERVATION.CHARGE_BOX_ID.equal(chargeBoxId))
212+ // .execute();
213+ //
214+ // if (count != 1) {
215+ // throw new SteveException("The desired reservation overlaps with another reservation");
216+ // }
217+ //
218+ // } catch (DataAccessException e) {
219+ // log.error("Exception occurred", e);
220+ // }
221+ // }
204222}
0 commit comments