Skip to content

Commit 11f0a2f

Browse files
authored
Merge pull request #8 from mcanoy/add-region
add region. streamline db update
2 parents e059098 + 9c7d832 commit 11f0a2f

File tree

10 files changed

+194
-207
lines changed

10 files changed

+194
-207
lines changed

src/main/java/com/redhat/labs/lodestar/model/Artifact.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
@EqualsAndHashCode(callSuper = true)
3030
public class Artifact extends PanacheMongoEntityBase {
3131

32-
private static final String ENGAGEMENT_UUID = "engagementUuid";
3332
private static final String MODIFIED = "modified";
34-
private static final String UUID = "uuid";
3533

3634
@BsonId
3735
@DiffIgnore
@@ -43,10 +41,9 @@ public class Artifact extends PanacheMongoEntityBase {
4341
@DiffIgnore
4442
private String created;
4543
@DiffIgnore
46-
@JsonbProperty(value = "last_modified")
44+
@JsonbProperty(value = "updated")
4745
private String modified;
4846

49-
@NotBlank
5047
private String engagementUuid;
5148
@NotBlank
5249
private String title;
@@ -56,6 +53,8 @@ public class Artifact extends PanacheMongoEntityBase {
5653
private String type;
5754
@NotBlank
5855
private String linkAddress;
56+
57+
private String region;
5958

6059
/**
6160
* Returns an {@link ArtifactCount} containing the count for the total number of
@@ -75,7 +74,7 @@ public static ArtifactCount countAllArtifacts() {
7574
* @return
7675
*/
7776
public static ArtifactCount countArtifactsByEngagementUuid(String engagementUuid) {
78-
return ArtifactCount.builder().count(count(ENGAGEMENT_UUID, engagementUuid)).build();
77+
return ArtifactCount.builder().count(count("engagementUuid", engagementUuid)).build();
7978
}
8079

8180
/**
@@ -88,6 +87,14 @@ public static ArtifactCount countArtifactsByEngagementUuid(String engagementUuid
8887
public static ArtifactCount countArtifactsByType(String type) {
8988
return ArtifactCount.builder().count(count("type", type)).build();
9089
}
90+
91+
public static ArtifactCount countArtifactsByRegion(List<String> regions) {
92+
return ArtifactCount.builder().count(count("region in ?1", regions)).build();
93+
}
94+
95+
public static ArtifactCount countArtifactsByRegionAndType(String type, List<String> regions) {
96+
return ArtifactCount.builder().count(count("{ $and: [ {'type':?1}, {'region':{'$in':[?2]}} ] }", type, regions)).build();
97+
}
9198

9299
/**
93100
* Returns {@link List} of {@link Artifact}s sorted descending on modified
@@ -112,6 +119,16 @@ public static List<Artifact> pagedArtifacts(int page, int pageSize) {
112119
public static List<Artifact> pagedArtifactsByType(String type, int page, int pageSize) {
113120
return find("type", Sort.descending(MODIFIED), type).page(page, pageSize).list();
114121
}
122+
123+
public static List<Artifact> pagedArtifactsByRegion(List<String> regions, int page, int pageSize) {
124+
return find("region in ?1", Sort.descending(MODIFIED), regions).page(page, pageSize).list();
125+
}
126+
127+
public static List<Artifact> pagedArtifactsByRegionAndType(String type, List<String> regions, int page, int pageSize) {
128+
//not sure why the commented query doesn't work but keep seeing this error - no viable alternative at input 'type='
129+
//return find("type = ?1 and region in ?2", Sort.descending(MODIFIED), type, regions).page(page, pageSize).list();
130+
return find("{ $and: [ {'type':?1}, {'region':{'$in':[?2]}} ] }", Sort.descending(MODIFIED), type, regions).page(page, pageSize).list();
131+
}
115132

116133
/**
117134
* Returns a {@link List} of {@link Artifact}s for the given engagement uuid,
@@ -123,7 +140,7 @@ public static List<Artifact> pagedArtifactsByType(String type, int page, int pag
123140
* @return
124141
*/
125142
public static List<Artifact> pagedArtifactsByEngagementUuid(String engagementUuid, int page, int pageSize) {
126-
return find(ENGAGEMENT_UUID, Sort.descending(MODIFIED), engagementUuid).page(page, pageSize).list();
143+
return find("engagementUuid", Sort.descending(MODIFIED), engagementUuid).page(page, pageSize).list();
127144
}
128145

129146
/**
@@ -134,7 +151,7 @@ public static List<Artifact> pagedArtifactsByEngagementUuid(String engagementUui
134151
* @return
135152
*/
136153
public static List<Artifact> findAllByEngagementUuid(String engagementUuid) {
137-
return list(ENGAGEMENT_UUID, engagementUuid);
154+
return list("engagementUuid", engagementUuid);
138155
}
139156

140157
/**
@@ -145,7 +162,7 @@ public static List<Artifact> findAllByEngagementUuid(String engagementUuid) {
145162
* @return
146163
*/
147164
public static Optional<Artifact> findByUuid(String uuid) {
148-
return find(UUID, uuid).singleResultOptional();
165+
return find("uuid", uuid).singleResultOptional();
149166
}
150167

151168
/**
@@ -155,7 +172,7 @@ public static Optional<Artifact> findByUuid(String uuid) {
155172
* @return
156173
*/
157174
public static long deleteByUuid(String uuid) {
158-
return Artifact.delete(UUID, uuid);
175+
return Artifact.delete("uuid", uuid);
159176
}
160177

161178
/**

src/main/java/com/redhat/labs/lodestar/model/GetOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.redhat.labs.lodestar.model;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.Optional;
46

57
import javax.ws.rs.QueryParam;
@@ -22,6 +24,11 @@ public class GetOptions {
2224
@Parameter(name = "type", required = false, description = "return only artifacts for the given type. Do not use with engagementUuid")
2325
@QueryParam("type")
2426
private String type;
27+
28+
@Parameter(name = "region", required = false, description = "return only artifacts for the given region. Do not use with engagementUuid")
29+
@QueryParam("region")
30+
31+
private List<String> region = new ArrayList<String>();
2532

2633
public Optional<String> getEngagementUuid() {
2734
return Optional.ofNullable(engagementUuid);
@@ -30,5 +37,13 @@ public Optional<String> getEngagementUuid() {
3037
public Optional<String> getType() {
3138
return Optional.ofNullable(type);
3239
}
40+
41+
public List<String> getRegion() {
42+
if(region == null) {
43+
region = new ArrayList<String>();
44+
}
45+
46+
return region;
47+
}
3348

3449
}

src/main/java/com/redhat/labs/lodestar/resource/ArtifactResource.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,18 @@ public class ArtifactResource {
3535

3636
@Inject
3737
ArtifactService service;
38-
39-
//may need to remove this and only accept by engagement uuid
40-
@PUT
41-
@APIResponses(value = {
42-
@APIResponse(responseCode = "200", description = "The list of artifacts has been processed."),
43-
@APIResponse(responseCode = "400", description = "Invalid list of artifacts provided.") })
44-
@Operation(summary = "Artifacts have been processed and persisted.")
45-
public Response processArtifacts(@Valid List<Artifact> artifacts,
46-
@QueryParam("authorEmail") Optional<String> authorEmail,
47-
@QueryParam("authorName") Optional<String> authorName) {
48-
49-
service.process(artifacts, authorEmail, authorName);
50-
return Response.ok().build();
51-
52-
}
5338

5439
@PUT
5540
@APIResponses(value = {
5641
@APIResponse(responseCode = "200", description = "The list of artifacts has been processed."),
5742
@APIResponse(responseCode = "400", description = "Invalid list of artifacts provided.") })
5843
@Operation(summary = "Artifacts have been processed and persisted for engagement.")
59-
@Path("/engagement/uuid/{engagementUuid}")
60-
public Response processEngagementArtifacts(List<Artifact> artifacts, @PathParam(value="engagementUuid") String engagementUuid,
61-
@QueryParam("authorEmail") Optional<String> authorEmail,
44+
@Path("/engagement/uuid/{engagementUuid}/{region}")
45+
public Response processEngagementArtifacts(@Valid List<Artifact> artifacts, @PathParam(value="engagementUuid") String engagementUuid,
46+
@PathParam(value="region") String region, @QueryParam("authorEmail") Optional<String> authorEmail,
6247
@QueryParam("authorName") Optional<String> authorName) {
6348

64-
service.updateArtifacts(engagementUuid, artifacts, authorEmail, authorName);
49+
service.updateArtifacts(engagementUuid, region, artifacts, authorEmail, authorName);
6550
return Response.ok().build();
6651

6752
}
@@ -98,9 +83,9 @@ public ArtifactCount countArtifacts(@BeanParam GetOptions options) {
9883
public Response refresh() {
9984

10085
service.purge();
101-
service.refresh();
86+
long count = service.refresh();
10287

103-
return Response.accepted().build();
88+
return Response.accepted().header("x-total-artifacts", count).build();
10489

10590
}
10691

0 commit comments

Comments
 (0)