UNPKG

tfabrica

Version:

library for TFabrica - TechSol

212 lines 10.6 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var http_1 = require("@angular/http"); require("rxjs/Rx"); var rxjs_1 = require("rxjs"); var tfabrica_crud_report_model_1 = require("./tfabrica.crud.report.model"); var tfabrica_shared_service_1 = require("../main/tfabrica.shared.service"); var TfabricaCrudService = (function () { function TfabricaCrudService(_http, _shared) { this._http = _http; this._shared = _shared; } TfabricaCrudService.prototype.initFromReport = function (report) { this.setFields(report.fields); this.setFieldsToDisplay(report.fieldsToDisplay); this.setFilters(report.filters); console.log(report.filters); }; TfabricaCrudService.prototype.readData = function (report) { var _this = this; var that = this; var urlToCall = this._shared.appSettings.getCommonApiValue("/api/Report/ReadData"); console.log("Start call service: " + urlToCall); report.filters.forEach(function (filter) { if (filter.conversionFunction != null && filter.conversionFunction != undefined && filter.conversionFunction != "") { if (filter.conversionFunction == "fromDateToSapDate") { that.conversionFromDateToSapDate(filter); } } }); var bodyString = JSON.stringify(report); var headers = new http_1.Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON var options = new http_1.RequestOptions({ headers: headers }); // Create a request option return this._http.post(urlToCall, bodyString, options) // ...using post request .map(function (res) { return _this.extractReadData(res); }) // ...and calling .json() on the response to return data .catch(function (err) { return _this.handleError(err); }); //...errors if any }; TfabricaCrudService.prototype.conversionFromDateToSapDate = function (filter) { if (filter.valueLow != "" && filter.valueLow != undefined) { filter.valueLow = this.fromDateToSapDate(filter.valueLow); } if (filter.valueHigh != "" && filter.valueHigh != undefined) { filter.valueHigh = this.fromDateToSapDate(filter.valueHigh); } }; TfabricaCrudService.prototype.fromDateToSapDate = function (actDateS) { var actDate = new Date(actDateS); var ddn = actDate.getDate(); var dd = ""; if (ddn < 10) dd = "0" + ddn.toString(); else dd = ddn.toString(); var mm = ""; var mmn = actDate.getMonth() + 1; //January is 0! if (mmn < 10) mm = "0" + mmn.toString(); else mm = mmn.toString(); var yyyy = actDate.getFullYear(); return yyyy.toString() + mm.toString() + dd.toString(); }; TfabricaCrudService.prototype.fromYYYYMMDDtoDDMMYYYYWithScore = function (actDateS) { var correctData = actDateS.split("T"); console.log("correctData: " + correctData); var actData = correctData[0].split("-"); var yyyy = actData[0]; var mm = actData[1]; var dd = actData[2]; return dd + "-" + mm + "-" + yyyy; }; TfabricaCrudService.prototype.fromSapDateToDate = function (actDateS) { var yyyy = actDateS.substring(0, 4); var mm = actDateS.substring(4, 6); var dd = actDateS.substring(6, 8); return new Date(yyyy + "-" + mm + "-" + dd); }; TfabricaCrudService.prototype.fromSapDateToDateString = function (actDateS) { var yyyy = actDateS.substring(0, 4); var mm = actDateS.substring(4, 6); var dd = actDateS.substring(6, 8); //return dd + "-" + mm + "-" + yyyy; return yyyy + "-" + mm + "-" + dd; }; TfabricaCrudService.prototype.extractReadData = function (res) { console.log(res); if (res.status < 200 || res.status >= 300) { throw new Error('Bad response status: ' + res.status); } var that = this; var reportData = new tfabrica_crud_report_model_1.TfabricaCrudReport(); reportData.setFromJson(res.json().report); // apply conversion on Output reportData.readedRows.forEach(function (row) { reportData.fields.forEach(function (field) { if (field.conversionFunction != undefined && field.conversionFunction != "") { if (field.conversionFunction == "fromDateToSapDate") { row[field.name] = that.fromSapDateToDateString(row[field.name]); //console.log(row[field.name]); } if (field.conversionFunction == "YYYY-MM-DD to DD-MM-YYYY") { row[field.name] = that.fromYYYYMMDDtoDDMMYYYYWithScore(row[field.name]); console.log(row[field.name]); } } }); }); return reportData; }; TfabricaCrudService.prototype.handleError = function (error) { console.error(error); // log to console instead var errMsg = error.message || 'Server error'; console.error(errMsg); // log to console instead return rxjs_1.Observable.throw(errMsg); }; TfabricaCrudService.prototype.updateData = function (report, rowData, operation) { var _this = this; var urlToCall = ""; var that = this; console.log(report.fields); report.fields.forEach(function (field) { if (field.conversionFunction != null && field.conversionFunction != undefined && field.conversionFunction != "") { console.log("conversionFunction: " + field.conversionFunction); if (field.conversionFunction == "fromDateToSapDate") { rowData[field.name] = that.fromDateToSapDate(rowData[field.name]); console.log(rowData[field.name]); } if (field.conversionFunction == "YYYY-MM-DD to DD-MM-YYYY") { rowData[field.name] = that.fromYYYYMMDDtoDDMMYYYYWithScore(rowData[field.name]); console.log(rowData[field.name]); } } }); report.updateRow = rowData; urlToCall = this._shared.appSettings.basePath + "/api/Report/InsertData"; if (operation == "U") { urlToCall = this._shared.appSettings.basePath + "/api/Report/UpdateData"; } console.log("Start call service: " + urlToCall); var bodyString = JSON.stringify(report); var headers = new http_1.Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON var options = new http_1.RequestOptions({ headers: headers }); // Create a request option return this._http.post(urlToCall, bodyString, options) // ...using post request .map(function (res) { return _this.extractUpdateData(res); }) // ...and calling .json() on the response to return data .catch(function (err) { return _this.handleError(err); }); //...errors if any }; TfabricaCrudService.prototype.extractUpdateData = function (res) { console.log(res); if (res.status < 200 || res.status >= 300) { throw new Error('Bad response status: ' + res.status); } var reportData = new tfabrica_crud_report_model_1.TfabricaCrudReport(); reportData.setFromJson(res.json().report); return reportData; }; TfabricaCrudService.prototype.setFieldsToDisplay = function (fields) { localStorage.setItem('fieldsToDisplay', JSON.stringify(fields)); }; TfabricaCrudService.prototype.getFieldsToDisplay = function () { return JSON.parse(localStorage.getItem('fieldsToDisplay')); }; TfabricaCrudService.prototype.setFields = function (fields) { localStorage.setItem('fields', JSON.stringify(fields)); }; TfabricaCrudService.prototype.getFields = function () { return JSON.parse(localStorage.getItem('fields')); }; TfabricaCrudService.prototype.setFilters = function (filters) { localStorage.setItem('filters', JSON.stringify(filters)); }; TfabricaCrudService.prototype.getFilters = function () { return JSON.parse(localStorage.getItem('filters')); }; TfabricaCrudService.prototype.setSelectedRow = function (row) { localStorage.setItem('crud-row', JSON.stringify(row)); }; TfabricaCrudService.prototype.getSelectedRow = function () { return JSON.parse(localStorage.getItem('crud-row')); }; TfabricaCrudService.prototype.setReport = function (report) { localStorage.setItem('crud-report', JSON.stringify(report)); }; TfabricaCrudService.prototype.getReport = function () { return JSON.parse(localStorage.getItem('crud-report')); }; TfabricaCrudService.prototype.setOperation = function (operation) { localStorage.setItem('crud-operation', JSON.stringify(operation)); }; TfabricaCrudService.prototype.getOperation = function () { return JSON.parse(localStorage.getItem('crud-operation')); }; return TfabricaCrudService; }()); TfabricaCrudService = __decorate([ core_1.Injectable(), __metadata("design:paramtypes", [http_1.Http, tfabrica_shared_service_1.TfabricaSharedService]) ], TfabricaCrudService); exports.TfabricaCrudService = TfabricaCrudService; //# sourceMappingURL=tfabrica.crud.service.js.map