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.result.AccountingResultFactory; 022import com.echothree.control.user.accounting.common.result.EditGlAccountCategoryResult; 023import com.echothree.control.user.accounting.common.spec.GlAccountCategorySpec; 024import com.echothree.model.control.accounting.server.control.AccountingControl; 025import com.echothree.model.control.accounting.server.logic.GlAccountCategoryLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.accounting.server.entity.GlAccountCategory; 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.server.control.BaseAbstractEditCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class EditGlAccountCategoryCommand 043 extends BaseAbstractEditCommand<GlAccountCategorySpec, GlAccountCategoryEdit, EditGlAccountCategoryResult, GlAccountCategory, GlAccountCategory> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 053 new SecurityRoleDefinition(SecurityRoleGroups.GlAccountCategory.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null) 059 ); 060 061 EDIT_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ParentGlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 065 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 067 ); 068 } 069 070 @Inject 071 AccountingControl accountingControl; 072 073 @Inject 074 GlAccountCategoryLogic glAccountCategoryLogic; 075 076 /** Creates a new instance of EditGlAccountCategoryCommand */ 077 public EditGlAccountCategoryCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditGlAccountCategoryResult getResult() { 083 return AccountingResultFactory.getEditGlAccountCategoryResult(); 084 } 085 086 @Override 087 public GlAccountCategoryEdit getEdit() { 088 return AccountingEditFactory.getGlAccountCategoryEdit(); 089 } 090 091 @Override 092 public GlAccountCategory getEntity(EditGlAccountCategoryResult result) { 093 return glAccountCategoryLogic.getGlAccountCategoryByName(this, spec.getGlAccountCategoryName(), editModeToEntityPermission(editMode)); 094 } 095 096 @Override 097 public GlAccountCategory getLockEntity(GlAccountCategory glAccountCategory) { 098 return glAccountCategory; 099 } 100 101 @Override 102 public void fillInResult(EditGlAccountCategoryResult result, GlAccountCategory glAccountCategory) { 103 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory)); 104 } 105 106 private GlAccountCategory parentGlAccountCategory; 107 108 @Override 109 public void doLock(GlAccountCategoryEdit edit, GlAccountCategory glAccountCategory) { 110 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage()); 111 var glAccountCategoryDetail = glAccountCategory.getLastDetail(); 112 var parentGlAccountCategory = glAccountCategoryDetail.getParentGlAccountCategory(); 113 114 edit.setGlAccountCategoryName(glAccountCategoryDetail.getGlAccountCategoryName()); 115 edit.setParentGlAccountCategoryName(parentGlAccountCategory == null? null: parentGlAccountCategory.getLastDetail().getGlAccountCategoryName()); 116 edit.setIsDefault(glAccountCategoryDetail.getIsDefault().toString()); 117 edit.setSortOrder(glAccountCategoryDetail.getSortOrder().toString()); 118 119 if(glAccountCategoryDescription != null) { 120 edit.setDescription(glAccountCategoryDescription.getDescription()); 121 } 122 } 123 124 @Override 125 public void canUpdate(GlAccountCategory glAccountCategory) { 126 var glAccountCategoryName = edit.getGlAccountCategoryName(); 127 var duplicateGlAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 128 129 if(duplicateGlAccountCategory == null || glAccountCategory.equals(duplicateGlAccountCategory)) { 130 var parentGlAccountCategoryName = edit.getParentGlAccountCategoryName(); 131 132 parentGlAccountCategory = parentGlAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(parentGlAccountCategoryName); 133 134 if(parentGlAccountCategoryName == null || parentGlAccountCategory != null) { 135 if(!accountingControl.isParentGlAccountCategorySafe(glAccountCategory, parentGlAccountCategory)) { 136 addExecutionError(ExecutionErrors.InvalidParentGlAccountCategory.name()); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownParentGlAccountCategoryName.name(), parentGlAccountCategoryName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.DuplicateGlAccountCategoryName.name(), glAccountCategoryName); 143 } 144 } 145 146 @Override 147 public void doUpdate(GlAccountCategory glAccountCategory) { 148 var partyPK = getPartyPK(); 149 var glAccountCategoryDetailValue = accountingControl.getGlAccountCategoryDetailValueForUpdate(glAccountCategory); 150 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescriptionForUpdate(glAccountCategory, getPreferredLanguage()); 151 var description = edit.getDescription(); 152 153 glAccountCategoryDetailValue.setGlAccountCategoryName(edit.getGlAccountCategoryName()); 154 glAccountCategoryDetailValue.setParentGlAccountCategoryPK(parentGlAccountCategory == null? null: parentGlAccountCategory.getPrimaryKey()); 155 glAccountCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 156 glAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 157 158 accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, partyPK); 159 160 if(glAccountCategoryDescription == null && description != null) { 161 accountingControl.createGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage(), description, partyPK); 162 } else if(glAccountCategoryDescription != null && description == null) { 163 accountingControl.deleteGlAccountCategoryDescription(glAccountCategoryDescription, partyPK); 164 } else if(glAccountCategoryDescription != null && description != null) { 165 var glAccountCategoryDescriptionValue = accountingControl.getGlAccountCategoryDescriptionValue(glAccountCategoryDescription); 166 167 glAccountCategoryDescriptionValue.setDescription(description); 168 accountingControl.updateGlAccountCategoryDescriptionFromValue(glAccountCategoryDescriptionValue, partyPK); 169 } 170 } 171 172}