V1_1.java [src/java/d/crlmod/crop] Revision: default Date:
/*
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API, and application suite.
*
* 2012-2024, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package d.crlmod.crop;
import crlmod.LMODData;
import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import crlmod.nodes.Crop;
import crlmod.utils.Utils;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.State.RELEASED;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import org.apache.commons.lang3.StringUtils;
/**
* @author od, ly
*/
@Name("crops")
@Description("Crop service for the CRLMOD Database. Results from this service are returned in the rotation v1 format.")
@VersionInfo("1.1")
@Path("d/crop/1.1")
@State(RELEASED)
@Resource(from = ServiceResources.class)
public class V1_1 extends ModelDataService {
LMODData lmodData = new LMODData();
private final String OBJECT_PREFIX = "crop";
private final String OBJECT_NAME = "Crop";
private boolean nativeFormats = false;
@Override
protected void doProcess() throws Exception {
nativeFormats = parameter().getBoolean(KEY_NATIVEFORMATS, false);
getCrops();
}
@Override
protected void postProcess() throws Exception {
boolean namesOnly = parameter().getBoolean(KEY_NAMESONLY, false);
if (namesOnly) {
for (Crop crop : lmodData.crops) {
crop.id = null;
crop.defaultYield = null;
crop.yieldUnit = null;
}
}
results().put("crlmod", lmodData.getJson());
}
private void getCrops() throws Exception {
ArrayList<String> params = new ArrayList<>();
String query = "SELECT crop_id, crop_name, crop_group1, crop_group2, crop_group3, crop_group4, \n"
+ "crop_group5, crop_yield, crop_yield_unit, weps_crop_param_set, wepp_crop_param_set\n"
+ "FROM crops\n"
+ " LEFT JOIN weps_crops on (fk_weps_crop_id = weps_crop_id)\n"
+ " LEFT JOIN wepp_crops on (fk_wepp_crop_id = wepp_crop_id)\n";
query += Utils.createWhere(params, OBJECT_PREFIX, OBJECT_NAME, parameter(), LOG);
String countQuery = "SELECT Count(*) AS 'Count' \n"
+ "FROM crops \n";
countQuery += Utils.createWhere(new ArrayList<String>(), OBJECT_PREFIX, OBJECT_NAME, parameter(), LOG);
try (Connection connection = resources().getJDBC(getJDBCId())) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(countQuery)) {
resultSet.next();
lmodData.cropCount = resultSet.getInt("Count");
}
query += Utils.createOffsetAndLimits(params, parameter(), LOG, OBJECT_NAME, OBJECT_PREFIX);
try (ResultSet resultSet = statement.executeQuery(query)) {
if (!resultSet.next()) {
String errors = "";
errors = "No crops found with ";
for (int i = 0; i < params.size(); i++) {
errors += params.get(i);
if (i < params.size() - 1) {
errors += "and ";
}
}
throw new ServiceException(errors);
}
do {
BigDecimal crop_id = resultSet.getBigDecimal("crop_id");
String crop_name = resultSet.getString("crop_name");
String crop_group1 = StringUtils.isEmpty(resultSet.getString("crop_group1")) ? null : resultSet.getString("crop_group1");
String crop_group2 = StringUtils.isEmpty(resultSet.getString("crop_group2")) ? null : resultSet.getString("crop_group2");
String crop_group3 = StringUtils.isEmpty(resultSet.getString("crop_group3")) ? null : resultSet.getString("crop_group3");
String crop_group4 = StringUtils.isEmpty(resultSet.getString("crop_group4")) ? null : resultSet.getString("crop_group4");
String crop_group5 = StringUtils.isEmpty(resultSet.getString("crop_group5")) ? null : resultSet.getString("crop_group5");
String crop_yield_unit = StringUtils.isEmpty(resultSet.getString("crop_yield_unit")) ? null : resultSet.getString("crop_yield_unit");
Double crop_yield = resultSet.getDouble("crop_yield");
if (crop_yield.isNaN())
crop_yield = null;
String weps_param_set = resultSet.getString("weps_crop_param_set");
String wepp_param_set = resultSet.getString("wepp_crop_param_set");
if (weps_param_set == null)
weps_param_set = "No Matching WEPS data is available at this time.";
if (wepp_param_set == null)
wepp_param_set = "No matching WEPP data is available at this time.";
weps_param_set = weps_param_set.replaceAll("\n", "").replaceAll("\t", "");
wepp_param_set = wepp_param_set.replaceAll("\n", "").replaceAll("\t", "");
Crop crop = new Crop(crop_id.toBigInteger(), crop_name);
crop.cropGroup1 = crop_group1;
crop.cropGroup2 = crop_group2;
crop.cropGroup3 = crop_group3;
crop.cropGroup4 = crop_group4;
crop.cropGroup5 = crop_group5;
crop.defaultYield = crop_yield;
crop.yieldUnit = crop_yield_unit;
if (nativeFormats) {
crop.wepsCropParamSet = weps_param_set;
crop.weppCropParamSet = wepp_param_set;
}
lmodData.addCrop(crop);
} while (resultSet.next());
}
statement.clearBatch();
}
}
}
protected String getJDBCId() {
return CR_LMOD_ID;
}
@Override
protected Map<String, Object> getConfigInfo() {
return new LinkedHashMap<String, Object>() {
{
put(getJDBCId(), resources().getResolved(getJDBCId()));
}
};
}
}