Skip to content

Commit 1b63c4a

Browse files
Add registration to composite key and use JpaRepository named methods
Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com>
1 parent c04eebf commit 1b63c4a

3 files changed

Lines changed: 51 additions & 33 deletions

File tree

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateEntity.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class StateEntity {
3232
@Id
3333
private String stateId;
3434

35+
@Id
3536
private UUID registration;
3637

3738
@Lob
@@ -176,6 +177,7 @@ public static class StateId implements Serializable {
176177
private String activityId;
177178
private String agentJson;
178179
private String stateId;
180+
private UUID registration;
179181

180182
/**
181183
* StateId Constructor.
@@ -185,10 +187,11 @@ public StateId() {}
185187
/**
186188
* StateId Constructor.
187189
*/
188-
public StateId(String activityId, String agentJson, String stateId) {
190+
public StateId(String activityId, String agentJson, String stateId, UUID registration) {
189191
this.activityId = activityId;
190192
this.agentJson = agentJson;
191193
this.stateId = stateId;
194+
this.registration = registration;
192195
}
193196

194197
@Override
@@ -201,12 +204,13 @@ public boolean equals(Object o) {
201204
}
202205
StateId stateId1 = (StateId) o;
203206
return activityId.equals(stateId1.activityId) && agentJson.equals(stateId1.agentJson)
204-
&& stateId.equals(stateId1.stateId);
207+
&& stateId.equals(stateId1.stateId) && registration.equals(stateId1.registration);
205208
}
206209

207210
@Override
208211
public int hashCode() {
209-
return activityId.hashCode() + agentJson.hashCode() + stateId.hashCode();
212+
return activityId.hashCode() + agentJson.hashCode() + stateId.hashCode()
213+
+ registration.hashCode();
210214
}
211215

212216
/**
@@ -262,5 +266,23 @@ public String getStateId() {
262266
public void setStateId(String stateId) {
263267
this.stateId = stateId;
264268
}
269+
270+
/**
271+
* Gets the registration.
272+
*
273+
* @return the registration
274+
*/
275+
public UUID getRegistration() {
276+
return registration;
277+
}
278+
279+
/**
280+
* Sets the registration.
281+
*
282+
* @param registration the registration to set
283+
*/
284+
public void setRegistration(UUID registration) {
285+
this.registration = registration;
286+
}
265287
}
266288
}

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateRepository.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import java.util.List;
88
import java.util.UUID;
99
import org.springframework.data.jpa.repository.JpaRepository;
10-
import org.springframework.data.jpa.repository.Modifying;
11-
import org.springframework.data.jpa.repository.Query;
12-
import org.springframework.data.repository.query.Param;
1310

1411
/**
1512
* State Repository.
@@ -19,31 +16,24 @@
1916
public interface StateRepository extends JpaRepository<StateEntity, StateEntity.StateId> {
2017

2118
/**
22-
* Find all stateIds for a given activityId and agent.
19+
* Find all StateEntities by activityId, agentJson, and registration.
2320
*
2421
* @param activityId the activityId
2522
* @param agentJson the agent in JSON format
26-
* @param registration the registration (nullable)
27-
* @return list of stateIds
23+
* @param registration the registration
24+
* @return list of StateEntities
2825
*/
29-
@Query("SELECT s.stateId FROM StateEntity s WHERE s.activityId = :activityId "
30-
+ "AND s.agentJson = :agentJson "
31-
+ "AND (:registration IS NULL OR s.registration = :registration)")
32-
List<String> findStateIds(@Param("activityId") String activityId,
33-
@Param("agentJson") String agentJson, @Param("registration") UUID registration);
26+
List<StateEntity> findByActivityIdAndAgentJsonAndRegistration(String activityId,
27+
String agentJson, UUID registration);
3428

3529
/**
36-
* Delete all states for a given activityId and agent.
30+
* Delete all states for a given activityId, agentJson, and registration.
3731
*
3832
* @param activityId the activityId
3933
* @param agentJson the agent in JSON format
40-
* @param registration the registration (nullable)
34+
* @param registration the registration
4135
*/
42-
@Modifying
43-
@Query("DELETE FROM StateEntity s WHERE s.activityId = :activityId "
44-
+ "AND s.agentJson = :agentJson "
45-
+ "AND (:registration IS NULL OR s.registration = :registration)")
46-
void deleteByActivityIdAndAgentJson(@Param("activityId") String activityId,
47-
@Param("agentJson") String agentJson, @Param("registration") UUID registration);
36+
void deleteByActivityIdAndAgentJsonAndRegistration(String activityId, String agentJson,
37+
UUID registration);
4838

