diff --git a/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy b/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy index f7daf78..835d800 100644 --- a/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy +++ b/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy @@ -83,6 +83,7 @@ class PredictedPieceSetController extends OkapiTenantAwareController } def activateRuleset() { - String serialRulesetId = params.get("serialRulesetId") + String serialRulesetId = params.get("serialRulesetId") SerialRuleset ruleset = SerialRuleset.findById(serialRulesetId) String activeRulesetId = serialRulesetService.findActive(ruleset?.owner?.id) @@ -51,18 +56,93 @@ class SerialRulesetController extends OkapiTenantAwareController respond result } - - - def save() { - def data = getObjectToBind() - SerialRuleset ruleset = new SerialRuleset(data) + //Check to see if new ruleset is active, if it is, additionally set the current active ruleset to deprecated + private void activeRulesetCheck(SerialRuleset ruleset) { if(ruleset?.rulesetStatus?.value == 'active'){ String activeRulesetId = serialRulesetService.findActive(ruleset?.owner?.id) if(activeRulesetId){ serialRulesetService.updateRulesetStatus(activeRulesetId, 'deprecated') } } - ruleset.save(flush: true, failOnError: true) - respond ruleset + } + + @Transactional + def replaceAndDelete() { + SerialRuleset.withTransaction { + def data = getObjectToBind() + // Existing ruleset + String existingRulesetId = params.get("serialRulesetId") + SerialRuleset existing = queryForResource(existingRulesetId) + // If existing ruleset doesnt exist, return error + if (existing == null) { + transactionStatus.setRollbackOnly() + notFound() + return + } + // Check to see if the existing ruleset has any predicted piece sets associated with it + Integer pieceSetCount = serialRulesetService.countPieceSets(existingRulesetId)[0] + if(pieceSetCount > 0){ + transactionStatus.setRollbackOnly() + render status: METHOD_NOT_ALLOWED, text: "Cannot delete a serial ruleset which has been used to generate a predicted piece set" + return + } + // New ruleset + SerialRuleset ruleset = new SerialRuleset(data) + activeRulesetCheck(ruleset) + + ruleset.save(failOnError: true) + if(ruleset.hasErrors()) { + transactionStatus.setRollbackOnly() + respond ruleset.errors + } + // Finally delete the predicted piece set if we get this far and respond. + deleteResource existing + respond ruleset + } + } + + @Transactional + def replaceAndDeprecate() { + SerialRuleset.withTransaction { + def data = getObjectToBind() + // Existing ruleset + String existingRulesetId = params.get("serialRulesetId") + SerialRuleset existing = queryForResource(existingRulesetId) + // If existing ruleset doesnt exist, return error + if (existing == null) { + transactionStatus.setRollbackOnly() + notFound() + return + } + // New ruleset + SerialRuleset ruleset = new SerialRuleset(data) + //Check to see if new ruleset is active, if it is, additionally set the current active ruleset to deprecated + activeRulesetCheck(ruleset) + + ruleset.save(failOnError: true) + if(ruleset.hasErrors()) { + transactionStatus.setRollbackOnly() + respond ruleset.errors + } + // Finally deprecate existing ruleset + serialRulesetService.updateRulesetStatus(existingRulesetId, 'deprecated') + respond ruleset + } + } + + @Transactional + def save() { + SerialRuleset.withTransaction { + def data = getObjectToBind() + SerialRuleset ruleset = new SerialRuleset(data) + activeRulesetCheck(ruleset) + + ruleset.save(failOnError: true) + if(ruleset.hasErrors()) { + transactionStatus.setRollbackOnly() + respond ruleset.errors + } + respond ruleset + } } } diff --git a/service/grails-app/controllers/org/olf/UrlMappings.groovy b/service/grails-app/controllers/org/olf/UrlMappings.groovy index c7d4a17..7fbe7f9 100644 --- a/service/grails-app/controllers/org/olf/UrlMappings.groovy +++ b/service/grails-app/controllers/org/olf/UrlMappings.groovy @@ -16,10 +16,13 @@ class UrlMappings { "/serials-management/serials" (resources: 'serial') "/serials-management/rulesets" (resources: 'serialRuleset'){ - + '/active' (controller: 'serialRuleset', action: 'activateRuleset', method: 'POST') '/deprecated' (controller: 'serialRuleset', action: 'deprecateRuleset', method: 'POST') '/draft' (controller: 'serialRuleset', action: 'draftRuleset', method: 'POST') + + '/replaceAndDeprecate' (controller: 'serialRuleset', action: 'replaceAndDeprecate', method: 'POST') + '/replaceAndDelete' (controller: 'serialRuleset', action: 'replaceAndDelete', method: 'POST') } "/serials-management/predictedPieces" (resources: 'predictedPieceSet') { diff --git a/service/grails-app/domain/org/olf/recurrence/Recurrence.groovy b/service/grails-app/domain/org/olf/recurrence/Recurrence.groovy index f653f54..98e562a 100644 --- a/service/grails-app/domain/org/olf/recurrence/Recurrence.groovy +++ b/service/grails-app/domain/org/olf/recurrence/Recurrence.groovy @@ -18,7 +18,7 @@ public class Recurrence implements MultiTenant { Integer issues // 7 Integer period // 3 / Frequency - + Set rules// Validate to have exactly #issues of these static belongsTo = [ diff --git a/service/grails-app/services/org/olf/HousekeepingService.groovy b/service/grails-app/services/org/olf/HousekeepingService.groovy index 8597d3a..358276a 100644 --- a/service/grails-app/services/org/olf/HousekeepingService.groovy +++ b/service/grails-app/services/org/olf/HousekeepingService.groovy @@ -82,7 +82,7 @@ class HousekeepingService { level.rawValue = level.value as Integer level.valueFormat = RefdataValue.lookupOrCreate('EnumerationNumericLevelTMRF.Format', 'Number') } - + // TODO Check this should actually be a flush level.save(flush:true, failOnError:true) } } diff --git a/service/grails-app/services/org/olf/SerialRulesetService.groovy b/service/grails-app/services/org/olf/SerialRulesetService.groovy index 9b0ee27..704d5ba 100644 --- a/service/grails-app/services/org/olf/SerialRulesetService.groovy +++ b/service/grails-app/services/org/olf/SerialRulesetService.groovy @@ -10,6 +10,12 @@ import com.k_int.web.toolkit.refdata.RefdataValue @Transactional class SerialRulesetService { + List countPieceSets(String rulesetId) { + return PredictedPieceSet.executeQuery(""" + SELECT COUNT(pps.id) from PredictedPieceSet as pps WHERE pps.ruleset.id = :rulesetId + """.toString(), [rulesetId: rulesetId,]) + } + String findActive(String serialId) { return SerialRuleset.executeQuery(""" SELECT id from SerialRuleset WHERE owner.id = :serialId AND rulesetStatus.value = :active @@ -21,7 +27,7 @@ class SerialRulesetService { SerialRuleset ruleset = SerialRuleset.findById(rulesetId) ruleset.rulesetStatus = updatedStatus - ruleset.save(flush: true, failOnError: true) + ruleset.save(failOnError: true) return ruleset } diff --git a/service/src/main/okapi/ModuleDescriptor-template.json b/service/src/main/okapi/ModuleDescriptor-template.json index b69c298..0bdd8f2 100644 --- a/service/src/main/okapi/ModuleDescriptor-template.json +++ b/service/src/main/okapi/ModuleDescriptor-template.json @@ -171,6 +171,20 @@ "serials-management.rulesets.set-draft.execute" ] }, + { + "methods": [ + "POST" + ], + "pathPattern": "/serials-management/rulesets/{id}/replaceAndDeprecate", + "permissionsRequired": [ "serials-management.rulesets.replace-deprecate.execute" ] + }, + { + "methods": [ + "POST" + ], + "pathPattern": "/serials-management/rulesets/{id}/replaceAndDelete", + "permissionsRequired": [ "serials-management.rulesets.replace-delete.execute" ] + }, { "methods": [ "GET" @@ -476,6 +490,16 @@ "serials-management.rulesets.item.put" ] }, + { + "permissionName": "serials-management.rulesets.replace-delete.execute", + "displayName": "Replace and delete a ruleset", + "description": "Permit the deletion and replacement of a ruleset" + }, + { + "permissionName": "serials-management.rulesets.replace-deprecate.execute", + "displayName": "Replace and deprecate a ruleset", + "description": "Permit the deprecation and replacement of a ruleset" + }, { "permissionName": "serials-management.rulesets.edit", "subPermissions": [ @@ -483,7 +507,9 @@ "serials-management.rulesets.item.post", "serials-management.rulesets.set-active.execute", "serials-management.rulesets.set-deprecated.execute", - "serials-management.rulesets.set-draft.execute" + "serials-management.rulesets.set-draft.execute", + "serials-management.rulesets.replace-delete.execute", + "serials-management.rulesets.replace-deprecate.execute" ] }, {