<commit_before>/// <reference path="../../submodules/DefinitelyTyped/requirejs/require.d.ts"/>
/// <reference path="../../submodules/DefinitelyTyped/angularjs/angular.d.ts"/>
/// <reference path="../../submodules/DefinitelyTyped/underscore/underscore.d.ts"/>
/// <reference path="../_all.d.ts"/>

import angular = require("angular");
import _ = require("underscore");

import Types = require("Adhocracy/Types");
import Util = require("Adhocracy/Util");


export interface HasIDocumentSheet {
    "adhocracy.sheets.document.IDocument": IDocumentSheet;
}

export interface HasIVersionsSheet {
    "adhocracy.sheets.versions.IVersions": IVersionsSheet;
}

export interface IDocumentSheet {
    title: string;
    description: string;
    elements: string[];
}

export interface IVersionsSheet {
    elements: string[];
}

export interface MeineHeine extends Resource, HasIDocumentSheet {}

export class Resource {
    content_type : string;
    data : Object;
    constructor(content_type: string) {
        this.content_type = content_type;
        this.data = {};
    }
    addISection(title: string, elements: string[]) {
        this.data["adhocracy.sheets.document.ISection"] = {
            title: title,
            elements: elements,
        };
        return this;
    }
    addIDocument(title: string, description: string, elements: string[]) {
        this.data["adhocracy.sheets.document.IDocument"] = {
            title: title,
            description: description,
            elements: elements,
        };
        return this;
    }
    addIVersionable(follows: string[], root_version: string[]) {
        this.data["adhocracy.sheets.versions.IVersionable"] = {
            follows: follows,
            root_version: root_version,
        };
        return this;
    }
    addIName(name: string) {
        this.data["adhocracy.sheets.name.IName"] = {
            name: name,
        };
        return this;
    }
    addIParagraph(content: string) {
        this.data["adhocracy.sheets.document.IParagraph"] = {
            content: content,
        };
        return this;
    }
}

export class Proposal extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.proposal.IProposal");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class Paragraph extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.paragraph.IParagraph");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class Section extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.section.ISection");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class SectionVersion extends Resource {
    constructor(title: string, elements: string[], follows: string[], root_version: string[]) {
        super("adhocracy.resources.section.ISectionVersion");
        this.addISection(title, elements)
            .addIVersionable(follows, root_version);
    }
}

export function addParagraph(proposalVersion: MeineHeine, paragraphPath: string) {
    return proposalVersion.data["adhocracy.sheets.document.IDocument"].elements.push(paragraphPath);
};

// takes an array of URL's to resource versions
//FIXME: backend should have LAST
export function newestVersion(versions: string[]) : string {
    return _.max(versions, (version_path) => parseInt(version_path.match(/\d*$/)[0], 10));
};

export function postProposal($http
                            ,$q: ng.IQService
                            ,proposalVersion: MeineHeine
                            ,paragraphVersions) {
    var proposalName = proposalVersion.data["adhocracy.sheets.document.IDocument"].title;

    return $http.post("/adhocracy", new Proposal(Util.normalizeName(proposalName))).then( (resp) => {
        var proposalPath = decodeURIComponent(resp.data.path);
        var proposalFirstVersionPath = decodeURIComponent(resp.data.first_version_path);

        var sectionPromiseStupid = $http.post(proposalPath, new Section());

        var paragraphPromises = paragraphVersions.map( (paragraph) =>
            $http.post(proposalPath, new Paragraph()).then( (resp) => {
                var paragraphPath = decodeURIComponent(resp.data.path);
                var paragraphFirstVersionPath = decodeURIComponent(resp.data.first_version_path);

                return $http.post(paragraphPath, paragraph.addIVersionable([paragraphFirstVersionPath], [proposalPath]));
            })
        );

        var sectionVersionPromise = sectionPromiseStupid.then( respSection => {
            return $q.all(paragraphPromises).then( (respParagraphs) => {
                var paths = respParagraphs.map( (resp) => decodeURIComponent(resp.data.path) );

                var sectionVersion = new SectionVersion(undefined, paths, [], [decodeURIComponent(respSection.data.first_version_path)]);

                return $http.post(decodeURIComponent(respSection.data.path), sectionVersion);
            })
        });

        return $q.all(paragraphPromises).then( (respParagraphs) => {
            return sectionVersionPromise.then( (respSectionVersion) => {
                addParagraph(proposalVersion, decodeURIComponent(respSectionVersion.data.path));

                proposalVersion.addIVersionable([], [proposalFirstVersionPath]);

                return $http.post(proposalPath, proposalVersion);
            })
        });
    });
};<commit_msg>add resource helper functions<commit_after>/// <reference path="../../submodules/DefinitelyTyped/requirejs/require.d.ts"/>
/// <reference path="../../submodules/DefinitelyTyped/angularjs/angular.d.ts"/>
/// <reference path="../../submodules/DefinitelyTyped/underscore/underscore.d.ts"/>
/// <reference path="../_all.d.ts"/>

import angular = require("angular");
import _ = require("underscore");

import Types = require("Adhocracy/Types");
import Util = require("Adhocracy/Util");


