001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.term.server.entity.Term; 024import com.echothree.model.data.term.server.entity.TermType; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.form.ValidationResult; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.server.control.BaseSimpleCommand; 032import com.echothree.util.server.persistence.Session; 033import com.echothree.util.server.validation.Validator; 034import java.util.Arrays; 035import java.util.Collections; 036import java.util.List; 037 038public class CreateTermCommand 039 extends BaseSimpleCommand<CreateTermForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 private final static List<FieldDefinition> standardFieldDefinitions; 043 private final static List<FieldDefinition> dateDrivenFieldDefinitions; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("TermName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("TermTypeName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 050 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 051 )); 052 053 standardFieldDefinitions = Collections.unmodifiableList(Arrays.asList( 054 new FieldDefinition("NetDueDays", FieldType.UNSIGNED_INTEGER, true, null, null), 055 new FieldDefinition("DiscountPercentage", FieldType.FRACTIONAL_PERCENT, true, null, null), 056 new FieldDefinition("DiscountDays", FieldType.UNSIGNED_INTEGER, true, null, null) 057 )); 058 059 dateDrivenFieldDefinitions = Collections.unmodifiableList(Arrays.asList( 060 new FieldDefinition("NetDueDayOfMonth", FieldType.UNSIGNED_INTEGER, true, null, null), 061 new FieldDefinition("DueNextMonthDays", FieldType.UNSIGNED_INTEGER, true, null, null), 062 new FieldDefinition("DiscountPercentage", FieldType.FRACTIONAL_PERCENT, true, null, null), 063 new FieldDefinition("DiscountBeforeDayOfMonth", FieldType.UNSIGNED_INTEGER, true, null, null) 064 )); 065 } 066 067 /** Creates a new instance of CreateTermCommand */ 068 public CreateTermCommand(UserVisitPK userVisitPK, CreateTermForm form) { 069 super(userVisitPK, form, null, null, false); 070 } 071 072 @Override 073 protected ValidationResult validate() { 074 Validator validator = new Validator(this); 075 ValidationResult validationResult = validator.validate(form, FORM_FIELD_DEFINITIONS); 076 077 if(!validationResult.getHasErrors()) { 078 String termTypeName = form.getTermTypeName(); 079 080 if(termTypeName.equals(TermTypes.STANDARD.name())) { 081 validationResult = validator.validate(form, standardFieldDefinitions); 082 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 083 validationResult = validator.validate(form, dateDrivenFieldDefinitions); 084 } 085 } 086 087 return validationResult; 088 } 089 090 @Override 091 protected BaseResult execute() { 092 var result = TermResultFactory.getCreateTermResult(); 093 var termControl = Session.getModelController(TermControl.class); 094 String termName = form.getTermName(); 095 Term term = termControl.getTermByName(termName); 096 097 if(term == null) { 098 String termTypeName = form.getTermTypeName(); 099 TermType termType = termControl.getTermTypeByName(termTypeName); 100 101 if(termType != null) { 102 var partyPK = getPartyPK(); 103 var isDefault = Boolean.valueOf(form.getIsDefault()); 104 var sortOrder = Integer.valueOf(form.getSortOrder()); 105 var description = form.getDescription(); 106 107 termTypeName = termType.getTermTypeName(); 108 term = termControl.createTerm(termName, termType, isDefault, sortOrder, partyPK); 109 110 if(termTypeName.equals(TermTypes.STANDARD.name())) { 111 Integer netDueDays = Integer.valueOf(form.getNetDueDays()); 112 Integer discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 113 Integer discountDays = Integer.valueOf(form.getDiscountDays()); 114 115 termControl.createStandardTerm(term, netDueDays, discountPercentage, discountDays, partyPK); 116 } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) { 117 Integer netDueDayOfMonth = Integer.valueOf(form.getNetDueDayOfMonth()); 118 Integer dueNextMonthDays = Integer.valueOf(form.getDueNextMonthDays()); 119 Integer discountPercentage = Integer.valueOf(form.getDiscountPercentage()); 120 Integer discountBeforeDayOfMonth = Integer.valueOf(form.getDiscountBeforeDayOfMonth()); 121 122 termControl.createDateDrivenTerm(term, netDueDayOfMonth, dueNextMonthDays, discountPercentage, 123 discountBeforeDayOfMonth, partyPK); 124 } 125 126 if(description != null) { 127 termControl.createTermDescription(term, getPreferredLanguage(), description, partyPK); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownTermTypeName.name(), termTypeName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.DuplicateTermName.name(), termName); 134 } 135 136 if(term != null) { 137 result.setEntityRef(term.getPrimaryKey().getEntityRef()); 138 result.setTermName(term.getLastDetail().getTermName()); 139 } 140 141 return result; 142 } 143 144}