Skip to content

Commit

Permalink
Changed format to include ProjectKind constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-rripken committed Aug 6, 2024
1 parent 6803358 commit a61b5ce
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.data.dto.project.ProjectChildLocations;

import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
import org.jooq.DSLContext;
Expand All @@ -48,7 +51,8 @@ public List<ProjectChildLocations> retrieveProjectChildLocations(String office,
return retrieveProjectChildLocations(office, projLike, ProjectKind.getMatchingKinds(kindRegex));
}

private List<ProjectChildLocations> retrieveProjectChildLocations(String office, String projLike, Set<ProjectKind> kinds) {
private List<ProjectChildLocations> retrieveProjectChildLocations(
String office, String projLike, Set<ProjectKind> kinds) {

Map<String, ProjectChildLocations.Builder> builderMap = new LinkedHashMap<>(); // proj-id->

Expand All @@ -64,25 +68,7 @@ private List<ProjectChildLocations> retrieveProjectChildLocations(String office,
.withOfficeId(office)
.withName(projId)
.build()));
switch (kind) {
case EMBANKMENT:
builder.withEmbankments(locs);
break;
case LOCK:
builder.withLocks(locs);
break;
case OUTLET:
builder.withOutlets(locs);
break;
case TURBINE:
builder.withTurbines(locs);
break;
case GATE:
builder.withGates(locs);
break;
default:
break;
}
builder.withLocations(kind, locs);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cwms.cda.data.dto.project;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import cwms.cda.data.dao.project.ProjectKind;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.formatters.Formats;
import cwms.cda.formatters.annotations.FormattableWith;
import cwms.cda.formatters.json.JsonV2;
import java.util.ArrayList;
import java.util.List;

@JsonDeserialize(builder = LocationsWithProjectKind.Builder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@FormattableWith(contentType = Formats.JSONV2, aliases = {Formats.JSON}, formatter = JsonV2.class)
public class LocationsWithProjectKind {
ProjectKind kind;
List<CwmsId> locations;

private LocationsWithProjectKind(Builder builder) {
kind = builder.kind;
locations = builder.locations;
}

public ProjectKind getKind() {
return kind;
}

public List<CwmsId> getLocations() {
return locations;
}

@JsonPOJOBuilder
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
public static class Builder {
private ProjectKind kind;
private List<CwmsId> locations = new ArrayList<>();

public Builder() {

}

public Builder withKind(ProjectKind kind) {
this.kind = kind;
return this;
}

public Builder withLocations(List<CwmsId> locations) {
if (locations == null) {
this.locations = null;
} else {
this.locations = new ArrayList<>(locations);
}
return this;
}

public LocationsWithProjectKind build() {
return new LocationsWithProjectKind(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@

package cwms.cda.data.dto.project;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import cwms.cda.data.dao.project.ProjectKind;
import cwms.cda.data.dto.CwmsDTOBase;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.formatters.Formats;
import cwms.cda.formatters.annotations.FormattableWith;
import cwms.cda.formatters.json.JsonV2;
import java.util.Collections;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import java.util.Map;

/**
* This class holds a project and lists of the project child locations by kind.
Expand All @@ -48,92 +53,132 @@ public class ProjectChildLocations extends CwmsDTOBase {

private final CwmsId project;

private final List<CwmsId> embankments;
private final List<CwmsId> locks;
private final List<CwmsId> outlets;
private final List<CwmsId> turbines;
private final List<CwmsId> gates;
@JsonIgnore
private EnumMap<ProjectKind, List<CwmsId>> locationsByKind;

private ProjectChildLocations(Builder builder) {
this.project = builder.project;
this.embankments = builder.embankments;
this.locks = builder.locks;
this.outlets = builder.outlets;
this.turbines = builder.turbines;
this.gates = builder.gates;

if (builder.locationsByKind != null) {
this.locationsByKind = new EnumMap<>(builder.locationsByKind);
}
}

public CwmsId getProject() {
return project;
}

@JsonProperty
public List<LocationsWithProjectKind> getLocationsByKind() {
List<LocationsWithProjectKind> result = null;

if (locationsByKind != null && !locationsByKind.isEmpty()) {
result = new ArrayList<>();

for (Map.Entry<ProjectKind, List<CwmsId>> entry : locationsByKind.entrySet()) {
result.add(new LocationsWithProjectKind.Builder()
.withKind(entry.getKey())
.withLocations(entry.getValue()).build());
}
}

return result;
}

public List<CwmsId> getLocations(ProjectKind kind) {
if (locationsByKind != null && !locationsByKind.isEmpty()) {
return locationsByKind.get(kind);
}
return null;
}

@JsonIgnore
public List<CwmsId> getEmbankments() {
return embankments;
return getLocations(ProjectKind.EMBANKMENT);
}

@JsonIgnore
public List<CwmsId> getLocks() {
return locks;
return getLocations(ProjectKind.LOCK);
}

@JsonIgnore
public List<CwmsId> getOutlets() {
return outlets;
return getLocations(ProjectKind.OUTLET);
}

@JsonIgnore
public List<CwmsId> getTurbines() {
return turbines;
return getLocations(ProjectKind.TURBINE);
}

@JsonIgnore
public List<CwmsId> getGates() {
return gates;
return getLocations(ProjectKind.GATE);
}


@JsonPOJOBuilder
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
public static class Builder {
private CwmsId project;
private List<CwmsId> embankments;
private List<CwmsId> locks;
private List<CwmsId> outlets;
private List<CwmsId> turbines;
private List<CwmsId> gates;

private EnumMap<ProjectKind, List<CwmsId>> locationsByKind;

public Builder withProject(CwmsId project) {
this.project = project;
return this;
}

public Builder withEmbankments(List<CwmsId> embankments) {
this.embankments = wrapList(embankments);
public Builder withLocationsByKind(List<LocationsWithProjectKind> locationsByKind) {
Builder retval = this;
if (locationsByKind != null) {
for (LocationsWithProjectKind item : locationsByKind) {
retval = retval.withLocations(item.getKind(), item.getLocations());
}
}

return retval;
}

public Builder withLocations(ProjectKind kind, List<CwmsId> locations) {

if (locationsByKind == null) {
locationsByKind = new EnumMap<>(ProjectKind.class);
}

if (locations != null) {
List<CwmsId> locOfKind = locationsByKind.computeIfAbsent(kind, k -> new ArrayList<>());
if (!locations.isEmpty()) {
locOfKind.addAll(locations);
}
}

return this;
}

@JsonIgnore
public Builder withEmbankments(List<CwmsId> embankments) {
return withLocations(ProjectKind.EMBANKMENT, embankments);
}

@JsonIgnore
public Builder withLocks(List<CwmsId> locks) {
this.locks = wrapList(locks);
return this;
return withLocations(ProjectKind.LOCK, locks);
}

@JsonIgnore
public Builder withOutlets(List<CwmsId> outlets) {
this.outlets = outlets;
return this;
return withLocations(ProjectKind.OUTLET, outlets);
}

@JsonIgnore
public Builder withTurbines(List<CwmsId> turbines) {
this.turbines = wrapList(turbines);
return this;
return withLocations(ProjectKind.TURBINE, turbines);
}

@JsonIgnore
public Builder withGates(List<CwmsId> gates) {
this.gates = wrapList(gates);
return this;
}

@Nullable
private static List<CwmsId> wrapList(@Nullable List<CwmsId> embankments) {
List<CwmsId> retval = null;
if (embankments != null) {
retval = Collections.unmodifiableList(embankments);
}
return retval;
return withLocations(ProjectKind.GATE, gates);
}

public ProjectChildLocations build() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cwms.cda.data.dto.project;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cwms.cda.data.dao.project.ProjectKind;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.formatters.json.JsonV2;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;


public class LocationsWithProjectKindTest {

@Test
void test_serialize() throws JsonProcessingException {
LocationsWithProjectKind.Builder builder = new LocationsWithProjectKind.Builder();
builder.withKind(ProjectKind.EMBANKMENT);
List<CwmsId> locs = new ArrayList<>();
locs.add(new CwmsId.Builder().withName("Emb1").withOfficeId("SPK").build());
locs.add(new CwmsId.Builder().withName("Emb2").withOfficeId("SPK").build());
builder.withLocations(locs);
LocationsWithProjectKind locWithKind = builder.build();

ObjectMapper objectMapper = JsonV2.buildObjectMapper();
String json = objectMapper.writeValueAsString(locWithKind);
assertNotNull(json);

}

@Test
void test_deserialize() throws IOException {

InputStream stream = LocationsWithProjectKind.class.getClassLoader().getResourceAsStream(
"cwms/cda/data/dto/location_with_kind.json");
assertNotNull(stream);
ObjectMapper objectMapper = JsonV2.buildObjectMapper();
LocationsWithProjectKind locationsWithKind = objectMapper.readValue(stream, LocationsWithProjectKind.class);
assertNotNull(locationsWithKind);
assertEquals(ProjectKind.EMBANKMENT, locationsWithKind.getKind());
assertEquals("TestEmbankment1", locationsWithKind.getLocations().get(0).getName());
assertEquals("TestEmbankment2", locationsWithKind.getLocations().get(1).getName());

}

}
Loading

0 comments on commit a61b5ce

Please sign in to comment.