<commit_before>/**
 * Created by suat on 17-May-17.
 */
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Category} from "../model/category/category";
import * as myGlobals from '../../globals';
import {Code} from "../model/publish/code";

@Injectable()
export class CategoryService {
    private headers = new Headers({'Accept': 'application/json'});
    private baseUrl = myGlobals.catalogue_endpoint + `/catalogue/category`;

    selectedCategories: Category[] = [];

    constructor(private http: Http) {
    }

    getCategoriesByName(keywords: string): Promise<Category[]> {
        const url = `${this.baseUrl}?categoryNames=${keywords}`;
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category[];
            })
            .catch(this.handleError);
    }

    getCategoriesByIds(codes: Code[]): Promise<Category[]> {
        if(!codes) {
            return Promise.resolve([]);
        }

        let customCategoryCodes: Code[] = [];
        let customCategories: Category[] = [];
        let categories:Category[];

        // remove default category
        codes = codes.filter(function (cat) {
            return cat.listID != 'Default';
        });

        customCategoryCodes = codes.filter(function (cat) {
           return cat.listID == 'Custom';
        });
        // get custom categories
        for(let cat of customCategoryCodes){
            customCategories.push(new Category(null,cat.name,null,null,null,null,null,[],[],'Custom',null));
        }

        categories = customCategories;

        // get non-custom categories
        codes = codes.filter(function (cat) {
           return cat.listID != 'Custom';
        });

        if(codes.length > 0){
            let url = this.baseUrl;
            let categoryIds:string = '';
            let taxonomyIds:string = '';

            let i = 0;
            for (; i<codes.length-1; i++) {
                categoryIds += encodeURIComponent(codes[i].value) + ",";
                taxonomyIds += codes[i].listID + ",";
            }
            categoryIds += encodeURIComponent(codes[i].value);
            taxonomyIds += codes[i].listID;

            url += "?taxonomyIds=" + taxonomyIds + "&categoryIds=" + categoryIds;

            return this.http
                .get(url, {headers: this.headers})
                .toPromise()
                .then(res => {
                    categories = categories.concat(res.json() as Category[]);
                    return categories;
                })
                .catch(this.handleError);
        }
        else{
            return Promise.resolve(categories);
        }
    }

    getCategory(category: Category): Promise<Category> {
        const url = `${this.baseUrl}?taxonomyIds=` + category.taxonomyId + `&categoryIds=` + encodeURIComponent(category.id);
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json()[0] as Category;
            })
            .catch(this.handleError);
    }

    getCategoryByCode(code: Code): Promise<Category> {
        const url = `${this.baseUrl}/` + code.listID + "/" + encodeURIComponent(code.value);
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category;
            })
            .catch(this.handleError);
    }

    getParentCategories(category: Category): Promise<Category>{
        const url = `${this.baseUrl}/` + category.taxonomyId + "/" + encodeURIComponent(category.id)+"/tree";
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category;
            })
            .catch(this.handleError);
    }

    addSelectedCategory(category: Category): void {
        // custom categories
        if(category != null && category.taxonomyId == 'Custom'){
            // Only add if category is not null and doesn't exist in selected categories
            if (this.selectedCategories.findIndex(c => c.preferredName == category.preferredName) == -1) {
                this.selectedCategories.push(category);
            }
        }
        else{
            // Only add if category is not null and doesn't exist in selected categories<commit_msg>changed category retrieval query according to the API change<commit_after>/**
 * Created by suat on 17-May-17.
 */
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Category} from "../model/category/category";
import * as myGlobals from '../../globals';
import {Code} from "../model/publish/code";

@Injectable()
export class CategoryService {
    private headers = new Headers({'Accept': 'application/json'});
    private baseUrl = myGlobals.catalogue_endpoint + `/catalogue/category`;

    selectedCategories: Category[] = [];

    constructor(private http: Http) {
    }

    getCategoriesByName(keywords: string): Promise<Category[]> {
        const url = `${this.baseUrl}?categoryNames=${keywords}`;
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category[];
            })
            .catch(this.handleError);
    }

    getCategoriesByIds(codes: Code[]): Promise<Category[]> {
        if(!codes) {
            return Promise.resolve([]);
        }

        let customCategoryCodes: Code[] = [];
        let customCategories: Category[] = [];
        let categories:Category[];

        // remove default category
        codes = codes.filter(function (cat) {
            return cat.listID != 'Default';
        });

        customCategoryCodes = codes.filter(function (cat) {
           return cat.listID == 'Custom';
        });
        // get custom categories
        for(let cat of customCategoryCodes){
            customCategories.push(new Category(null,cat.name,null,null,null,null,null,[],[],'Custom',null));
        }

        categories = customCategories;

        // get non-custom categories
        codes = codes.filter(function (cat) {
           return cat.listID != 'Custom';
        });

        if(codes.length > 0){
            let url = this.baseUrl;
            let categoryIds:string = '';
            let taxonomyIds:string = '';

            let i = 0;
            for (; i<codes.length-1; i++) {
                categoryIds += encodeURIComponent(codes[i].value) + ",";
                taxonomyIds += codes[i].listID + ",";
            }
            categoryIds += encodeURIComponent(codes[i].value);
            taxonomyIds += codes[i].listID;

            url += "?taxonomyIds=" + taxonomyIds + "&categoryIds=" + categoryIds;

            return this.http
                .get(url, {headers: this.headers})
                .toPromise()
                .then(res => {
                    categories = categories.concat(res.json() as Category[]);
                    return categories;
                })
                .catch(this.handleError);
        }
        else{
            return Promise.resolve(categories);
        }
    }

    getCategory(category: Category): Promise<Category> {
        const url = `${this.baseUrl}?taxonomyIds=` + category.taxonomyId + `&categoryIds=` + encodeURIComponent(category.id);
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json()[0] as Category;
            })
            .catch(this.handleError);
    }

    getCategoryByCode(code: Code): Promise<Category> {
        const url = `${this.baseUrl}/` + code.listID + "/" + encodeURIComponent(code.value);
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category;
            })
            .catch(this.handleError);
    }

    getParentCategories(category: Category): Promise<Category>{
        const url = `${this.baseUrl}/tree?taxonomyId=${category.taxonomyId}&categoryId${encodeURIComponent(category.id)}`;
        return this.http
            .get(url, {headers: this.headers})
            .toPromise()
            .then(res => {
                return res.json() as Category;
            })
            .catch(this.handleError);
    }

    addSelectedCategory(category: Category): void {
        // custom categories
        if(category != null && category.taxonomyId == 'Custom'){
            // Only add if category is not null and doesn't exist in selected categories
            if (this.selectedCategories.findIndex(c => c.preferredName == category.preferredName) == -1) {
                this.selectedCategories.push(category);
            }
        }
        else{
            // Only add if category is not null and doesn't exist in selected categories
{'fraction_non_alphanumeric': 0.13503086419753085, 'fraction_numerical': 0.001984126984126984, 'mean_word_length': 1.9239445697711892, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4815350', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2385, 'n_words': 527}