<commit_before>define(['logger', 'voc', 'underscore'], function(Logger, Voc, _){
    return {
        init : function(vie) {
            this.LOG.debug("initialize Version");
            this.vie = vie;
            this.vie.entities.on('add', this.filter, this);
        },
        LOG : Logger.get('VersionModel'),
        /** 
         * Filters entities from added entities to vie.entities
         */
        filter: function(model, collection, options) {
        },
        newVersion: function(episode, fromVersion) {
            var newVersion;
            if( !fromVersion ){
                newVersion = new this.vie.Entity();
            } else {
                // TODO rename to deepCopy
                newVersion = fromVersion.clone();
            }
            this.LOG.debug("newVersion", newVersion);
            newVersion.set(Voc.timestamp, new Date());
            newVersion.set('@type', Voc.VERSION);
            newVersion.set(Voc.belongsToEpisode, episode.getSubject());
            newVersion.set(Voc.hasWidget, []);

            var vie = this.vie;
            var vm = this;
            this.vie.save({
                'entity' : newVersion
            }).from('sss').execute().success(
                function(version) {
                    vie.entities.addOrUpdate(version);
                    if( fromVersion ) {
                        vm.getWidgets(fromVersion).each( function(widget){
                            var newWidget = CopyMachine.deepCopy(widget);
                            vie.save({
                                'entity' : newWidget
                            }).to('sss').execute();
                        });
                        
                    }
                }
            );
            /*
             * TODO refactor widget deepcopy
            this.versionCollection.create(newVersion, {
                'wait' : true,
                'success' : function(model, response, options) {
                    version.LOG.debug('version saved', model);
                    if( fromVersion)
                        fromVersion.widgetCollection.each(function(widget){
                            var newWidget = widget.clone({'version': model.getSubject()});
                            model.createWidget(newWidget);
                        });
            }});
            */
            return newVersion;
        },
        createWidget: function(widget, version) {
            widget.set(Voc.belongsToVersion, version.getSubject());
            this.LOG.debug("createWidget", widget, version);
            var LOG = this.LOG;
            var vie = this.vie;
            this.vie.save({
                'entity' : widget
            }).from('sss').execute().success(
                function(widget_uri) {
                    widget.set(widget.idAttribute, widget_uri['uri']);
                    LOG.debug('created', widget_uri, 'version', version.getSubject());
                    version = vie.entities.get(version.getSubject());
                    var widgets = version.get(Voc.hasWidget) || [];
                    LOG.debug('widgets', JSON.stringify(widgets));
                    if( !_.isArray(widgets)) widgets = [widgets.getSubject()];
                    widgets.push(widget_uri['uri']);
                    vie.entities.addOrUpdate(widget);
                    version.set(Voc.hasWidget, widgets);
                    widgets = version.get( Voc.hasWidget) || [];
                    LOG.debug('widgets', JSON.stringify(widgets));
                }
            );<commit_msg>Versioning Widgeting corrected<commit_after>define(['logger', 'voc', 'underscore'], function(Logger, Voc, _){
    return {
        init : function(vie) {
            this.LOG.debug("initialize Version");
            this.vie = vie;
            this.vie.entities.on('add', this.filter, this);
        },
        LOG : Logger.get('VersionModel'),
        /** 
         * Filters entities from added entities to vie.entities
         */
        filter: function(model, collection, options) {
        },
        newVersion: function(episode, fromVersion) {
            var newVersion;
            if( !fromVersion ){
                newVersion = new this.vie.Entity();
            } else {
                // TODO rename to deepCopy
                newVersion = fromVersion.clone();
            }
            this.LOG.debug("newVersion", newVersion);
            newVersion.set(Voc.timestamp, new Date());
            newVersion.set('@type', Voc.VERSION);
            newVersion.set(Voc.belongsToEpisode, episode.getSubject());
            newVersion.set(Voc.hasWidget, []);

            var vie = this.vie;
            var vm = this;
            this.vie.save({
                'entity' : newVersion
            }).from('sss').execute().success(
                function(version) {
                    vie.entities.addOrUpdate(version);
                    if( fromVersion ) {
                        vm.getWidgets(fromVersion).each( function(widget){
                            var newWidget = CopyMachine.deepCopy(widget);
                            vie.save({
                                'entity' : newWidget
                            }).to('sss').execute();
                        });
                        
                    }
                }
            );
            /*
             * TODO refactor widget deepcopy
            this.versionCollection.create(newVersion, {
                'wait' : true,
                'success' : function(model, response, options) {
                    version.LOG.debug('version saved', model);
                    if( fromVersion)
                        fromVersion.widgetCollection.each(function(widget){
                            var newWidget = widget.clone({'version': model.getSubject()});
                            model.createWidget(newWidget);
                        });
            }});
            */
            return newVersion;
        },
        createWidget: function(widget, version) {
            widget.set(Voc.belongsToVersion, version.getSubject());
            this.LOG.debug("createWidget", widget, version);
            var LOG = this.LOG;
            var vie = this.vie;
            this.vie.save({
                'entity' : widget
            }).from('sss').execute().success(
                function(widget_uri) {
                    widget.set(widget.idAttribute, widget_uri['uri']);
                    LOG.debug('created', widget_uri, 'version', version.getSubject());
                    //version = vie.entities.get(version.getSubject());
                    var widgets = version.get(Voc.hasWidget) || [];
                    if( !_.isArray(widgets)) widgets = [widgets];
                    widgets = widgets.map(function(w){
                        return w.getSubject();
                    });
                    if( _.indexOf(widgets, widget_uri['uri']) === -1 ) {
                        widgets.push(widget_uri['uri']);
                        vie.entities.addOrUpdate(widget);
                        version.set(Voc.hasWidget, widgets);
                    }
                }
            );
{'fraction_non_alphanumeric': 0.12101732811626607, 'fraction_numerical': 0.00013974287311347122, 'mean_word_length': 1.2822066326530612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '950010', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1677, 'n_words': 297}