Index: grails-app/domain/com/lemans/program/Program.groovy =================================================================== diff -u -r3ffb52c21327dfedaed221cd48c7d7595adebf24 -rff9dd01224b335c081847e00a61e800ae4f00fdb --- grails-app/domain/com/lemans/program/Program.groovy (.../Program.groovy) (revision 3ffb52c21327dfedaed221cd48c7d7595adebf24) +++ grails-app/domain/com/lemans/program/Program.groovy (.../Program.groovy) (revision ff9dd01224b335c081847e00a61e800ae4f00fdb) @@ -1,65 +1,68 @@ -package com.lemans.program - -import com.lemans.services.Auditable - -class Program extends Auditable { - - Integer id - Integer domainId - - Integer programTypeId - String programCode - String programName - String programDescr - Integer brandId - - boolean inactive - boolean shippableProductOnly = true - boolean combineAllowed - boolean multipleAllowed - boolean onGoing - boolean domesticDealersOnly = true - boolean whileSuppliesLast - boolean visibleOnPNW - boolean trackProgram - boolean visibleOnPurchaseHistory - boolean platformValidation - boolean isFeaturedProgram - boolean isApplicableOnOrders - boolean isSearchable - - String levelTreatment = 'O' - String rewardTreatment = 'A' - Integer limitTypeId - Integer quantityLimit - Date startDate - Date endDate - - static constraints = { - programCode blank: false, maxSize: 20, validator: { val, obj -> - if (!obj.id && obj.programCodeInUse(val)) { return['duplicate'] } - } - programName blank: false, maxSize: 50 - programDescr nullable: true, blank: false, maxSize: 2000 - brandId nullable: true - levelTreatment blank: false, maxSize: 1 - rewardTreatment blank: false, maxSize: 1 - limitTypeId nullable: true - quantityLimit nullable: true - startDate nullable: true - endDate nullable: true, validator: { val, obj -> - if (val && val < obj.startDate) { - return['invalid'] - } - } - } - - static mapping = { - table 'Program' - id column: 'programId' - } - - static boolean programCodeInUse(String programCode) { - findByProgramCodeAndDateDeletedIsNull(programCode) - } -} +package com.lemans.program + +import com.lemans.services.Auditable + +class Program extends Auditable { + + Integer id + Integer domainId + + Integer programTypeId + String programCode + String programName + String programDescr + Integer brandId + + boolean inactive + boolean shippableProductOnly = true + boolean combineAllowed + boolean multipleAllowed + boolean onGoing + boolean domesticDealersOnly = true + boolean whileSuppliesLast + boolean visibleOnPNW + boolean trackProgram + boolean visibleOnPurchaseHistory + boolean platformValidation + boolean isFeaturedProgram + boolean isApplicableOnOrders + boolean isSearchable + + boolean salesRepOnly + boolean internalUseOnly + + String levelTreatment = 'O' + String rewardTreatment = 'A' + Integer limitTypeId + Integer quantityLimit + Date startDate + Date endDate + + static constraints = { + programCode blank: false, maxSize: 20, validator: { val, obj -> + if (!obj.id && obj.programCodeInUse(val)) { return['duplicate'] } + } + programName blank: false, maxSize: 50 + programDescr nullable: true, blank: false, maxSize: 2000 + brandId nullable: true + levelTreatment blank: false, maxSize: 1 + rewardTreatment blank: false, maxSize: 1 + limitTypeId nullable: true + quantityLimit nullable: true + startDate nullable: true + endDate nullable: true, validator: { val, obj -> + if (val && val < obj.startDate) { + return['invalid'] + } + } + } + + static mapping = { + table 'Program' + id column: 'programId' + } + + static boolean programCodeInUse(String programCode) { + findByProgramCodeAndDateDeletedIsNull(programCode) + } +} Index: grails-app/services/com/lemans/programs/ProgramManagerService.groovy =================================================================== diff -u -r3e79641600358593d638b0697a8def1b7ce4b064 -rff9dd01224b335c081847e00a61e800ae4f00fdb --- grails-app/services/com/lemans/programs/ProgramManagerService.groovy (.../ProgramManagerService.groovy) (revision 3e79641600358593d638b0697a8def1b7ce4b064) +++ grails-app/services/com/lemans/programs/ProgramManagerService.groovy (.../ProgramManagerService.groovy) (revision ff9dd01224b335c081847e00a61e800ae4f00fdb) @@ -1,85 +1,92 @@ -package com.lemans.programs - -import com.lemans.program.Program -import com.lemans.services.LemansManager -import grails.gorm.transactions.Transactional - -@Transactional -class ProgramManagerService extends LemansManager { - - private static final Integer PNW_DOMAIN_ID = 4 - - /** - * Finds Program by id. - * - * @param criteria containing id - * - * @return Map with List of results - */ - Map findProgramById(Map criteria) { - dqx(criteria).executeOneFrom('dbo.vwProgram', ['x.programId = :id']) - } - - /** - * Finds all Programs. - * - * @param criteria that may contain programTypeId - * - * @return Map with List of results - */ - Map findProgram(Map criteria) { - List clauses = [] - if (criteria.programType) { clauses << 'x.programTypeId = :programType' } - dqx(criteria).executeFrom('dbo.vwProgram', clauses) - } - - /** - * Saves an existing or newly created Program. - * - * @param values Map of property values - * @param id if not null updates the Program, else inserts it - * @param username - * - * @return Program which may have errors - */ - Program saveProgram(Map values, Integer id, String username) { - Program program = (id != null) ? Program.get(id) : new Program() - if (!id && !program.domainId) { - program.domainId = PNW_DOMAIN_ID - } - applyValuesToDomain(values, program) - saveOrDiscardDomain(program, username) - program - } - - /** - * Soft deletes Program. - * - * @param id - * @param username - * - * @return true if successful - */ - boolean deleteProgram(Integer id, String username) { - Program program = Program.get(id) - if (program) { softDelete(program, username) } - } - - /** - * Replicates a program. - * - * @param oldProgramCode - * @param newProgramCode - * @param userName - * - * @return the replicated Program or an error message - */ - List replicateProgram(String oldProgramCode, String newProgramCode, String userName) { - sql().rows('EXEC dbo.spDuplicateProgram @oldProgramCode = ?, @newProgramCode = ?, @createdBy = ?', - [oldProgramCode, newProgramCode, userName]) - } - - void refreshQualifierPartList(Integer programId) { - sql().execute( 'EXEC dbo.spRefreshQualifierPartListHistoryReport @programId = ?', [programId]) - } -} +package com.lemans.programs + +import com.lemans.program.Program +import com.lemans.services.LemansManager +import grails.gorm.transactions.Transactional + +@Transactional +class ProgramManagerService extends LemansManager { + + private static final Integer PNW_DOMAIN_ID = 4 + + /** + * Finds Program by id. + * + * @param criteria containing id + * + * @return Map with List of results + */ + Map findProgramById(Map criteria) { + dqx(criteria).executeOneFrom('dbo.vwProgram', ['x.programId = :id']) + } + + /** + * Finds all Programs. + * + * @param criteria that may contain programTypeId + * + * @return Map with List of results + */ + Map findProgram(Map criteria) { + List clauses = [] + if (criteria.programType) { clauses << 'x.programTypeId = :programType' } + dqx(criteria).executeFrom('dbo.vwProgram', clauses) + } + + /** + * Saves an existing or newly created Program. + * + * @param values Map of property values + * @param id if not null updates the Program, else inserts it + * @param username + * + * @return Program which may have errors + */ + Program saveProgram(Map values, Integer id, String username) { + Program program = (id != null) ? Program.get(id) : new Program() + if (!id && !program.domainId) { + program.domainId = PNW_DOMAIN_ID + } + applyValuesToDomain(values, program) + validateProgramFlag(program) + saveOrDiscardDomain(program, username) + program + } + + /** + * Soft deletes Program. + * + * @param id + * @param username + * + * @return true if successful + */ + boolean deleteProgram(Integer id, String username) { + Program program = Program.get(id) + if (program) { softDelete(program, username) } + } + + /** + * Replicates a program. + * + * @param oldProgramCode + * @param newProgramCode + * @param userName + * + * @return the replicated Program or an error message + */ + List replicateProgram(String oldProgramCode, String newProgramCode, String userName) { + sql().rows('EXEC dbo.spDuplicateProgram @oldProgramCode = ?, @newProgramCode = ?, @createdBy = ?', + [oldProgramCode, newProgramCode, userName]) + } + + void refreshQualifierPartList(Integer programId) { + sql().execute( 'EXEC dbo.spRefreshQualifierPartListHistoryReport @programId = ?', [programId]) + } + + void validateProgramFlag(Program program) { + if (program.salesRepOnly == program.internalUseOnly) { + program.errors.rejectValue('salesRepOnly', 'salesRepOnly.boolean',"Both salesRepOnly & interUseOnly cannot be $program.salesRepOnly") + } + } +} Index: src/test/groovy/com/lemans/programs/ProgramLevelSpec.groovy =================================================================== diff -u -r3ffb52c21327dfedaed221cd48c7d7595adebf24 -rff9dd01224b335c081847e00a61e800ae4f00fdb --- src/test/groovy/com/lemans/programs/ProgramLevelSpec.groovy (.../ProgramLevelSpec.groovy) (revision 3ffb52c21327dfedaed221cd48c7d7595adebf24) +++ src/test/groovy/com/lemans/programs/ProgramLevelSpec.groovy (.../ProgramLevelSpec.groovy) (revision ff9dd01224b335c081847e00a61e800ae4f00fdb) @@ -1,80 +1,79 @@ -package com.lemans.programs - -import com.lemans.program.Program -import com.lemans.program.ProgramLevel -import com.lemans.program.ProgramReward -import com.lemans.testing.Auditable32Spec -import grails.testing.gorm.DomainUnitTest -import spock.lang.Unroll - -class ProgramLevelSpec extends Auditable32Spec implements DomainUnitTest { - - @Override - protected getAuditable() { level } - - ProgramLevel level = new ProgramLevel(program: new Program(), displayText: 'text') - - def 'a valid ProgramLevel has no errors'() { - expect: - level.validate() - } - - @Unroll - def 'displayText with value #value validation error is #error'() { - given: - level.displayText = value - - when: - level.validate() - - then: - level.errors['displayText']?.code == error - - where: - value || error - null || 'nullable' - '' || null - 'a' || null - '100chars'.padRight(100, '_') || null - '101chars'.padRight(101, '_') || 'maxSize.exceeded' - } - - @Unroll - def 'description with value #value validation error is #error'() { - given: - level.description = value - - when: - level.validate() - - then: - level.errors['description']?.code == error - - where: - value || error - null || null - '' || 'blank' - 'a' || null - '2000chars'.padRight(2000, '_') || null - '2001chars'.padRight(2001, '_') || 'maxSize.exceeded' - } - - @Unroll - def 'rewardTreatment with value #value validation error is #error'() { - given: - level.rewardTreatment = value - - when: - level.validate() - - then: - level.errors['rewardTreatment']?.code == error - - where: - value || error - null || null - ' ' || 'blank' - '1' || null - '01' || 'maxSize.exceeded' - } -} +package com.lemans.programs + +import com.lemans.program.Program +import com.lemans.program.ProgramLevel +import com.lemans.testing.Auditable32Spec +import grails.testing.gorm.DomainUnitTest +import spock.lang.Unroll + +class ProgramLevelSpec extends Auditable32Spec implements DomainUnitTest { + + @Override + protected getAuditable() { level } + + ProgramLevel level = new ProgramLevel(program: new Program(), displayText: 'text') + + def 'a valid ProgramLevel has no errors'() { + expect: + level.validate() + } + + @Unroll + def 'displayText with value #value validation error is #error'() { + given: + level.displayText = value + + when: + level.validate() + + then: + level.errors['displayText']?.code == error + + where: + value || error + null || 'nullable' + '' || null + 'a' || null + '100chars'.padRight(100, '_') || null + '101chars'.padRight(101, '_') || 'maxSize.exceeded' + } + + @Unroll + def 'description with value #value validation error is #error'() { + given: + level.description = value + + when: + level.validate() + + then: + level.errors['description']?.code == error + + where: + value || error + null || null + '' || 'blank' + 'a' || null + '2000chars'.padRight(2000, '_') || null + '2001chars'.padRight(2001, '_') || 'maxSize.exceeded' + } + + @Unroll + def 'rewardTreatment with value #value validation error is #error'() { + given: + level.rewardTreatment = value + + when: + level.validate() + + then: + level.errors['rewardTreatment']?.code == error + + where: + value || error + null || null + ' ' || 'blank' + '1' || null + '01' || 'maxSize.exceeded' + } +}