001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.term.server.command; 018 019import com.echothree.control.user.term.common.form.CreateTermForm; 020import com.echothree.control.user.term.common.result.TermResultFactory; 021import com.echothree.model.control.term.common.TermTypes; 022import com.echothree.model.control.term.server.control.TermControl; 023import com.echothree.model.data.user.common.pk.UserVisitPK; 024import com.echothree.util.common.command.BaseResult; 025import com.echothree.util.common.form.ValidationResult; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.validation.FieldDefinition; 028import com.echothree.util.common.validation.FieldType; 029import com.echothree.util.server.control.BaseSimpleCommand; 030import com.echothree.util.server.persistence.Session; 031import com.echothree.util.server.validation.Validator; 032import java.util.List; 033import javax.enterprise.context.Dependent; 034 035@Dependent 036public class CreateTermCommand 037 extends BaseSimpleCommand<CreateTermForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 private final static List<FieldDefinition> standardFieldDefinitions; 041 private final static List<FieldDefinition> dateDrivenFieldDefinitions; 042 043 static { 044 FORM_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("TermName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("TermTypeName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 048 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 049 ); 050 051 standardFieldDefinitions = List.of( 052 new FieldDefinition("NetDueDays", FieldType.UNSIGNED_INTEGER, true, null, null), 053 new FieldDefinition("DiscountPercentage", FieldType.FRACTIONAL_PERCENT, true, null, null), 054 new FieldDefinition("DiscountDays", FieldType.UNSIGNED_INTEGER, true, null, null) 055 ); 056 057 dateDrivenFieldDefinitions = List.of( 058 new FieldDefinition("NetDueDayOfMonth", FieldType.UNSIGNED_INTEGER, true, null, null), 059 new FieldDefinition("DueNextMonthDays", FieldType.UNSIGNED_INTEGER, true, null, null), 060 new FieldDefinition("DiscountPercentage", FieldType.FRACTIONAL_PERCENT, true, null, null), 061 new FieldDefinition("DiscountBeforeDayOfMonth", FieldType.UNSIGNED_INTEGER, true, null, null) 062 ); 063 } 064 065 /** Creates a new instance of CreateTermCommand */ 066 public CreateTermCommand() { 067 super(null, null, false); 068 } 069 070 @Override 071 protected ValidationResult validate() { 072 var validator = new Validator(this); 073 var validationResult = validator.validate(form, FORM_FIELD_DEFINITIONS); 074 075 if(!validationResult.getHasErrors()) { 076 var termTypeName = form.getTermTypeName(); 077 078 if(termTypeName.equals(TermTypes.STANDARD.name())) { 079 validationResult = validator.validate(form, standardFieldDefinitions); 080 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 081 validationResult = validator.validate(form, dateDrivenFieldDefinitions); 082 } 083 } 084 085 return validationResult; 086 } 087 088 @Override 089 protected BaseResult execute() { 090 var result = TermResultFactory.getCreateTermResult(); 091 var termControl = Session.getModelController(TermControl.class); 092 var termName = form.getTermName(); 093 var term = termControl.getTermByName(termName); 094 095 if(term == null) { 096 var termTypeName = form.getTermTypeName(); 097 var termType = termControl.getTermTypeByName(termTypeName); 098 099 if(termType != null) { 100 var partyPK = getPartyPK(); 101 var isDefault = Boolean.valueOf(form.getIsDefault()); 102 var sortOrder = Integer.valueOf(form.getSortOrder()); 103 var description = form.getDescription(); 104 105 termTypeName = termType.getTermTypeName(); 106 term = termControl.createTerm(termName, termType, isDefault, sortOrder, partyPK); 107 108 if(termTypeName.equals(TermTypes.STANDARD.name())) { 109 var netDueDays = Integer.valueOf(form.getNetDueDays()); 110 var discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 111 var discountDays = Integer.valueOf(form.getDiscountDays()); 112 113 termControl.createStandardTerm(term, netDueDays, discountPercentage, discountDays, partyPK); 114 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 115 var netDueDayOfMonth = Integer.valueOf(form.getNetDueDayOfMonth()); 116 var dueNextMonthDays = Integer.valueOf(form.getDueNextMonthDays()); 117 var discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 118 var discountBeforeDayOfMonth = Integer.valueOf(form.getDiscountBeforeDayOfMonth()); 119 120 termControl.createDateDrivenTerm(term, netDueDayOfMonth, dueNextMonthDays, discountPercentage, 121 discountBeforeDayOfMonth, partyPK); 122 } 123 124 if(description != null) { 125 termControl.createTermDescription(term, getPreferredLanguage(), description, partyPK); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownTermTypeName.name(), termTypeName); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.DuplicateTermName.name(), termName); 132 } 133 134 if(term != null) { 135 result.setEntityRef(term.getPrimaryKey().getEntityRef()); 136 result.setTermName(term.getLastDetail().getTermName()); 137 } 138 139 return result; 140 } 141 142}