CreateBusinessMetadataLayoutView.js 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define(['require',
    'backbone',
    'hbs!tmpl/business_metadata/CreateBusinessMetadataLayoutView_tmpl',
    'utils/Utils',
    'utils/Messages',
    'views/business_metadata/BusinessMetadataAttributeItemView',
25 26
    'models/VEntity'
], function(require, Backbone, CreateBusinessMetadataLayoutViewTmpl, Utils, Messages, BusinessMetadataAttributeItemView, VEntity) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    var CreateBusinessMetadataLayoutView = Backbone.Marionette.CompositeView.extend(
        /** @lends CreateBusinessMetadataLayoutView */
        {
            _viewName: 'CreateBusinessMetadataLayoutView',

            template: CreateBusinessMetadataLayoutViewTmpl,

            templateHelpers: function() {
                return {
                    create: this.create,
                    description: this.description,
                    fromTable: this.fromTable,
                    isEditAttr: this.isEditAttr
                };
            },

            /** Layout sub regions */
            regions: {},

            childView: BusinessMetadataAttributeItemView,

            childViewContainer: "[data-id='addAttributeDiv']",

            childViewOptions: function() {
                return {
53 54 55 56 57 58
                    typeHeaders: this.typeHeaders,
                    businessMetadataDefCollection: this.businessMetadataDefCollection,
                    enumDefCollection: this.enumDefCollection,
                    isAttrEdit: this.isAttrEdit,
                    viewId: this.cid,
                    collection: this.collection
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
                };
            },
            /** ui selector cache */
            ui: {
                name: "[data-id='name']",
                description: "[data-id='description']",
                title: "[data-id='title']",
                attributeData: "[data-id='attributeData']",
                addAttributeDiv: "[data-id='addAttributeDiv']",
                createForm: '[data-id="createForm"]',
                businessMetadataAttrPageCancle: '[data-id="businessMetadataAttrPageCancle"]',
                businessMetadataAttrPageOk: '[data-id="businessMetadataAttrPageOk"]'
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["click " + this.ui.attributeData] = "onClickAddAttriBtn";
                events["click " + this.ui.businessMetadataAttrPageOk] = function(e) {
                    var that = this,
                        modal = that.$el;
                    if (e.target.dataset.action == "attributeEdit" || e.target.dataset.action == "addAttribute") {
                        that.onUpdateAttr();
                    } else {
82
                        that.onCreateBusinessMetadata();
83 84 85 86 87 88 89 90 91 92 93 94 95
                    }

                };
                events["click " + this.ui.businessMetadataAttrPageCancle] = function(e) {
                    this.options.onUpdateBusinessMetadata();
                };
                return events;
            },
            /**
             * intialize a new CreateBusinessMetadataLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
96
                _.extend(this, _.pick(options, 'businessMetadataDefCollection', 'selectedBusinessMetadata', 'enumDefCollection', 'model', 'isNewBusinessMetadata', 'isAttrEdit', 'typeHeaders', 'attrDetails'));
97 98 99 100 101 102 103 104 105
                this.fromTable = this.isNewBusinessMetadata ? true : false;
                this.isEditAttr = this.isAttrEdit ? false : true;
                this.businessMetadataModel = new VEntity();
                if (this.model) {
                    this.description = this.model.get('description');
                } else {
                    this.create = true;
                }
                if (!this.isNewBusinessMetadata) {
106 107 108
                    this.collection = this.isAttrEdit ? new Backbone.Collection([
                        this.attrDetails
                    ]) : new Backbone.Collection([{
109 110 111 112 113 114 115
                        "name": "",
                        "typeName": "string",
                        "isOptional": true,
                        "cardinality": "SINGLE",
                        "valuesMinCount": 0,
                        "valuesMaxCount": 1,
                        "isUnique": false,
116
                        "isIndexable": true
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                    }]);
                } else {
                    this.collection = new Backbone.Collection();
                }

            },
            bindEvents: function() {},
            onRender: function() {
                var that = this;
                this.$('.fontLoader').show();
                if (!('placeholder' in HTMLInputElement.prototype)) {
                    this.ui.createForm.find('input,textarea').placeholder();
                }
                if (this.isNewBusinessMetadata == true) {
                    that.ui.businessMetadataAttrPageOk.text("Create");
                    that.ui.businessMetadataAttrPageOk.attr('data-action', 'newBusinessMetadata');
                } else {
                    that.ui.businessMetadataAttrPageOk.text("Save");
                    that.ui.businessMetadataAttrPageOk.attr('data-action', 'attributeEdit');
                }
                this.hideLoader();
            },
            hideLoader: function() {
                this.$('.fontLoader').hide();
                this.$('.hide').removeClass('hide');
            },
            collectionAttribute: function() {
                this.collection.add(new Backbone.Model({
                    "name": "",
                    "typeName": "string",
                    "isOptional": true,
                    "cardinality": "SINGLE",
                    "valuesMinCount": 0,
                    "valuesMaxCount": 1,
                    "isUnique": false,
152
                    "isIndexable": true
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                }));
            },
            onClickAddAttriBtn: function() {
                this.collectionAttribute();
                if (!('placeholder' in HTMLInputElement.prototype)) {
                    this.ui.addAttributeDiv.find('input,textarea').placeholder();
                }
            },
            loaderStatus: function(isActive) {
                var that = this;
                if (isActive) {
                    parent.$('.business-metadata-attr-tableOverlay').show();
                    parent.$('.business-metadata-attr-fontLoader').show();
                } else {
                    parent.$('.business-metadata-attr-tableOverlay').hide();
                    parent.$('.business-metadata-attr-fontLoader').hide();
                }
            },
171 172 173 174 175 176 177 178
            validateValues: function(attributeDefs) {
                var isValidate = true,
                    isAttrDuplicate = true,
                    validationFileds = this.$el.find('.require'),
                    attrNames = [];
                if (attributeDefs && !this.isAttrEdit) {
                    attrNames = _.map(attributeDefs, function(model) {
                        return model.name.toLowerCase();
179 180
                    });
                }
181 182 183 184 185 186 187
                validationFileds.each(function(elements) {
                    $(this).removeClass('errorValidate');
                    if (validationFileds[elements].value == '' || validationFileds[elements].value == null) {
                        if (validationFileds[elements].style.display != 'none') {
                            $(validationFileds[elements]).addClass('errorValidate');
                            $(this).addClass('errorValidate');
                            if (isValidate) { isValidate = false; }
188
                        }
189 190 191 192 193 194 195 196 197 198 199 200 201 202
                    }
                });
                if (isValidate) {
                    this.$el.find('.attributeInput').each(function(element) {
                        var attrValue = this.value.toLowerCase();
                        if (attrNames.indexOf(attrValue) > -1) {
                            Utils.notifyInfo({
                                content: "Attribute name already exist"
                            });
                            $(this).addClass('errorValidate');
                            if (isAttrDuplicate) { isAttrDuplicate = false; }
                        } else {
                            if (attrValue.length) {
                                attrNames.push(attrValue);
203 204 205 206
                            }
                        }
                    });
                }
207 208

                if (!isValidate) {
209
                    Utils.notifyInfo({
210
                        content: "Please fill the details"
211 212 213
                    });
                    return true;
                }
214
                if (!isAttrDuplicate) {
215 216
                    return true;
                }
217

218 219
            },
            onCreateBusinessMetadata: function() {
220
                var that = this;
221 222 223 224 225
                if (this.validateValues()) {
                    return;
                };
                this.loaderStatus(true);
                var name = this.ui.name.val(),
226
                    description = this.ui.description.val();
227 228 229 230
                var attributeObj = this.collection.toJSON();
                if (this.collection.length === 1 && this.collection.first().get("name") === "") {
                    attributeObj = [];
                }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                this.json = {
                    "enumDefs": [],
                    "structDefs": [],
                    "classificationDefs": [],
                    "entityDefs": [],
                    "businessMetadataDefs": [{
                        "category": "BUSINESS_METADATA",
                        "createdBy": "admin",
                        "updatedBy": "admin",
                        "version": 1,
                        "typeVersion": "1.1",
                        "name": name.trim(),
                        "description": description.trim(),
                        "attributeDefs": attributeObj
                    }]
                };
                var apiObj = {
                    sort: false,
                    data: this.json,
                    success: function(model, response) {
                        var nameSpaveDef = model.businessMetadataDefs;
                        if (nameSpaveDef) {
                            that.businessMetadataDefCollection.fullCollection.add(nameSpaveDef);
                            Utils.notifySuccess({
                                content: "Business Metadata " + name + Messages.getAbbreviationMsg(false, 'addSuccessMessage')
                            });
257
                        }
258
                        that.options.onUpdateBusinessMetadata(true);
259 260 261 262 263 264
                    },
                    silent: true,
                    reset: true,
                    complete: function(model, status) {
                        that.loaderStatus(false);
                    }
265
                }
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                apiObj.type = "POST";
                that.businessMetadataModel.saveBusinessMetadata(apiObj);
            },
            onUpdateAttr: function() {
                var that = this,
                    selectedBusinessMetadataClone = $.extend(true, {}, that.selectedBusinessMetadata.toJSON()),
                    attributeDefs = selectedBusinessMetadataClone['attributeDefs'],
                    isvalidName = true;
                if (this.validateValues(attributeDefs)) {
                    return;
                };
                if (this.collection.length > 0) {
                    this.loaderStatus(true);
                    if (selectedBusinessMetadataClone.attributeDefs === undefined) {
                        selectedBusinessMetadataClone.attributeDefs = [];
                    }
                    selectedBusinessMetadataClone.attributeDefs = selectedBusinessMetadataClone.attributeDefs.concat(this.collection.toJSON());
283 284 285 286 287
                    this.json = {
                        "enumDefs": [],
                        "structDefs": [],
                        "classificationDefs": [],
                        "entityDefs": [],
288
                        "businessMetadataDefs": [selectedBusinessMetadataClone]
289 290 291 292 293
                    };
                    var apiObj = {
                        sort: false,
                        data: this.json,
                        success: function(model, response) {
294
                            Utils.notifySuccess({
295
                                content: "One or more Business Metadada attribute" + Messages.getAbbreviationMsg(true, 'editSuccessMessage')
296 297 298
                            });
                            if (model.businessMetadataDefs && model.businessMetadataDefs.length) {
                                that.selectedBusinessMetadata.set(model.businessMetadataDefs[0]);
299
                            }
300
                            that.options.onEditCallback();
301
                            that.options.onUpdateBusinessMetadata(true);
302 303 304 305 306 307 308
                        },
                        silent: true,
                        reset: true,
                        complete: function(model, status) {
                            that.loaderStatus(false);
                        }
                    }
309
                    apiObj.type = "PUT";
310 311 312 313 314 315 316 317 318 319 320 321
                    that.businessMetadataModel.saveBusinessMetadata(apiObj);
                } else {
                    Utils.notifySuccess({
                        content: "No attribute updated"
                    });
                    this.loaderStatus(false);
                    that.options.onUpdateBusinessMetadata();
                }
            }
        });
    return CreateBusinessMetadataLayoutView;
});