Skip to content
Merged
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
40 changes: 22 additions & 18 deletions cwms_radar_api/src/main/java/cwms/radar/api/LocationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,51 @@

import io.javalin.apibuilder.CrudHandler;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import cwms.radar.data.CwmsDataManager;
import cwms.radar.data.dao.Location;


/**
*
* @author mike
*/
public class LocationController implements CrudHandler {
public static final String ALL_LOCATIONS_QUERY = "select * from cwms_20.av_loc2"; //TODO: put a where clause in here with everything



@Override
public void getAll(Context ctx) {
try (
Connection conn = ctx.attribute("database");
PreparedStatement stmt = conn.prepareStatement(ALL_LOCATIONS_QUERY);
ResultSet rs = stmt.executeQuery();
CwmsDataManager cdm = new CwmsDataManager(ctx);
) {
ArrayList<Location> locations = new ArrayList<>();
while (rs.next()) {
Location l = new Location(rs);
locations.add(l);
String format = ctx.queryParam("format","json");
String names = ctx.queryParam("names");
String units = ctx.queryParam("units");
String datum = ctx.queryParam("datum");
String office = ctx.queryParam("office");


switch(format){
case "json": {ctx.contentType("application/json"); break;}
case "tab": {ctx.contentType("text/tab-sperated-values");break;}
case "csv": {ctx.contentType("text/csv"); break;}
case "xml": {ctx.contentType("application/xml");break;}
case "wml2": {ctx.contentType("application/xml");break;}
}


String results = cdm.getLocations(names,format,units,datum,office);
ctx.status(HttpServletResponse.SC_OK);
ctx.json(locations);
ctx.result(results);
} catch (SQLException ex) {
Logger.getLogger(LocationController.class.getName()).log(Level.SEVERE, null, ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
Expand Down
46 changes: 20 additions & 26 deletions cwms_radar_api/src/main/java/cwms/radar/api/OfficeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,32 @@

import io.javalin.apibuilder.CrudHandler;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import cwms.radar.api.Office;


import cwms.radar.data.CwmsDataManager;
import cwms.radar.data.dao.Office;

/**
*
* @author mike
*/
public class OfficeController implements CrudHandler {
public static final String ALL_OFFICES_QUERY = "select office_id,long_name,office_type,report_to_office_id from cwms_20.av_office"; //TODO: put a where clause in here with everything


@Override
public void getAll(Context ctx) {
try (
Connection conn = ctx.attribute("database");
PreparedStatement stmt = conn.prepareStatement(ALL_OFFICES_QUERY);
ResultSet rs = stmt.executeQuery();
CwmsDataManager cdm = new CwmsDataManager(ctx);
) {
ArrayList<Office> offices = new ArrayList<>();
while (rs.next()) {
Office l = new Office(rs);
offices.add(l);

}

HashMap<String,Object> results = new HashMap<>();
results.put("offices",offices);
results.put("offices",cdm.getOffices());
ctx.status(HttpServletResponse.SC_OK);
ctx.json(results);
} catch (SQLException ex) {
Expand All @@ -98,8 +82,18 @@ public void getAll(Context ctx) {
}

@Override
public void getOne(Context ctx, String location_code) {
throw new UnsupportedOperationException("Not supported yet.");
public void getOne(Context ctx, String office_id) {
try(
CwmsDataManager cdm = new CwmsDataManager(ctx);
) {
Office office = cdm.getOfficeById(office_id);
ctx.status(HttpServletResponse.SC_OK);
ctx.json(office);
} catch( SQLException ex ){
Logger.getLogger(LocationController.class.getName()).log(Level.SEVERE, null, ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ctx.result("Failed to process request");
}
}

@Override
Expand Down
104 changes: 104 additions & 0 deletions cwms_radar_api/src/main/java/cwms/radar/data/CwmsDataManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cwms.radar.data;

import java.util.logging.Level;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import cwms.radar.data.dao.Location;
import cwms.radar.data.dao.Office;
import io.javalin.http.Context;

public class CwmsDataManager implements AutoCloseable {
private static final Logger logger = Logger.getLogger("CwmsDataManager");
public static final String ALL_OFFICES_QUERY = "select office_id,long_name,office_type,report_to_office_id from cwms_20.av_office"; // TODO:
// put
// a
// where
// clause
// in
// here
// with
// everything
public static final String SINGLE_OFFICE = "select office_id,long_name,office_type,report_to_office_id from cwms_20.av_office where office_id=?";
public static final String ALL_LOCATIONS_QUERY = "select cwms_loc.retrieve_locations_f(?,?,?,?,?) from dual";



private Connection conn;

public CwmsDataManager(Context ctx) {
conn = ctx.attribute("database");
}

@Override
public void close() throws SQLException {
conn.close();
}

public String getLocations(String names,String format, String units, String datum, String OfficeId) {
ResultSet rs = null;
try( PreparedStatement stmt = conn.prepareStatement(ALL_LOCATIONS_QUERY); ) {
stmt.setString(1,names);
stmt.setString(2,format);
stmt.setString(3,units);
stmt.setString(4,datum);
stmt.setString(5,OfficeId);
rs = stmt.executeQuery();

if(rs.next()){
Clob clob = rs.getClob(1);
return clob.getSubString(1L, (int)clob.length());
}

}catch (SQLException err) {
logger.log(Level.WARNING, err.getLocalizedMessage(), err);
} finally {
if( rs != null ){
try{ rs.close(); } catch (Exception err) {logger.log(Level.WARNING, err.getLocalizedMessage(), err);}
}
}
return null;
}

public List<Office> getOffices() throws SQLException {
try (PreparedStatement stmt = conn.prepareStatement(ALL_OFFICES_QUERY); ResultSet rs = stmt.executeQuery();) {
List<Office> offices = new ArrayList<>();
while (rs.next()) {
Office l = new Office(rs);
offices.add(l);
}
return offices;
} catch (SQLException err) {
logger.log(Level.WARNING, err.getLocalizedMessage(), err);
}
return null;
}

public Office getOfficeById(String office_id) {

try(
PreparedStatement stmt = conn.prepareStatement(SINGLE_OFFICE);
) {
stmt.setString(1, office_id);
try(
ResultSet rs = stmt.executeQuery();
){
if(rs.next()){
return new Office(rs);
}
}
} catch( SQLException err ){
logger.warning("Failed to process database request" + err.getLocalizedMessage() );
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package cwms.radar.api;
package cwms.radar.data.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package cwms.radar.api;
package cwms.radar.data.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down