Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
|#756|+Sriram| Override Isa api for facility program product. Reverti…
Browse files Browse the repository at this point in the history
…ng to originial value on cancel of modal. Saving allocation program products only on success of facility save
  • Loading branch information
mananbharara committed Jun 19, 2013
1 parent 3d5e03e commit bddb59a
Show file tree
Hide file tree
Showing 10 changed files with 540 additions and 467 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,40 @@ public class AllocationProgramProductController extends BaseController {

public static final String PROGRAM_PRODUCT_LIST = "programProductList";

@RequestMapping(value = "/programProducts/programId/{programId}", method = GET, headers = BaseController.ACCEPT_JSON)
@RequestMapping(value = "/programProducts/programId/{programId}", method = GET, headers = ACCEPT_JSON)
@PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_PROGRAM_PRODUCT')")
public ResponseEntity<AllocationResponse> getProgramProductsByProgram(@PathVariable Long programId) {
List<AllocationProgramProduct> programProductsByProgram = service.get(programId);
return AllocationResponse.response(PROGRAM_PRODUCT_LIST, programProductsByProgram);
}

@RequestMapping(value = "/programProducts/{programProductId}/isa", method = POST, headers = BaseController.ACCEPT_JSON)
@RequestMapping(value = "/facility/{facilityId}/program/{programId}/isa", method = GET, headers = ACCEPT_JSON)
@PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_PROGRAM_PRODUCT')")
public ResponseEntity<AllocationResponse> getProgramProductsByProgramAndFacility(@PathVariable Long programId, @PathVariable Long facilityId) {
List<AllocationProgramProduct> programProductsByProgram = service.getForProgramAndFacility(programId, facilityId);
return AllocationResponse.response(PROGRAM_PRODUCT_LIST, programProductsByProgram);
}

@RequestMapping(value = "/programProducts/{programProductId}/isa", method = POST, headers = ACCEPT_JSON)
@PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_PROGRAM_PRODUCT')")
public void insertIsa(@PathVariable Long programProductId, @RequestBody ProgramProductISA programProductISA) {
programProductISA.setProgramProductId(programProductId);
service.insertISA(programProductISA);
}


@RequestMapping(value = "/programProducts/{programProductId}/isa/{isaId}", method = PUT, headers = BaseController.ACCEPT_JSON)
@RequestMapping(value = "/programProducts/{programProductId}/isa/{isaId}", method = PUT, headers = ACCEPT_JSON)
@PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_PROGRAM_PRODUCT')")
public void updateIsa(@PathVariable Long isaId,@PathVariable Long programProductId, @RequestBody ProgramProductISA programProductISA) {
programProductISA.setId(isaId);
programProductISA.setProgramProductId(programProductId);
service.updateISA(programProductISA);
}

@RequestMapping(value = "/facility/{facilityId}/programProduct/{programProductId}/isa", method = POST, headers = BaseController.ACCEPT_JSON)
@RequestMapping(value = "/facility/{facilityId}/program/{programId}/isa", method = POST, headers = ACCEPT_JSON)
@PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_FACILITY')")
public void overrideIsa(@PathVariable Long facilityId, @PathVariable Long programProductId, @RequestBody AllocationProgramProductList products) {
service.saveOverriddenIsa(facilityId, programProductId, products);
public void overrideIsa(@PathVariable Long facilityId, @RequestBody AllocationProgramProductList products) {
service.saveOverriddenIsa(facilityId, products);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public AllocationProgramProduct getByProgramProductId(Long programProductId) {
ProgramProductISA isa = isaMapper.getIsa(programProductId);
AllocationProgramProduct allocationProgramProduct = new AllocationProgramProduct();
allocationProgramProduct.setProgramProductIsa(isa);
allocationProgramProduct.setProgramProductId(programProductId);
return allocationProgramProduct;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ public void updateISA(ProgramProductISA isa) {
repository.updateISA(isa);
}

public void saveOverriddenIsa(final Long facilityId, final Long programProductId, AllocationProgramProductList products) {
public void saveOverriddenIsa(final Long facilityId, AllocationProgramProductList products) {
forAllDo(products, new Closure() {
@Override
public void execute(Object o) {
AllocationProgramProduct product = (AllocationProgramProduct) o;
product.setFacilityId(facilityId);
product.setProgramProductId(programProductId);
repository.save(product);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void shouldGetAllocationProgramProductWithIsa() throws Exception {
AllocationProgramProduct allocationProgramProduct = repository.getByProgramProductId(1L);

assertThat(allocationProgramProduct.getProgramProductIsa(), is(expectedIsa));
assertThat(allocationProgramProduct.getProgramProductId(), is(1l));
verify(isaMapper).getIsa(1L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void shouldSaveAllocationProgramProductList() throws Exception {
add(allocationProduct2);
}};

service.saveOverriddenIsa(1l, 2l, allocationProgramProducts);
service.saveOverriddenIsa(1l, allocationProgramProducts);

verify(repository).save(allocationProduct1);
verify(repository).save(allocationProduct2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ error.geo.level.invalid = Invalid Geographic Level Code
error.geo.zone.parent.invalid = Invalid Geographic Zone Parent Code
error.facility.program.mapping.exists = Facility has already been mapped to the program
error.facility.requisition.group.mapping.exists = Facility to Requisition Group mapping already exists
error.facility.allocation.product.save = Error saving facility allocation products

title.manage.users = Manage Users - OpenLMIS
user.data.length.incorrect = Incorrect data type or length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

function FacilityController($scope, facilityReferenceData, $routeParams, facility, Facility, RestoreFacility, $location, ProgramProducts, $dialog, messageService) {
function FacilityController($scope, facilityReferenceData, $routeParams, facility, Facility, RestoreFacility, $location, FacilityProgramProducts, $q, $dialog, messageService) {
$scope.message = "";
initialize();

Expand All @@ -25,7 +25,7 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
updateProgramsToDisplay();
$scope.facility.dataReportable = "true";
}
$scope.allocationProgramProducts = [];
$scope.allocationProgramProductsList = [];
}

function getFacilityWithDateObjects(facility) {
Expand All @@ -48,6 +48,27 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
$scope.programProductsISAModal = true;
};

function saveAllocationProgramProducts() {
var defers = [];

var keys = _.keys($scope.allocationProgramProductsList);

$(keys).each(function(index, key) {
defers.push($q.defer());

var program = $scope.allocationProgramProductsList[key][0].program;

FacilityProgramProducts.post({facilityId: $routeParams.facilityId, programId: program.id}, $scope.allocationProgramProductsList[key], function (data) {
defers[index].resolve();
}, function () {
defers[index].reject("error.facility.allocation.product.save");
});

})

return defers;
}

$scope.saveFacility = function () {
if ($scope.facilityForm.$error.pattern || $scope.facilityForm.$error.required) {
$scope.showError = "true";
Expand All @@ -56,14 +77,23 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
return;
}

var successFn = function (data) {
$scope.showError = "true";
$scope.error = "";
$scope.$parent.message = data.success;
$scope.facility = getFacilityWithDateObjects(data.facility);
$scope.$parent.facilityId = $scope.facility.id;
populateFlags($scope);
$location.path('');
var facilitySaveCallback = function (data) {

var promises = saveAllocationProgramProducts();

$q.all(promises).then(function () {
$scope.showError = "true";
$scope.error = "";
$scope.$parent.message = data.success;
$scope.facility = getFacilityWithDateObjects(data.facility);
$scope.$parent.facilityId = $scope.facility.id;
populateFlags($scope);
$location.path('');
}, function(error) {
$scope.showError = "true";
$scope.message = "";
$scope.error = error;
})
};

var errorFn = function (data) {
Expand All @@ -73,9 +103,9 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
};

if (!$scope.isEdit) {
Facility.save({}, $scope.facility, successFn, errorFn);
Facility.save({}, $scope.facility, facilitySaveCallback, errorFn);
} else {
Facility.update({id:$scope.facility.id}, $scope.facility, successFn, errorFn);
Facility.update({id: $scope.facility.id}, $scope.facility, facilitySaveCallback, errorFn);
}
};

Expand All @@ -98,9 +128,9 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
$scope.showConfirmDateChangeWindow = function (program) {
window.program = program;
var dialogOpts = {
id:"dateChangeConfirmModal",
header:messageService.get('message.setProgramStartDate'),
body:messageService.get('message.dateChangeConfirmMessage')
id: "dateChangeConfirmModal",
header: messageService.get('message.setProgramStartDate'),
body: messageService.get('message.dateChangeConfirmMessage')
};
OpenLmisDialog.newDialog(dialogOpts, $scope.dateChangeCallback, $dialog, messageService);
};
Expand All @@ -121,7 +151,7 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit


function getProgramById(id) {
return (_.findWhere($scope.programs, {'id':id}));
return (_.findWhere($scope.programs, {'id': id}));
}

var successFunc = function (data) {
Expand All @@ -143,28 +173,28 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit
$scope.restoreFacility = function (active) {
$scope.activeConfirmModal = false;
$scope.facility.active = active;
RestoreFacility.update({id:$scope.facility.id, active:active}, {}, successFunc, errorFunc);
RestoreFacility.update({id: $scope.facility.id, active: active}, {}, successFunc, errorFunc);
};

$scope.deleteFacilityCallBack = function (result) {
if (!result) return;
Facility.remove({id:$scope.facility.id}, {}, successFunc, errorFunc);
Facility.remove({id: $scope.facility.id}, {}, successFunc, errorFunc);
};

$scope.showConfirmFacilityDeleteWindow = function () {
var dialogOpts = {
id:"deleteFacilityDialog",
header:messageService.get('delete.facility.header'),
body:messageService.get('delete.facility.confirm', $scope.originalFacilityName, $scope.originalFacilityCode)
id: "deleteFacilityDialog",
header: messageService.get('delete.facility.header'),
body: messageService.get('delete.facility.confirm', $scope.originalFacilityName, $scope.originalFacilityCode)
};
OpenLmisDialog.newDialog(dialogOpts, $scope.deleteFacilityCallBack, $dialog, messageService);
};

$scope.showConfirmFacilityRestore = function () {
var dialogOpts = {
id:"restoreConfirmModal",
header:messageService.get("create.facility.restoreFacility"),
body:"'{0}' / '{1}' will be restored to the system.".format($scope.originalFacilityName, $scope.originalFacilityCode)
id: "restoreConfirmModal",
header: messageService.get("create.facility.restoreFacility"),
body: "'{0}' / '{1}' will be restored to the system.".format($scope.originalFacilityName, $scope.originalFacilityCode)
};
OpenLmisDialog.newDialog(dialogOpts, $scope.restoreFacilityCallBack, $dialog, messageService);
};
Expand All @@ -176,9 +206,9 @@ function FacilityController($scope, facilityReferenceData, $routeParams, facilit

$scope.showConfirmFacilityActivate = function () {
var dialogOpts = {
id:"activeConfirmModel",
header:messageService.get("create.facility.activateFacility"),
body:messageService.get("create.facility.setFacilityActive")
id: "activeConfirmModel",
header: messageService.get("create.facility.activateFacility"),
body: messageService.get("create.facility.setFacilityActive")
};
OpenLmisDialog.newDialog(dialogOpts, $scope.activateFacilityCallBack, $dialog, messageService);
};
Expand Down Expand Up @@ -207,7 +237,7 @@ var populateFlags = function ($scope) {

FacilityController.resolve = {

facilityReferenceData:function ($q, $timeout, FacilityReferenceData) {
facilityReferenceData: function ($q, $timeout, FacilityReferenceData) {
var deferred = $q.defer();
$timeout(function () {
FacilityReferenceData.get({}, function (data) {
Expand All @@ -217,14 +247,14 @@ FacilityController.resolve = {
return deferred.promise;
},

facility:function ($q, $timeout, Facility, $route) {
facility: function ($q, $timeout, Facility, $route) {
if ($route.current.params.facilityId == undefined) return undefined;

var deferred = $q.defer();
var facilityId = $route.current.params.facilityId;

$timeout(function () {
Facility.get({id:facilityId}, function (data) {
Facility.get({id: facilityId}, function (data) {
deferred.resolve(data.facility);
}, {});
}, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,49 @@
*
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
function IsaModalController($scope, ProgramProducts) {
function IsaModalController($scope, FacilityProgramProducts, $routeParams) {

$scope.$watch('$parent.programProductsISAModal', function () {
if (!$scope.$parent.programProductsISAModal) return;

$scope.$watch('currentProgram', function () {
if (!$scope.currentProgram) return;

if ($scope.allocationProgramProducts[$scope.currentProgram.id]) return;
$scope.currentProgramProducts = [];

if ($scope.$parent.allocationProgramProductsList[$scope.currentProgram.id]) {
$scope.currentProgramProducts = angular.copy($scope.$parent.allocationProgramProductsList[$scope.currentProgram.id]);
return;
}

ProgramProducts.get({programId: $scope.currentProgram.id}, function (data) {
$scope.allocationProgramProducts[$scope.currentProgram.id] = data.programProductList;
FacilityProgramProducts.get({programId: $scope.currentProgram.id, facilityId: $routeParams.facilityId}, function (data) {

var population = $scope.$parent.facility.catchmentPopulation;
$scope.$parent.allocationProgramProductsList[$scope.currentProgram.id] = angular.copy(data.programProductList);

if (isUndefined(population)) return;
$($scope.$parent.allocationProgramProductsList[$scope.currentProgram.id]).each(function (index, product) {

$($scope.allocationProgramProducts[$scope.currentProgram.id]).each(function (index, product) {
var population = $scope.$parent.facility.catchmentPopulation;

if (isUndefined(product.programProductIsa))
if (isUndefined(population) || isUndefined(product.programProductIsa)) return;

product.calculatedIsa = Math.ceil(parseInt(population) * parseInt(product.programProductIsa.whoRatio) * parseInt(product.programProductIsa.dosesPerYear) *
parseInt(product.programProductIsa.wastageRate) / 12 * parseInt(product.programProductIsa.bufferPercentage) + parseInt(product.programProductIsa.adjustmentValue));
product.calculatedIsa = Math.ceil(utils.parseIntWithBaseTen(population) * utils.parseIntWithBaseTen(product.programProductIsa.whoRatio) *
utils.parseIntWithBaseTen(product.programProductIsa.dosesPerYear) * utils.parseIntWithBaseTen(product.programProductIsa.wastageRate) / 12 *
utils.parseIntWithBaseTen(product.programProductIsa.bufferPercentage) + utils.parseIntWithBaseTen(product.programProductIsa.adjustmentValue));

});

$scope.currentProgramProducts = angular.copy($scope.$parent.allocationProgramProductsList[$scope.currentProgram.id]);

}, function (data) {
});
});

$scope.saveISA = function () {
$scope.updateISA = function () {
$scope.$parent.allocationProgramProductsList[$scope.currentProgram.id] = angular.copy($scope.currentProgramProducts);
$scope.$parent.programProductsISAModal = false;
}

$scope.resetISAModal = function () {
$scope.$parent.programProductsISAModal = false;
}

}
Loading

0 comments on commit bddb59a

Please sign in to comment.