Exemplo n.º 1
0
define(function(require) {

    var _ = require('underscore'),
        i18n = require('locale/i18n'),
        routes = require('routes'),
        BaseModel = require('model/base-model'),
        Validator = require('common/validator');

    var GroupModel = BaseModel.extend({
        modelParams: {},
        defaults: {
            name: '',
            description: ''
        },
        urlRoot: routes.absoluteUrl(routes.GROUPS_DATA),
        initialize: function(){
            this.constructor.__super__.initialize.apply(this, arguments);
            this.validator = new Validator(this.modelParams.fields);
        },

        toJSON: function(){
            var keys = _.keys(this.modelParams.fields);
            return _.pick(this.attributes, keys);
        },

        validate: function(attrs){
            return this.validator.validate(attrs);
        }
    });

    return GroupModel;
});
Exemplo n.º 2
0
define(function(require) {
    var util = require('util'),
        i18n = require('locale/i18n'),
        routes = require('routes'),
        BaseModel = require('model/base-model'),
        Cron = require('common/cron-util'),
        LocalDate = require('common/local-date'),
        Validator = require('common/validator');

    /**
     * Task descriptor object
     * @class
     */
    var TaskDescriptor = BaseModel.extend({
        /**
         * abstract collection, must be overwritten in SyncController.applyConfig
         */
        TASK_TYPES: {},

        defaults: function() {
            return {
                description: '',
                parameters: {}
            }
        },

        urlRoot: routes.absoluteUrl(routes.TASKS_DATA),
        initialize: function() {
            this.constructor.__super__.initialize.apply(this,arguments);
            this.task_type = this.TASK_TYPES[this.get('name')];
            this.validator = new Validator(this.task_type.fields);
            _.defaults(this.get('parameters'), this.task_type.defaultParameters);
        },

        /**
         * Parses raw hash from server to correct Objects
         *
         * Replace timestamps with Date objects, cron's string with Cron object
         *
         * @param {*} attr  recieved hash of attributes
         * @returns         hash with typed values
         */
        parse: function(attr) {
            attr = _.clone(attr);
            attr.cron = new Cron(attr.cron);
            attr.nextScheduledDate = attr.nextScheduledDate && new LocalDate(attr.nextScheduledDate);
            return attr;
        },
        
        validate: function(attrs){
            return this.validator.validate(attrs);
        }
    });

    return TaskDescriptor;
});
Exemplo n.º 3
0
define(function(require) {

    var _ = require('underscore'),
        $ = require('jquery'),
        i18n = require('locale/i18n'),
        routes = require('routes'),
        BaseModel = require('model/base-model'),
        Validator = require('common/validator');

    /**
     * Model for uploading dictionaries. Is used to validate fields before sending form
     * @type {*|void|Backbone.Wreqr.Handlers.extend|Backbone.Wreqr.EventAggregator.extend|Marionette.extend|Marionette.Controller.extend}
     */
    var DictionaryModelUpload = BaseModel.extend({
        modelParams: {},
        setModelParams: function(params){
            this.modelParams = $.extend(true, {}, params, {
                fields: {
                    file: {
                        required: true,
                        uploadable: true
                    }
                }
            });
        },
        defaults: function() {
            return _.extend({
                file: '',
                name: '',
                description: '',
                groupId: ''
            }, this.modelParams.defaultParameters);
        },
        urlRoot: routes.absoluteUrl(routes.DICTIONARIES_DATA),
        initialize: function(){
            this.constructor.__super__.initialize.apply(this, arguments);
            this.validator = new Validator(this.modelParams.fields);
        },
        validate: function(attrs){
            return this.validator.validate(attrs);
        }
    });

    return DictionaryModelUpload;
});
Exemplo n.º 4
0
define(function(require) {
    var _ = require('underscore'),
        BaseModel = require('model/base-model'),
        TaskDescriptor = require('./task-descriptor'),
        LocalDate = require('common/local-date');

        /**
         * All available state values
         */
    var STATES = {
        UNKNOWN: 'UNKNOWN',
        READY: 'READY',
        PENDING: 'PENDING',
        RUNNING: 'RUNNING',
        INTERRUPTING: 'INTERRUPTING',
        SKIPPED: 'SKIPPED',
        FINISHED: 'FINISHED',
        ERROR: 'ERROR',
        CANCELED: 'CANCELED'
    };

    var TASK_STATES_EXECUTING = [STATES.PENDING, STATES.RUNNING, STATES.INTERRUPTING];

    /**
     * Sync source description.
     * @class
     */
    var SyncSource = BaseModel.extend({

        initialize: function(){
            this.constructor.__super__.initialize.apply(this,arguments);
            var descriptor = this.get('descriptor');
            if (!descriptor) {
                descriptor = new TaskDescriptor();
                this.set('descriptor', descriptor);
            }
            this.listenTo(descriptor, {
                'change': this.onDescriptorChanged
            });
            // Associate descriptor with collection to let 410 error proceed
            descriptor.collection = this.collection;
        },

        /**
         * Parses raw hash from server to correct Objects
         *
         * Replaces <code>descriptor</code>'s data with <code>TaskDescriptor</code> model,
         * and all timestamps with <code>LocalDate</code> objects
         *
         * @param {*} attr  received hash of attributes
         * @returns         hash with typed values
         */
        parse: function(attr) {
            if (attr.descriptor) {
                var descriptor = this.get('descriptor');
                if (descriptor) {
                    descriptor.set(descriptor.parse(attr.descriptor));
                    attr.descriptor = descriptor;
                } else {
                    attr.descriptor = new TaskDescriptor(attr.descriptor, {parse: true});
                }
            }
            var r = attr.result;
            if (r) {
                r.registrationDate = r.registrationDate && new LocalDate(r.registrationDate);
                r.startDate = r.startDate && new LocalDate(r.startDate);
                r.endDate = r.endDate && new LocalDate(r.endDate);
            }
            return attr;
        },

        toggleExecute: function(execute){
            if (_.isUndefined(execute)) {
                execute = !this.isExecuting();
            }
            var self = this,
                method = execute ? 'execute' : 'cancel';
            this.sync(method, this)
                .done( function(resp){
                    self.set(self.parse(resp));
                });
        },

        onDescriptorChanged: function(){
            this.trigger('change:descriptor');
        },

        isOfStatus: function(status){
            return this.get('status') === status;
        },
        isStatusReady: function(){
            return this.isOfStatus(STATES.READY);
        },
        isStatusInterrupting: function(){
            return this.isOfStatus(STATES.INTERRUPTING);
        },
        isExecuting: function(){
            return TASK_STATES_EXECUTING.indexOf(this.get('status')) >= 0;
        },

        isResultCanceled: function(){
            var result = this.get('result');
            return result && result.status === STATES.CANCELED;
        },
        isResultFinished: function(){
            var result = this.get('result');
            return result && result.status === STATES.FINISHED;
        },
        isResultError: function(){
            var result = this.get('result');
            return result && result.status === STATES.ERROR;
        },
        isResultDictsErrors: function(){
            var result = this.get('result');
            return result && result.content && result.content.errorCount > 0;
        },
        isResultIncomplete: function(){
            return this.isResultFinished() && this.isResultDictsErrors();
        },
        hasErrors: function() {
            return this.isResultError() || this.isResultIncomplete();
        },
        hasMultipleResults: function() {
            var result = this.get('result');
            return result && result.content && result.content.hasOwnProperty('totalCount');
        }
    });

    SyncSource.STATES = STATES;

    return SyncSource;
});