export interface HasIDocumentSheet {
    "adhocracy.sheets.document.IDocument": IDocumentSheet;
}

export interface HasIVersionsSheet {
    "adhocracy.sheets.versions.IVersions": IVersionsSheet;
}

export interface IDocumentSheet {
    title: string;
    description: string;
    elements: string[];
}

export interface IVersionsSheet {
    elements: string[];
}

export interface MeineHeine extends Resource, HasIDocumentSheet {}

export class Resource {
    content_type : string;
    data : Object;
    constructor(content_type: string) {
        this.content_type = content_type;
        this.data = {};
    }
    addISection(title: string, elements: string[]) {
        this.data["adhocracy.sheets.document.ISection"] = {
            title: title,
            elements: elements,
        };
        return this;
    }
    addIDocument(title: string, description: string, elements: string[]) {
        this.data["adhocracy.sheets.document.IDocument"] = {
            title: title,
            description: description,
            elements: elements,
        };
        return this;
    }
    addIVersionable(follows: string[], root_version: string[]) {
        this.data["adhocracy.sheets.versions.IVersionable"] = {
            follows: follows,
            root_version: root_version,
        };
        return this;
    }
    addIName(name: string) {
        this.data["adhocracy.sheets.name.IName"] = {
            name: name,
        };
        return this;
    }
    addIParagraph(content: string) {
        this.data["adhocracy.sheets.document.IParagraph"] = {
            content: content,
        };
        return this;
    }
}

export class Proposal extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.proposal.IProposal");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class Paragraph extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.paragraph.IParagraph");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class Section extends Resource {
    constructor(name?: string) {
        super("adhocracy.resources.section.ISection");
        if (name !== undefined) {
            this.addIName(name);
        }
    }
}

export class SectionVersion extends Resource {
    constructor(title: string, elements: string[], follows: string[], root_version: string[]) {
        super("adhocracy.resources.section.ISectionVersion");
        this.addISection(title, elements)
            .addIVersionable(follows, root_version);
    }
}

export function addParagraph(proposalVersion: MeineHeine, paragraphPath: string) {
    return proposalVersion.data["adhocracy.sheets.document.IDocument"].elements.push(paragraphPath);
};

// takes an array of URL's to resource versions
//FIXME: backend should have LAST
export function newestVersion(versions: string[]) : string {
    return _.max(versions, (version_path) => parseInt(version_path.match(/\d*$/)[0], 10));
};

export function newestVersionInContainer(container) {
    return newestVersion(container.data.data["adhocracy.sheets.versions.IVersions"].elements);
};

export function getNewestVersion($http, path: string) : ng.IPromise<any> {
    return $http.get(path).then( (container) =>
        $http.get(decodeURIComponent(newestVersionInContainer(container)))
    );
};

export function postProposal($http
                            ,$q: ng.IQService
                            ,proposalVersion: MeineHeine
                            ,paragraphVersions) {
    var proposalName = proposalVersion.data["adhocracy.sheets.document.IDocument"].title;

    return $http.post("/adhocracy", new Proposal(Util.normalizeName(proposalName))).then( (resp) => {
        var proposalPath = decodeURIComponent(resp.data.path);
        var proposalFirstVersionPath = decodeURIComponent(resp.data.first_version_path);

        var sectionPromiseStupid = $http.post(proposalPath, new Section());

        var paragraphPromises = paragraphVersions.map( (paragraph) =>
            $http.post(proposalPath, new Paragraph()).then( (resp) => {
                var paragraphPath = decodeURIComponent(resp.data.path);
                var paragraphFirstVersionPath = decodeURIComponent(resp.data.first_version_path);

                return $http.post(paragraphPath, paragraph.addIVersionable([paragraphFirstVersionPath], [proposalPath]));
            })
        );

        var sectionVersionPromise = sectionPromiseStupid.then( respSection => {
            return $q.all(paragraphPromises).then( (respParagraphs) => {
                var paths = respParagraphs.map( (resp) => decodeURIComponent(resp.data.path) );

                var sectionVersion = new SectionVersion(undefined, paths, [], [decodeURIComponent(respSection.data.first_version_path)]);

                return $http.post(decodeURIComponent(respSection.data.path), sectionVersion);
            })
        });

        return $q.all(paragraphPromises).then( (respParagraphs) => {
            return sectionVersionPromise.then( (respSectionVersion) => {
                addParagraph(proposalVersion, decodeURIComponent(respSectionVersion.data.path));

                proposalVersion.addIVersionable([], [proposalFirstVersionPath]);

                return $http.post(proposalPath, proposalVersion);
            })
        });
    });
};

export function followNewestVersion($http, resourceVersion) {
    return $http.get(resourceVersion.path).then( (newResourceVersion) => {
        resourceVersion["adhocracy.sheets.versions.IVersionable"].follows = [newResourceVersion.data.path];
        return resourceVersion;
    });
};

{'fraction_non_alphanumeric': 0.12440068493150686, 'fraction_numerical': 0.0005136986301369863, 'mean_word_length': 2.8500329597890572, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 32, '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': '5044083', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3174, 'n_words': 583}