<commit_before>/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

using GUPnP;

internal class Rygel.ReferenceCreator : GLib.Object, Rygel.StateMachine {
    private ContentDirectory content_directory;
    private ServiceAction action;

    // Props
    public Cancellable cancellable { get; set; }
    public string container_id;

    // Local props
    public string object_id;

    public ReferenceCreator (ContentDirectory    content_dir,
                             owned ServiceAction action) {
        this.content_directory = content_dir;
        this.cancellable = content_dir.cancellable;
        this.action = (owned) action;
    }

    public void parse_arguments () throws Error {
        this.action.get ("ContainerID",
                            typeof (string), out this.container_id,
                         "ObjectID",
                             typeof (string), out this.object_id);
        if (this.container_id == null) {
            throw new ContentDirectoryError.INVALID_ARGS
                                        (_("'ContainerID' agument missing."));
        }

        if (this.object_id == null) {
            throw new ContentDirectoryError.INVALID_ARGS
                                        (_("'ObjectID' argument missing."));
        }
    }

    public async void run () {
        try {
            this.parse_arguments ();
            var container = yield this.fetch_container ();
            var root_container = this.content_directory.root_container;
            var object = yield root_container.find_object
                                        (this.object_id, this.cancellable);
            if (object == null) {
                throw new ContentDirectoryError.NO_SUCH_OBJECT
                                        (_("No such object"));
            }

            var new_id = yield container.add_reference (object,
                                                        this.cancellable);

            this.action.set ("NewID", typeof (string), new_id);
            this.action.return ();
            this.completed ();
        } catch (Error error) {
            if (error is ContentDirectoryError) {
                this.action.return_error (error.code, error.message);
            } else {
                this.action.return_error (402, error.message);
            }

            warning (_("Failed to create object under '%s': %s"),<commit_msg>server: Fix order of fetching/adding<commit_after>/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

using GUPnP;

internal class Rygel.ReferenceCreator : GLib.Object, Rygel.StateMachine {
    private ContentDirectory content_directory;
    private ServiceAction action;

    // Props
    public Cancellable cancellable { get; set; }
    public string container_id;

    // Local props
    public string object_id;

    public ReferenceCreator (ContentDirectory    content_dir,
                             owned ServiceAction action) {
        this.content_directory = content_dir;
        this.cancellable = content_dir.cancellable;
        this.action = (owned) action;
    }

    public void parse_arguments () throws Error {
        this.action.get ("ContainerID",
                            typeof (string), out this.container_id,
                         "ObjectID",
                             typeof (string), out this.object_id);
        if (this.container_id == null) {
            throw new ContentDirectoryError.INVALID_ARGS
                                        (_("'ContainerID' agument missing."));
        }

        if (this.object_id == null) {
            throw new ContentDirectoryError.INVALID_ARGS
                                        (_("'ObjectID' argument missing."));
        }
    }

    public async void run () {
        try {
            this.parse_arguments ();
            var root_container = this.content_directory.root_container;

            var object = yield root_container.find_object
                                        (this.object_id, this.cancellable);
            if (object == null) {
                throw new ContentDirectoryError.NO_SUCH_OBJECT
                                        (_("No such object"));
            }

            var container = yield this.fetch_container ();

            var new_id = yield container.add_reference (object,
                                                        this.cancellable);

            this.action.set ("NewID", typeof (string), new_id);
            this.action.return ();
            this.completed ();
        } catch (Error error) {
            if (error is ContentDirectoryError) {
                this.action.return_error (error.code, error.message);
            } else {
                this.action.return_error (402, error.message);
            }

            warning (_("Failed to create object under '%s': %s"),
{'fraction_non_alphanumeric': 0.08043647700701481, 'fraction_numerical': 0.005923616523772409, 'mean_word_length': 1.8797127468581687, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6030932', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1498, 'n_words': 537}