<commit_before>import {HttpClient, HttpParams} from "@angular/common/http";
import {Component, EventEmitter, Inject, Input, OnDestroy, OnInit, Output} from "@angular/core";
import {FormArray, FormControl, FormGroup,Validators} from "@angular/forms";
import {TdDialogService} from "@covalent/core/dialogs";
import {StateService} from "@uirouter/angular";
import {Subscription} from "rxjs/Subscription";
import * as _ from "underscore";

import {ProcessorRef} from "../../../../../../lib/feed/processor/processor-ref";
import {Step} from "../../../../model/feed/feed-step.model";
import {Feed} from "../../../../model/feed/feed.model";
import {FeedDetailsProcessorRenderingHelper} from "../../../../services/FeedDetailsProcessorRenderingHelper";
import {RegisterTemplatePropertyService} from "../../../../services/RegisterTemplatePropertyService";
import {RestUrlConstants} from "../../../../services/RestUrlConstants";
import {Templates} from "../../../../services/TemplateTypes";
import {DefineFeedService} from "../../services/define-feed.service";
import {FeedLoadingService} from "../../services/feed-loading-service";
import {FeedNifiPropertiesService} from "../../services/feed-nifi-properties.service";
import {FeedSideNavService} from "../../services/feed-side-nav.service";


export enum FeedDetailsMode {
    INPUT = "INPUT", ADDITIONAL = "ADDITIONAL", ALL = "ALL"
}

export class NiFiPropertiesProcessorsChangeEvent {
    constructor(public mode: FeedDetailsMode, public feed: Feed, public inputProcessors: ProcessorRef[], public nonInputProcessors: ProcessorRef[], public noPropertiesExist: boolean) {
    }
}


@Component({
    selector: "feed-nifi-properties",
    styleUrls: ["js/feed-mgr/feeds/define-feed-ng2/steps/feed-details/feed-nifi-properties.component.css"],
    templateUrl: "js/feed-mgr/feeds/define-feed-ng2/steps/feed-details/feed-nifi-properties.component.html"
})
export class FeedNifiPropertiesComponent implements OnInit, OnDestroy {

    @Input()
    step: Step;

    @Input()
    feed: Feed;

    @Input()
    mode?: FeedDetailsMode

    @Output()
    updatedFormControls = new EventEmitter<any>();

    @Output()
    processorsChange = new EventEmitter<NiFiPropertiesProcessorsChangeEvent>()

    @Input()
    formGroup: FormGroup;

    public noPropertiesExist: boolean = false;


    form = new FormArray([]);

    formSubscription: Subscription;

    inputProcessor: ProcessorRef;

    inputProcessorControl = new FormControl('',[Validators.required]);

    inputProcessorSubscription: Subscription;

    /**
     * processors modified for display
     */
    inputProcessors: ProcessorRef[];

    nonInputProcessors: ProcessorRef[];

    constructor(private defineFeedService: DefineFeedService, stateService: StateService, private feedNifiPropertiesService: FeedNifiPropertiesService, private http: HttpClient, feedLoadingService: FeedLoadingService, dialogService: TdDialogService,
                feedSideNavService: FeedSideNavService, @Inject("RegisterTemplatePropertyService") private registerTemplatePropertyService: RegisterTemplatePropertyService) {

        this.formSubscription = this.form.statusChanges.subscribe(status => {
            if (status === "VALID" && this.form.dirty) {
                this.step.markDirty();
            }
        });
        this.inputProcessorSubscription = this.inputProcessorControl.valueChanges.subscribe((value: ProcessorRef) => {
            this.inputProcessor = value;
            this.form.setControl(0, (value != null) ? value.form : new FormControl());
            this.updateProcessors();
        });
    }

    ngOnDestroy() {
        if (this.formSubscription) {
            this.formSubscription.unsubscribe();
        }
        if (this.inputProcessorSubscription) {
            this.inputProcessorSubscription.unsubscribe();
        }
    }

