Skip to content

CWMS-2024: New LRTS identifier support #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
import cwms.cda.security.CwmsAuthException;
import cwms.cda.security.MissingRolesException;
import cwms.cda.security.Role;
import cwms.cda.spi.CdaIdentityProviders;
import io.javalin.Javalin;
import io.javalin.apibuilder.CrudFunction;
import io.javalin.apibuilder.CrudHandler;
Expand Down Expand Up @@ -205,7 +204,6 @@
import java.util.concurrent.TimeUnit;
import java.util.jar.Manifest;
import javax.annotation.Resource;
import javax.management.ServiceNotFoundException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand Down Expand Up @@ -268,6 +266,7 @@ public class ApiServlet extends HttpServlet {
public static final String DATA_SOURCE = "data_source";
public static final String RAW_DATA_SOURCE = "data_source";
public static final String DATABASE = "database";
public static final String IS_NEW_LRTS = "X-CWMS-LRTS-Formatting";

// The VERSION should match the gradle version but not contain the patch version.
// For example 2.4 not 2.4.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@
import java.time.Instant;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;
import org.jooq.exception.DataAccessException;


public class BinaryTimeSeriesController implements CrudHandler {
Expand All @@ -79,6 +82,8 @@ public class BinaryTimeSeriesController implements CrudHandler {
private static final String DEFAULT_BIN_TYPE_MASK = "*";
public static final String BINARY_TYPE_MASK = "binary-type-mask";
private final MetricRegistry metrics;
private static final Pattern TS_ID_NOT_FOUND = Pattern.compile("(ORA-20001: TS_ID_NOT_FOUND: "
+ "The timeseries identifier \".+\" was not found for office \".+\")");


public BinaryTimeSeriesController(MetricRegistry metrics) {
Expand Down Expand Up @@ -208,8 +213,20 @@ public void create(@NotNull Context ctx) {
boolean maxVersion = true;
boolean replaceAll = ctx.queryParamAsClass(REPLACE_ALL, Boolean.class).getOrDefault(false);

dao.store(tts, maxVersion, replaceAll);
ctx.status(HttpServletResponse.SC_CREATED);
try {
dao.store(tts, maxVersion, replaceAll);
ctx.status(HttpServletResponse.SC_CREATED);
} catch (DataAccessException ex) {
Matcher matcher = TS_ID_NOT_FOUND.matcher(ex.getMessage());
if (matcher.find()) {
ctx.status(HttpServletResponse.SC_CONFLICT);
CdaError re = new CdaError("Failed to store binary timeseries: " + matcher.group(1));
logger.log(Level.SEVERE, re.toString(), ex);
ctx.json(re);
} else {
throw ex; // re-throw if it doesn't match the expected pattern
}
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/JooqDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.PolicyFactory;
import usace.cwms.db.jooq.codegen.packages.CWMS_ENV_PACKAGE;
import usace.cwms.db.jooq.codegen.packages.CWMS_UTIL_PACKAGE;


public abstract class JooqDao<T> extends Dao<T> {
Expand All @@ -78,6 +79,9 @@ public abstract class JooqDao<T> extends Dao<T> {
public static final int DEFAULT_FETCH_SIZE = 1000;
public static final int DEFAULT_SMALL_FETCH_SIZE = 500;
public static final int ORACLE_ECID_MAX_LENGTH = 22;
public static final int REQUIRE_NEW_LRTS_ID_FORMAT = 6;
public static final int REQUIRE_OLD_LRTS_ID_FORMAT = 0;
public static final String SESSION_USE_LRTS_ID_FORMAT = "USE_NEW_LRTS_ID_FORMAT";

static ExecuteListener listener = new ExceptionWrappingListener();
private static final Pattern INVALID_OFFICE_ID = Pattern.compile(
Expand Down Expand Up @@ -122,6 +126,9 @@ public static DSLContext getDslContext(Context ctx) {
DSLContext retVal;
final String officeId = ctx.attribute(ApiServlet.OFFICE_ID);
final DataSource dataSource = ctx.attribute(ApiServlet.DATA_SOURCE);
final Boolean isNewLRTS = ctx.header(ApiServlet.IS_NEW_LRTS) == null
? null : Boolean.parseBoolean(ctx.header(ApiServlet.IS_NEW_LRTS));

if (dataSource != null) {
DataSource wrappedDataSource = new ConnectionPreparingDataSource(connection ->
setClientInfo(ctx, connection), dataSource);
Expand All @@ -136,6 +143,12 @@ public static DSLContext getDslContext(Context ctx) {

retVal.configuration().set(new DefaultExecuteListenerProvider(listener));

if (isNewLRTS != null) {
String requireBool = isNewLRTS ? "T" : "F";
int requireIntValue = isNewLRTS ? REQUIRE_NEW_LRTS_ID_FORMAT : REQUIRE_OLD_LRTS_ID_FORMAT;
CWMS_UTIL_PACKAGE.call_SET_SESSION_INFO(retVal.configuration(),
SESSION_USE_LRTS_ID_FORMAT, requireBool, requireIntValue);
}
return retVal;
}

Expand Down
Loading
Loading