495 lines
26 KiB
JavaScript
495 lines
26 KiB
JavaScript
"use strict";
|
|
/*
|
|
* Author: Srinivas R
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
angular.module('assetModule')
|
|
.controller('AssetLinkController', ['$scope', '$q', 'assetModel', 'metadataModel', 'consoleService', '$filter', 'relationModel', 'userModel', 'configurationModel', 'categoriesService', '$rootScope', 'systemAlertService', 'i18nService', 'events',
|
|
function ($scope, $q, assetModel, metadataModel, consoleService, $filter, relationModel, userModel, configurationModel, categoriesService, $rootScope, systemAlertService, i18nService, events) {
|
|
$scope.currentAsset = $scope.linkParams.selectedItem;
|
|
var criteria = {
|
|
"filterCriteria": {
|
|
"assetConsoleKeywords": [],
|
|
"assetTypes": []
|
|
}
|
|
}, selected = {
|
|
linkOption: {},
|
|
relationSubType: {
|
|
label: $filter('i18n')('asset.relationship.parent.option'),
|
|
value: 'parent'
|
|
}
|
|
}, state = {
|
|
searching: false,
|
|
processing: false,
|
|
relateProcessing: false
|
|
};
|
|
$scope.state = state;
|
|
$scope.availableEntities = null;
|
|
$scope.selected = selected;
|
|
$scope.assetTypes = [];
|
|
$scope.relationshipTypes = [];
|
|
$scope.criteria = criteria;
|
|
$scope.expanded = false;
|
|
$scope.chunkInfo = { startIndex: 0, chunkSize: 100 };
|
|
$scope.showLoadMore = false;
|
|
$scope.toggleMenu = function () {
|
|
$scope.expanded = !$scope.expanded;
|
|
};
|
|
$scope.assetTypesSelected = [];
|
|
$scope.relationshipSubTypes = [
|
|
{
|
|
label: $filter('i18n')('asset.relationship.child.option'),
|
|
value: 'child'
|
|
},
|
|
{
|
|
label: $filter('i18n')('asset.relationship.parent.option'),
|
|
value: 'parent'
|
|
}
|
|
];
|
|
var assetMetadata, filterDictionary;
|
|
$scope.filterConfig = angular.copy(configurationModel.get('ciSearch.assetConsoleFilter'));
|
|
filterDictionary = _.keyBy($scope.filterConfig, 'label');
|
|
$scope.columnPreferenceGroup = 'CIFilter';
|
|
//get saved presets from the backend
|
|
userModel.getUserPreferences($scope.columnPreferenceGroup).then(function (config) {
|
|
$scope.userSavedSearchPresets = _.uniqBy(config, 'name') || [];
|
|
var configIndex = _.findIndex($scope.filterConfig, { name: 'custom' });
|
|
_.forEach($scope.userSavedSearchPresets, function (userPreset) {
|
|
if (!userPreset.systemgenerated) {
|
|
var savedPreset = {
|
|
name: userPreset.name,
|
|
id: userPreset.id,
|
|
type: 'custom',
|
|
filters: _.map(userPreset.value, function (value) {
|
|
var filterValue, filterName, filterLabel, option;
|
|
filterName = value.option;
|
|
if (filterDictionary[value.filter]) {
|
|
option = _.find(filterDictionary[value.filter].options, { name: value.option });
|
|
}
|
|
else if (value.filter === 'sites' && filterDictionary.site) {
|
|
option = _.find(filterDictionary.site.options, { name: value.option });
|
|
}
|
|
if (value.realOption && value.realOption.criteria) {
|
|
filterValue = value.realOption.criteria.value;
|
|
}
|
|
else if (option && option.criteria) {
|
|
filterValue = option.criteria.value;
|
|
}
|
|
else {
|
|
filterValue = value;
|
|
}
|
|
var item = {
|
|
active: true,
|
|
criteria: { name: value.filter, value: filterValue },
|
|
name: filterName,
|
|
label: filterLabel,
|
|
type: value.filter !== 'keywords' ? 'dynamic' : 'keywords'
|
|
};
|
|
return item;
|
|
})
|
|
};
|
|
if (configIndex !== -1 && savedPreset.id !== 'allAssets') {
|
|
$scope.filterConfig[configIndex].expanded = true;
|
|
$scope.filterConfig[configIndex].options.push(savedPreset);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
var userPromise = userModel.getFullCurrentUserData().then(function () {
|
|
var userData = userModel.userFullData, filterApprovedByMe = _.find(filterDictionary.approvedBy.options, { label: 'me' }), filterCreatedByMe = _.find(filterDictionary.createdBy.options, { label: 'me' }), filterManagedByMe = _.find(filterDictionary.managedBy.options, { label: 'me' }), filterOwnedByMe = _.find(filterDictionary.owner.options, { label: 'me' }), filterSupportedByMe = _.find(filterDictionary.supportedBy.options, { label: 'me' }), filterUsedByMe = _.find(filterDictionary.usedBy.options, { label: 'me' });
|
|
filterApprovedByMe.subLabel = filterCreatedByMe.subLabel = filterManagedByMe.subLabel =
|
|
filterOwnedByMe.subLabel = filterSupportedByMe.subLabel = filterUsedByMe.subLabel = userData.fullName;
|
|
filterApprovedByMe.criteria.value = filterCreatedByMe.criteria.value = filterManagedByMe.criteria.value =
|
|
filterOwnedByMe.criteria.value = filterSupportedByMe.criteria.value = filterUsedByMe.criteria.value =
|
|
[{ id: userData.personId }];
|
|
var mySiteFilter = _.find(filterDictionary.site.options, { label: 'mySite' });
|
|
mySiteFilter.criteria.value = [{
|
|
name: userModel.userFullData.site.name,
|
|
companyName: userModel.userFullData.company.name,
|
|
region: userModel.userFullData.site.region,
|
|
siteGroup: userModel.userFullData.site.siteGroup
|
|
}];
|
|
mySiteFilter.subLabel = userModel.userFullData.site.name;
|
|
var mySiteGroupFilter = _.find(filterDictionary.siteGroup.options, { label: 'mySiteGroup' });
|
|
mySiteGroupFilter.criteria.value = [{
|
|
companyName: userModel.userFullData.company.name,
|
|
region: userModel.userFullData.site.region,
|
|
siteGroup: userModel.userFullData.site.siteGroup
|
|
}];
|
|
mySiteGroupFilter.subLabel = userModel.userFullData.site.siteGroup;
|
|
var myRegionFilter = _.find(filterDictionary.region.options, { label: 'myRegion' });
|
|
myRegionFilter.criteria.value = [{
|
|
companyName: userModel.userFullData.company.name,
|
|
region: userModel.userFullData.site.region
|
|
}];
|
|
myRegionFilter.subLabel = userModel.userFullData.site.region;
|
|
});
|
|
var assetPromise = metadataModel.getMetadataByType(EntityVO.TYPE_ASSET).then(function (metadata) {
|
|
assetMetadata = metadata;
|
|
$scope.metadata = assetMetadata;
|
|
$scope.assetTypes = $scope.metadata.assetTypes;
|
|
_.forEach(metadata.statuses, function (status) {
|
|
filterDictionary.statuses.options.push({
|
|
name: status.label,
|
|
type: 'dynamic',
|
|
criteria: {
|
|
name: 'ticketSpecificStatuses',
|
|
value: [status.name]
|
|
}
|
|
});
|
|
});
|
|
_.forEach(metadata.assetTypes, function (assetType) {
|
|
filterDictionary.ciType.options.push({
|
|
name: assetType.label,
|
|
type: 'dynamic',
|
|
criteria: {
|
|
name: 'assetTypes',
|
|
value: [assetType.name]
|
|
},
|
|
criteriaValue: assetType.name
|
|
});
|
|
//add subtypes
|
|
assetType.subType.forEach(function (subType) {
|
|
filterDictionary.assetSubType.options.push({
|
|
name: subType.label,
|
|
type: 'dynamic',
|
|
filterName: 'assetSubTypes',
|
|
isHidden: false,
|
|
criteria: {
|
|
name: 'assetSubTypes',
|
|
value: [subType.name],
|
|
typeName: assetType.name
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
state.processing = true;
|
|
$q.all([userPromise, assetPromise]).then(function () {
|
|
state.processing = false;
|
|
}).finally(function () {
|
|
state.processing = false;
|
|
});
|
|
$scope.search = {
|
|
config: $scope.filterConfig,
|
|
isSearching: false,
|
|
selectedFilters: [],
|
|
searchKeywords: []
|
|
};
|
|
function buildFilterCriteria(filters, searchKeywords) {
|
|
var filterCriteria = {}, regionKey, siteGroupKey, siteKey, sitesKey = 'sites', sites = [], regionValueObj = {}, siteGroupValueObj = {}, siteValueObj = {};
|
|
_.forEach(filters, function (filter) {
|
|
var criteria = filter.criteria, isMultiSelect = _.isArray(criteria.value), name = criteria.name;
|
|
if (filterCriteria[name]) {
|
|
if (isMultiSelect) {
|
|
filterCriteria[name] = _.union(filterCriteria[name], criteria.value);
|
|
}
|
|
else {
|
|
filters.splice(_.findIndex(filters, { name: filter.name }), 1);
|
|
filterCriteria[name] = criteria.value;
|
|
}
|
|
}
|
|
else {
|
|
if (criteria.name === 'region' || criteria.name === 'siteGroup' || criteria.name === 'sites') {
|
|
if (criteria.name === 'region') {
|
|
regionKey = criteria.name;
|
|
regionValueObj = criteria.value[0];
|
|
regionValueObj.id = regionKey;
|
|
sites.push(regionValueObj);
|
|
}
|
|
else if (criteria.name === 'siteGroup') {
|
|
siteGroupKey = criteria.name;
|
|
siteGroupValueObj = criteria.value[0];
|
|
siteGroupValueObj.id = siteGroupKey;
|
|
sites.push(siteGroupValueObj);
|
|
}
|
|
else if (criteria.name === 'sites') {
|
|
siteKey = criteria.name;
|
|
siteValueObj = criteria.value[0];
|
|
siteValueObj.id = siteKey;
|
|
sites.push(siteValueObj);
|
|
}
|
|
filterCriteria[sitesKey] = sites;
|
|
}
|
|
else {
|
|
filterCriteria[name] = criteria.value;
|
|
}
|
|
}
|
|
});
|
|
if (searchKeywords && searchKeywords.length > 0) {
|
|
searchKeywords = _.map(searchKeywords.split(','), _.trim);
|
|
filterCriteria.keywords = searchKeywords;
|
|
}
|
|
return filterCriteria;
|
|
}
|
|
$scope.getProductCategoriesByCompany = function (searchText) {
|
|
var categoryName = 'product', params = {
|
|
entityType: EntityVO.TYPE_ASSET,
|
|
categoryType: categoryName,
|
|
searchText: searchText,
|
|
criteria: {
|
|
company: userModel.userFullData.company
|
|
}
|
|
};
|
|
return categoriesService.searchCategories(params).then(function (result) {
|
|
var category = _.find(assetMetadata.categories, function (category) {
|
|
return category.name === categoryName;
|
|
});
|
|
return _.map(result.items, function (item) {
|
|
if (item.length) {
|
|
var serializedValue = _.compact(item).join(' > '), tiers = {};
|
|
for (var i = 0; i < category.listOfTiers.length; i++) {
|
|
tiers[category.listOfTiers[i].name] = item[i];
|
|
}
|
|
return {
|
|
value: serializedValue,
|
|
attributeMap: { categorizations: [{ name: categoryName, tiers: tiers }] }
|
|
};
|
|
}
|
|
});
|
|
});
|
|
};
|
|
var oldFileter;
|
|
$scope.searchCIs = function (filter) {
|
|
$scope.chunkInfo.startIndex = 0;
|
|
oldFileter = filter;
|
|
searchCI(filter);
|
|
};
|
|
function searchCI(filter) {
|
|
state.processing = true;
|
|
filter.isSearching = true;
|
|
var filterCriteria = {
|
|
filterCriteria: buildFilterCriteria(filter.selectedFilters, filter.searchKeywords),
|
|
chunkInfo: $scope.chunkInfo,
|
|
sortInfos: [{
|
|
sortFieldName: 'name',
|
|
sortFieldOrder: 'ASC'
|
|
}],
|
|
attributeNames: ['name', 'desc', 'status', 'classId']
|
|
};
|
|
filter.lastUsedFilterCriteria = filterCriteria;
|
|
consoleService.getAssetList(filterCriteria, 'cisearch').then(function (result) {
|
|
if (filterCriteria.chunkInfo.startIndex) {
|
|
$scope.availableEntities = $scope.availableEntities.concat(result.itemList);
|
|
}
|
|
else {
|
|
$scope.availableEntities = result.itemList;
|
|
}
|
|
$scope.totalAvailableEntitiesCount = result.totalCount;
|
|
if ($scope.totalAvailableEntitiesCount > $scope.availableEntities.length) {
|
|
$scope.showLoadMore = true;
|
|
}
|
|
else {
|
|
$scope.showLoadMore = false;
|
|
}
|
|
_.forEach($scope.availableEntities, function (result) {
|
|
if (result.status) {
|
|
result.status.value = $filter('localizeLabel')(result.status.value, EntityVO.TYPE_STATUS, EntityVO.TYPE_ASSET);
|
|
}
|
|
});
|
|
}).finally(function () {
|
|
filter.isSearching = false;
|
|
state.processing = false;
|
|
});
|
|
}
|
|
$scope.loadMore = function () {
|
|
$scope.chunkInfo.startIndex += $scope.chunkInfo.chunkSize;
|
|
searchCI(oldFileter);
|
|
};
|
|
$scope.selectEntity = function () {
|
|
selected.entities = _.filter($scope.availableEntities, { 'isSelected': true });
|
|
getRelationshipTypes();
|
|
};
|
|
$scope.checkItem = function (item) {
|
|
item.checked = !item.checked;
|
|
criteria.filterCriteria.assetTypes = [];
|
|
$scope.assetTypesSelected = [];
|
|
_.forOwn($scope.assetTypes, function (type) {
|
|
if (type.checked) {
|
|
criteria.filterCriteria.assetTypes.push(type.label);
|
|
$scope.assetTypesSelected.push(type);
|
|
}
|
|
}, $scope);
|
|
};
|
|
$scope.addRelations = function () {
|
|
state.processing = true;
|
|
relationModel.getRelations($scope.currentAsset.reconciliationId, "asset").then(function (currentRelations) {
|
|
var data = {}, failureCount = 0;
|
|
currentRelations = _.filter(currentRelations, { relationshipClassId: selected.relation.value });
|
|
if (selected.relation && selected.relation.direction && selected.relationSubType.value === 'child') {
|
|
data = {
|
|
'ids': [],
|
|
'relationship': [
|
|
{
|
|
"id": $scope.currentAsset.instanceId,
|
|
"relationshipType": selected.relation.value,
|
|
"tag": "linkeditem",
|
|
"type": "asset",
|
|
"desc": ""
|
|
}
|
|
]
|
|
};
|
|
_.forOwn(selected.entities, function (entity) {
|
|
var temp = _.find(currentRelations, function (relation) {
|
|
return relation.realObject.instanceId === entity.instanceId && relation.realObject.isParent;
|
|
});
|
|
if (!temp) {
|
|
data.ids.push({
|
|
"uuid": entity.instanceId,
|
|
"type": "asset"
|
|
});
|
|
}
|
|
else {
|
|
failureCount++;
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
data = {
|
|
'ids': [
|
|
{
|
|
"uuid": $scope.currentAsset.instanceId,
|
|
"type": "asset"
|
|
}
|
|
],
|
|
'relationship': []
|
|
};
|
|
_.forOwn(selected.entities, function (entity) {
|
|
var temp = _.find(currentRelations, function (relation) {
|
|
return relation.realObject.instanceId === entity.instanceId && relation.realObject.isChild;
|
|
});
|
|
if (!temp) {
|
|
data.relationship.push({
|
|
"id": entity.instanceId,
|
|
"relationshipType": selected.relation.value,
|
|
"tag": "linkeditem",
|
|
"type": "asset",
|
|
"desc": ""
|
|
});
|
|
}
|
|
else {
|
|
failureCount++;
|
|
}
|
|
});
|
|
}
|
|
if (data.ids.length && data.relationship.length) {
|
|
assetModel.addBulkRelations(data).then(function (response) {
|
|
state.processing = false;
|
|
$scope.modalInstance.close(response);
|
|
$rootScope.$broadcast('new-relations');
|
|
var successCount = 0;
|
|
var needsReconciliation = false;
|
|
_.each(response, function (res) {
|
|
if (res.additionalInformation && res.additionalInformation.listOfMessage) {
|
|
var resMsgs = res.additionalInformation.listOfMessage;
|
|
var count = _.countBy(resMsgs, 'type');
|
|
if (count.Success) {
|
|
successCount += count.Success;
|
|
}
|
|
if (count.Failure) {
|
|
failureCount += count.Failure;
|
|
}
|
|
if (res.additionalInformation.listOfMessage.length > 0 && res.additionalInformation.listOfMessage[0].additionalInformation && res.additionalInformation.listOfMessage[0].additionalInformation.needsReconciliation) {
|
|
needsReconciliation = true;
|
|
}
|
|
}
|
|
});
|
|
if (needsReconciliation) {
|
|
systemAlertService.error({
|
|
text: $filter('i18n')('asset.actionBlade.assetAction.warning.reconciliation'),
|
|
clear: false
|
|
});
|
|
}
|
|
if (!failureCount) {
|
|
systemAlertService.success({
|
|
text: $filter('i18n')('ticket.notification.link.message', successCount),
|
|
hide: 10000
|
|
});
|
|
}
|
|
else if (!successCount) {
|
|
systemAlertService.error({
|
|
text: $filter('i18n')('ticket.notification.link.message.error', failureCount),
|
|
clear: false
|
|
});
|
|
}
|
|
else {
|
|
systemAlertService.success({
|
|
text: $filter('i18n')('ticket.notification.link.message.mixed', [successCount, failureCount]),
|
|
hide: 10000
|
|
});
|
|
}
|
|
}).catch(function (error) {
|
|
state.processing = false;
|
|
$scope.modalInstance.close(error);
|
|
if (error.data && error.data.error) {
|
|
systemAlertService.error({
|
|
text: error.data.error,
|
|
clear: false
|
|
});
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
state.processing = false;
|
|
if (failureCount) {
|
|
systemAlertService.error({
|
|
text: $filter('i18n')('ticket.notification.link.message.error', failureCount),
|
|
clear: false
|
|
});
|
|
}
|
|
$scope.modalInstance.close();
|
|
}
|
|
});
|
|
};
|
|
function getRelationshipTypes() {
|
|
state.relateProcessing = true;
|
|
delete selected.relation;
|
|
var data = {
|
|
classId1: $scope.currentAsset.classId
|
|
};
|
|
var selectedClasses = _.uniq(_.map(selected.entities, "classId"));
|
|
data.classId2 = (selectedClasses.length === 1) ? selectedClasses[0] : "BMC_ASSETBASE";
|
|
assetModel.getRelationshipTypes(data).then(function (relationshipTypes) {
|
|
if (selected.entities.length > 1) {
|
|
relationshipTypes = _.filter(relationshipTypes, function (relation) {
|
|
return relation.cardinality !== "1-1";
|
|
});
|
|
}
|
|
$scope.relationshipTypes = relationshipTypes;
|
|
state.relateProcessing = false;
|
|
});
|
|
}
|
|
$scope.close = function () {
|
|
if ((selected.linkOption !== {}) || selected.searchText || ($scope.availableEntities && $scope.availableEntities.length)) {
|
|
var modalInstance = systemAlertService.modal({
|
|
title: i18nService.getLocalizedString('common.notification.dirty.title'),
|
|
text: i18nService.getLocalizedString('common.notification.dirty.message'),
|
|
buttons: [
|
|
{
|
|
text: i18nService.getLocalizedString('common.labels.yes'),
|
|
data: true
|
|
},
|
|
{
|
|
text: i18nService.getLocalizedString('common.labels.no'),
|
|
data: false
|
|
}
|
|
]
|
|
});
|
|
modalInstance.result.then(function (data) {
|
|
if (data) {
|
|
$scope.modalInstance.dismiss();
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
$scope.modalInstance.dismiss();
|
|
}
|
|
};
|
|
function handleModalBackdropClick() {
|
|
$scope.close();
|
|
}
|
|
$scope.$on(events.MODAL_BACKDROP_CLICK_PROPOGATE, handleModalBackdropClick);
|
|
}]);
|
|
})();
|