Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public final class Controllers {
public static final String BLOB_ID = "blob-id";
public static final String INCLUDE_VALUES = "include-values";
public static final String FAIL_IF_EXISTS = "fail-if-exists";
public static final String CREATE_POOL_NAME = "create-pool-name";
public static final String IGNORE_NULLS = "ignore-nulls";
public static final String EFFECTIVE_DATE = "effective-date";
public static final String DATE = "date";
Expand Down
133 changes: 102 additions & 31 deletions cwms-data-api/src/main/java/cwms/cda/api/PoolController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
package cwms.cda.api;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.ANY_MASK;
import static cwms.cda.api.Controllers.BOTTOM_MASK;
import static cwms.cda.api.Controllers.CURSOR;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.GET_ONE;
import static cwms.cda.api.Controllers.ID_MASK;
import static cwms.cda.api.Controllers.INCLUDE_EXPLICIT;
import static cwms.cda.api.Controllers.INCLUDE_IMPLICIT;
import static cwms.cda.api.Controllers.NAME_MASK;
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.PAGE;
import static cwms.cda.api.Controllers.PAGE_SIZE;
import static cwms.cda.api.Controllers.POOL_ID;
import static cwms.cda.api.Controllers.PROJECT_ID;
import static cwms.cda.api.Controllers.RESULTS;
import static cwms.cda.api.Controllers.SIZE;
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.STATUS_404;
import static cwms.cda.api.Controllers.STATUS_501;
import static cwms.cda.api.Controllers.TOP_MASK;
import static cwms.cda.api.Controllers.*;
import static cwms.cda.api.Controllers.queryParamAsClass;
import static cwms.cda.api.Controllers.requiredParam;
import static cwms.cda.data.dao.JooqDao.getDslContext;
Expand All @@ -31,24 +12,31 @@
import com.google.common.flogger.FluentLogger;
import cwms.cda.api.errors.CdaError;
import cwms.cda.api.errors.ExceptionTraceSupport;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dao.PoolDao;
import cwms.cda.data.dto.Pool;
import cwms.cda.data.dto.Pools;
import cwms.cda.data.dto.StatusResponse;
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
import io.javalin.apibuilder.CrudHandler;
import io.javalin.core.util.Header;
import io.javalin.http.Context;
import io.javalin.plugin.openapi.annotations.HttpMethod;
import io.javalin.plugin.openapi.annotations.OpenApi;
import io.javalin.plugin.openapi.annotations.OpenApiContent;
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

public class PoolController implements CrudHandler {
public final class PoolController implements CrudHandler {

static final String TAG = "Pools";

private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final int defaultPageSize = 100;

Expand Down Expand Up @@ -103,7 +91,7 @@ private Timer.Context markAndTime(String subject) {
@OpenApiResponse(status = STATUS_501, description = "request format is not"
+ " implemented")},
description = "Returns Pools Data",
tags = {"Pools"})
tags = {TAG})
@Override
public void getAll(@NotNull Context ctx) {
try (final Timer.Context timeContext = markAndTime(GET_ALL)) {
Expand Down Expand Up @@ -191,7 +179,7 @@ public void getAll(@NotNull Context ctx) {
+ "inputs provided the Location Category was not found."),
@OpenApiResponse(status = STATUS_501, description = "request format is not "
+ "implemented")},
description = "Retrieves requested Pool", tags = {"Pools"})
description = "Retrieves requested Pool", tags = {TAG})
@Override
public void getOne(@NotNull Context ctx, @NotNull String poolId) {
try (final Timer.Context timeContext = markAndTime(GET_ONE)) {
Expand Down Expand Up @@ -246,21 +234,104 @@ public void getOne(@NotNull Context ctx, @NotNull String poolId) {
}
}

@OpenApi(ignore = true)
@OpenApi(
requestBody = @OpenApiRequestBody(
content = {
@OpenApiContent(from = Pool.class, type = Formats.JSONV2)
},
required = true),
queryParams = {
@OpenApiParam(name = FAIL_IF_EXISTS, type = Boolean.class,
description = "Create will fail if provided ID already exists. Default: true"),
@OpenApiParam(name = CREATE_POOL_NAME, type = Boolean.class,
description = "Create will create the pool name if it doesn't already exists. Default: true")
},
description = "Create CWMS Pool",
method = HttpMethod.POST,
tags = {TAG},
responses = {
@OpenApiResponse(status = STATUS_204, description = "Pool successfully stored to CWMS.")
}
)
@Override
public void create(@NotNull Context ctx) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
try (Timer.Context ignored = markAndTime(CREATE)) {
DSLContext dsl = getDslContext(ctx);
boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class)
.getOrDefault(true);
boolean createPoolName = ctx.queryParamAsClass(CREATE_POOL_NAME, Boolean.class)
.getOrDefault(true);
String formatHeader = ctx.req.getContentType();
ContentType contentType = Formats.parseHeader(formatHeader, Pool.class);
Pool pool = Formats.parseContent(contentType, ctx.body(), Pool.class);
PoolDao dao = new PoolDao(dsl);
dao.createPool(pool, failIfExists, createPoolName);
StatusResponse re = new StatusResponse(pool.getPoolName().getOfficeId(),
"Pool: " + pool.getPoolName().getPoolName() + " successfully created.",
pool.getPoolName().getPoolName());
ctx.status(HttpServletResponse.SC_CREATED).json(re);
}
}

