<commit_before>/**
 * Created by suat on 17-May-17.
 */
import { Injectable } from "@angular/core";
import { Headers, Http } from "@angular/http";
import { catalogue_endpoint } from "../globals";
import { Catalogue } from "./model/publish/catalogue";
import { UserService } from "../user-mgmt/user.service";
import { CatalogueLine } from "./model/publish/catalogue-line";
import { Category } from "./model/category/category";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
import {CookieService} from "ng2-cookies";
import { copy } from "../common/utils";
import {BinaryObject} from './model/publish/binary-object';

@Injectable()
export class CatalogueService {
    private headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
    private baseUrl = catalogue_endpoint;

    catalogue: Catalogue;
    draftCatalogueLine: CatalogueLine;
    // To save a reference to the original version of the item being edited
    originalCatalogueLine: CatalogueLine;
    // edit mode switch (observable as it is provided by parent to its grandchild components)
    private editMode = new BehaviorSubject<boolean>(false);
    editModeObs = this.editMode.asObservable();

    constructor(private http: Http,
                private userService: UserService,
                private cookieService: CookieService) {
    }

    getCatalogueForceUpdate(userId: string, forceUpdate:boolean): Promise<Catalogue> {
        // if the default catalogue is already fetched, return it
        if (this.catalogue == null || forceUpdate == true) {

            // chain the promise for getting the user's party with the promise for getting the default catalogue
            // for the party
            return this.userService.getUserParty(userId).then(party => {

                // using the party query the default catalogue
                let url = this.baseUrl + `/catalogue/${party.id}/default`;
                return this.http
                    .get(url, {headers: this.getAuthorizedHeaders()})
                    .toPromise()
                    .then(res => {
                        if (res.status == 204) {
                            // no default catalogue yet, create new one
                            this.catalogue = new Catalogue("default", null, party, "", "", []);
                        } else {
                            this.catalogue = res.json() as Catalogue;
                        }
                        return this.catalogue;
                    })
                    .catch(this.handleError);
            });
        } else {
            return Promise.resolve(this.catalogue);
        }
    }

    getCatalogue(userId: string): Promise<Catalogue> {<commit_msg>checking 404 response code instead of 204 while getting default catalogue<commit_after>/**
 * Created by suat on 17-May-17.
 */
import { Injectable } from "@angular/core";
import { Headers, Http } from "@angular/http";
import { catalogue_endpoint } from "../globals";
import { Catalogue } from "./model/publish/catalogue";
import { UserService } from "../user-mgmt/user.service";
import { CatalogueLine } from "./model/publish/catalogue-line";
import { Category } from "./model/category/category";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
import {CookieService} from "ng2-cookies";
import { copy } from "../common/utils";
import {BinaryObject} from './model/publish/binary-object';

@Injectable()
export class CatalogueService {
    private headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
    private baseUrl = catalogue_endpoint;

    catalogue: Catalogue;
    draftCatalogueLine: CatalogueLine;
    // To save a reference to the original version of the item being edited
    originalCatalogueLine: CatalogueLine;
    // edit mode switch (observable as it is provided by parent to its grandchild components)
    private editMode = new BehaviorSubject<boolean>(false);
    editModeObs = this.editMode.asObservable();

    constructor(private http: Http,
                private userService: UserService,
                private cookieService: CookieService) {
    }

    getCatalogueForceUpdate(userId: string, forceUpdate:boolean): Promise<Catalogue> {
        // if the default catalogue is already fetched, return it
        if (this.catalogue == null || forceUpdate == true) {

            // chain the promise for getting the user's party with the promise for getting the default catalogue
            // for the party
            return this.userService.getUserParty(userId).then(party => {

                // using the party query the default catalogue
                let url = this.baseUrl + `/catalogue/${party.id}/default`;
                return this.http
                    .get(url, {headers: this.getAuthorizedHeaders()})
                    .toPromise()
                    .then(res => {
                        this.catalogue = res.json() as Catalogue;
                        return this.catalogue;
                    })
                    .catch(res => {
                        if (res.status == 404) {
                            // no default catalogue yet, create new one
                            this.catalogue = new Catalogue("default", null, party, "", "", []);
                            return this.catalogue;

                        } else {
                            this.handleError(res.getBody());
                        }
                    });
            });
        } else {
            return Promise.resolve(this.catalogue);
        }
    }

    getCatalogue(userId: string): Promise<Catalogue> {
{'fraction_non_alphanumeric': 0.10793538079176282, 'fraction_numerical': 0.0039055565418072077, 'mean_word_length': 2.1058434399117973, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, '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': '5202017', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1362, 'n_words': 413}