Merge "[sdc] - merged 1707_build into sdc LF"
[sdc.git] / catalog-ui / src / app / models / components / product.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by obarda on 2/4/2016.
23  */
24 'use strict';
25 import {Component} from "./component";
26 import {IProductService} from "../../services/components/product-service";
27 import {IGroup, ISubCategory, IMainCategory, ICategoryBase} from "../category";
28 import {ComponentMetadata} from "../component-metadata";
29
30 export class Product extends Component {
31
32     public contacts:Array<string>;
33     public componentService:IProductService;
34     public fullName:string;
35
36     constructor(componentService:IProductService, $q:ng.IQService, component?:Product) {
37         super(componentService, $q, component);
38
39         if (component) {
40             this.fullName = component.fullName;
41             this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
42             this.contacts = component.contacts;
43         }
44         this.componentService = componentService;
45         this.iconSprite = "sprite-product-icons";
46     }
47
48     public deleteGroup = (uniqueId:string):void => {
49         _.forEach(this.categories, (category:IMainCategory) => {
50             _.forEach(category.subcategories, (subcategory:ISubCategory) => {
51                 subcategory.groupings = _.reject(subcategory.groupings, (group:IGroup) => {
52                     return group.uniqueId === uniqueId;
53                 });
54                 if (subcategory.groupings.length == 0) { // if there is no groups, delete the subcategory
55                     category.subcategories = _.reject(category.subcategories, (subcategoryObj:ISubCategory) => {
56                         return subcategoryObj.uniqueId === subcategory.uniqueId;
57                     });
58                     if (category.subcategories.length == 0) { // if there is no subcategory, delete the category
59                         this.categories = _.reject(this.categories, (categoryObj:IMainCategory) => {
60                             return categoryObj.uniqueId === category.uniqueId;
61                         });
62                     }
63                 }
64             });
65         });
66     };
67
68     private getCategoryObjectById = (categoriesArray:Array<ICategoryBase>, categoryUniqueId:string):ICategoryBase => {
69         let categorySelected = _.find(categoriesArray, (category) => {
70             return category.uniqueId === categoryUniqueId;
71         });
72         return categorySelected;
73     };
74
75     public addGroup = (category:IMainCategory, subcategory:ISubCategory, group:IGroup):void => {
76         if (!this.categories) {
77             this.categories = new Array<IMainCategory>();
78         }
79         let existingCategory:IMainCategory = <IMainCategory>this.getCategoryObjectById(this.categories, category.uniqueId);
80         let newGroup = angular.copy(group);
81         newGroup.filterTerms = undefined;
82         newGroup.isDisabled = undefined;
83         if (!existingCategory) {
84             let newCategory:IMainCategory = angular.copy(category);
85             newCategory.filteredGroup = undefined;
86             newCategory.subcategories = [];
87             let newSubcategory:ISubCategory = angular.copy(subcategory);
88             newSubcategory.groupings = [];
89             newSubcategory.groupings.push(newGroup);
90             newCategory.subcategories.push(newSubcategory);
91             this.categories.push(newCategory);
92         }
93         else {
94             let existingSubcategory:ISubCategory = <ISubCategory> this.getCategoryObjectById(existingCategory.subcategories, subcategory.uniqueId);
95             if (!existingSubcategory) {
96                 let newSubcategory:ISubCategory = angular.copy(subcategory);
97                 newSubcategory.groupings = [];
98                 newSubcategory.groupings.push(newGroup);
99                 existingCategory.subcategories.push(newSubcategory);
100
101             } else {
102                 let existingGroup:IGroup = <IGroup> this.getCategoryObjectById(existingSubcategory.groupings, group.uniqueId);
103                 if (!existingGroup) {
104                     existingSubcategory.groupings.push(newGroup);
105                 }
106             }
107         }
108     };
109
110     getTypeUrl():string {
111         return 'products/';
112     }
113
114     public setComponentMetadata(componentMetadata:ComponentMetadata) {
115         super.setComponentMetadata(componentMetadata);
116         this.setComponentDisplayData();
117     };
118
119     setComponentDisplayData():void {
120         this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
121         this.iconSprite = "sprite-product-icons";
122     }
123 }
124
125