<commit_before>import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES, ActivatedRoute, Router, Params} from '@angular/router';
import {REACTIVE_FORM_DIRECTIVES, FormBuilder, FormGroup, FORM_DIRECTIVES} from '@angular/forms';

import {AbstractPageComponent} from '../abstract-page/abstract-page.component';
import {PageHeaderAuthComponent} from '../../components/page-header/page-header-auth.component';
import {SearchResultPaginationComponent, SearchResultPaginationDelegate}
    from '../../components/search-result-pagination/search-result-pagination.component';
import {RAMServices} from '../../services/ram-services';
import {RAMConstants} from '../../services/ram-constants.service';

import {
    IHrefValue,
    ISearchResult,
    IAgencyUser,
    IIdentity,
    IRole,
    IRoleStatus,
    IRoleType
} from '../../../../commons/RamAPI';

@Component({
    selector: 'ram-roles',
    templateUrl: 'roles.component.html',
    directives: [
        REACTIVE_FORM_DIRECTIVES,
        FORM_DIRECTIVES,
        ROUTER_DIRECTIVES,
        PageHeaderAuthComponent,
        SearchResultPaginationComponent
    ]
})

export class RolesComponent extends AbstractPageComponent {

    public identityHref: string;
    public page: number;

    public roleSearchResult: ISearchResult<IHrefValue<IRole>>;

    public giveAuthorisationsEnabled: boolean = true; // todo need to set this
    public agencyUser: IAgencyUser;
    public identity: IIdentity;
    public roleTypeRefs: IHrefValue<IRoleType>[];
    public roleStatusRefs: IHrefValue<IRoleStatus>[];

    public paginationDelegate: SearchResultPaginationDelegate;
    public form: FormGroup;

    private _isLoading = false; // set to true when you want the UI indicate something is getting loaded.

    constructor(route: ActivatedRoute, router: Router, fb: FormBuilder, services: RAMServices) {
        super(route, router, fb, services);
        this.setBannerTitle('Authorisations');
    }

    public onInit(params: {path:Params, query:Params}) {

        // extract path and query parameters
        this.identityHref = params.path['identityHref'];
        this.page = params.query['page'] ? +params.query['page'] : 1;

        // agency user
        this.services.rest.findMyPrincipal().subscribe((me) => {
            this.agencyUser = me.agencyUser;
        });

        // identity in focus
        this.services.rest.findIdentityByHref(this.identityHref).subscribe((identity) => {

            this.identity = identity;

            // roles
            const rolesHref = this.services.model.getLinkHrefByType('role-list', this.identity);
            this.services.rest.searchRolesByHref(rolesHref, null, this.page)
                .subscribe((searchResult) => {
                    this.roleSearchResult = searchResult;
                    this._isLoading = false;
                }, (err) => {
                    this.addGlobalErrorMessages(err);
                    this._isLoading = false;
                });

        });

        // role types
        this.services.rest.listRoleTypes().subscribe((roleTypeRefs) => {
            this.roleTypeRefs = roleTypeRefs;
        });

        // role statuses
        this.services.rest.listRoleStatuses().subscribe((roleStatusRefs) => {
            this.roleStatusRefs = roleStatusRefs;
        });

        // pagination delegate
        this.paginationDelegate = {
            goToPage: (page: number) => {
                this.services.route.goToRolesPage(this.identityHref, page);
            }
        } as SearchResultPaginationDelegate;

        // forms
        this.form = this.fb.group({
        });

    }

    public get isLoading() {
        return this._isLoading;
    }

    public goToAddRolePage() {
        if (this.agencyUser && this.identity) {
            this.services.route.goToAddRolePage(this.services.model.getLinkHrefByType('self', this.identity));
        }
    }

    public goToRolePage(roleRef: IHrefValue<IRole>) {
        this.services.route.goToEditRolePage(
            this.identityHref,
            this.services.model.getLinkHrefByType('self', roleRef.value)
        );
    }<commit_msg>RAM-4 Refactored to use constants<commit_after>import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES, ActivatedRoute, Router, Params} from '@angular/router';
