<commit_before>import { Diff, DiffTracker } from "../helpers/diffTracker";
import { PersistablePage } from '../pages/persistablePage';
import { Router } from '@angular/router';
import { remote, shell } from 'electron';
import { ToastsManager } from 'ng2-toastr';
import { Subscription } from 'rxjs';

import DataApiService from '../stores/dataApiService';
import * as path from 'path';

const APP = remote.app;
const DATA_DIR = APP.getPath('userData');
const CONTENT_DIR = path.join(DATA_DIR, 'contents');

var $ = require('jquery');

export default class PageSaver {
    mergeContent: any;
    diffTracker: DiffTracker;
    trackDiffsMethod: any;
    bundleSchemas = {};
    bundleData = {};
    afterSaveAction: string;
    currentDiffs: any;
    selectedDiff: string;
    subscription: Subscription;

    constructor(private page: PersistablePage) {
        this.diffTracker = new DiffTracker();
    }

    getContent(callback: any): void {
        let me = this;
        let localBundle = this.page.dataApiService.getLocalContent(this.page.type, this.bundleSchemas);
        let changeId = localBundle.changeId ? localBundle.changeId : 0;
        let mergedResult = null;

        console.log("getContent local bundle is: ")
        console.dir(localBundle);
        this.subscription = this.page.dataApiService.getContent(this.page.type, this.page.subType, changeId, this.page.progressListener.bind(this.page))
            .subscribe(
            result => {
                console.log("getContent succeed with result:");
                console.dir(result);

                if (result['change_id'] === localBundle.changeId)
                    mergedResult = this.page.mergeContent(localBundle, localBundle, localBundle['columns']);
                else
                    mergedResult = this.page.mergeContent(result, localBundle, result["columns"]);

                console.log("content merged:");
                console.dir(mergedResult);

                let modifications = this.getNumOfModifications(mergedResult);
                let hasAnyDiffs = modifications > 0;
                if(hasAnyDiffs){
                    this.page.toastr.info("Terdapat "+modifications+" perubahan pada data");
                    this.saveContent(false, 
                        result => {
                            console.log("saveContent succeed");
                            let jsonFile = path.join(CONTENT_DIR, this.page.type + '.json');
                            this.page.dataApiService.writeFile(result, jsonFile, null);
                            callback(result);
                        },
                        error => {
                            /* If save failed, act like get content failed, return the local bundle */
                            mergedResult = this.page.mergeContent(localBundle, localBundle);
                            callback(mergedResult);
                        }
                    );
                } else {
                    console.log("doesn't have any diff, don't need to saveContent");
                    let jsonFile = path.join(CONTENT_DIR, this.page.type + '.json');
                    this.page.dataApiService.writeFile(result, jsonFile, null);
                    callback(mergedResult);
                }
            },
            error => {
                console.error("getContent failed with error", error);

                let errors = error.split('-');
                let errorMesssage = '';
                if (errors[0].trim() === '0')
                    errorMesssage = 'Anda tidak terhubung internet';
                else
                    errorMesssage = 'Terjadi kesalahan pada server';
                this.page.toastr.error(errorMesssage);

                mergedResult = this.page.mergeContent(localBundle, localBundle);
                callback(mergedResult);
            }
        )
    }

