/* Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR This file is part of simcaq-node. simcaq-node is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. simcaq-node is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ const express = require('express'); const universityEnrollmentApp = express.Router(); const libs = `${process.cwd()}/libs`; const log = require(`${libs}/log`)(module); const squel = require('squel'); const query = require(`${libs}/middlewares/query`).query; const response = require(`${libs}/middlewares/response`); const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); const id2str = require(`${libs}/middlewares/id2str`); const addMissing = require(`${libs}/middlewares/addMissing`); const config = require(`${libs}/config`); const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; let rqf = new ReqQueryFields(); universityEnrollmentApp.get('/years', (req, res, next) => { req.sql.from('aluno_ens_superior') .field('DISTINCT aluno_ens_superior.ano_censo', 'year'); next(); }, query, response('years')); universityEnrollmentApp.get('/year_range', (req, res, next) => { req.sql.from('aluno_ens_superior') .field('MIN(aluno_ens_superior.ano_censo)', 'start_year') .field('MAX(aluno_ens_superior.ano_censo)', 'end_year'); next(); }, query, response('range')); universityEnrollmentApp.get('/academic_organization', (req, res, next) => { req.result = []; for(let i = 1; i <= 5; ++i) { req.result.push({ id: i, name: id2str.academicOrganization(i) }); }; next(); }, response('academic_organization')); universityEnrollmentApp.get('/upper_adm_dependency', (req, res, next) => { req.result = []; for(let i = 1; i <= 7; ++i) { req.result.push({ id: i, name: id2str.upperAdmDependency(i) }); }; next(); }, response('upper_adm_dependency')); universityEnrollmentApp.get('/ocde_geral', (req, res, next) => { req.result = []; for(let i = 0; i <= 8; ++i) { req.result.push({ id: i, name: id2str.ocdeGeral(i) }); }; next(); }, response('ocde_geral')); universityEnrollmentApp.get('/ocde_specific', (req, res, next) => { req.result = []; const defaultCase = null; for(let i = 1; i <= 86; ++i) { let obj = { id: i, name: id2str.ocdeSpecific(i) }; if (obj.name !== id2str.ocdeSpecific(defaultCase)){ req.result.push(obj); } }; req.result.push({ id: defaultCase, name: id2str.ocdeSpecific(defaultCase) }); next(); }, response('ocde_specific')); universityEnrollmentApp.get('/ocde_detailed', (req, res, next) => { req.result = []; const defaultCase = null; for(let i = 142; i <= 863; ++i) { let obj = { id: i, name: id2str.ocdeDetailed(i) }; if (obj.name !== id2str.ocdeDetailed(defaultCase)){ req.result.push(obj); } }; req.result.push({ id: defaultCase, name: id2str.ocdeDetailed(defaultCase) }); next(); }, response('ocde_detailed')); universityEnrollmentApp.get('/upper_turn', (req, res, next) => { req.result = []; for(let i = 1; i <= 4; ++i) { req.result.push({ id: i, name: id2str.upperTurn(i) }); }; next(); }, response('upper_turn')); universityEnrollmentApp.get('/student_deficiency', (req, res, next) => { req.result = []; for(let i = 0; i <= 1; ++i) { req.result.push({ id: i, name: id2str.studentDeficiency(i) }); }; next(); }, response('student_deficiency')); universityEnrollmentApp.get('/ethnic_group_ies', (req, res, next) => { req.result = []; for(let i = 1; i <=5; ++i) { req.result.push({ id: i, name: id2str.ethnicGroupIES(i) }); } next(); }, response('ethnic_group_ies')); universityEnrollmentApp.get('/school_type', (req, res, next) => { req.result = []; for(let i = 1; i <= 2; ++i) { req.result.push({ id: i, name: id2str.schoolType(i) }); }; next(); }, response('school_type')); universityEnrollmentApp.get('/university', (req, res, next) => { req.sql.from('aluno_ens_superior') .field('DISTINCT aluno_ens_superior.nome_ies', 'nome') .field('aluno_ens_superior.cod_ies', 'cod') next(); }, query, response('university')); universityEnrollmentApp.get('/academic_level', (req, res, next) => { req.result = []; for(let i = 1; i <= 4; ++i) { req.result.push({ id: i, name: id2str.academicLevel(i) }); }; next(); }, response('academic_level')); universityEnrollmentApp.get('/gender_ies', function (req, res, next) { req.result = []; for (var i = 1; i <= 2; ++i) { req.result.push({ id: i, name: id2str.genderIES(i) }); }; next(); }, response('gender_ies')); universityEnrollmentApp.get('/upper_education_mod', function (req, res, next) { req.result = []; for (var i = 1; i <= 3; ++i) { req.result.push({ id: i, name: id2str.upperEducationMod(i) }); }; next(); }, response('upper_education_mod')); universityEnrollmentApp.get('/age_student_code', function (req, res, next) { req.result = []; for (var i = 1; i <= 6; ++i) { req.result.push({ id: i, name: id2str.ageStudentCode(i) }); }; next(); }, response('age_student_code')); rqf.addField({ name: 'filter', field: false, where: true }).addField({ name: 'dims', field: true, where: false }).addValue({ name: 'min_year', table: '@', tableField: 'ano_censo', resultField: 'year', where: { relation: '>=', type: 'integer', table: '@', field: 'ano_censo' } }).addValue({ name: 'max_year', table: '@', tableField: 'ano_censo', resultField: 'year', where: { relation: '<=', type: 'integer', table: '@', field: 'ano_censo' } }).addValue({ name: 'state', table: 'estado', tableField: ['nome', 'id'], resultField: ['state_name', 'state_id'], where: { relation: '=', type: 'integer', field: 'cod_uf_ies', table: '@' }, join: { primary: 'id', foreign: 'cod_uf_ies', foreignTable: '@' } }).addValue({ name: 'city', table: 'municipio', tableField: ['nome', 'id'], resultField: ['city_name', 'city_id'], where: { relation: '=', type: 'integer', field: 'cod_municipio_ies', table: '@' }, join: { primary: 'id', foreign: 'cod_municipio_ies', foreignTable: '@' } }).addValue({ name: 'region', table: 'regiao', tableField: ['nome', 'id'], resultField: ['region_name', 'region_id'], where: { relation: '=', type: 'integer', field: 'id' }, join: { primary: 'id', foreign: 'cod_regiao_ies', foreignTable: 'aluno_ens_superior' } }).addValue({ name: 'university', table: 'aluno_ens_superior', tableField: ['cod_ies', 'nome_ies'], resultField: ['university_id', 'university_name'], where: { relation: '=', type: 'integer', field: 'cod_ies' } }).addValue({ name: 'age_student_code', table: 'aluno_ens_superior', tableField: 'idade_aluno_codigo', resultField: 'age_student_code_id', where: { relation: '=', type: 'integer', field: 'idade_aluno_codigo' } }).addValue({ name: 'upper_adm_dependency', table: 'aluno_ens_superior', tableField: 'par_categoria_administrativa', resultField: 'upper_adm_dependency_id', where: { relation: '=', type: 'integer', table: 'aluno_ens_superior', field: 'par_categoria_administrativa' } }).addValue({ name: 'academic_organization', table: 'aluno_ens_superior', tableField: 'cod_organizacao_academica', resultField: 'academic_organization_id', where: { relation: '=', type: 'integer', table: 'aluno_ens_superior', field: 'cod_organizacao_academica' } }).addValue({ name:'ocde_specific', table: 'aluno_ens_superior', tableField: 'par_cod_ocde_area_especifica', resultField: 'ocde_specific_id', where: { relation: '=', type: 'integer', field: 'par_cod_ocde_area_especifica' } }).addValue({ name:'ocde_geral', table: 'aluno_ens_superior', tableField: 'par_cod_ocde_area_geral', resultField: 'ocde_geral_id', where: { relation: '=', type: 'integer', field: 'par_cod_ocde_area_geral' } }).addValue({ name:'ocde_detailed', table: 'aluno_ens_superior', tableField: 'par_cod_ocde_area_detalhada', resultField: 'ocde_detailed_id', where: { relation: '=', type: 'integer', field: 'par_cod_ocde_area_detalhada' } }).addValue({ name:'academic_level', table: 'aluno_ens_superior', tableField: 'cod_grau_academico', resultField: 'academic_level_id', where: { relation: '=', type: 'integer', field: 'cod_grau_academico' } }).addValue({ name:'upper_education_mod', table: 'aluno_ens_superior', tableField: 'cod_modalidade_ensino', resultField: 'upper_education_mod_id', where: { relation: '=', type: 'integer', field: 'cod_modalidade_ensino' } }).addValue({ name:'upper_turn', table: 'aluno_ens_superior', tableField: 'cod_turno_aluno', resultField: 'upper_turn_id', where: { relation: '=', type: 'integer', field: 'cod_turno_aluno' } }).addValue({ name:'ethnic_group_ies', table: 'aluno_ens_superior', tableField: 'par_cod_cor_raca_aluno', resultField: 'ethnic_group_ies_id', where: { relation: '=', type: 'integer', field: 'par_cod_cor_raca_aluno' } }).addValue({ name:'student_deficiency', table: 'aluno_ens_superior', tableField: 'par_aluno_deficiencia_transtorno_superdotacao', resultField: 'student_deficiency_id', where: { relation: '=', type: 'integer', field: 'par_aluno_deficiencia_transtorno_superdotacao' } }).addValue({ name:'school_type', table: 'aluno_ens_superior', tableField: 'par_tipo_escola_ensino_medio', resultField: 'school_type_id', where: { relation: '=', type: 'integer', field: 'par_tipo_escola_ensino_medio' } }).addValue({ name: 'university', table: 'aluno_ens_superior', tableField: ['cod_ies', 'nome_ies'], resultField: ['university_id', 'university_name'], where: { relation: '=', type: 'integer', field: 'cod_ies' } }).addValue({ name: 'gender_ies', table: 'aluno_ens_superior', tableField: 'par_genero_aluno', resultField: 'gender_ies_id', where: { relation: '=', type: 'integer', field: 'par_genero_aluno' } }).addValue({ name:'academic_level', table: 'aluno_ens_superior', tableField: 'cod_grau_academico', resultField: 'academic_level_id', where: { relation: '=', type: 'integer', field: 'cod_grau_academico' } }); universityEnrollmentApp.get('/', rqf.parse(), (req, res, next) => { if ("university" in req.dims) { req.sql.field('COUNT(*)', 'total') .field("'Brasil'", 'name') .field('aluno_ens_superior.ano_censo', 'year') .from('aluno_ens_superior') .where('aluno_ens_superior.matriculado = 1 AND aluno_ens_superior.cod_nivel_academico = 1') .group('aluno_ens_superior.cod_ies') .group('aluno_ens_superior.ano_censo') .order('aluno_ens_superior.cod_ies') .order('aluno_ens_superior.ano_censo') } else { req.sql.field('COUNT(*)', 'total') .field("'Brasil'", 'name') .field('aluno_ens_superior.ano_censo', 'year') .from('aluno_ens_superior') .where('aluno_ens_superior.matriculado = 1 AND aluno_ens_superior.cod_nivel_academico = 1') .group('aluno_ens_superior.ano_censo') .order('aluno_ens_superior.ano_censo') } next(); }, rqf.build(), query, addMissing(rqf), id2str.transform(false), response('universityEnrollment')); module.exports = universityEnrollmentApp;