<commit_before>import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {UserService} from '../../service/user.service';
import {ProjectService} from '../../service/project.service';
import {AnalyzerConfiguration} from '../../model/analyzer-configuration';
import {FilePatterns} from '../../model/file-patterns';
import {FORBIDDEN} from 'http-status-codes';
import {Module} from '../../model/module';

@Component({
  selector: 'app-configure-project',
  templateUrl: './configure-project.component.html',
  styleUrls: ['./configure-project.component.scss']
})
export class ConfigureProjectComponent implements OnInit {


  projectName: string;
  analyzers: AnalyzerConfiguration[];
  filePatterns: FilePatterns[];

  // Fields for input binding
  filePatternIncludeInput;
  filePatternExcludeInput;
  modulesInput;
  modules: Module[];
  deletedModules: Module[];
  startScan: boolean;
  // Error fields
  noAnalyzersForJob: boolean;
  analyzersExist: boolean;
  private projectId: any;

  constructor(private router: Router, private userService: UserService,
              private projectService: ProjectService, private route: ActivatedRoute) {
    this.projectName = '';
    this.filePatternIncludeInput = '';
    this.filePatternExcludeInput = '';
    this.modulesInput = '';
    this.modules = [];
    this.deletedModules = [];
    this.analyzers = [];
    this.filePatterns = [];
    this.startScan = false;
    this.noAnalyzersForJob = false;
    this.analyzersExist = false;
  }

  ngOnInit(): void {
    this.analyzersExist = false;
    this.route.params.subscribe(params => {
      this.projectId = params.id;
      this.getAvailableAnalyzers();
      this.getModulesForProject();
      this.getProjectName();
      this.getProjectFilePatterns();
      this.getProjectAnalyzers();
    });
  }

  /**
   * Is called when the form is submitted.
   * Does input validation and calls the appropriate submit method for each part of the form.
   */
  submitForm(): void {
    this.noAnalyzersForJob = this.analyzers.filter(analyzer => analyzer.enabled).length === 0 && this.startScan === true;
    if (!this.noAnalyzersForJob) {
      this.noAnalyzersForJob = true;
      Promise.all([
        this.submitAnalyzerConfigurations(),
        this.submitFilePatterns(),
        this.submitModules()
      ]).then(() => {
        if (this.startScan) {
          this.projectService.startAnalyzingJob(this.projectId, true).catch(error => {
            if (error.status && error.status === FORBIDDEN) {
              this.userService.refresh().then(() => this.getModulesForProject());
            }
          });
        }
        this.router.navigate(['/dashboard']);
      }).catch(error => {
        if (error.status && error.status === FORBIDDEN) {
          this.userService.refresh().then(() => this.getModulesForProject());
        }
      });
    }
  }

  /**
   * Constructs a new FilePatterns object (INCLUDE) with whatever is in filePatternIncludeInput
   * and adds it to filePatterns.
   */
  addToIncludedPatterns(): void {
    if (this.filePatternIncludeInput.trim() !== '' &&
      this.filePatterns.filter(p => p.pattern === this.filePatternIncludeInput).length === 0) {
      const pattern = new FilePatterns();
      pattern.pattern = this.filePatternIncludeInput;
      pattern.fileSetType = 'SOURCE';
      pattern.inclusionType = 'INCLUDE';
      this.filePatterns.push(pattern);
      this.filePatternIncludeInput = '';
    }
  }

