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.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.accounting.server.entity.Currency; 026import com.echothree.model.data.accounting.server.entity.SymbolPosition; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import com.echothree.util.server.persistence.Session; 037import java.util.Arrays; 038import java.util.Collections; 039import java.util.List; 040 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(UserVisitPK userVisitPK, CreateCurrencyForm form) { 079 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 080 } 081 082 @Override 083 protected BaseResult execute() { 084 var accountingControl = Session.getModelController(AccountingControl.class); 085 String currencyIsoName = form.getCurrencyIsoName(); 086 Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 087 088 if(currency == null) { 089 SymbolPosition symbolPosition = SymbolPositionLogic.getInstance().getSymbolPositionByName(this, form.getSymbolPositionName()); 090 091 if(!hasExecutionErrors()) { 092 String groupingSeparator = form.getGroupingSeparator(); 093 Integer groupingSize = Integer.valueOf(form.getGroupingSize()); 094 String minusSign = form.getMinusSign(); 095 var isDefault = Boolean.valueOf(form.getIsDefault()); 096 var sortOrder = Integer.valueOf(form.getSortOrder()); 097 String symbol = form.getSymbol(); 098 Boolean symbolOnListStart = symbol == null? null: Boolean.valueOf(form.getSymbolOnListStart()); 099 Boolean symbolOnListMember = symbol == null? null: Boolean.valueOf(form.getSymbolOnListMember()); 100 Boolean symbolOnSubtotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnSubtotal()); 101 Boolean 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 String fractionSeparator = form.getFractionSeparator(); 106 Integer defaultFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getDefaultFractionDigits()); 107 Integer priceUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceUnitFractionDigits()); 108 Integer priceLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceLineFractionDigits()); 109 Integer costUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostUnitFractionDigits()); 110 Integer 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}