    saveContent(isTrackingDiff: boolean, onSuccess: any, onError?: any): void {
        let localBundle = this.page.dataApiService.getLocalContent(this.page.type, this.bundleSchemas);

        if (isTrackingDiff) {
            let diffs = this.page.trackDiffs(localBundle["data"], this.bundleData);
            let keys = Object.keys(diffs);

            keys.forEach(key => {
                localBundle['diffs'][key] = localBundle['diffs'][key] ? localBundle['diffs'][key] : [];

                if (diffs[key].total > 0)<commit_msg>save content with merged result<commit_after>import { Diff, DiffTracker } from "../helpers/diffTracker";
import { PersistablePage } from '../pages/persistablePage';
import { Router } from '@angular/router';
import { remote, shell } from 'electron';
import { ToastsManager } from 'ng2-toastr';
import { Subscription } from 'rxjs';

import DataApiService from '../stores/dataApiService';
import * as path from 'path';

const APP = remote.app;
const DATA_DIR = APP.getPath('userData');
const CONTENT_DIR = path.join(DATA_DIR, 'contents');

var $ = require('jquery');

export default class PageSaver {
    mergeContent: any;
    diffTracker: DiffTracker;
    trackDiffsMethod: any;
    bundleSchemas = {};
    bundleData = {};
    afterSaveAction: string;
    currentDiffs: any;
    selectedDiff: string;
    subscription: Subscription;

    constructor(private page: PersistablePage) {
        this.diffTracker = new DiffTracker();
    }

    getContent(callback: any): void {
        let me = this;
        let localBundle = this.page.dataApiService.getLocalContent(this.page.type, this.bundleSchemas);
        let changeId = localBundle.changeId ? localBundle.changeId : 0;
        let mergedResult = null;

        console.log("getContent local bundle is: ")
        console.dir(localBundle);
        this.subscription = this.page.dataApiService.getContent(this.page.type, this.page.subType, changeId, this.page.progressListener.bind(this.page))
            .subscribe(
            result => {
                console.log("getContent succeed with result:");
                console.dir(result);

                if (result['change_id'] === localBundle.changeId)
                    mergedResult = this.page.mergeContent(localBundle, localBundle, localBundle['columns']);
                else
                    mergedResult = this.page.mergeContent(result, localBundle, result["columns"]);

                console.log("content merged:");
                console.dir(mergedResult);

                let modifications = this.getNumOfModifications(mergedResult);
                let hasAnyDiffs = modifications > 0;
                if(hasAnyDiffs){
                    this.page.toastr.info("Terdapat "+modifications+" perubahan pada data");
                    this.saveContent(false, 
                        result => {
                            console.log("saveContent succeed");
                            let jsonFile = path.join(CONTENT_DIR, this.page.type + '.json');
                            this.page.dataApiService.writeFile(result, jsonFile, null);
                            callback(result);
                        },
                        error => {
                            /* If save failed, act like get content failed, return the local bundle */
                            mergedResult = this.page.mergeContent(localBundle, localBundle);
                            callback(mergedResult);
                        }
                    );
                } else {
                    console.log("doesn't have any diff, don't need to saveContent");
                    let jsonFile = path.join(CONTENT_DIR, this.page.type + '.json');
                    this.page.dataApiService.writeFile(mergedResult, jsonFile, null);
                    callback(mergedResult);
                }
            },
            error => {
                console.error("getContent failed with error", error);

                let errors = error.split('-');
                let errorMesssage = '';
                if (errors[0].trim() === '0')
                    errorMesssage = 'Anda tidak terhubung internet';
                else
                    errorMesssage = 'Terjadi kesalahan pada server';
                this.page.toastr.error(errorMesssage);

                mergedResult = this.page.mergeContent(localBundle, localBundle);
                callback(mergedResult);
            }
        )
    }

    saveContent(isTrackingDiff: boolean, onSuccess: any, onError?: any): void {
        let localBundle = this.page.dataApiService.getLocalContent(this.page.type, this.bundleSchemas);

        if (isTrackingDiff) {
            let diffs = this.page.trackDiffs(localBundle["data"], this.bundleData);
            let keys = Object.keys(diffs);

            keys.forEach(key => {
                localBundle['diffs'][key] = localBundle['diffs'][key] ? localBundle['diffs'][key] : [];

                if (diffs[key].total > 0)
{'fraction_non_alphanumeric': 0.1100338218714769, 'fraction_numerical': 0.0013528748590755355, 'mean_word_length': 1.901864573110893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3075116', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2220, 'n_words': 485}