import {REACTIVE_FORM_DIRECTIVES, FormBuilder, FormGroup, FORM_DIRECTIVES} from '@angular/forms';

import {AbstractPageComponent} from '../abstract-page/abstract-page.component';
import {PageHeaderAuthComponent} from '../../components/page-header/page-header-auth.component';
import {SearchResultPaginationComponent, SearchResultPaginationDelegate}
    from '../../components/search-result-pagination/search-result-pagination.component';
import {RAMServices} from '../../services/ram-services';
import {RAMConstants} from '../../services/ram-constants.service';

import {
    IHrefValue,
    ISearchResult,
    IAgencyUser,
    IIdentity,
    IRole,
    IRoleStatus,
    IRoleType
} from '../../../../commons/RamAPI';

@Component({
    selector: 'ram-roles',
    templateUrl: 'roles.component.html',
    directives: [
        REACTIVE_FORM_DIRECTIVES,
        FORM_DIRECTIVES,
        ROUTER_DIRECTIVES,
        PageHeaderAuthComponent,
        SearchResultPaginationComponent
    ]
})

export class RolesComponent extends AbstractPageComponent {

    public identityHref: string;
    public page: number;

    public roleSearchResult: ISearchResult<IHrefValue<IRole>>;

    public giveAuthorisationsEnabled: boolean = true; // todo need to set this
    public agencyUser: IAgencyUser;
    public identity: IIdentity;
    public roleTypeRefs: IHrefValue<IRoleType>[];
    public roleStatusRefs: IHrefValue<IRoleStatus>[];

    public paginationDelegate: SearchResultPaginationDelegate;
    public form: FormGroup;

    private _isLoading = false; // set to true when you want the UI indicate something is getting loaded.

    constructor(route: ActivatedRoute, router: Router, fb: FormBuilder, services: RAMServices) {
        super(route, router, fb, services);
        this.setBannerTitle('Authorisations');
    }

    public onInit(params: {path:Params, query:Params}) {

        // extract path and query parameters
        this.identityHref = params.path['identityHref'];
        this.page = params.query['page'] ? +params.query['page'] : 1;

        // agency user
        this.services.rest.findMyPrincipal().subscribe((me) => {
            this.agencyUser = me.agencyUser;
        });

        // identity in focus
        this.services.rest.findIdentityByHref(this.identityHref).subscribe((identity) => {

            this.identity = identity;

            // roles
            const rolesHref = this.services.model.getLinkHrefByType(RAMConstants.Link.ROLE_LIST, this.identity);
            this.services.rest.searchRolesByHref(rolesHref, null, this.page)
                .subscribe((searchResult) => {
                    this.roleSearchResult = searchResult;
                    this._isLoading = false;
                }, (err) => {
                    this.addGlobalErrorMessages(err);
                    this._isLoading = false;
                });

        });

        // role types
        this.services.rest.listRoleTypes().subscribe((roleTypeRefs) => {
            this.roleTypeRefs = roleTypeRefs;
        });

        // role statuses
        this.services.rest.listRoleStatuses().subscribe((roleStatusRefs) => {
            this.roleStatusRefs = roleStatusRefs;
        });

        // pagination delegate
        this.paginationDelegate = {
            goToPage: (page: number) => {
                this.services.route.goToRolesPage(this.identityHref, page);
            }
        } as SearchResultPaginationDelegate;

        // forms
        this.form = this.fb.group({
        });

    }

    public get isLoading() {
        return this._isLoading;
    }

    public goToAddRolePage() {
        if (this.agencyUser && this.identity) {
            this.services.route.goToAddRolePage(this.services.model.getLinkHrefByType(RAMConstants.Link.SELF, this.identity));
        }
    }

    public goToRolePage(roleRef: IHrefValue<IRole>) {
        this.services.route.goToEditRolePage(
            this.identityHref,
            this.services.model.getLinkHrefByType('self', roleRef.value)
        );
    }
{'fraction_non_alphanumeric': 0.11433365292425696, 'fraction_numerical': 0.0003595397890699904, 'mean_word_length': 2.614118666089216, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '3864934', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2307, 'n_words': 434}