Commit dab7762c authored by Edileuton Henrique de Oliveira's avatar Edileuton Henrique de Oliveira

Merge branch 'master' of git:./proinfodata/unstable

parents 20388edd 83548767
logs
project/project
project/target
target
tmp
.history
dist
/.idea
/*.iml
/out
/.idea_modules
/.classpath
/.project
/RUNNING_PID
/.settings
proinfodata-web
===============
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index() {
return ok(index.render());
}
public static Result jsRoutes() {
return ok(Routes.javascriptRouter("jsRoutes",
routes.javascript.Application.index(),
routes.javascript.Attendance.school(),
routes.javascript.Attendance.availabilityBrasil(),
routes.javascript.Attendance.availabilityRegion(),
routes.javascript.Attendance.availabilityState(),
routes.javascript.Attendance.inventoryBrasil(),
routes.javascript.Attendance.inventoryRegion(),
routes.javascript.Attendance.inventoryState(),
routes.javascript.Attendance.auditBrasil(),
routes.javascript.Attendance.auditRegion(),
routes.javascript.Attendance.auditState(),
routes.javascript.Attendance.alertBrasil(),
routes.javascript.Attendance.alertRegion(),
routes.javascript.Attendance.alertState(),
routes.javascript.Reports.availabilityCity(),
routes.javascript.Reports.inventoryCity(),
routes.javascript.Reports.auditCity(),
routes.javascript.Reports.alertCity(),
routes.javascript.Data.netUsage(),
routes.javascript.Data.availability(),
routes.javascript.Data.inventory(),
routes.javascript.Data.audit(),
routes.javascript.Data.alert(),
routes.javascript.Data.os(),
routes.javascript.Combo.projects(),
routes.javascript.Combo.reports(),
routes.javascript.Combo.regions(),
routes.javascript.Combo.states(),
routes.javascript.Combo.cities()
)
).as("text/javascript");
}
}
package controllers;
import java.util.ArrayList;
import play.*;
import play.mvc.*;
import play.data.*;
import views.html.attendance.*;
import models.*;
public class Attendance extends Controller {
public static Result index() {
return redirect(controllers.routes.Attendance.availabilityBrasil("proinfo"));
}
public static Result school(Long id) throws java.sql.SQLException {
School school = School.getById(id);
return ok(views.html.attendance.school.render(school));
}
public static Result searchSchool() throws java.sql.SQLException {
DynamicForm requestData = DynamicForm.form().bindFromRequest();
String query = requestData.get("query");
String region = requestData.get("region");
String state = requestData.get("state");
String city = requestData.get("city");
if ((query == null) || (query.length() == 0)) {
return ok(views.html.attendance.searchSchool.render(null, null, null, null, null));
}
if ((region == null) || (region.length() == 0)) {
ArrayList<SearchResult> results = SearchResult.getByQuery(query);
return ok(views.html.attendance.searchSchool.render(query, null, null, null, results));
}
if ((state == null) || (state.length() == 0)) {
ArrayList<SearchResult> results = SearchResult.getByQuery(query, region);
return ok(views.html.attendance.searchSchool.render(query, region, null, null, results));
}
if ((city == null) || (city.length() == 0)) {
ArrayList<SearchResult> results = SearchResult.getByQuery(query, region, state);
return ok(views.html.attendance.searchSchool.render(query, region, state, null, results));
}
ArrayList<SearchResult> results = SearchResult.getByQuery(query, region, state, city);
return ok(views.html.attendance.searchSchool.render(query, region, state, city, results));
}
public static Result searchReport() {
return ok(views.html.attendance.searchReport.render());
}
public static Result availabilityBrasil(String project) {
return ok(availability.render(project, null, null));
}
public static Result availabilityRegion(String project, String region) {
return ok(availability.render(project, region, null));
}
public static Result availabilityState(String project, String region, String state) {
return ok(availability.render(project, region, state));
}
public static Result inventoryBrasil(String project) {
return ok(inventory.render(project, null, null));
}
public static Result inventoryRegion(String project, String region) {
return ok(inventory.render(project, region, null));
}
public static Result inventoryState(String project, String region, String state) {
return ok(inventory.render(project, region, state));
}
public static Result auditBrasil(String project) {
return ok(audit.render(project, null, null));
}
public static Result auditRegion(String project, String region) {
return ok(audit.render(project, region, null));
}
public static Result auditState(String project, String region, String state) {
return ok(audit.render(project, region, state));
}
public static Result alertBrasil(String project) {
return ok(alert.render(project, null, null));
}
public static Result alertRegion(String project, String region) {
return ok(alert.render(project, region, null));
}
public static Result alertState(String project, String region, String state) {
return ok(alert.render(project, region, state));
}
public static Result osBrasil(String project) {
return ok(os.render(project, null, null));
}
}
package controllers;
import play.*;
import play.mvc.*;
import play.libs.Json;
import org.codehaus.jackson.JsonNode;
import play.mvc.BodyParser;
import models.*;
public class Combo extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result projects() {
String options = "[ " +
"{ \"value\": \"proinfo\", \"content\": \"Máquina PROINFO\" }," +
"{ \"value\": \"uca_classmate\", \"content\": \"UCA Classmate\" }," +
"{ \"value\": \"uca_server\", \"content\": \"UCA Server\" }," +
"{ \"value\": \"projector\", \"content\": \"Projetor PROINFO\" }" +
" ]";
return ok(Json.toJson(options)).as("application/json");
}
@BodyParser.Of(BodyParser.Json.class)
public static Result reports() {
JsonNode json = request().body().asJson();
String options;
String project = json.findPath("project").getTextValue();
if (project == null)
return badRequest("Missing project");
if (project.equals("proinfo"))
return ok("[ " +
"{ \"value\": \"availability\", \"content\": \"Disponibilidade\" }," +
"{ \"value\": \"inventory\", \"content\": \"Inventário\" }," +
"{ \"value\": \"audit\", \"content\": \"Parque computacional instalado\" }," +
"{ \"value\": \"alert\", \"content\": \"Alteração de inventário\" }" +
" ]").as("application/json");
else if (project.equals("projector"))
return ok("[ " +
"{ \"value\": \"availability\", \"content\": \"Disponibilidade\" }," +
"{ \"value\": \"inventory\", \"content\": \"Inventário\" }" +
" ]").as("application/json");
else
return ok("[ ]").as("application/json");
}
@BodyParser.Of(BodyParser.Json.class)
public static Result regions() throws java.sql.SQLException {
ComboOptions options = ComboOptions.getRegions();
return ok(options.toJson()).as("application/json");
}
@BodyParser.Of(BodyParser.Json.class)
public static Result states() throws java.sql.SQLException {
JsonNode json = request().body().asJson();
String region = json.findPath("region").getTextValue();
ComboOptions options = ComboOptions.getStates(region);
return ok(options.toJson()).as("application/json");
}
@BodyParser.Of(BodyParser.Json.class)
public static Result cities() throws java.sql.SQLException {
JsonNode json = request().body().asJson();
String state = json.findPath("state").getTextValue();
ComboOptions options = ComboOptions.getCities(state);
return ok(options.toJson()).as("application/json");
}
}
This diff is collapsed.
package controllers;
import java.util.ArrayList;
import play.*;
import play.mvc.*;
import play.data.*;
import views.html.doc.*;
import models.*;
public class Doc extends Controller {
public static Result index() {
return ok(views.html.doc.index.render());
}
}
package controllers;
import java.io.*;
import java.util.ArrayList;
import play.*;
import play.mvc.*;
import play.data.*;
import models.*;
public class Install extends Controller {
private static String MAKE_PACKAGE_BIN = "/home/seed2/agent/make-package.sh";
private static String MAKE_OUTPUT_DIR = "/tmp/";
public static Result index() {
return ok(views.html.install.index.render());
}
public static Result makePackage() {
DynamicForm requestData = DynamicForm.form().bindFromRequest();
String inep = requestData.get("inep");
String useProxyStr = requestData.get("useProxy");
boolean useProxy = false;
if (useProxyStr.equals("true")) useProxy = true;
String proxyHost = requestData.get("proxyHost");
String proxyPort = requestData.get("proxyPort");
String proxyUser = requestData.get("proxyUser");
String proxyPass = requestData.get("proxyPass");
if ((inep == null) || (inep.length() < 6) || (inep.length() > 12))
return badRequest("Inep inválido");
School school = null;
try {
school = School.getByInep(inep);
}
catch (java.sql.SQLException e) {
e.printStackTrace();
return internalServerError();
}
if (school == null)
return badRequest("Inep inválido");
// build the package
String command = "sh " + MAKE_PACKAGE_BIN + " " + inep;
if (useProxy) {
command += " " + proxyHost + " " + proxyPort + " " +
proxyUser + " " + proxyPass;
}
StringBuffer sb = new StringBuffer();
try {
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = reader.readLine();
while (line != null) {
sb.append(line+'\n');
line = reader.readLine();
}
}
catch (Exception e) {
e.printStackTrace();
return internalServerError();
}
String filename = "agent_"+inep+".run";
response().setHeader("Content-Disposition", "attachment; filename="+filename);
return ok(new File(MAKE_OUTPUT_DIR+filename));
}
}
package controllers;
import java.io.*;
import java.util.*;
import play.*;
import play.mvc.*;
import lib.reports.*;
public class Reports extends Controller {
public static Result availabilityCity(String project, String region, String state, String city) throws Exception {
Map params = new HashMap();
params.put("REGIAO", region);
params.put("ESTADO", state);
params.put("CIDADE", city);
response().setHeader("Content-Disposition", "inline; filename="+project+"Avail.pdf");
JasperReport report = new JasperReport(project+"AvailCity.jrxml", params);
return ok(report.renderAs("application/pdf")).as("application/pdf");
}
public static Result inventoryCity(String project, String region, String state, String city) throws Exception {
Map params = new HashMap();
params.put("REGIAO", region);
params.put("ESTADO", state);
params.put("CIDADE", city);
response().setHeader("Content-Disposition", "inline; filename="+project+"Inventory.pdf");
JasperReport report = new JasperReport(project+"InventoryCity.jrxml", params);
return ok(report.renderAs("application/pdf")).as("application/pdf");
}
public static Result auditCity(String project, String region, String state, String city) throws Exception {
Map params = new HashMap();
params.put("REGIAO", region);
params.put("ESTADO", state);
params.put("CIDADE", city);
response().setHeader("Content-Disposition", "inline; filename="+project+"Audit.pdf");
JasperReport report = new JasperReport(project+"AuditCity.jrxml", params);
return ok(report.renderAs("application/pdf")).as("application/pdf");
}
public static Result alertCity(String project, String region, String state, String city) throws Exception {
Map params = new HashMap();
params.put("REGIAO", region);
params.put("ESTADO", state);
params.put("CIDADE", city);
response().setHeader("Content-Disposition", "inline; filename="+project+"Alert.pdf");
JasperReport report = new JasperReport(project+"AlertCity.jrxml", params);
return ok(report.renderAs("application/pdf")).as("application/pdf");
}
}
package lib.reports;
import java.io.*;
import java.util.*;
import play.*;
import play.cache.Cache;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
public class JasperReport {
private static String REPORTS_PATH = "/reports/";
private static String IMAGES_PATH = "/reports/images/";
private net.sf.jasperreports.engine.JasperReport report;
private JasperPrint print;
public JasperReport(String fileName, Map params) throws JRException {
// TODO: cache compiled report
this.report = JasperCompileManager.compileReport(Play.application().path() + REPORTS_PATH + fileName);
params.put("imagesPath", Play.application().path() + IMAGES_PATH);
this.print = JasperFillManager.fillReport(this.report, params, play.db.DB.getConnection());
}
public InputStream renderAs(String mimeType) throws JRException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JRExporter exporter;
if (mimeType.equals("application/pdf"))
exporter = new JRPdfExporter();
else
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, this.print);
exporter.exportReport();
return new ByteArrayInputStream(out.toByteArray());
}
}
package models;
import java.util.ArrayList;
import java.sql.*;
import play.db.*;
import play.libs.Json;
import play.cache.Cache;
import org.codehaus.jackson.node.*;
public class Alert implements java.io.Serializable {
private static final long serialVersionUID = -5477084817752317906L;
private ArrayList<String> categories;
private ArrayList<Long> hd;
private ArrayList<Long> mem;
public Alert() {
this.categories = new ArrayList<String>();
this.hd = new ArrayList<Long>();
this.mem = new ArrayList<Long>();
}
public void addRow(String category, Long hd, Long mem) {
this.categories.add(category);
this.hd.add(hd);
this.mem.add(mem);
}
public ObjectNode toJson() {
ObjectNode result = Json.newObject();
result.put("categories", Json.toJson(this.categories));
result.put("hd", Json.toJson(this.hd));
result.put("mem", Json.toJson(this.mem));
return result;
}
public static Alert getCurrent(String project) throws SQLException {
Alert alert = (Alert) Cache.get("alertCurrent"+project);
if (alert != null)
return alert;
Connection conn = DB.getConnection();
try {
PreparedStatement st = conn.prepareStatement("SELECT * FROM alert_brazil(?::project_enum);");
st.setString(1, project);
ResultSet res = st.executeQuery();
alert = new Alert();
while (res.next()) {
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
Long hd = res.getLong(1);
Long mem = res.getLong(2);
alert.addRow(date.toString(), hd, mem);
}
Cache.set("alertCurrent"+project, alert);
}
finally {
conn.close();
}
return alert;
}
public static Alert getHistorical(String project) throws SQLException {
Alert alert = (Alert) Cache.get("alertHistorical"+project);
if (alert != null)
return alert;
Connection conn = DB.getConnection();
try {
PreparedStatement st = conn.prepareStatement("SELECT * FROM alert_brazil_monthly_history(?::project_enum);");
st.setString(1, project);
ResultSet res = st.executeQuery();
alert = new Alert();
while (res.next()) {
String date = res.getString(1);
Long hd = res.getLong(2);
Long mem = res.getLong(3);
alert.addRow(date, hd, mem);
}
Cache.set("alertHistorical"+project, alert);
}
finally {
conn.close();
}
return alert;
}
public static Alert getCurrentRegions(String project) throws SQLException {
Alert alert = (Alert) Cache.get("alertCurrentRegions"+project);
if (alert != null)
return alert;
Connection conn = DB.getConnection();
try {
PreparedStatement st = conn.prepareStatement("SELECT * FROM alert_region(?::project_enum);");
st.setString(1, project);
ResultSet res = st.executeQuery();
alert = new Alert();
while (res.next()) {
String region = res.getString(1);
Long hd = res.getLong(2);
Long mem = res.getLong(3);
alert.addRow(region, hd, mem);
}
Cache.set("alertCurrentRegions"+project, alert);
}
finally {
conn.close();
}
return alert;
}
public static Alert getCurrentByRegion(String project, String region) throws SQLException {
Alert alert = (Alert) Cache.get("alertCurrentByRegion"+project+region);
if (alert != null)
return alert;
Connection conn = DB.getConnection();
try {
PreparedStatement st = conn.prepareStatement("SELECT * FROM alert_region(?::project_enum) WHERE \"region\"=?;");
st.setString(1, project);
st.setString(2, region);
ResultSet res = st.executeQuery();
alert = new Alert();
while (res.next()) {
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
Long hd = res.getLong(2);
Long mem = res.getLong(3);
alert.addRow(date.toString(), hd, mem);
}
Cache.set("alertCurrentByRegion"+project+region, alert);
}
finally {
conn.close();
}
return alert;
}
public static Alert getHistoricalByRegion(String project, String region) throws SQLException {
Alert alert = (Alert) Cache.get("alertHistoricalByRegion"+project+region);
if (alert != null)
return alert;
Connection conn = DB.getConnection();
try {
PreparedStatement st = conn.prepareStatement("SELECT * FROM alert_region_monthly_history(?::project_enum, ?);");
st.setString(1, project);
st.setString(2, region);