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.accounting.server.command; 018 019import com.echothree.control.user.accounting.common.edit.AccountingEditFactory; 020import com.echothree.control.user.accounting.common.edit.TransactionGlAccountCategoryEdit; 021import com.echothree.control.user.accounting.common.form.EditTransactionGlAccountCategoryForm; 022import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 023import com.echothree.control.user.accounting.common.spec.TransactionGlAccountCategorySpec; 024import com.echothree.model.control.accounting.server.control.AccountingControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041 042@Dependent 043public class EditTransactionGlAccountCategoryCommand 044 extends BaseEditCommand<TransactionGlAccountCategorySpec, TransactionGlAccountCategoryEdit> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 048 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.TransactionGlAccountCategory.name(), SecurityRoles.Edit.name()) 055 )) 056 )); 057 058 SPEC_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("TransactionTypeName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 069 ); 070 } 071 072 /** Creates a new instance of EditTransactionGlAccountCategoryCommand */ 073 public EditTransactionGlAccountCategoryCommand() { 074 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var accountingControl = Session.getModelController(AccountingControl.class); 080 var result = AccountingResultFactory.getEditTransactionGlAccountCategoryResult(); 081 var transactionTypeName = spec.getTransactionTypeName(); 082 var transactionType = accountingControl.getTransactionTypeByNameForUpdate(transactionTypeName); 083 084 if(transactionType != null) { 085 var transactionGlAccountCategoryName = spec.getTransactionGlAccountCategoryName(); 086 087 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 088 var transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName); 089 090 if(transactionGlAccountCategory != null) { 091 if(editMode.equals(EditMode.LOCK)) { 092 if(lockEntity(transactionGlAccountCategory)) { 093 var transactionGlAccountCategoryDescription = accountingControl.getTransactionGlAccountCategoryDescription(transactionGlAccountCategory, getPreferredLanguage()); 094 var transactionGlAccount = accountingControl.getTransactionGlAccount(transactionGlAccountCategory); 095 var edit = AccountingEditFactory.getTransactionGlAccountCategoryEdit(); 096 var transactionGlAccountCategoryDetail = transactionGlAccountCategory.getLastDetail(); 097 var glAccountCategory = transactionGlAccountCategoryDetail.getGlAccountCategory(); 098 099 result.setTransactionGlAccountCategory(accountingControl.getTransactionGlAccountCategoryTransfer(getUserVisit(), transactionGlAccountCategory)); 100 101 result.setEdit(edit); 102 edit.setTransactionGlAccountCategoryName(transactionGlAccountCategoryDetail.getTransactionGlAccountCategoryName()); 103 edit.setGlAccountCategoryName(glAccountCategory == null? null: glAccountCategory.getLastDetail().getGlAccountCategoryName()); 104 edit.setSortOrder(transactionGlAccountCategoryDetail.getSortOrder().toString()); 105 edit.setGlAccountName(transactionGlAccount == null? null: transactionGlAccount.getGlAccount().getLastDetail().getGlAccountName()); 106 107 if(transactionGlAccountCategoryDescription != null) { 108 edit.setDescription(transactionGlAccountCategoryDescription.getDescription()); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 112 } 113 114 result.setEntityLock(getEntityLockTransfer(transactionGlAccountCategory)); 115 } else { // EditMode.ABANDON 116 unlockEntity(transactionGlAccountCategory); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName); 120 } 121 } else if(editMode.equals(EditMode.UPDATE)) { 122 var transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByNameForUpdate(transactionType, transactionGlAccountCategoryName); 123 124 if(transactionGlAccountCategory != null) { 125 transactionGlAccountCategoryName = edit.getTransactionGlAccountCategoryName(); 126 var duplicateTransactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName); 127 128 if(duplicateTransactionGlAccountCategory == null || transactionGlAccountCategory.equals(duplicateTransactionGlAccountCategory)) { 129 var glAccountCategoryName = edit.getGlAccountCategoryName(); 130 var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 131 132 if(glAccountCategoryName == null || glAccountCategory != null) { 133 var glAccountName = edit.getGlAccountName(); 134 var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName); 135 136 if(glAccountName == null || glAccount != null) { 137 if(glAccountCategory == null || glAccount == null? true: glAccountCategory.equals(glAccount.getLastDetail().getGlAccountCategory())) { 138 if(lockEntityForUpdate(transactionGlAccountCategory)) { 139 try { 140 var partyPK = getPartyPK(); 141 var transactionGlAccountCategoryDetailValue = accountingControl.getTransactionGlAccountCategoryDetailValueForUpdate(transactionGlAccountCategory); 142 var transactionGlAccountCategoryDescription = accountingControl.getTransactionGlAccountCategoryDescriptionForUpdate(transactionGlAccountCategory, getPreferredLanguage()); 143 var transactionGlAccount = accountingControl.getTransactionGlAccount(transactionGlAccountCategory); 144 var description = edit.getDescription(); 145 146 transactionGlAccountCategoryDetailValue.setTransactionGlAccountCategoryName(edit.getTransactionGlAccountCategoryName()); 147 transactionGlAccountCategoryDetailValue.setGlAccountCategoryPK(glAccountCategory == null? null: glAccountCategory.getPrimaryKey()); 148 transactionGlAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 149 150 accountingControl.updateTransactionGlAccountCategoryFromValue(transactionGlAccountCategoryDetailValue, partyPK); 151 152 if(transactionGlAccountCategoryDescription == null && description != null) { 153 accountingControl.createTransactionGlAccountCategoryDescription(transactionGlAccountCategory, getPreferredLanguage(), description, partyPK); 154 } else if(transactionGlAccountCategoryDescription != null && description == null) { 155 accountingControl.deleteTransactionGlAccountCategoryDescription(transactionGlAccountCategoryDescription, partyPK); 156 } else if(transactionGlAccountCategoryDescription != null && description != null) { 157 var transactionGlAccountCategoryDescriptionValue = accountingControl.getTransactionGlAccountCategoryDescriptionValue(transactionGlAccountCategoryDescription); 158 159 transactionGlAccountCategoryDescriptionValue.setDescription(description); 160 accountingControl.updateTransactionGlAccountCategoryDescriptionFromValue(transactionGlAccountCategoryDescriptionValue, partyPK); 161 } 162 163 if(transactionGlAccount == null && glAccount != null) { 164 accountingControl.createTransactionGlAccount(transactionGlAccountCategory, glAccount, partyPK); 165 } else if(transactionGlAccount != null && glAccount == null) { 166 accountingControl.deleteTransactionGlAccount(transactionGlAccount, partyPK); 167 } else if(transactionGlAccount != null && glAccount != null) { 168 var transactionGlAccountValue = accountingControl.getTransactionGlAccountValue(transactionGlAccount); 169 170 transactionGlAccountValue.setGlAccountPK(glAccount.getPrimaryKey()); 171 accountingControl.updateTransactionGlAccountFromValue(transactionGlAccountValue, partyPK); 172 } 173 } finally { 174 unlockEntity(transactionGlAccountCategory); 175 } 176 } else { 177 addExecutionError(ExecutionErrors.EntityLockStale.name()); 178 } 179 } else { 180 addExecutionError(ExecutionErrors.InvalidGlAccountCategory.name()); 181 } 182 } else { 183 addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName); 184 } 185 } else { 186 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 187 } 188 } else { 189 addExecutionError(ExecutionErrors.DuplicateTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName); 190 } 191 192 if(hasExecutionErrors()) { 193 result.setTransactionGlAccountCategory(accountingControl.getTransactionGlAccountCategoryTransfer(getUserVisit(), transactionGlAccountCategory)); 194 result.setEntityLock(getEntityLockTransfer(transactionGlAccountCategory)); 195 } 196 } else { 197 addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName); 198 } 199 } 200 } else { 201 addExecutionError(ExecutionErrors.UnknownTransactionTypeName.name(), transactionTypeName); 202 } 203 204 return result; 205 } 206 207}