SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/asset/asset-service.js

416 lines
19 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('assetModule').service('assetService', ['$resource', '$http', '$filter', function ($resource, $http, $filter) {
var resource = $resource('/smartit/rest/asset/:reconciliationId/:assetClassId', {}, {
getAssetDetailsByID: {
method: 'GET',
url: '/smartit/rest/asset/details/:reconciliationId/:assetClassId',
isArray: true
},
update: {
method: 'PUT',
url: '/smartit/rest/asset/update/all/:reconciliationId',
isArray: true
},
getAssetOwnerDetails: {
method: 'GET',
url: '/smartit/rest/person/:id',
isArray: true
},
saveCIPresetConfiguration: {
url: '/smartit/rest/mtc/preference/:clientType/CI_FILTER_GROUP',
method: 'PUT',
isArray: false,
transformResponse: function (data) {
return { id: data };
}
},
removeCIPresetConfiguration: {
url: '/smartit/rest/mtc/preference/',
method: 'DELETE',
isArray: false
},
getRelationshipTypes: {
url: '/smartit/rest/asset/relationshiptypes',
method: 'POST',
isArray: true
},
addBulkRelations: {
url: '/smartit/rest/bulkupdate/relations',
method: 'POST',
isArray: true
},
bulkUpdateRelateAsset: {
url: '/smartit/rest/v2/action/bulk/execute',
method: 'POST',
isArray: false
},
searchInventory: {
url: '/smartit/rest/inventory/search',
method: 'POST',
isArray: true
},
putIntoInventory: {
url: '/smartit/rest/inventory/asset/:assetId/:assetClassId',
method: 'POST',
isArray: true
},
getBcmFilteredActions: {
url: '/smartit/rest/bcm/device/:deviceId/filteredactions',
method: 'GET',
isArray: false
},
getBcmAssetGeneralData: {
url: '/smartit/rest/bcm/device/:deviceId/summary',
method: 'GET',
isArray: false
},
getBcmAssetHardwareData: {
url: '/smartit/rest/bcm/device/:deviceId/inventory/hardware/summary',
method: 'GET',
isArray: false
},
getBcmAssetSoftwareData: {
url: '/smartit/rest/bcm/device/:deviceId/inventory/software/summary?offset=:offset',
method: 'GET',
isArray: false
},
getBcmAssetFinancialData: {
url: '/smartit/rest/bcm/device/:deviceId/financial/summary',
method: 'GET',
isArray: false
},
getBcmAssetSecurityData: {
url: '/smartit/rest/bcm/device/:deviceId/inventory/security/summary',
method: 'GET',
isArray: false
},
getBcmAssetActionHistory: {
url: '/smartit/rest/bcm/device/:deviceId/oprule/history?offset=:offset',
method: 'GET',
isArray: false
},
performAuditFromBCM: {
url: '/smartit/rest/bcm/device/:deviceId/audit?reconId=:reconId',
method: 'POST',
isArray: false
},
shutdownDeviceFromBCM: {
url: '/smartit/rest/bcm/device/:deviceId/shutdown?reconId=:reconId',
method: 'POST',
isArray: false
},
pingDeviceFromBCM: {
url: '/smartit/rest/bcm/device/:deviceId/ping?reconId=:reconId',
method: 'POST',
isArray: false
},
rebootDeviceFromBCM: {
url: '/smartit/rest/bcm/device/:deviceId/reboot?reconId=:reconId',
method: 'POST',
isArray: false
},
wakeupDeviceFromBCM: {
url: '/smartit/rest/bcm/device/:deviceId/wakeup?reconId=:reconId',
method: 'POST',
isArray: false
}
}), assetResource = $resource('/smartit/rest/asset/search', {}, {
GetListOfAssets: { method: 'GET', isArray: true },
getListOfAssetsWithFilter: {
method: 'POST',
url: '/smartit/rest/asset/search/filter',
isArray: true
}
});
this.getAssetDetailsByID = function (assetId, assetClassId) {
return resource.getAssetDetailsByID({
reconciliationId: assetId,
assetClassId: assetClassId
}).$promise.then(function (result) {
return processAssetDetails(result[0].items[0]);
});
};
this.update = function (reconciliationId, instanceId, assetClassId, data) {
data.classId = assetClassId;
data.instanceId = instanceId;
return resource.update({ reconciliationId: reconciliationId }, data).$promise;
};
this.getListOfAssets = function (term) {
return assetResource.GetListOfAssets({ searchParam: term }).$promise;
};
this.getListOfAssetsForCompany = function (term, companyName, ticketType, customerId) {
var getListParams = { searchParam: term, company: { name: companyName } };
if (ticketType === EntityVO.TYPE_INCIDENT) {
getListParams.customerId = customerId;
getListParams.ticket = ticketType;
getListParams.affected = true;
}
if (ticketType === EntityVO.TYPE_PROBLEM || ticketType === EntityVO.TYPE_KNOWNERROR) {
getListParams.affected = true;
}
return assetResource.GetListOfAssets(getListParams).$promise;
};
this.getListOfAssetsByType = function (term, assetType) {
return assetResource.GetListOfAssets({ searchParam: term, assetType: assetType }).$promise;
};
this.getListOfAssetsByTypeForCompany = function (term, assetType, companyName, ticketType, customerId) {
var getListParams = {
searchParam: term,
assetType: assetType,
company: { name: companyName }
};
if (ticketType === EntityVO.TYPE_INCIDENT) {
getListParams.customerId = customerId;
getListParams.ticket = ticketType;
getListParams.affected = true;
}
return assetResource.GetListOfAssets(getListParams).$promise;
};
this.getListOfAssetsWithFilter = function (filterCriteria) {
return assetResource.getListOfAssetsWithFilter(filterCriteria).$promise;
};
this.removePeople = function (person) {
return $http({
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
url: '/smartit/rest/relations/asset/' + person.parentId,
data: [person]
});
};
this.getAssetOwnerDetails = function (id) {
return resource.getAssetOwnerDetails({ id: id }).$promise.then(function (response) {
return processAssetOwnerDetails(response[0].items[0] || []);
});
};
this.followAsset = function (userLoginId, assetId) {
userLoginId = encodeURIComponent(userLoginId);
return $http({
method: 'POST',
headers: { 'Content-Type': 'application/json' },
url: '/smartit/rest/v2/profile/user/' + userLoginId + '/following?follow_type=asset&following_id=' + assetId
});
};
this.unfollowAsset = function (userLoginId, assetId) {
userLoginId = encodeURIComponent(userLoginId);
return $http({
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
url: '/smartit/rest/v2/profile/user/' + userLoginId + '/following?follow_type=asset&following_id=' + assetId
});
};
this.searchInventory = function (data) {
return resource.searchInventory(data).$promise;
};
this.putIntoInventory = function (assetId, assetClassId, params) {
return resource.putIntoInventory({ assetId: assetId, assetClassId: assetClassId }, params).$promise;
};
this.saveCIPresetConfiguration = function (name, filterCriteria) {
var params = {
name: name,
criteria: filterCriteria
};
return resource.saveCIPresetConfiguration({ clientType: 'SHARED' }, params).$promise;
};
this.removeCIPresetConfiguration = function (name) {
return resource.removeCIPresetConfiguration({ name: name }).$promise;
};
this.getRelationshipTypes = function (data) {
return resource.getRelationshipTypes(data).$promise;
};
this.addBulkRelations = function (data) {
return resource.addBulkRelations(data).$promise;
};
this.bulkUpdateRelateAsset = function (data) {
return resource.bulkUpdateRelateAsset(data).$promise;
};
this.getBcmFilteredActions = function (deviceId) {
return resource.getBcmFilteredActions({
deviceId: deviceId
}).$promise.then(function (result) {
return processBcmData(result, 'filterActions');
});
};
this.getBcmAssetGeneralData = function (deviceId) {
return resource.getBcmAssetHardwareData({
deviceId: deviceId
}).$promise.then(function (result) {
return processBcmData(result, 'general');
});
};
this.getBcmAssetHardwareData = function (deviceId) {
return resource.getBcmAssetHardwareData({
deviceId: deviceId
}).$promise.then(function (result) {
return processBcmData(result, 'hardware');
});
};
this.getBcmAssetSoftwareData = function (deviceId, offset) {
return resource.getBcmAssetSoftwareData({
deviceId: deviceId,
offset: offset
}).$promise.then(function (result) {
return processBcmData(result, 'software');
});
};
this.getBcmAssetFinancialData = function (deviceId) {
return resource.getBcmAssetFinancialData({
deviceId: deviceId
}).$promise.then(function (result) {
return processBcmData(result, 'financial');
});
};
this.getBcmAssetSecurityData = function (deviceId) {
return resource.getBcmAssetSecurityData({
deviceId: deviceId
}).$promise.then(function (result) {
return processBcmData(result, 'security');
});
};
this.getBcmAssetActionHistory = function (deviceId, offset) {
return resource.getBcmAssetActionHistory({
deviceId: deviceId,
offset: offset
}).$promise.then(function (result) {
return sanitizeBcmActionData(result);
});
};
this.performAuditFromBCM = function (deviceId, reconId) {
return resource.performAuditFromBCM({
deviceId: deviceId,
reconId: reconId
}, {}).$promise;
};
this.pingDeviceFromBCM = function (deviceId, reconId) {
return resource.pingDeviceFromBCM({
deviceId: deviceId,
reconId: reconId
}, {}).$promise;
};
this.rebootDeviceFromBCM = function (deviceId, reconId) {
return resource.rebootDeviceFromBCM({
deviceId: deviceId,
reconId: reconId
}, {}).$promise;
};
this.wakeupDeviceFromBCM = function (deviceId, reconId) {
return resource.wakeupDeviceFromBCM({
deviceId: deviceId,
reconId: reconId
}, {}).$promise;
};
this.shutdownDeviceFromBCM = function (deviceId, reconId) {
return resource.shutdownDeviceFromBCM({
deviceId: deviceId,
reconId: reconId
}, {}).$promise;
};
/**
* Private functions
*/
function sanitizeBcmActionData(result) {
var data = [], keywordTranslation = {}, res;
if (result.Total) {
var objects = _.find(result.struct.member, { "name": "Values" });
var kObj = _.find(result.struct.member, { "name": "KeywordTranslation" });
var kItem = kObj.value.struct.member;
_.each(kItem, function (sub) {
keywordTranslation[sub.name] = sub.value.i4 || sub.value.string;
});
if (result.Total === 1) {
objects.value.array.data.value = [objects.value.array.data.value];
}
_.each(objects.value.array.data.value, function (item) {
var temp = {};
_.each(item.struct.member, function (sub) {
if (sub.name !== "ObjectId") {
var tKey, tVal;
tKey = keywordTranslation[sub.name] || sub.name;
tVal = sub.value.i4 || sub.value.string;
temp[tKey] = keywordTranslation[tVal] || tVal;
if (sub.name === "_DB_ATTR_LASTSTATUSUPDATE_") {
temp[tKey] = $filter("date")(temp[tKey], 'medium');
}
}
});
data.push({ "fields": temp });
});
}
res = {
"aObjects": data,
"total": result.Total
};
return res;
}
function processBcmData(result, type) {
var k, item, data = {}, component = $filter('i18n')('asset.bcm.component'), details = $filter('i18n')('asset.bcm.details');
if (!result.ErrorCode) {
delete result.ErrorCode;
}
delete result.$promise;
delete result.$resolved;
if (type === 'general' || type === 'financial') {
if (type === 'general') {
data = {
'hardware': angular.copy(result)
};
result = result.Device;
result.KeywordTranslation = data.hardware.KeywordTranslation;
delete data.hardware.Device;
}
var objects = [], dateFields = ["ServiceStartDate", "SupportExpirationDate", "WarrantyExpirationDate", "LastUpdate"];
for (k in result) {
if (result.hasOwnProperty(k) && k !== 'KeywordTranslation') {
item = {};
item[component] = result.KeywordTranslation[k] || k;
item[details] = result.KeywordTranslation[result[k]] || result[k];
if (dateFields.indexOf(k) !== -1) {
item[details] = $filter("date")(item[details], 'medium');
}
objects.push({ "fields": item });
}
}
if (type === 'general') {
data.general = objects;
}
else {
data = objects;
}
}
else if (type === 'software') {
var sObjects = [];
_.each(result._DB_OBJECTTYPCLASS_SOFTWAREINVENTORY_, function (item) {
var sItem = {}, tKey, tVal;
for (k in item) {
tKey = result.KeywordTranslation[k] || k;
tVal = result.KeywordTranslation[item[k]] || item[k];
sItem[tKey] = tVal;
}
sObjects.push({ "fields": sItem });
});
data = {
"sObjects": sObjects,
"total": result.Total
};
}
else {
data = result;
}
return data;
}
/**
* Process raw data from server and transform it to VO.
*
* @param data
* @return {AssetVO}
*/
function processAssetDetails(data) {
return new AssetVO().build(data);
}
function processAssetOwnerDetails(data) {
return new PersonVO().build(data);
}
}]);
}());