    ngOnInit() {
         if(this.formGroup) {
             this.formGroup.addControl("processors", this.form);
         }

         //touch the input control to show validation error
        this.inputProcessorControl.markAsTouched()

        if (this.mode == undefined) {
            this.mode = FeedDetailsMode.ALL;
        }

        this.inputProcessors = [];

        if (this.feed.isNew()) {
            this.initializeTemplateProperties();
        } else {
            this.mergeTemplateDataWithFeed(this.feed);
        }

    }

    hasProcessor(downstreamProcessors: any[], processorId: string, processorName: string): boolean {
        if (downstreamProcessors) {
            return downstreamProcessors.find(p => p.name == processorName) != undefined;
        }
        return false;
    }

    applyUpdatesToFeed(): void {

        let properties = [];
        let inputProperties: any = []; //Templates.Property[]
        let otherProperties: any = []; //Templates.Property[]

        if (this.mode == FeedDetailsMode.ALL || this.mode == FeedDetailsMode.INPUT) {
            inputProperties = this.inputProcessor.processor.properties;
            this.feed.inputProcessor = this.inputProcessor.processor as any;
            this.feed.inputProcessorName = this.inputProcessor.name;
            this.feed.inputProcessorType = this.inputProcessor.type;
        }
        else {
            inputProperties = this.feed.inputProcessor.properties
        }

        if (this.mode == FeedDetailsMode.ALL || this.mode == FeedDetailsMode.ADDITIONAL) {
            otherProperties = _.chain(this.nonInputProcessors)
                .map(ref => ref.processor.properties as any[])
                .flatten(true)
                .value();

        } else {<commit_msg>fix saving additional nifi properties if source not selected<commit_after>import {HttpClient, HttpParams} from "@angular/common/http";
import {Component, EventEmitter, Inject, Input, OnDestroy, OnInit, Output} from "@angular/core";
import {FormArray, FormControl, FormGroup,Validators} from "@angular/forms";
import {TdDialogService} from "@covalent/core/dialogs";
import {StateService} from "@uirouter/angular";
import {Subscription} from "rxjs/Subscription";
import * as _ from "underscore";

import {ProcessorRef} from "../../../../../../lib/feed/processor/processor-ref";
import {Step} from "../../../../model/feed/feed-step.model";
import {Feed} from "../../../../model/feed/feed.model";
import {FeedDetailsProcessorRenderingHelper} from "../../../../services/FeedDetailsProcessorRenderingHelper";
import {RegisterTemplatePropertyService} from "../../../../services/RegisterTemplatePropertyService";
import {RestUrlConstants} from "../../../../services/RestUrlConstants";
import {Templates} from "../../../../services/TemplateTypes";
import {DefineFeedService} from "../../services/define-feed.service";
import {FeedLoadingService} from "../../services/feed-loading-service";
import {FeedNifiPropertiesService} from "../../services/feed-nifi-properties.service";
import {FeedSideNavService} from "../../services/feed-side-nav.service";


export enum FeedDetailsMode {
    INPUT = "INPUT", ADDITIONAL = "ADDITIONAL", ALL = "ALL"
}

export class NiFiPropertiesProcessorsChangeEvent {
    constructor(public mode: FeedDetailsMode, public feed: Feed, public inputProcessors: ProcessorRef[], public nonInputProcessors: ProcessorRef[], public noPropertiesExist: boolean) {
    }
}


@Component({
    selector: "feed-nifi-properties",
    styleUrls: ["js/feed-mgr/feeds/define-feed-ng2/steps/feed-details/feed-nifi-properties.component.css"],
    templateUrl: "js/feed-mgr/feeds/define-feed-ng2/steps/feed-details/feed-nifi-properties.component.html"
})
export class FeedNifiPropertiesComponent implements OnInit, OnDestroy {

    @Input()
    step: Step;

    @Input()
    feed: Feed;

    @Input()
    mode?: FeedDetailsMode

    @Output()
    updatedFormControls = new EventEmitter<any>();

    @Output()
    processorsChange = new EventEmitter<NiFiPropertiesProcessorsChangeEvent>()

    @Input()
    formGroup: FormGroup;

    public noPropertiesExist: boolean = false;


    form = new FormArray([]);

    formSubscription: Subscription;

    inputProcessor: ProcessorRef;

    inputProcessorControl = new FormControl('',[Validators.required]);

    inputProcessorSubscription: Subscription;

    /**
     * processors modified for display
     */
    inputProcessors: ProcessorRef[];

    nonInputProcessors: ProcessorRef[];

    constructor(private defineFeedService: DefineFeedService, stateService: StateService, private feedNifiPropertiesService: FeedNifiPropertiesService, private http: HttpClient, feedLoadingService: FeedLoadingService, dialogService: TdDialogService,
                feedSideNavService: FeedSideNavService, @Inject("RegisterTemplatePropertyService") private registerTemplatePropertyService: RegisterTemplatePropertyService) {

        this.formSubscription = this.form.statusChanges.subscribe(status => {
            if (status === "VALID" && this.form.dirty) {
                this.step.markDirty();
            }
        });
        this.inputProcessorSubscription = this.inputProcessorControl.valueChanges.subscribe((value: ProcessorRef) => {
            this.inputProcessor = value;
            this.form.setControl(0, (value != null) ? value.form : new FormControl());
            this.updateProcessors();
        });
    }

    ngOnDestroy() {
        if (this.formSubscription) {
            this.formSubscription.unsubscribe();
        }
        if (this.inputProcessorSubscription) {
            this.inputProcessorSubscription.unsubscribe();
        }
    }

    ngOnInit() {
         if(this.formGroup) {
             this.formGroup.addControl("processors", this.form);
         }

         //touch the input control to show validation error
        this.inputProcessorControl.markAsTouched()

        if (this.mode == undefined) {
            this.mode = FeedDetailsMode.ALL;
        }

        this.inputProcessors = [];

        if (this.feed.isNew()) {
            this.initializeTemplateProperties();
        } else {
            this.mergeTemplateDataWithFeed(this.feed);
        }

    }

    hasProcessor(downstreamProcessors: any[], processorId: string, processorName: string): boolean {
        if (downstreamProcessors) {
            return downstreamProcessors.find(p => p.name == processorName) != undefined;
        }
        return false;
    }

    applyUpdatesToFeed(): void {

        let properties = [];
        let inputProperties: any = []; //Templates.Property[]
        let otherProperties: any = []; //Templates.Property[]

        if (this.mode == FeedDetailsMode.ALL || this.mode == FeedDetailsMode.INPUT) {
            inputProperties = this.inputProcessor.processor.properties;
            this.feed.inputProcessor = this.inputProcessor.processor as any;
            this.feed.inputProcessorName = this.inputProcessor.name;
            this.feed.inputProcessorType = this.inputProcessor.type;
        }
        else {
            if(this.feed.inputProcessor == undefined && this.feed.registeredTemplate) {
                //attempt to get the first one
                //get the first input processor and select it
                let inputProcessors = this.feed.inputProcessors && this.feed.inputProcessors.length >0 ? this.feed.inputProcessors : this.feed.registeredTemplate && this.feed.registeredTemplate.inputProcessors && this.feed.registeredTemplate.inputProcessors.length >0 ? this.feed.registeredTemplate.inputProcessors : []
                if(inputProcessors.length >0) {
                    let input: Templates.Processor = inputProcessors[0];
                    this.feed.inputProcessor = input;
                    this.feed.inputProcessorName = input.name;
                    this.feed.inputProcessorType = input.type;
                }
            }
            inputProperties = this.feed.inputProcessor.properties
        }

        if (this.mode == FeedDetailsMode.ALL || this.mode == FeedDetailsMode.ADDITIONAL) {
            otherProperties = _.chain(this.nonInputProcessors)
                .map(ref => ref.processor.properties as any[])
                .flatten(true)
                .value();

        } else {
{'fraction_non_alphanumeric': 0.1259595542715642, 'fraction_numerical': 0.0008254230293025176, 'mean_word_length': 2.9659574468085106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '837047', 'n_tokens_mistral': 3194, 'n_tokens_neox': 3128, 'n_words': 640}