@OpenApi(ignore = true)
@OpenApi(
pathParams = {
@OpenApiParam(name = NAME, description = "Specifies the name of "
+ "the pool to be renamed."),
},
queryParams = {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the owning office of "
+ "the pool to be renamed."),
@OpenApiParam(name = NAME, required = true, description = "Specifies the new pool name. ")
},
description = "Rename CWMS Pool",
method = HttpMethod.PATCH,
tags = {TAG},
responses = {
@OpenApiResponse(status = STATUS_204, description = "Pool successfully renamed in CWMS.")
}
)
@Override
public void update(@NotNull Context ctx, @NotNull String locationCode) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
public void update(@NotNull Context ctx, @NotNull String poolId) {
try (Timer.Context ignored = markAndTime(UPDATE)) {
String office = requiredParam(ctx, OFFICE);
String newName = requiredParam(ctx, NAME);
DSLContext dsl = getDslContext(ctx);
PoolDao dao = new PoolDao(dsl);
dao.renamePool(office, poolId, newName);
StatusResponse re = new StatusResponse(office, "Pool: " + poolId + " successfully renamed to: " + newName, newName);
ctx.status(HttpServletResponse.SC_OK).json(re);
}
}

@OpenApi(ignore = true)
@OpenApi(
pathParams = {
@OpenApiParam(name = NAME, description = "Specifies the name of "
+ "the pool to be deleted."),
},
queryParams = {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the owning office of "
+ "the pool to be deleted."),
@OpenApiParam(name = PROJECT_ID, required = true, description = "Specifies the owning project of "
+ "the pool to be deleted.")
},
description = "Delete CWMS Pool",
method = HttpMethod.DELETE,
tags = {TAG},
responses = {
@OpenApiResponse(status = STATUS_200, description = "Pool successfully deleted from CWMS."),
@OpenApiResponse(status = STATUS_404, description = "Based on the combination of "
+ "inputs provided the pool was not found.")
}
)
@Override
public void delete(@NotNull Context ctx, @NotNull String locationCode) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
public void delete(@NotNull Context ctx, @NotNull String poolId) {
try (Timer.Context ignored = markAndTime(DELETE)) {
DSLContext dsl = getDslContext(ctx);
PoolDao dao = new PoolDao(dsl);
String office = requiredParam(ctx, OFFICE);
String projectId = requiredParam(ctx, PROJECT_ID);
dao.deletePool(office, projectId, poolId);
ctx.status(HttpServletResponse.SC_NO_CONTENT);
}
}
}
53 changes: 51 additions & 2 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/PoolDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import static java.util.stream.Collectors.toList;

import com.google.common.flogger.FluentLogger;
import cwms.cda.api.errors.NotFoundException;
import cwms.cda.data.dto.Pool;
import cwms.cda.data.dto.PoolNameType;
import cwms.cda.data.dto.Pools;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.common.flogger.FluentLogger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.TooManyRowsException;
import org.jooq.impl.DSL;
import usace.cwms.db.jooq.codegen.packages.CWMS_POOL_PACKAGE;
Expand Down Expand Up @@ -217,4 +218,52 @@ public Pools retrievePools(String cursor, int pageSize,
return builder.build();
}

public void deletePool(String office, String projectId, String poolId) {
connection(dsl, c -> CWMS_POOL_PACKAGE.call_DELETE_POOL(
getDslContext(c, office).configuration(),
projectId, poolId, office)
);
}

public void createPool(Pool pool, boolean failIfExists, boolean createPoolName) {
String projectId = pool.getProjectId();
String office = pool.getPoolName().getOfficeId();
String poolId = pool.getPoolName().getPoolName();
String bottomLevelId = pool.getBottomLevelId();
String topLevelId = pool.getTopLevelId();
Number attribute = pool.getAttribute();
String description = pool.getDescription();
String clobText = pool.getClobText();
connection(dsl, c -> CWMS_POOL_PACKAGE.call_STORE_POOL2(
getDslContext(c, office).configuration(),
projectId,
poolId,
bottomLevelId,
topLevelId,
attribute,
description,
clobText,
formatBool(failIfExists),
formatBool(createPoolName),
office)
);
}

public void renamePool(String office, String poolId, String newName) {
try {
connection(dsl, c -> CWMS_POOL_PACKAGE.call_RENAME_POOL(
getDslContext(c, office).configuration(),
poolId,
newName,
office)
);
} catch (DataAccessException e) {
//Typo being address in: https://github.com/HydrologicEngineeringCenter/cwms-database/pull/215
if(e.getMessage().contains("ITEM DOES NOT EXIST")) {
throw new NotFoundException(e);
} else {
throw e;
}
}
}
}
7 changes: 7 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dto/Pool.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package cwms.cda.data.dto;

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 cwms.cda.formatters.Formats;
import cwms.cda.formatters.annotations.FormattableWith;
import cwms.cda.formatters.json.JsonV2;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@JsonDeserialize(builder = Pool.Builder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON})
public class Pool extends CwmsDTOBase {

Expand Down
Loading
Loading