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.validation.Validator; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033import javax.inject.Inject; 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 @Inject 066 TermControl termControl; 067 068 069 /** Creates a new instance of CreateTermCommand */ 070 public CreateTermCommand() { 071 super(null, null, false); 072 } 073 074 @Override 075 protected ValidationResult validate() { 076 var validator = new Validator(this); 077 var validationResult = validator.validate(form, FORM_FIELD_DEFINITIONS); 078 079 if(!validationResult.getHasErrors()) { 080 var termTypeName = form.getTermTypeName(); 081 082 if(termTypeName.equals(TermTypes.STANDARD.name())) { 083 validationResult = validator.validate(form, standardFieldDefinitions); 084 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 085 validationResult = validator.validate(form, dateDrivenFieldDefinitions); 086 } 087 } 088 089 return validationResult; 090 } 091 092 @Override 093 protected BaseResult execute() { 094 var result = TermResultFactory.getCreateTermResult(); 095 var termName = form.getTermName(); 096 var term = termControl.getTermByName(termName); 097 098 if(term == null) { 099 var termTypeName = form.getTermTypeName(); 100 var termType = termControl.getTermTypeByName(termTypeName); 101 102 if(termType != null) { 103 var partyPK = getPartyPK(); 104 var isDefault = Boolean.valueOf(form.getIsDefault()); 105 var sortOrder = Integer.valueOf(form.getSortOrder()); 106 var description = form.getDescription(); 107 108 termTypeName = termType.getTermTypeName(); 109 term = termControl.createTerm(termName, termType, isDefault, sortOrder, partyPK); 110 111 if(termTypeName.equals(TermTypes.STANDARD.name())) { 112 var netDueDays = Integer.valueOf(form.getNetDueDays()); 113 var discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 114 var discountDays = Integer.valueOf(form.getDiscountDays()); 115 116 termControl.createStandardTerm(term, netDueDays, discountPercentage, discountDays, partyPK); 117 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 118 var netDueDayOfMonth = Integer.valueOf(form.getNetDueDayOfMonth()); 119 var dueNextMonthDays = Integer.valueOf(form.getDueNextMonthDays()); 120 var discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 121 var discountBeforeDayOfMonth = Integer.valueOf(form.getDiscountBeforeDayOfMonth()); 122 123 termControl.createDateDrivenTerm(term, netDueDayOfMonth, dueNextMonthDays, discountPercentage, 124 discountBeforeDayOfMonth, partyPK); 125 } 126 127 if(description != null) { 128 termControl.createTermDescription(term, getPreferredLanguage(), description, partyPK); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownTermTypeName.name(), termTypeName); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.DuplicateTermName.name(), termName); 135 } 136 137 if(term != null) { 138 result.setEntityRef(term.getPrimaryKey().getEntityRef()); 139 result.setTermName(term.getLastDetail().getTermName()); 140 } 141 142 return result; 143 } 144 145}