V1_1.java [src/java/m/weppws/channeldb] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package m.weppws.channeldb;
import csip.ModelDataService;
import csip.annotations.*;
import csip.api.server.ServiceException;
import java.io.File;
import javax.ws.rs.*;
import org.codehaus.jettison.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import util.Extract;
@Name("returns the channel parameters assigned to channel IDs")
@Description("channel database records")
@Path("m/channeldb/1.1")
@Polling(first = 2000, next = 2000)
@Resource(file = "/data/channelDatabase.json", id = "channeldatabase")
public class V1_1 extends ModelDataService {
JSONArray channelOrderData;
JSONArray channelParameterData;
/**
* doProcess() Get input parameters from WEPP request.
*
* @throws Exception
*/
@Override
protected void doProcess() throws Exception {
readChannelDatabaseJSON(resources().getFile("channeldatabase"));
String linkFile = parameter().getString("link_file");
int[] linkIds = Extract.extractCSVColumnAsInt(workspace().getFile(linkFile), "LINKNO");
int[] strmOrder = Extract.extractCSVColumnAsInt(workspace().getFile(linkFile), "strmOrder");
int[] cpIds = new int[linkIds.length * 3];
Map<String, JSONObject> indexed_params = new HashMap<>();
for (int i = 0; i < channelParameterData.length(); i++) {
JSONObject ps = channelParameterData.getJSONObject(i);
indexed_params.put(ps.getString("id"), ps);
}
Map<Integer, String> indexed_order = new HashMap<>();
for (int i = 0; i < channelOrderData.length(); i++) {
JSONObject ps = channelOrderData.getJSONObject(i);
indexed_order.put(ps.getInt("order"), ps.getString("id"));
}
List<JSONObject> psets = new ArrayList<>();
int mapindex = 0;
for (int i = 0; i < linkIds.length; i++) {
int linkId = linkIds[i];
int strmOrderId = strmOrder[i];
String ps_id = indexed_order.get(strmOrderId);
JSONObject ps = indexed_params.get(ps_id);
if (!psets.contains(ps))
psets.add(ps);
cpIds[mapindex++] = linkId;
cpIds[mapindex++] = strmOrderId;
cpIds[mapindex++] = psets.indexOf(ps);
}
results().put("links", linkIds.length);
results().put("mapping", cpIds);
results().put("channel_parameter", psets);
}
void readChannelDatabaseJSON(File dbFile) throws ServiceException {
try {
String chandata = FileUtils.readFileToString(dbFile, "UTF-8");
JSONObject st = new JSONObject(chandata);
JSONObject chdb = st.getJSONObject("channelDatabase");
channelOrderData = chdb.getJSONArray("channelOrders");
channelParameterData = chdb.getJSONArray("channels");
} catch (IOException | JSONException e) {
throw new ServiceException("Could not read channel database file: " + dbFile.getName());
}
}
}