  /**
   * Constructs a new FilePatterns object (EXCLUDE) with whatever is in filePatternIncludeInput
   * and adds it to filePatterns.
   */
  addToExcludedPatterns(): void {
    if (this.filePatternExcludeInput.trim() !== '' &&
      this.filePatterns.filter(p => p.pattern === this.filePatternExcludeInput).length === 0) {
      const pattern = new FilePatterns();
      pattern.pattern = this.filePatternExcludeInput;
      pattern.fileSetType = 'SOURCE';<commit_msg>fixed configure project form<commit_after>import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {UserService} from '../../service/user.service';
import {ProjectService} from '../../service/project.service';
import {AnalyzerConfiguration} from '../../model/analyzer-configuration';
import {FilePatterns} from '../../model/file-patterns';
import {FORBIDDEN} from 'http-status-codes';
import {Module} from '../../model/module';

@Component({
  selector: 'app-configure-project',
  templateUrl: './configure-project.component.html',
  styleUrls: ['./configure-project.component.scss']
})
export class ConfigureProjectComponent implements OnInit {


  projectName: string;
  analyzers: AnalyzerConfiguration[];
  filePatterns: FilePatterns[];

  // Fields for input binding
  filePatternIncludeInput;
  filePatternExcludeInput;
  modulesInput;
  modules: Module[];
  deletedModules: Module[];
  startScan: boolean;
  // Error fields
  noAnalyzersForJob: boolean;
  analyzersExist: boolean;
  private projectId: any;

  constructor(private router: Router, private userService: UserService,
              private projectService: ProjectService, private route: ActivatedRoute) {
    this.projectName = '';
    this.filePatternIncludeInput = '';
    this.filePatternExcludeInput = '';
    this.modulesInput = '';
    this.modules = [];
    this.deletedModules = [];
    this.analyzers = [];
    this.filePatterns = [];
    this.startScan = false;
    this.noAnalyzersForJob = false;
    this.analyzersExist = false;
  }

  ngOnInit(): void {
    this.analyzersExist = false;
    this.route.params.subscribe(params => {
      this.projectId = params.id;
      this.getAvailableAnalyzers();
      this.getModulesForProject();
      this.getProjectName();
      this.getProjectFilePatterns();
      this.getProjectAnalyzers();
    });
  }

  /**
   * Is called when the form is submitted.
   * Does input validation and calls the appropriate submit method for each part of the form.
   */
  submitForm(): void {
    this.noAnalyzersForJob = this.analyzers.filter(analyzer => analyzer.enabled).length === 0 && this.startScan === true;
    if (!this.noAnalyzersForJob) {
      this.noAnalyzersForJob = true;
      Promise.all([
        this.submitAnalyzerConfigurations(),
        this.submitFilePatterns(),
        this.submitModules()
      ]).then(() => {
        if (this.startScan) {
          this.projectService.startAnalyzingJob(this.projectId, true).catch(error => {
            if (error.status && error.status === FORBIDDEN) {
              this.userService.refresh().then(() => this.projectService.startAnalyzingJob(this.projectId, true));
            }
          });
        }
        this.router.navigate(['/dashboard']);
      }).catch(error => {
        if (error.status && error.status === FORBIDDEN) {
          this.userService.refresh().then(() => this.submitForm());
        }
      });
    }
  }

  /**
   * Constructs a new FilePatterns object (INCLUDE) with whatever is in filePatternIncludeInput
   * and adds it to filePatterns.
   */
  addToIncludedPatterns(): void {
    if (this.filePatternIncludeInput.trim() !== '' &&
      this.filePatterns.filter(p => p.pattern === this.filePatternIncludeInput).length === 0) {
      const pattern = new FilePatterns();
      pattern.pattern = this.filePatternIncludeInput;
      pattern.fileSetType = 'SOURCE';
      pattern.inclusionType = 'INCLUDE';
      this.filePatterns.push(pattern);
      this.filePatternIncludeInput = '';
    }
  }

  /**
   * Constructs a new FilePatterns object (EXCLUDE) with whatever is in filePatternIncludeInput
   * and adds it to filePatterns.
   */
  addToExcludedPatterns(): void {
    if (this.filePatternExcludeInput.trim() !== '' &&
      this.filePatterns.filter(p => p.pattern === this.filePatternExcludeInput).length === 0) {
      const pattern = new FilePatterns();
      pattern.pattern = this.filePatternExcludeInput;
      pattern.fileSetType = 'SOURCE';
{'fraction_non_alphanumeric': 0.1360280315354774, 'fraction_numerical': 0.0007508447002878238, 'mean_word_length': 3.24429102496017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126416', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2295, 'n_words': 477}