Skip to content
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

Refactor DatatypeUnitResources Controller #279

Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -33,15 +33,21 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import decodes.db.CompositeConverter;
import decodes.db.DataTypeSet;
import decodes.db.DatabaseException;
import decodes.db.DatabaseIO;
import decodes.db.EngineeringUnit;
import decodes.db.EngineeringUnitList;
import decodes.db.LinearConverter;
import decodes.db.NullConverter;
import decodes.db.Poly5Converter;
import decodes.db.UnitConverter;
import decodes.db.UnitConverterDb;
import decodes.db.UnitConverterSet;
import decodes.db.UsgsStdConverter;
import decodes.sql.DbKey;
import decodes.sql.UnitConverterIO;
import org.opendcs.odcsapi.beans.ApiDataType;
import org.opendcs.odcsapi.beans.ApiUnit;
import org.opendcs.odcsapi.beans.ApiUnitConverter;
import org.opendcs.odcsapi.dao.DbException;
Expand All @@ -57,28 +63,51 @@
public class DatatypeUnitResources extends OpenDcsResource
{
@Context HttpHeaders httpHeaders;
private static final String NO_UNIT_CONVERTER = "No UnitConverterIO available.";

@GET
@Path("datatypelist")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_GUEST})
public Response getDataTypeList(@QueryParam("standard") String std) throws DbException
{
// TODO: Add support for standard filtering in OpenDCS
try
{
DatabaseIO dbIo = getLegacyDatabase();
DataTypeSet set = new DataTypeSet();
dbIo.readDataTypeSet(set);
return Response.status(HttpServletResponse.SC_OK).entity(set).build();
dbIo.readDataTypeSet(set, std);
dbIo.close();
rma-rripken marked this conversation as resolved.
Show resolved Hide resolved
return Response.status(HttpServletResponse.SC_OK).entity(map(set)).build();
}
catch(DatabaseException e)
{
throw new DbException("Unable to retrieve data type list", e);
}
}

static ArrayList<ApiDataType> map(DataTypeSet set)
{
ArrayList<ApiDataType> ret = new ArrayList<>();
Iterator<decodes.db.DataType> it = set.iterator();
while(it.hasNext())
{
decodes.db.DataType dt = it.next();
ApiDataType adt = new ApiDataType();
if (dt.getId() != null)
{
adt.setId(dt.getId().getValue());
}
else
{
adt.setId(DbKey.NullKey.getValue());
}
adt.setCode(dt.getCode());
adt.setStandard(dt.getStandard());
adt.setDisplayName(dt.getDisplayName());
ret.add(adt);
}
return ret;
}


@GET
@Path("unitlist")
Expand All @@ -91,7 +120,7 @@ public Response getUnitList() throws DbException
DatabaseIO dbIo = getLegacyDatabase();
EngineeringUnitList euList = new EngineeringUnitList();
dbIo.readEngineeringUnitList(euList);

