EntityBusinessMetaDataView.js 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * 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",
22 23
    "hbs!tmpl/entity/EntityBusinessMetaDataView_tmpl",
    "views/entity/EntityBusinessMetaDataItemView",
24 25 26 27 28
    "models/VEntity",
    "utils/Utils",
    "utils/Messages",
    "utils/CommonViewFunction",
    'moment'
29
], function(require, Backbone, EntityBusinessMetaDataView_tmpl, EntityBusinessMetaDataItemView, VEntity, Utils, Messages, CommonViewFunction, moment) {
30 31 32
    "use strict";

    return Backbone.Marionette.CompositeView.extend({
33 34 35
        _viewName: "EntityBusinessMetaDataView",
        template: EntityBusinessMetaDataView_tmpl,
        childView: EntityBusinessMetaDataItemView,
36 37 38 39 40
        childViewContainer: "[data-id='itemView']",
        childViewOptions: function() {
            return {
                editMode: this.editMode,
                entity: this.entity,
41
                businessMetadataCollection: this.businessMetadataCollection,
42 43 44 45 46 47
                enumDefCollection: this.enumDefCollection
            };
        },
        /** ui selector cache */
        ui: {
            addItem: "[data-id='addItem']",
48 49 50
            addBusinessMetadata: "[data-id='addBusinessMetadata']",
            saveBusinessMetadata: "[data-id='saveBusinessMetadata']",
            businessMetadataTree: "[data-id='businessMetadataTree']",
51 52 53 54 55
            cancel: "[data-id='cancel']"
        },
        events: function() {
            var events = {};
            events["click " + this.ui.addItem] = 'createNameElement';
56 57
            events["click " + this.ui.addBusinessMetadata] = "onAddBusinessMetadata";
            events["click " + this.ui.saveBusinessMetadata] = "onSaveBusinessMetadata";
58 59 60 61 62
            events["click " + this.ui.cancel] = "onCancel";
            return events;
        },
        initialize: function(options) {
            var that = this;
63
            _.extend(this, _.pick(options, "entity", "businessMetadataCollection", "enumDefCollection", "guid", "fetchCollection"));
64
            this.editMode = false;
65 66
            this.$("editBox").hide();
            this.actualCollection = new Backbone.Collection(
67 68 69
                _.map(this.entity.businessAttributes, function(val, key) {
                    var foundBusinessMetadata = that.businessMetadataCollection[key];
                    if (foundBusinessMetadata) {
70
                        _.each(val, function(aVal, aKey) {
71
                            var foundAttr = _.find(foundBusinessMetadata, function(o) {
72 73 74 75 76 77 78
                                return o.name === aKey
                            });
                            if (foundAttr) {
                                val[aKey] = { value: aVal, typeName: foundAttr.typeName };
                            }
                        })
                    }
79
                    return _.extend({}, val, { __internal_UI_businessMetadataName: key });
80 81 82 83 84 85
                }));
            this.collection = new Backbone.Collection();
            this.entityModel = new VEntity();
        },
        updateToActualData: function(options) {
            var silent = options && options.silent || false;
86
            this.collection.reset($.extend(true, [], this.actualCollection.toJSON()), { silent: silent });
87
        },
88 89 90
        onAddBusinessMetadata: function() {
            this.ui.addBusinessMetadata.hide();
            this.ui.saveBusinessMetadata.show();
91 92
            this.ui.cancel.show();
            this.editMode = true;
93
            this.ui.businessMetadataTree.hide();
94 95 96 97 98 99 100 101 102 103 104
            this.$(".editBox").show();
            this.updateToActualData({ silent: true });
            if (this.collection.length === 0) {
                this.createNameElement();
            } else {
                this.collection.trigger("reset");
            }
            this.panelOpenClose();
        },
        onCancel: function() {
            this.ui.cancel.hide();
105 106
            this.ui.saveBusinessMetadata.hide();
            this.ui.addBusinessMetadata.show();
107
            this.editMode = false;
108
            this.ui.businessMetadataTree.show();
109 110 111 112 113 114 115 116 117
            this.$(".editBox").hide();
            this.updateToActualData();
            this.panelOpenClose();
        },
        panelOpenClose: function() {
            var collection = this.editMode ? this.collection : this.actualCollection;
            if (collection && collection.length === 0) {
                this.$el.find(".panel-heading").addClass("collapsed");
                this.$el.find(".panel-collapse.collapse").removeClass("in");
118
                this.ui.addBusinessMetadata.text("Add");
119
            } else {
120
                this.ui.addBusinessMetadata.text("Edit");
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
                this.$el.find(".panel-heading").removeClass("collapsed");
                this.$el.find(".panel-collapse.collapse").addClass("in");
            }
        },
        validate: function() {
            var validation = true;
            this.$el.find('.custom-col-1[data-id="value"] [data-key]').each(function(el) {
                var val = $(this).val(),
                    elIsSelect2 = $(this).hasClass("select2-hidden-accessible");
                if (_.isString(val)) {
                    val = val.trim();
                }
                if (_.isEmpty(val)) {
                    if (validation) {
                        validation = false;
                    }
                    if (elIsSelect2) {
                        $(this).siblings(".select2").find(".select2-selection").attr("style", "border-color : red !important");
                    } else {
                        $(this).css("borderColor", "red");
                    }
                } else {
                    if (elIsSelect2) {
                        $(this).siblings(".select2").find(".select2-selection").attr("style", "");
                    } else {
                        $(this).css("borderColor", "");
                    }
                }
            });
            return validation;
        },
152
        onSaveBusinessMetadata: function() {
153 154 155 156
            var that = this;
            if (!this.validate()) {
                return;
            }
157 158 159 160 161
            var nData = this.generateData();
            if (this.actualCollection.length === 0 && _.isEmpty(nData)) {
                this.onCancel();
                return;
            }
162
            this.entityModel.saveBusinessMetadataEntity(this.guid, {
163
                data: JSON.stringify(nData),
164 165 166
                type: "POST",
                success: function(data) {
                    Utils.notifySuccess({
167
                        content: "One or more Business Metadada attributes" + Messages.getAbbreviationMsg(false, 'editSuccessMessage')
168
                    });
169
                    that.entity.businessAttributes = data;
170 171 172 173 174 175 176 177 178 179 180 181 182
                    this.editMode = false;
                    that.fetchCollection();
                    that.onCancel();
                },
                complete: function(model, response) {
                    //that.hideLoader();
                }
            });
        },
        generateData: function() {
            var finalObj = {};
            this.collection.forEach(function(model) {
                if (!model.has("addAttrButton")) {
183
                    var businessMetadataName = model.get("__internal_UI_businessMetadataName"),
184 185
                        modelObj = model.toJSON();
                    _.each(modelObj, function(o, k) {
186
                        if (k === "isNew" || k === "__internal_UI_businessMetadataName") {
187 188 189 190 191 192 193
                            delete modelObj[k];
                            return;
                        }
                        if (_.isObject(o) && o.value !== undefined) {
                            modelObj[k] = o.value;
                        }
                    })
194 195 196
                    if (businessMetadataName !== undefined) {
                        if (finalObj[businessMetadataName]) {
                            finalObj[businessMetadataName] = _.extend(finalObj[businessMetadataName], modelObj);
197
                        } else {
198
                            finalObj[businessMetadataName] = modelObj;
199 200 201 202 203 204
                        }
                    }
                }
            });
            if (_.isEmpty(finalObj)) {
                this.actualCollection.forEach(function(model) {
205 206 207
                    var businessMetadataName = model.get("__internal_UI_businessMetadataName");
                    if (businessMetadataName) {
                        finalObj[businessMetadataName] = {};
208 209 210 211 212 213 214 215 216
                    }
                })
            }
            return finalObj;
        },
        createNameElement: function(options) {
            var modelObj = { isNew: true };
            this.collection.unshift(modelObj);
        },
217
        renderBusinessMetadata: function() {
218 219 220 221
            var li = ""
            this.actualCollection.forEach(function(obj) {
                var attrLi = "";
                _.each(obj.attributes, function(val, key) {
222
                    if (key !== "__internal_UI_businessMetadataName") {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
                        var newVal = val;
                        if (_.isObject(val) && !_.isUndefinedNull(val.value)) {
                            newVal = val.value;
                            if (newVal.length > 0 && val.typeName.indexOf("date") > -1) {
                                newVal = _.map(newVal, function(dates) {
                                    return moment(dates).format("MM/DD/YYYY");
                                });
                            }
                            if (val.typeName === "date") {
                                newVal = moment(newVal).format("MM/DD/YYYY");
                            }

                        }
                        attrLi += "<tr><td>" + _.escape(key) + " (" + _.escape(val.typeName) + ")</td><td>" + _.escape(newVal) + "</td></tr>";
                    }
                });
239 240
                li += "<ul class='business-metadata-tree-parent'><li class='table'>" + _.escape(obj.get("__internal_UI_businessMetadataName")) + "</li>" +
                    "<li class='business-metadata-tree-child entity-detail-table'>" +
241 242 243
                    "<table class='table'>" + attrLi + "</table>" +
                    "</li></ul>";
            });
244
            this.ui.businessMetadataTree.html(li);
245 246 247
        },
        onRender: function() {
            this.panelOpenClose();
248
            this.renderBusinessMetadata();
249 250 251
        }
    });
});