4939
}

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateService.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public StateService(StateRepository repository, ObjectMapper mapper) {
4444
* @param activityId the activityId
4545
* @param agent the agent
4646
* @param stateId the stateId
47-
* @param registration the registration (nullable)
47+
* @param registration the registration
4848
* @return the state entity or Optional.empty() if not found
4949
*/
5050
public Optional<StateEntity> getState(String activityId, Agent agent, String stateId,
@@ -54,7 +54,8 @@ public Optional<StateEntity> getState(String activityId, Agent agent, String sta
5454
stateId, registration);
5555

5656
final String agentJson = serializeAgent(agent);
57-
final StateEntity.StateId id = new StateEntity.StateId(activityId, agentJson, stateId);
57+
final StateEntity.StateId id =
58+
new StateEntity.StateId(activityId, agentJson, stateId, registration);
5859

5960
return repository.findById(id);
6061
}
@@ -64,7 +65,7 @@ public Optional<StateEntity> getState(String activityId, Agent agent, String sta
6465
*
6566
* @param activityId the activityId
6667
* @param agent the agent
67-
* @param registration the registration (nullable)
68+
* @param registration the registration
6869
* @return list of stateIds
6970
*/
7071
public List<String> getStateIds(String activityId, Agent agent, UUID registration) {
@@ -74,7 +75,10 @@ public List<String> getStateIds(String activityId, Agent agent, UUID registratio
7475

7576
final String agentJson = serializeAgent(agent);
7677

77-
return repository.findStateIds(activityId, agentJson, registration);
78+
return repository
79+
.findByActivityIdAndAgentJsonAndRegistration(activityId, agentJson, registration).stream()
80+
.map(StateEntity::getStateId)
81+
.toList();
7882
}
7983

8084
/**
@@ -83,7 +87,7 @@ public List<String> getStateIds(String activityId, Agent agent, UUID registratio
8387
* @param activityId the activityId
8488
* @param agent the agent
8589
* @param stateId the stateId
86-
* @param registration the registration (nullable)
90+
* @param registration the registration
8791
* @param stateDocument the state document
8892
* @param contentType the content type
8993
*/
@@ -106,7 +110,7 @@ public void putState(String activityId, Agent agent, String stateId, UUID regist
106110
* @param activityId the activityId
107111
* @param agent the agent
108112
* @param stateId the stateId
109-
* @param registration the registration (nullable)
113+
* @param registration the registration
110114
* @param stateDocument the state document to merge
111115
* @param contentType the content type
112116
*/
@@ -117,7 +121,8 @@ public void postState(String activityId, Agent agent, String stateId, UUID regis
117121
stateId, registration);
118122

119123
final String agentJson = serializeAgent(agent);
120-
final StateEntity.StateId id = new StateEntity.StateId(activityId, agentJson, stateId);
124+
final StateEntity.StateId id =
125+
new StateEntity.StateId(activityId, agentJson, stateId, registration);
121126

122127
final Optional<StateEntity> existingEntity = repository.findById(id);
123128

@@ -144,15 +149,16 @@ public void postState(String activityId, Agent agent, String stateId, UUID regis
144149
* @param activityId the activityId
145150
* @param agent the agent
146151
* @param stateId the stateId
147-
* @param registration the registration (nullable)
152+
* @param registration the registration
148153
*/
149154
public void deleteState(String activityId, Agent agent, String stateId, UUID registration) {
150155

151156
log.info("delete state: activityId={}, agent={}, stateId={}, registration={}", activityId,
152157
agent, stateId, registration);
153158

154159
final String agentJson = serializeAgent(agent);
155-
final StateEntity.StateId id = new StateEntity.StateId(activityId, agentJson, stateId);
160+
final StateEntity.StateId id =
161+
new StateEntity.StateId(activityId, agentJson, stateId, registration);
156162

157163
repository.deleteById(id);
158164
}
@@ -162,7 +168,7 @@ public void deleteState(String activityId, Agent agent, String stateId, UUID reg
162168
*
163169
* @param activityId the activityId
164170
* @param agent the agent
165-
* @param registration the registration (nullable)
171+
* @param registration the registration
166172
*/
167173
@Transactional
168174
public void deleteStates(String activityId, Agent agent, UUID registration) {
@@ -172,7 +178,7 @@ public void deleteStates(String activityId, Agent agent, UUID registration) {
172178

173179
final String agentJson = serializeAgent(agent);
174180

175-
repository.deleteByActivityIdAndAgentJson(activityId, agentJson, registration);
181+
repository.deleteByActivityIdAndAgentJsonAndRegistration(activityId, agentJson, registration);
176182
}
177183

178184
private String serializeAgent(Agent agent) {

0 commit comments

Comments
 (0)