Skip to content

Commit

Permalink
UISER-153: Support edit for publication patterns (#114)
Browse files Browse the repository at this point in the history
* feat: Ruleset edit endpoint

Added ruleset edit endpoint

* feat: Pieceset delete endpoint

Added predicted piece set delete endpoint to module descriptor, along with associated permissions

* refactor: WIP Changes after call

Updated Todos and changes after futher discussion

* feat: Added endpoints

Added replaceAndDelete and replaceAndDeprecate endpoints and placeholder methods

* feat: Replace and delete/deprecate functions

Added replaceAndDelete and replaceAndDeprecate functions to the serial ruleset controller class

* fix: Updated endpoint permissions

Added new permissions to endpoints following the standard used by the ruleset set status endpoints

* refactor: Private active check method

Moved logic for checking active ruleset to private method within controller
  • Loading branch information
Jack-Golding authored Oct 17, 2024
1 parent 128d83f commit e28100a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class PredictedPieceSetController extends OkapiTenantAwareController<PredictedPi
startDate: data?.startDate,
firstPieceTemplateMetadata: firstPieceTemplateMetadata,
nextPieceTemplateMetadata: nextPieceTemplateMetadata
// TODO Check that this should be a flush
]).save(flush: true, failOnError: true)

respond pps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package org.olf
import org.olf.SerialRuleset
import org.olf.SerialRulesetService

import org.olf.recurrence.Recurrence

import com.k_int.okapi.OkapiTenantAwareController
import com.k_int.web.toolkit.refdata.RefdataValue

import grails.rest.*
import grails.converters.*
import grails.gorm.multitenancy.CurrentTenant
import grails.gorm.transactions.Transactional

import static org.springframework.http.HttpStatus.*

import groovy.util.logging.Slf4j

Expand Down Expand Up @@ -39,7 +44,7 @@ class SerialRulesetController extends OkapiTenantAwareController<SerialRuleset>
}

def activateRuleset() {
String serialRulesetId = params.get("serialRulesetId")
String serialRulesetId = params.get("serialRulesetId")
SerialRuleset ruleset = SerialRuleset.findById(serialRulesetId)

String activeRulesetId = serialRulesetService.findActive(ruleset?.owner?.id)
Expand All @@ -51,18 +56,93 @@ class SerialRulesetController extends OkapiTenantAwareController<SerialRuleset>
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
}
}
}
5 changes: 4 additions & 1 deletion service/grails-app/controllers/org/olf/UrlMappings.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Recurrence implements MultiTenant<Recurrence> {

Integer issues // 7
Integer period // 3 / Frequency

Set<RecurrenceRule> rules// Validate to have exactly #issues of these

static belongsTo = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import com.k_int.web.toolkit.refdata.RefdataValue
@Transactional
class SerialRulesetService {

List<Integer> 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
Expand All @@ -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
}
Expand Down
28 changes: 27 additions & 1 deletion service/src/main/okapi/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -476,14 +490,26 @@
"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": [
"serials-management.rulesets.view",
"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"
]
},
{
Expand Down

0 comments on commit e28100a

Please sign in to comment.