Passed
Push — master ( a5deca...eaab07 )
by
unknown
11:30 queued 14s
created

myems-admin/app/controllers/settings/hybridpowerstation/hybridpowerstation.controller.js   C

Complexity

Total Complexity 53
Complexity/F 1.66

Size

Lines of Code 299
Function Count 32

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 53
eloc 216
dl 0
loc 299
rs 6.96
c 0
b 0
f 0
mnd 21
bc 21
fnc 32
bpm 0.6562
cpm 1.6562
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/hybridpowerstation/hybridpowerstation.controller.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
app.controller('HybridPowerStationController', function(
4
    $scope,
5
    $rootScope,
6
    $window,
7
    $translate,
8
    $uibModal,
9
    CostCenterService,
10
    ContactService,
11
    SVGService,
12
	PointService,
13
    HybridPowerStationService,
14
    toaster,
15
    SweetAlert) {
16
		$scope.svgs = [];
17
		$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
18
		$scope.getAllCostCenters = function() {
19
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
20
			CostCenterService.getAllCostCenters(headers, function (response) {
21
				if (angular.isDefined(response.status) && response.status === 200) {
22
					$scope.costcenters = response.data;
23
				} else {
24
					$scope.costcenters = [];
25
				}
26
			});
27
		};
28
29
	$scope.getAllContacts = function() {
30
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
31
		ContactService.getAllContacts(headers, function (response) {
32
			if (angular.isDefined(response.status) && response.status === 200) {
33
				$scope.contacts = response.data;
34
			} else {
35
				$scope.contacts = [];
36
			}
37
		});
38
	};
39
40
	$scope.getAllPoints = function() {
41
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
42
		PointService.getAllPoints(headers, function (response) {
43
			if (angular.isDefined(response.status) && response.status === 200) {
44
				$scope.points = response.data;
45
			} else {
46
				$scope.points = [];
47
			}
48
		});
49
	};
50
51
	$scope.getAllSVGs = function() {
52
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token, "Quickmode": 'true'  };
53
		SVGService.getAllSVGs(headers, function (response) {
54
			if (angular.isDefined(response.status) && response.status === 200) {
55
				$scope.svgs = response.data;
56
			} else {
57
				$scope.svgs = [];
58
			}
59
		});
60
	};
61
62
	$scope.getAllPhaseOfLifecycles = function() {
63
		$scope.phaseoflifecycles = [
64
			{"code":"1use", "name": $translate.instant("HYBRID_POWER_STATION.PHASE_1USE")},
65
			{"code":"2commissioning", "name": $translate.instant("HYBRID_POWER_STATION.PHASE_2COMMISSIONING")},
66
			{"code":"3installation", "name": $translate.instant("HYBRID_POWER_STATION.PHASE_3INSTALLATION")}
67
		];
68
	};
69
70
	$scope.getAllHybridPowerStations = function() {
71
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
72
		HybridPowerStationService.getAllHybridPowerStations(headers, function (response) {
73
			if (angular.isDefined(response.status) && response.status === 200) {
74
				$scope.hybridpowerstations = response.data;
75
			} else {
76
				$scope.hybridpowerstations = [];
77
			}
78
		});
79
	};
80
81
	$scope.addHybridPowerStation = function() {
82
		var modalInstance = $uibModal.open({
83
			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstation.model.html',
84
			controller: 'ModalAddHybridPowerStationCtrl',
85
			windowClass: "animated fadeIn",
86
			resolve: {
87
				params: function() {
88
					return {
89
						costcenters: angular.copy($scope.costcenters),
90
						contacts: angular.copy($scope.contacts),
91
						svgs: angular.copy($scope.svgs),
92
						points: angular.copy($scope.points),
93
						phaseoflifecycles: angular.copy($scope.phaseoflifecycles)
94
					};
95
				}
96
			}
97
		});
98
		modalInstance.result.then(function(hybridpowerstation) {
99
			hybridpowerstation.cost_center_id = hybridpowerstation.cost_center.id;
100
			hybridpowerstation.contact_id = hybridpowerstation.contact.id;
101
			hybridpowerstation.svg_id = hybridpowerstation.svg.id;
102
			if (hybridpowerstation.svg2 != null && hybridpowerstation.svg2.id != null) {
103
				hybridpowerstation.svg2_id = hybridpowerstation.svg2.id;
104
			}
105
			if (hybridpowerstation.svg3 != null && hybridpowerstation.svg3.id != null) {
106
				hybridpowerstation.svg3_id = hybridpowerstation.svg3.id;
107
			}
108
			if (hybridpowerstation.svg4 != null && hybridpowerstation.svg4.id != null) {
109
				hybridpowerstation.svg4_id = hybridpowerstation.svg4.id;
110
			}
111
			if (hybridpowerstation.svg5 != null && hybridpowerstation.svg5.id != null) {
112
				hybridpowerstation.svg5_id = hybridpowerstation.svg5.id;
113
			}
114
			if (hybridpowerstation.longitude_point != null && hybridpowerstation.longitude_point.id != null ) {
115
				hybridpowerstation.longitude_point_id = hybridpowerstation.longitude_point.id;
116
			} else {
117
				hybridpowerstation.longitude_point_id = undefined;
118
			}
119
			if (hybridpowerstation.latitude_point != null && hybridpowerstation.latitude_point.id != null ) {
120
				hybridpowerstation.latitude_point_id = hybridpowerstation.latitude_point.id;
121
			} else {
122
				hybridpowerstation.latitude_point_id = undefined;
123
			}
124
125
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
126
			HybridPowerStationService.addHybridPowerStation(hybridpowerstation, headers, function(response) {
127
				if (angular.isDefined(response.status) && response.status === 201) {
128
					toaster.pop({
129
						type: "success",
130
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
131
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
132
						showCloseButton: true,
133
					});
134
					$scope.$emit('handleEmitHybridPowerStationChanged');
135
				} else {
136
					toaster.pop({
137
						type: "error",
138
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.HYBRID_POWER_STATION") }),
139
						body: $translate.instant(response.data.description),
140
						showCloseButton: true,
141
					});
142
				}
143
			});
144
		}, function() {
145
146
		});
147
		$rootScope.modalInstance = modalInstance;
148
	};
149
150
	$scope.editHybridPowerStation = function(hybridpowerstation) {
151
		var modalInstance = $uibModal.open({
152
			windowClass: "animated fadeIn",
153
			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstation.model.html',
154
			controller: 'ModalEditHybridPowerStationCtrl',
155
			resolve: {
156
				params: function() {
157
					return {
158
						hybridpowerstation: angular.copy(hybridpowerstation),
159
						costcenters:angular.copy($scope.costcenters),
160
						contacts:angular.copy($scope.contacts),
161
						svgs:angular.copy($scope.svgs),
162
						points:angular.copy($scope.points),
163
						phaseoflifecycles: angular.copy($scope.phaseoflifecycles)
164
					};
165
				}
166
			}
167
		});
168
169
		modalInstance.result.then(function(modifiedHybridPowerStation) {
170
			modifiedHybridPowerStation.cost_center_id=modifiedHybridPowerStation.cost_center.id;
171
			modifiedHybridPowerStation.contact_id=modifiedHybridPowerStation.contact.id;
172
			modifiedHybridPowerStation.svg_id=modifiedHybridPowerStation.svg.id;
173
			if (modifiedHybridPowerStation.longitude_point != null && modifiedHybridPowerStation.longitude_point.id != null ) {
174
				modifiedHybridPowerStation.longitude_point_id = modifiedHybridPowerStation.longitude_point.id;
175
			} else {
176
				modifiedHybridPowerStation.longitude_point_id = undefined;
177
			}
178
			if (modifiedHybridPowerStation.latitude_point != null && modifiedHybridPowerStation.latitude_point.id != null ) {
179
				modifiedHybridPowerStation.latitude_point_id = modifiedHybridPowerStation.latitude_point.id;
180
			} else {
181
				modifiedHybridPowerStation.latitude_point_id = undefined;
182
			}
183
			if (modifiedHybridPowerStation.svg2 != null && modifiedHybridPowerStation.svg2.id != null) {
184
				modifiedHybridPowerStation.svg2_id = modifiedHybridPowerStation.svg2.id;
185
			}
186
			if (modifiedHybridPowerStation.svg3 != null && modifiedHybridPowerStation.svg3.id != null) {
187
				modifiedHybridPowerStation.svg3_id = modifiedHybridPowerStation.svg3.id;
188
			}
189
			if (modifiedHybridPowerStation.svg4 != null && modifiedHybridPowerStation.svg4.id != null) {
190
				modifiedHybridPowerStation.svg4_id = modifiedHybridPowerStation.svg4.id;
191
			}
192
			if (modifiedHybridPowerStation.svg5 != null && modifiedHybridPowerStation.svg5.id != null) {
193
				modifiedHybridPowerStation.svg5_id = modifiedHybridPowerStation.svg5.id;
194
			}
195
196
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
197
			HybridPowerStationService.editHybridPowerStation(modifiedHybridPowerStation, headers, function(response) {
198
				if (angular.isDefined(response.status) && response.status === 200) {
199
					toaster.pop({
200
						type: "success",
201
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
202
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
203
						showCloseButton: true,
204
					});
205
					$scope.$emit('handleEmitHybridPowerStationChanged');
206
				} else {
207
					toaster.pop({
208
						type: "error",
209
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
210
						body: $translate.instant(response.data.description),
211
						showCloseButton: true,
212
					});
213
				}
214
			});
215
		}, function() {
216
			//do nothing;
217
		});
218
		$rootScope.modalInstance = modalInstance;
219
	};
220
221
	$scope.deleteHybridPowerStation=function(hybridpowerstation){
222
		SweetAlert.swal({
223
		        title: $translate.instant("SWEET.TITLE"),
224
		        text: $translate.instant("SWEET.TEXT"),
225
		        type: "warning",
226
		        showCancelButton: true,
227
		        confirmButtonColor: "#DD6B55",
228
		        confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
229
		        cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
230
		        closeOnConfirm: true,
231
		        closeOnCancel: true },
232
		    function (isConfirm) {
233
		        if (isConfirm) {
234
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
235
		            HybridPowerStationService.deleteHybridPowerStation(hybridpowerstation, headers, function(response) {
236
		            	if (angular.isDefined(response.status) && response.status === 204) {
237
							toaster.pop({
238
								type: "success",
239
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
240
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
241
								showCloseButton: true,
242
							});
243
							$scope.$emit('handleEmitHybridPowerStationChanged');
244
						}else {
245
							toaster.pop({
246
								type: "error",
247
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
248
								body: $translate.instant(response.data.description),
249
								showCloseButton: true,
250
							});
251
		            	}
252
		            });
253
		        }
254
		    });
255
	};
256
	$scope.getAllHybridPowerStations();
257
	$scope.getAllCostCenters();
258
	$scope.getAllContacts();
259
	$scope.getAllSVGs();
260
	$scope.getAllPoints();
261
	$scope.getAllPhaseOfLifecycles();
262
	$scope.$on('handleBroadcastHybridPowerStationChanged', function(event) {
263
  		$scope.getAllHybridPowerStations();
264
	});
265
});
266
267
app.controller('ModalAddHybridPowerStationCtrl', function($scope, $uibModalInstance,params) {
268
269
	$scope.operation = "SETTING.ADD_HYBRID_POWER_STATION";
270
	$scope.costcenters=params.costcenters;
271
	$scope.contacts=params.contacts;
272
	$scope.svgs=params.svgs;
273
	$scope.points=params.points;
274
	$scope.phaseoflifecycles=params.phaseoflifecycles;
275
	$scope.ok = function() {
276
		$uibModalInstance.close($scope.hybridpowerstation);
277
	};
278
279
    $scope.cancel = function() {
280
		$uibModalInstance.dismiss('cancel');
281
	};
282
});
283
284
app.controller('ModalEditHybridPowerStationCtrl', function($scope, $uibModalInstance, params) {
285
	$scope.operation = "SETTING.EDIT_HYBRID_POWER_STATION";
286
	$scope.hybridpowerstation = params.hybridpowerstation;
287
	$scope.costcenters=params.costcenters;
288
	$scope.contacts=params.contacts;
289
	$scope.svgs=params.svgs;
290
	$scope.points=params.points;
291
	$scope.phaseoflifecycles=params.phaseoflifecycles;
292
	$scope.ok = function() {
293
		$uibModalInstance.close($scope.hybridpowerstation);
294
	};
295
296
	$scope.cancel = function() {
297
		$uibModalInstance.dismiss('cancel');
298
	};
299
});
300