Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UISER-153: Support edit for publication patterns #114

Merged
merged 9 commits into from
Oct 17, 2024
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