001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.accounting.server.command; 018 019import com.echothree.control.user.accounting.common.form.CreateCurrencyForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.accounting.server.logic.SymbolPositionLogic; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 030import com.echothree.util.server.control.BaseSimpleCommand; 031import com.echothree.util.server.control.CommandSecurityDefinition; 032import com.echothree.util.server.control.PartyTypeDefinition; 033import com.echothree.util.server.control.SecurityRoleDefinition; 034import com.echothree.util.server.persistence.Session; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.List; 038import javax.enterprise.context.RequestScoped; 039 040@RequestScoped 041public class CreateCurrencyCommand 042 extends BaseSimpleCommand<CreateCurrencyForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 051 new SecurityRoleDefinition(SecurityRoleGroups.Currency.name(), SecurityRoles.Create.name()) 052 ))) 053 ))); 054 055 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, 3L), 057 new FieldDefinition("Symbol", FieldType.STRING, false, 1L, 20L), 058 new FieldDefinition("SymbolPositionName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("SymbolOnListStart", FieldType.BOOLEAN, false, null, null), 060 new FieldDefinition("SymbolOnListMember", FieldType.BOOLEAN, false, null, null), 061 new FieldDefinition("SymbolOnSubtotal", FieldType.BOOLEAN, false, null, null), 062 new FieldDefinition("SymbolOnTotal", FieldType.BOOLEAN, false, null, null), 063 new FieldDefinition("GroupingSeparator", FieldType.STRING, true, 1L, 1L), 064 new FieldDefinition("GroupingSize", FieldType.SIGNED_INTEGER, true, null, null), 065 new FieldDefinition("FractionSeparator", FieldType.STRING, false, 1L, 1L), 066 new FieldDefinition("DefaultFractionDigits", FieldType.SIGNED_INTEGER, false, null, null), 067 new FieldDefinition("PriceUnitFractionDigits", FieldType.SIGNED_INTEGER, false, null, null), 068 new FieldDefinition("PriceLineFractionDigits", FieldType.SIGNED_INTEGER, false, null, null), 069 new FieldDefinition("CostUnitFractionDigits", FieldType.SIGNED_INTEGER, false, null, null), 070 new FieldDefinition("CostLineFractionDigits", FieldType.SIGNED_INTEGER, false, null, null), 071 new FieldDefinition("MinusSign", FieldType.STRING, true, 1L, 1L), 072 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 073 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 074 )); 075 } 076 077 /** Creates a new instance of CreateCurrencyCommand */ 078 public CreateCurrencyCommand() { 079 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 080 } 081 082 @Override 083 protected BaseResult execute() { 084 var accountingControl = Session.getModelController(AccountingControl.class); 085 var currencyIsoName = form.getCurrencyIsoName(); 086 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 087 088 if(currency == null) { 089 var symbolPosition = SymbolPositionLogic.getInstance().getSymbolPositionByName(this, form.getSymbolPositionName()); 090 091 if(!hasExecutionErrors()) { 092 var groupingSeparator = form.getGroupingSeparator(); 093 var groupingSize = Integer.valueOf(form.getGroupingSize()); 094 var minusSign = form.getMinusSign(); 095 var isDefault = Boolean.valueOf(form.getIsDefault()); 096 var sortOrder = Integer.valueOf(form.getSortOrder()); 097 var symbol = form.getSymbol(); 098 var symbolOnListStart = symbol == null? null: Boolean.valueOf(form.getSymbolOnListStart()); 099 var symbolOnListMember = symbol == null? null: Boolean.valueOf(form.getSymbolOnListMember()); 100 var symbolOnSubtotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnSubtotal()); 101 var symbolOnTotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnTotal()); 102 103 if(symbol == null || (symbol != null && symbolOnListStart != null && symbolOnListMember != null 104 && symbolOnSubtotal != null && symbolOnTotal != null)) { 105 var fractionSeparator = form.getFractionSeparator(); 106 var defaultFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getDefaultFractionDigits()); 107 var priceUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceUnitFractionDigits()); 108 var priceLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceLineFractionDigits()); 109 var costUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostUnitFractionDigits()); 110 var costLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostLineFractionDigits()); 111 112 if(fractionSeparator == null || (defaultFractionDigits != null && priceUnitFractionDigits != null 113 && priceLineFractionDigits != null && costUnitFractionDigits != null && costLineFractionDigits != null)) { 114 accountingControl.createCurrency(currencyIsoName, symbol, symbolPosition, symbolOnListStart, symbolOnListMember, 115 symbolOnSubtotal, symbolOnTotal, groupingSeparator, groupingSize, fractionSeparator, defaultFractionDigits, 116 priceUnitFractionDigits, priceLineFractionDigits, costUnitFractionDigits, costLineFractionDigits, minusSign, 117 isDefault, sortOrder, getPartyPK()); 118 } else { 119 if(defaultFractionDigits == null) { 120 addExecutionError(ExecutionErrors.MissingDefaultFractionDigits.name()); 121 } 122 if(priceUnitFractionDigits == null) { 123 addExecutionError(ExecutionErrors.MissingPriceUnitFractionDigits.name()); 124 } 125 if(priceLineFractionDigits == null) { 126 addExecutionError(ExecutionErrors.MissingPriceLineFractionDigits.name()); 127 } 128 if(costUnitFractionDigits == null) { 129 addExecutionError(ExecutionErrors.MissingCostUnitFractionDigits.name()); 130 } 131 if(costLineFractionDigits == null) { 132 addExecutionError(ExecutionErrors.MissingCostLineFractionDigits.name()); 133 } 134 } 135 } else { 136 if(symbolOnListStart == null) { 137 addExecutionError(ExecutionErrors.MissingSymbolOnListStart.name()); 138 } 139 if(symbolOnListMember == null) { 140 addExecutionError(ExecutionErrors.MissingSymbolOnListMember.name()); 141 } 142 if(symbolOnSubtotal == null) { 143 addExecutionError(ExecutionErrors.MissingSymbolOnSubtotal.name()); 144 } 145 if(symbolOnTotal == null) { 146 addExecutionError(ExecutionErrors.MissingSymbolOnTotal.name()); 147 } 148 } 149 } 150 } else { 151 addExecutionError(ExecutionErrors.DuplicateCurrencyIsoName.name(), currencyIsoName); 152 } 153 154 return null; 155 } 156 157}