Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public DbException(String message, Exception cause)
{
super(message, cause);
}

public DbException(String message)
{
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import javax.ws.rs.core.Context;

import decodes.cwms.CwmsDatabaseProvider;
import decodes.db.Database;
import decodes.db.DatabaseException;
import decodes.util.DecodesSettings;
import opendcs.opentsdb.OpenTsdbProvider;
import decodes.db.DatabaseIO;
import decodes.tsdb.TimeSeriesDb;
import org.opendcs.database.DatabaseService;
import org.opendcs.database.api.OpenDcsDao;
import org.opendcs.database.api.OpenDcsDatabase;
Expand Down Expand Up @@ -84,4 +87,16 @@ final OpenDcsDatabase createDb()
// throw new IllegalStateException("Error connecting to the database via JNDI", e);
}
}

DatabaseIO getLegacyDatabase()
{
return createDb().getLegacyDatabase(Database.class).map(Database::getDbIo)
.orElseThrow(() -> new UnsupportedOperationException("Endpoint is unsupported by the OpenDCS REST API."));
}

TimeSeriesDb getLegacyTimeseriesDB()
{
return createDb().getLegacyDatabase(TimeSeriesDb.class)
.orElseThrow(() -> new UnsupportedOperationException("Endpoint is unsupported by the OpenDCS REST API."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

package org.opendcs.odcsapi.res;

import java.sql.SQLException;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Vector;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -31,18 +32,23 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import decodes.db.DataPresentation;
import decodes.db.DataType;
import decodes.db.DatabaseException;
import decodes.db.DatabaseIO;
import decodes.db.PresentationGroup;
import decodes.db.PresentationGroupList;
import decodes.sql.DbKey;
import org.opendcs.odcsapi.beans.ApiPresentationElement;
import org.opendcs.odcsapi.beans.ApiPresentationGroup;
import org.opendcs.odcsapi.dao.ApiPresentationDAO;
import org.opendcs.odcsapi.beans.ApiPresentationRef;
import org.opendcs.odcsapi.dao.DbException;
import org.opendcs.odcsapi.errorhandling.ErrorCodes;
import org.opendcs.odcsapi.errorhandling.WebAppException;
import org.opendcs.odcsapi.hydrojson.DbInterface;
import org.opendcs.odcsapi.sec.AuthorizationCheck;
import org.opendcs.odcsapi.util.ApiConstants;
import org.opendcs.odcsapi.util.ApiHttpUtil;

@Path("/")
public class PresentationResources
public class PresentationResources extends OpenDcsResource
{
@Context HttpHeaders httpHeaders;

Expand All @@ -52,74 +58,179 @@ public class PresentationResources
@RolesAllowed({AuthorizationCheck.ODCS_API_GUEST})
public Response getPresentationRefs() throws DbException
{
Logger.getLogger(ApiConstants.loggerName).fine("getPresentationRefs");
try (DbInterface dbi = new DbInterface();
ApiPresentationDAO dao = new ApiPresentationDAO(dbi))
try
{
return ApiHttpUtil.createResponse(dao.getPresentationRefs());
DatabaseIO dbio = getLegacyDatabase();
PresentationGroupList groupList = new PresentationGroupList();
dbio.readPresentationGroupList(groupList);
return Response.status(HttpServletResponse.SC_OK).entity(map(groupList)).build();
}
catch (DatabaseException e)
{
throw new DbException("Unable to retrieve presentation groups", e);
}
}

static ArrayList<ApiPresentationRef> map(PresentationGroupList groupList)
{
ArrayList<ApiPresentationRef> ret = new ArrayList<>();
for (PresentationGroup group : groupList.getVector())
{
ApiPresentationRef presRef = new ApiPresentationRef();
presRef.setGroupId(group.getId().getValue());
presRef.setName(group.groupName);
presRef.setInheritsFrom(group.inheritsFrom);
presRef.setProduction(group.isProduction);
if (group.parent != null)
presRef.setInheritsFromId(group.parent.getId().getValue());
presRef.setLastModified(group.lastModifyTime);
ret.add(presRef);
}
return ret;
}

@GET
@Path("presentation")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_GUEST})
public Response getPresentation(@QueryParam("groupid") Long groupId)
throws WebAppException, DbException, SQLException
throws WebAppException, DbException
{
if (groupId == null)
throw new WebAppException(ErrorCodes.MISSING_ID,
"Missing required groupid parameter.");

Logger.getLogger(ApiConstants.loggerName).fine("getPresentation id=" + groupId);
try (DbInterface dbi = new DbInterface();
ApiPresentationDAO dao = new ApiPresentationDAO(dbi))
try
{
DatabaseIO dbio = getLegacyDatabase();
PresentationGroup group = new PresentationGroup();
group.setId(DbKey.createDbKey(groupId));
dbio.readPresentationGroup(group);
return Response.status(HttpServletResponse.SC_OK).entity(map(group)).build();
}
catch (DatabaseException e)
{
return ApiHttpUtil.createResponse(dao.getPresentation(groupId));
throw new DbException(String.format("Unable to retrieve presentation group with ID: %s", groupId), e);
}
}

static ApiPresentationGroup map(PresentationGroup group)
{
ApiPresentationGroup presGrp = new ApiPresentationGroup();
presGrp.setLastModified(group.lastModifyTime);
presGrp.setName(group.groupName);
presGrp.setProduction(group.isProduction);
if (group.parent != null)
{
presGrp.setInheritsFrom(group.parent.groupName);
presGrp.setInheritsFromId(group.parent.getId().getValue());
}
presGrp.setGroupId(group.getId().getValue());
presGrp.setElements(map(group.dataPresentations));
return presGrp;
}

static ArrayList<ApiPresentationElement> map(Vector<DataPresentation> dataPresentations)
{
ArrayList<ApiPresentationElement> ret = new ArrayList<>();
for(DataPresentation dp : dataPresentations)
{
ApiPresentationElement ape = new ApiPresentationElement();
ape.setDataTypeCode(dp.getDataType().getCode());
ape.setDataTypeStd(dp.getDataType().getStandard());
ape.setFractionalDigits(dp.getMaxDecimals());
ape.setMax(dp.getMaxValue());
ape.setMin(dp.getMinValue());
ape.setUnits(dp.getUnitsAbbr());
ret.add(ape);
}
return ret;
}

@POST
@Path("presentation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_ADMIN, AuthorizationCheck.ODCS_API_USER})
public Response postPresentation(ApiPresentationGroup presGrp) throws WebAppException, DbException, SQLException
public Response postPresentation(ApiPresentationGroup presGrp) throws DbException
{
Logger.getLogger(ApiConstants.loggerName)
.fine("post presentation received presentation " + presGrp.getName()
+ " with ID=" + presGrp.getGroupId());
try
{
DatabaseIO dbio = getLegacyDatabase();
dbio.writePresentationGroup(map(presGrp));
return Response.status(HttpServletResponse.SC_OK)
.entity("Successfully stored presentation group")
.build();
}
catch (DatabaseException e)
{
throw new DbException("Unable to store presentation group", e);
}
}

try (DbInterface dbi = new DbInterface();
ApiPresentationDAO dao = new ApiPresentationDAO(dbi))
static PresentationGroup map(ApiPresentationGroup presGrp) throws DatabaseException
{
PresentationGroup group = new PresentationGroup();
group.lastModifyTime = presGrp.getLastModified();
group.groupName = presGrp.getName();
group.setId(DbKey.createDbKey(presGrp.getGroupId()));
group.isProduction = presGrp.isProduction();
group.inheritsFrom = presGrp.getInheritsFrom();
PresentationGroup apiGroup = new PresentationGroup();
apiGroup.groupName = presGrp.getInheritsFrom();
if (presGrp.getInheritsFromId() != null)
{
dao.writePresentation(presGrp);
return ApiHttpUtil.createResponse(presGrp);
apiGroup.setId(DbKey.createDbKey(presGrp.getInheritsFromId()));
}
group.parent = apiGroup;
group.dataPresentations = map(presGrp.getElements());

return group;
}

static Vector<DataPresentation> map(ArrayList<ApiPresentationElement> elements) {
Vector<DataPresentation> ret = new Vector<>();

for (ApiPresentationElement ape : elements)
{
DataPresentation dataPres = new DataPresentation();
dataPres.setUnitsAbbr(ape.getUnits());
dataPres.setDataType(new DataType(ape.getDataTypeStd(), ape.getDataTypeCode()));
dataPres.setMaxDecimals(ape.getFractionalDigits());
dataPres.setMinValue(ape.getMin());
dataPres.setMaxValue(ape.getMax());
ret.add(dataPres);
}
return ret;
}

@DELETE
@Path("presentation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_ADMIN, AuthorizationCheck.ODCS_API_USER})
public Response deletePresentation(@QueryParam("groupid") Long groupId) throws DbException, SQLException
public Response deletePresentation(@QueryParam("groupid") Long groupId) throws DbException
{
Logger.getLogger(ApiConstants.loggerName)
.fine("DELETE presentation received groupid=" + groupId);

// Use username and password to attempt to connect to the database
try (DbInterface dbi = new DbInterface();
ApiPresentationDAO dao = new ApiPresentationDAO(dbi))
try
{
DatabaseIO dbio = getLegacyDatabase();
PresentationGroup group = new PresentationGroup();
group.setId(DbKey.createDbKey(groupId));

// TODO: Add support for this check
// String s = dao.routSpecsUsing(groupId);
// if (s != null)
// return ApiHttpUtil.createResponse("Cannot delete presentation group " + groupId
// + " because it is used by the following routing specs: "
// + s, ErrorCodes.NOT_ALLOWED);
dbio.deletePresentationGroup(group);
return Response.status(HttpServletResponse.SC_OK)
.entity("Presentation Group with ID " + groupId + " deleted")
.build();
}
catch (DatabaseException e)
{
String s = dao.routSpecsUsing(groupId);
if (s != null)
return ApiHttpUtil.createResponse("Cannot delete presentation group " + groupId
+ " because it is used by the following routing specs: "
+ s, ErrorCodes.NOT_ALLOWED);

dao.deletePresentation(groupId);
return ApiHttpUtil.createResponse("Presentation Group with ID " + groupId + " deleted");
throw new DbException("Unable to delete presentation group", e);
}
}
}
Loading