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.GlAccountCategoryEdit; 021import com.echothree.control.user.accounting.common.form.EditGlAccountCategoryForm; 022import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 023import com.echothree.control.user.accounting.common.spec.GlAccountCategorySpec; 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.accounting.server.entity.GlAccountCategory; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.server.control.BaseEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class EditGlAccountCategoryCommand 047 extends BaseEditCommand<GlAccountCategorySpec, GlAccountCategoryEdit> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.GlAccountCategory.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("ParentGlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 069 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 070 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 071 ); 072 } 073 074 @Inject 075 AccountingControl accountingControl; 076 077 078 /** Creates a new instance of EditGlAccountCategoryCommand */ 079 public EditGlAccountCategoryCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 protected BaseResult execute() { 085 var result = AccountingResultFactory.getEditGlAccountCategoryResult(); 086 087 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 088 var glAccountCategoryName = spec.getGlAccountCategoryName(); 089 var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 090 091 if(glAccountCategory != null) { 092 if(editMode.equals(EditMode.LOCK)) { 093 if(lockEntity(glAccountCategory)) { 094 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage()); 095 var edit = AccountingEditFactory.getGlAccountCategoryEdit(); 096 var glAccountCategoryDetail = glAccountCategory.getLastDetail(); 097 var parentGlAccountCategory = glAccountCategoryDetail.getParentGlAccountCategory(); 098 099 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory)); 100 101 result.setEdit(edit); 102 edit.setGlAccountCategoryName(glAccountCategoryDetail.getGlAccountCategoryName()); 103 edit.setParentGlAccountCategoryName(parentGlAccountCategory == null? null: parentGlAccountCategory.getLastDetail().getGlAccountCategoryName()); 104 edit.setIsDefault(glAccountCategoryDetail.getIsDefault().toString()); 105 edit.setSortOrder(glAccountCategoryDetail.getSortOrder().toString()); 106 107 if(glAccountCategoryDescription != null) { 108 edit.setDescription(glAccountCategoryDescription.getDescription()); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 112 } 113 114 result.setEntityLock(getEntityLockTransfer(glAccountCategory)); 115 } else { // EditMode.ABANDON 116 unlockEntity(glAccountCategory); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 120 } 121 } else if(editMode.equals(EditMode.UPDATE)) { 122 var glAccountCategoryName = spec.getGlAccountCategoryName(); 123 var glAccountCategory = accountingControl.getGlAccountCategoryByNameForUpdate(glAccountCategoryName); 124 125 if(glAccountCategory != null) { 126 glAccountCategoryName = edit.getGlAccountCategoryName(); 127 var duplicateGlAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 128 129 if(duplicateGlAccountCategory == null || glAccountCategory.equals(duplicateGlAccountCategory)) { 130 var parentGlAccountCategoryName = edit.getParentGlAccountCategoryName(); 131 GlAccountCategory parentGlAccountCategory = null; 132 133 if(parentGlAccountCategoryName != null) { 134 parentGlAccountCategory = accountingControl.getGlAccountCategoryByName(parentGlAccountCategoryName); 135 } 136 137 if(parentGlAccountCategoryName == null || parentGlAccountCategory != null) { 138 if(accountingControl.isParentGlAccountCategorySafe(glAccountCategory, parentGlAccountCategory)) { 139 if(lockEntityForUpdate(glAccountCategory)) { 140 try { 141 var partyPK = getPartyPK(); 142 var glAccountCategoryDetailValue = accountingControl.getGlAccountCategoryDetailValueForUpdate(glAccountCategory); 143 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescriptionForUpdate(glAccountCategory, getPreferredLanguage()); 144 var description = edit.getDescription(); 145 146 glAccountCategoryDetailValue.setGlAccountCategoryName(edit.getGlAccountCategoryName()); 147 glAccountCategoryDetailValue.setParentGlAccountCategoryPK(parentGlAccountCategory == null? null: parentGlAccountCategory.getPrimaryKey()); 148 glAccountCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 149 glAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 150 151 accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, partyPK); 152 153 if(glAccountCategoryDescription == null && description != null) { 154 accountingControl.createGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage(), description, partyPK); 155 } else if(glAccountCategoryDescription != null && description == null) { 156 accountingControl.deleteGlAccountCategoryDescription(glAccountCategoryDescription, partyPK); 157 } else if(glAccountCategoryDescription != null && description != null) { 158 var glAccountCategoryDescriptionValue = accountingControl.getGlAccountCategoryDescriptionValue(glAccountCategoryDescription); 159 160 glAccountCategoryDescriptionValue.setDescription(description); 161 accountingControl.updateGlAccountCategoryDescriptionFromValue(glAccountCategoryDescriptionValue, partyPK); 162 } 163 } finally { 164 unlockEntity(glAccountCategory); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.EntityLockStale.name()); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.InvalidParentGlAccountCategory.name()); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.UnknownParentGlAccountCategoryName.name(), parentGlAccountCategoryName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.DuplicateGlAccountCategoryName.name(), glAccountCategoryName); 177 } 178 } else { 179 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 180 } 181 182 if(hasExecutionErrors()) { 183 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory)); 184 result.setEntityLock(getEntityLockTransfer(glAccountCategory)); 185 } 186 } 187 188 return result; 189 } 190 191}