dbIo.close();
return Response.status(HttpServletResponse.SC_OK).entity(map(euList)).build();
}
catch(DatabaseException e)
Expand Down Expand Up @@ -128,11 +157,13 @@ public Response postEU(@QueryParam("fromabbr") String fromabbr, ApiUnit eu)
{
try
{
EngineeringUnit unit = new EngineeringUnit(fromabbr, eu.getName(), eu.getAbbr(), eu.getMeasures());
EngineeringUnit unit = new EngineeringUnit(fromabbr, eu.getName(), eu.getFamily(), eu.getMeasures());
DatabaseIO dbIo = getLegacyDatabase();
EngineeringUnitList euList = new EngineeringUnitList();
dbIo.readEngineeringUnitList(euList);
euList.add(unit);
dbIo.writeEngineeringUnitList(euList);
dbIo.close();
return Response.status(HttpServletResponse.SC_OK)
.entity(map(euList)).build();
}
Expand Down Expand Up @@ -161,6 +192,7 @@ public Response deleteEU(@QueryParam("abbr") String abbr) throws DbException, We
EngineeringUnitList euList = new EngineeringUnitList();
euList.add(unit);
dbIo.writeEngineeringUnitList(euList);
dbIo.close();
return Response.status(HttpServletResponse.SC_OK).entity("EU with abbr " + abbr + " deleted").build();
}
catch(DatabaseException e)
Expand All @@ -180,6 +212,7 @@ public Response getUnitConvList() throws DbException
DatabaseIO dbIo = getLegacyDatabase();
UnitConverterSet unitConverterSet = new UnitConverterSet();
dbIo.readUnitConverterSet(unitConverterSet);
dbIo.close();
return Response.status(HttpServletResponse.SC_OK).entity(map(unitConverterSet)).build();
}
catch(DatabaseException e)
Expand All @@ -196,16 +229,23 @@ static List<ApiUnitConverter> map(UnitConverterSet unitSet)
{
UnitConverterDb unitConv = it.next();
ApiUnitConverter euc = new ApiUnitConverter();
euc.setUcId(unitConv.getId().getValue());
if (unitConv.getId() != null)
{
euc.setUcId(unitConv.getId().getValue());
}
else
{
euc.setUcId(DbKey.NullKey.getValue());
}
euc.setFromAbbr(unitConv.fromAbbr);
euc.setToAbbr(unitConv.toAbbr);
euc.setAlgorithm(unitConv.algorithm);
euc.setA(unitConv.coefficients[0]);
euc.setB(unitConv.coefficients[1]);
euc.setC(unitConv.coefficients[2]);
euc.setD(unitConv.coefficients[3]);
euc.setE(unitConv.coefficients[4]);
euc.setF(unitConv.coefficients[5]);
euc.setA(unitConv.coefficients[0] == 0.0 ? null : unitConv.coefficients[0]);
euc.setB(unitConv.coefficients[1] == 0.0 ? null : unitConv.coefficients[1]);
euc.setC(unitConv.coefficients[2] == 0.0 ? null : unitConv.coefficients[2]);
euc.setD(unitConv.coefficients[3] == 0.0 ? null : unitConv.coefficients[3]);
euc.setE(unitConv.coefficients[4] == 0.0 ? null : unitConv.coefficients[4]);
euc.setF(unitConv.coefficients[5] == 0.0 ? null : unitConv.coefficients[5]);
ret.add(euc);
}
return ret;
Expand All @@ -220,11 +260,11 @@ public Response postEUConv(ApiUnitConverter euc) throws DbException
{
try
{
// TODO: Create a write method for UnitConverter in OpenDCS
UnitConverterIO unitDao = createDb().getLegacyDatabase(UnitConverterIO.class)
.orElseThrow(() -> new DbException(NO_UNIT_CONVERTER));
unitDao.write(map(euc));
return Response.status(HttpServletResponse.SC_OK).entity("EUConv Saved").build();
DatabaseIO dbIo = getLegacyDatabase();
UnitConverterSet unitSet = map(euc);
dbIo.writeUnitConverterSet(unitSet);
dbIo.close();
return Response.status(HttpServletResponse.SC_OK).entity(map(unitSet)).build();
}
catch(DatabaseException e)
{
Expand All @@ -237,16 +277,58 @@ static UnitConverterSet map(ApiUnitConverter euc) throws DbException
try
{
UnitConverterSet unitConverterSet = new UnitConverterSet();
UnitConverter unitConverter = null;
EngineeringUnit fromEU = new EngineeringUnit(euc.getFromAbbr(), "", "", "");
EngineeringUnit toEU = new EngineeringUnit(euc.getToAbbr(), "", "", "");
switch(euc.getAlgorithm().toUpperCase())
{
case "POLY-5":
unitConverter = new Poly5Converter(fromEU, toEU);
break;
case "LINEAR":
unitConverter = new LinearConverter(fromEU, toEU);
break;
case "COMPOSITE":
unitConverter = CompositeConverter.build(fromEU, toEU);
break;
case "NULL":
unitConverter = new NullConverter(fromEU, toEU);
break;
case "USGS":
unitConverter = new UsgsStdConverter(fromEU, toEU);
break;
default:
throw new DbException("Unknown algorithm: " + euc.getAlgorithm());
}
double[] coeffs = new double[6];
coeffs[0] = euc.getA() == null ? 0.0 : euc.getA();
coeffs[1] = euc.getB() == null ? 0.0 : euc.getB();
coeffs[2] = euc.getC() == null ? 0.0 : euc.getC();
coeffs[3] = euc.getD() == null ? 0.0 : euc.getD();
coeffs[4] = euc.getE() == null ? 0.0 : euc.getE();
coeffs[5] = euc.getF() == null ? 0.0 : euc.getF();
unitConverter.setCoefficients(coeffs);
UnitConverterDb unitConverterDb = new UnitConverterDb(euc.getFromAbbr(), euc.getToAbbr());
unitConverterDb.setId(DbKey.createDbKey(euc.getUcId()));
if (euc.getUcId() != null)
{
unitConverterDb.setId(DbKey.createDbKey(euc.getUcId()));
}
else
{
unitConverterDb.setId(DbKey.NullKey);
}

unitConverterDb.execConverter = unitConverter;
unitConverterDb.algorithm = euc.getAlgorithm();
unitConverterDb.coefficients[0] = euc.getA();
unitConverterDb.coefficients[1] = euc.getB();
unitConverterDb.coefficients[2] = euc.getC();
unitConverterDb.coefficients[3] = euc.getD();
unitConverterDb.coefficients[4] = euc.getE();
unitConverterDb.coefficients[5] = euc.getF();
unitConverterDb.coefficients[0] = euc.getA() == null ? 0.0 : euc.getA();
unitConverterDb.coefficients[1] = euc.getB() == null ? 0.0 : euc.getB();
unitConverterDb.coefficients[2] = euc.getC() == null ? 0.0 : euc.getC();
unitConverterDb.coefficients[3] = euc.getD() == null ? 0.0 : euc.getD();
unitConverterDb.coefficients[4] = euc.getE() == null ? 0.0 : euc.getE();
unitConverterDb.coefficients[5] = euc.getF() == null ? 0.0 : euc.getF();
unitConverterSet.addDbConverter(unitConverterDb);
unitConverterSet.prepareForExec();
unitConverterSet.addExecConverter(unitConverter);
return unitConverterSet;
}
catch(DatabaseException e)
Expand All @@ -269,14 +351,10 @@ public Response deleteEUConv(@QueryParam("euconvid") Long id) throws DbException

try
{
// TODO: Create a delete method for UnitConverter in OpenDCS
UnitConverterIO unitDao = createDb().getLegacyDatabase(UnitConverterIO.class)
.orElseThrow(() -> new DbException(NO_UNIT_CONVERTER));
UnitConverterDb unitConvDB = new UnitConverterDb("", "");
unitConvDB.setId(DbKey.createDbKey(id));
// unitDao.delete(unitConvDB);
// return Response.status(HttpServletResponse.SC_OK).entity("EUConv with id=" + id + " deleted").build();
return Response.status(HttpServletResponse.SC_NOT_IMPLEMENTED).build();
DatabaseIO dbIo = getLegacyDatabase();
dbIo.deleteUnitConverterSet(id);
dbIo.close();
return Response.status(HttpServletResponse.SC_OK).entity("EUConv with id=" + id + " deleted").build();
}
catch(DatabaseException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.ArrayList;
import java.util.List;

import decodes.db.DataType;
import decodes.db.DataTypeSet;
import decodes.db.EngineeringUnit;
import decodes.db.EngineeringUnitList;
import decodes.db.UnitConverterDb;
import decodes.db.UnitConverterSet;
import decodes.sql.DbKey;
import org.junit.jupiter.api.Test;
import org.opendcs.odcsapi.beans.ApiDataType;
import org.opendcs.odcsapi.beans.ApiUnit;
import org.opendcs.odcsapi.beans.ApiUnitConverter;

Expand All @@ -25,7 +28,7 @@ void testMapUnitConverter() throws Exception
ApiUnitConverter auc = new ApiUnitConverter();
auc.setFromAbbr("ft");
auc.setToAbbr("m");
auc.setAlgorithm("none");
auc.setAlgorithm("NULL");
auc.setA(1.0);
auc.setB(2.0);
auc.setC(3.0);
Expand All @@ -44,14 +47,13 @@ void testMapUnitConverter() throws Exception
assertEquals("ft->m", ucdb.toString());
assertEquals("ft", ucdb.fromAbbr);
assertEquals("m", ucdb.toAbbr);
assertEquals("none", ucdb.algorithm);
assertEquals("NULL", ucdb.algorithm);
assertEquals(1.0, ucdb.coefficients[0]);
assertEquals(2.0, ucdb.coefficients[1]);
assertEquals(3.0, ucdb.coefficients[2]);
assertEquals(4.0, ucdb.coefficients[3]);
assertEquals(5.0, ucdb.coefficients[4]);
assertEquals(6.0, ucdb.coefficients[5]);

}

@Test
Expand Down Expand Up @@ -106,4 +108,32 @@ void testUnitConvertMap()
assertEquals(unitDb.coefficients[5], unitConverter.getF());
assertEquals(unitDb.getId().getValue(), unitConverter.getUcId());
}

@Test
void testDataTypeSetMap() throws Exception
{
DataTypeSet dts = new DataTypeSet();
DataType dt = new DataType("SHEF-PE", "TEST_CODE");
dt.setId(DbKey.createDbKey(1234L));
dt.setDisplayName("Test Display Name");
dts.add(dt);
DataType dt2 = new DataType("EPA-CODE", "995842215");
dt2.setId(DbKey.createDbKey(5678L));
dt2.setDisplayName("Test Display Name 2");
dts.add(dt2);

ArrayList<ApiDataType> apiDataTypes = map(dts);
assertNotNull(apiDataTypes);
assertEquals(2, apiDataTypes.size());
ApiDataType apiDataType = apiDataTypes.get(0);
assertEquals(dt.getCode(), apiDataType.getCode());
assertEquals(dt.getId().getValue(), apiDataType.getId());
assertEquals(dt.getDisplayName(), apiDataType.getDisplayName());
assertEquals(dt.getStandard(), apiDataType.getStandard());
ApiDataType apiDataType2 = apiDataTypes.get(1);
assertEquals(dt2.getCode(), apiDataType2.getCode());
assertEquals(dt2.getId().getValue(), apiDataType2.getId());
assertEquals(dt2.getDisplayName(), apiDataType2.getDisplayName());
assertEquals(dt2.getStandard(), apiDataType2.getStandard());
}
}
Loading