Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.30 KB | None | 0 0
  1. import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import { TranslateService } from '@ngx-translate/core';
  4. import { CollaboratorAutoCompleteService, PerimeterAutoCompleteService } from 'app/pages/services/auto-complete.service';
  5. import { TrackingStatusService } from 'app/pages/services/tracking-status.service';
  6. import { ICollaboratorEventTracking } from 'app/shared/model/collaborator-event-tracking.model';
  7. import { ICollaborator } from 'app/shared/model/collaborator.model';
  8. import { TypeRelation } from 'app/shared/model/enumerations/type-relation.model';
  9. import { IPerimeter } from 'app/shared/model/perimeter.model';
  10. import { ITrackingDefinition } from 'app/shared/model/tracking-definition.model';
  11. import { Pagination } from 'app/shared/util/pagination-util';
  12. import { ChartData } from 'chart.js';
  13. import * as moment from 'moment';
  14. import { SelectItem } from 'primeng/api';
  15. import { UIChart } from 'primeng/chart';
  16. import { Subscription } from 'rxjs';
  17.  
  18. interface ISearchCriteria {
  19.   trackingDefinition: ITrackingDefinition;
  20.   collaborator: ICollaborator;
  21.   perimeter: IPerimeter;
  22.   withSubPerimeters: boolean;
  23.   responsible: ICollaborator;
  24.   relationType: TypeRelation;
  25.   startContractDateAfter: Date;
  26.   startContractDateBefore: Date;
  27.   endContractDateAfter: Date;
  28.   endContractDateBefore: Date;
  29.   valid: boolean[];
  30. }
  31.  
  32. @Component({
  33.   selector: 'jhi-trackings',
  34.   templateUrl: './trackings.component.html',
  35.   styleUrls: ['./trackings.scss']
  36. })
  37. export class TrackingsComponent implements OnInit, AfterViewInit, OnDestroy {
  38.   searchCriteria: ISearchCriteria;
  39.  
  40.   currentSearchCriteria: ISearchCriteria;
  41.  
  42.   trackingPagination = new Pagination<ICollaboratorEventTracking>();
  43.  
  44.   enumTypeRelation: SelectItem[] = [
  45.     { value: TypeRelation.HIERARCHICAL, label: TypeRelation.HIERARCHICAL },
  46.     { value: TypeRelation.OPERATIONAL, label: TypeRelation.OPERATIONAL }
  47.   ];
  48.  
  49.   chartData: ChartData = {
  50.     labels: ['Valides', 'Invalides', 'Alertes']
  51.   };
  52.  
  53.   @ViewChild('chart', { static: false })
  54.   chart: UIChart;
  55.  
  56.   exporting = false;
  57.  
  58.   translationSubscription: Subscription;
  59.  
  60.   constructor(
  61.     private activatedRoute: ActivatedRoute,
  62.     private trackingStatusService: TrackingStatusService,
  63.     public collaboratorAutoComplete: CollaboratorAutoCompleteService,
  64.     public perimeterAutoComplete: PerimeterAutoCompleteService,
  65.     public translateService: TranslateService
  66.   ) {}
  67.  
  68.   ngOnInit() {
  69.     this.trackingPagination.itemsPerPage = 10;
  70.     this.trackingPagination.sort = 'limitDate';
  71.     this.trackingPagination.order = 1;
  72.  
  73.     this.translationSubscription = this.translateService
  74.       .stream('wfmanagementApp.TypeRelation')
  75.       .subscribe(val => this.enumTypeRelation.forEach(item => (item.label = val[item.value])));
  76.  
  77.     this.reset();
  78.     this.activatedRoute.data.subscribe(({ trackingDefinition }) => {
  79.       this.searchCriteria.trackingDefinition = trackingDefinition;
  80.       this.reset();
  81.       this.search();
  82.     });
  83.   }
  84.  
  85.   ngAfterViewInit() {
  86.     this.translateService.stream('wfmanagementApp.tracking.trackings').subscribe(map => {
  87.       this.chartData.labels[0] = map['valids'];
  88.       this.chartData.labels[1] = map['invalids'];
  89.       this.chartData.labels[2] = map['alerts'];
  90.       this.chart.refresh();
  91.     });
  92.   }
  93.  
  94.   search() {
  95.     this.currentSearchCriteria = Object.assign({}, this.searchCriteria);
  96.     this.trackingPagination.first = 0;
  97.     this.lazyLoadStatus();
  98.   }
  99.  
  100.   reset() {
  101.     this.searchCriteria = {
  102.       trackingDefinition: this.searchCriteria ? this.searchCriteria.trackingDefinition : null,
  103.       collaborator: null,
  104.       perimeter: null,
  105.       withSubPerimeters: true,
  106.       responsible: null,
  107.       relationType: null,
  108.       startContractDateAfter: null,
  109.       startContractDateBefore: new Date(),
  110.       endContractDateAfter: new Date(),
  111.       endContractDateBefore: null,
  112.       valid: [true, false]
  113.     };
  114.   }
  115.  
  116.   clearCollaboratorCriteria() {
  117.     setTimeout(() => (this.searchCriteria.collaborator = null));
  118.   }
  119.  
  120.   clearResponsibleCriteria() {
  121.     setTimeout(() => (this.searchCriteria.responsible = null));
  122.   }
  123.  
  124.   clearPerimeterCriteria() {
  125.     setTimeout(() => (this.searchCriteria.perimeter = null));
  126.   }
  127.  
  128.   lazyLoadStatus(event?: any) {
  129.     if (this.currentSearchCriteria.trackingDefinition) {
  130.       this.trackingPagination.load(event, options => {
  131.         return this.trackingStatusService.search(
  132.           this.currentSearchCriteria.trackingDefinition.id,
  133.           this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
  134.           this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
  135.           this.currentSearchCriteria.withSubPerimeters,
  136.           undefined,
  137.           this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
  138.           this.currentSearchCriteria.relationType,
  139.           this.currentSearchCriteria.startContractDateAfter,
  140.           this.currentSearchCriteria.startContractDateBefore,
  141.           this.currentSearchCriteria.endContractDateAfter,
  142.           this.currentSearchCriteria.endContractDateBefore,
  143.           this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1
  144.             ? this.currentSearchCriteria.valid[0]
  145.             : undefined,
  146.           options
  147.         );
  148.       });
  149.  
  150.       // If it is not a table event (sort or page)
  151.       if (!event) {
  152.         this.trackingStatusService
  153.           .summary(
  154.             this.currentSearchCriteria.trackingDefinition.id,
  155.             this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
  156.             this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
  157.             this.currentSearchCriteria.withSubPerimeters,
  158.             undefined,
  159.             this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
  160.             this.currentSearchCriteria.relationType,
  161.             this.currentSearchCriteria.startContractDateAfter,
  162.             this.currentSearchCriteria.startContractDateBefore,
  163.             this.currentSearchCriteria.endContractDateAfter,
  164.             this.currentSearchCriteria.endContractDateBefore,
  165.             this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1
  166.               ? this.currentSearchCriteria.valid[0]
  167.               : undefined
  168.           )
  169.           .subscribe(result => {
  170.             const summary = result.body;
  171.             this.chartData.datasets = [
  172.               {
  173.                 data: [summary.total - summary.invalidNoAlert - summary.alert, summary.invalidNoAlert, summary.alert],
  174.                 backgroundColor: ['#b2be3a', '#ffca28', '#d64b13'],
  175.                 hoverBackgroundColor: ['#b2be3a', '#ffca28', '#d64b13']
  176.               }
  177.             ];
  178.             this.chart.refresh();
  179.           });
  180.       }
  181.     }
  182.   }
  183.  
  184.   public dateIsNullOrAfterNow(dateStr) {
  185.     if (!dateStr) {
  186.       return true;
  187.     }
  188.  
  189.     const now = moment();
  190.     const date = moment(dateStr);
  191.  
  192.     return date.isSameOrAfter(now, 'day');
  193.   }
  194.  
  195.   public exportCSV() {
  196.     this.exporting = true;
  197.  
  198.     const options = {
  199.       page: 0,
  200.       size: this.trackingPagination.totalItems,
  201.       sort: this.trackingPagination.sortRequest()
  202.     };
  203.  
  204.     this.trackingStatusService
  205.       .search(
  206.         this.currentSearchCriteria.trackingDefinition.id,
  207.         this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
  208.         this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
  209.         this.currentSearchCriteria.withSubPerimeters,
  210.         undefined,
  211.         this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
  212.         this.currentSearchCriteria.relationType,
  213.         this.currentSearchCriteria.startContractDateAfter,
  214.         this.currentSearchCriteria.startContractDateBefore,
  215.         this.currentSearchCriteria.endContractDateAfter,
  216.         this.currentSearchCriteria.endContractDateBefore,
  217.         this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1 ? this.currentSearchCriteria.valid[0] : undefined,
  218.         options
  219.       )
  220.       .subscribe(result => this.exportDataCSV(result.body), () => (this.exporting = false));
  221.   }
  222.  
  223.   private exportDataCSV(trackings: ICollaboratorEventTracking[]) {
  224.     try {
  225.       const separator = ';';
  226.       let csv = '\ufeff';
  227.  
  228.       csv += this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.serialNumber'));
  229.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.firstName'));
  230.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.lastName'));
  231.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.email'));
  232.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.lastDate'));
  233.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.limitDate'));
  234.       csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.plannedDate'));
  235.       csv += '\r\n';
  236.  
  237.       trackings.forEach(tracking => {
  238.         csv += this.csvData(tracking.collaborator.serialNumber || '');
  239.         csv += separator + this.csvData(tracking.collaborator.firstName || '');
  240.         csv += separator + this.csvData(tracking.collaborator.lastName || '');
  241.         csv += separator + this.csvData(tracking.collaborator.email || '');
  242.         csv +=
  243.           separator +
  244.           this.csvData(
  245.             tracking.lastRealizedEvent ? tracking.lastRealizedEvent.date.locale(this.translateService.currentLang).format('YYYY-MM-DD') : ''
  246.           );
  247.         csv +=
  248.           separator +
  249.           this.csvData(tracking.limitDate ? tracking.limitDate.locale(this.translateService.currentLang).format('YYYY-MM-DD') : '');
  250.         csv +=
  251.           separator +
  252.           this.csvData(
  253.             tracking.plannedEvent ? tracking.plannedEvent.date.locale(this.translateService.currentLang).format('YYYY-MM-DD') : ''
  254.           );
  255.         csv += '\r\n';
  256.       });
  257.  
  258.       const blob = new Blob([csv], {
  259.         type: 'text/csv;charset=utf-8;'
  260.       });
  261.  
  262.       if (window.navigator.msSaveOrOpenBlob) {
  263.         navigator.msSaveOrOpenBlob(blob, 'trackings.csv');
  264.       } else {
  265.         const link = document.createElement('a');
  266.         link.style.display = 'none';
  267.         document.body.appendChild(link);
  268.         if (link.download !== undefined) {
  269.           link.setAttribute('href', URL.createObjectURL(blob));
  270.           link.setAttribute('download', 'trackings.csv');
  271.           link.click();
  272.         } else {
  273.           csv = 'data:text/csv;charset=utf-8,' + csv;
  274.           window.open(encodeURI(csv));
  275.         }
  276.         document.body.removeChild(link);
  277.       }
  278.     } finally {
  279.       this.exporting = false;
  280.     }
  281.   }
  282.  
  283.   private csvData(value): string {
  284.     return '"' + String(value).replace(/"/g, '""') + '"';
  285.  }
  286.  
  287.  ngOnDestroy() {
  288.    if (this.translationSubscription) {
  289.      this.translationSubscription.unsubscribe();
  290.    }
  291.  }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement