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.GlAccountClassEdit; 021import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 022import com.echothree.control.user.accounting.common.result.EditGlAccountClassResult; 023import com.echothree.control.user.accounting.common.spec.GlAccountClassSpec; 024import com.echothree.model.control.accounting.server.control.AccountingControl; 025import com.echothree.model.control.accounting.server.logic.GlAccountClassLogic; 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.GlAccountClass; 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 EditGlAccountClassCommand 043 extends BaseAbstractEditCommand<GlAccountClassSpec, GlAccountClassEdit, EditGlAccountClassResult, GlAccountClass, GlAccountClass> { 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.GlAccountClass.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null) 059 ); 060 061 EDIT_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ParentGlAccountClassName", 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 GlAccountClassLogic glAccountClassLogic; 075 076 /** Creates a new instance of EditGlAccountClassCommand */ 077 public EditGlAccountClassCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditGlAccountClassResult getResult() { 083 return AccountingResultFactory.getEditGlAccountClassResult(); 084 } 085 086 @Override 087 public GlAccountClassEdit getEdit() { 088 return AccountingEditFactory.getGlAccountClassEdit(); 089 } 090 091 @Override 092 public GlAccountClass getEntity(EditGlAccountClassResult result) { 093 return glAccountClassLogic.getGlAccountClassByName(this, spec.getGlAccountClassName(), editModeToEntityPermission(editMode)); 094 } 095 096 @Override 097 public GlAccountClass getLockEntity(GlAccountClass glAccountClass) { 098 return glAccountClass; 099 } 100 101 @Override 102 public void fillInResult(EditGlAccountClassResult result, GlAccountClass glAccountClass) { 103 result.setGlAccountClass(accountingControl.getGlAccountClassTransfer(getUserVisit(), glAccountClass)); 104 } 105 106 private GlAccountClass parentGlAccountClass; 107 108 @Override 109 public void doLock(GlAccountClassEdit edit, GlAccountClass glAccountClass) { 110 var glAccountClassDescription = accountingControl.getGlAccountClassDescription(glAccountClass, getPreferredLanguage()); 111 var glAccountClassDetail = glAccountClass.getLastDetail(); 112 var parentGlAccountClass = glAccountClassDetail.getParentGlAccountClass(); 113 114 edit.setGlAccountClassName(glAccountClassDetail.getGlAccountClassName()); 115 edit.setParentGlAccountClassName(parentGlAccountClass == null? null: parentGlAccountClass.getLastDetail().getGlAccountClassName()); 116 edit.setIsDefault(glAccountClassDetail.getIsDefault().toString()); 117 edit.setSortOrder(glAccountClassDetail.getSortOrder().toString()); 118 119 if(glAccountClassDescription != null) { 120 edit.setDescription(glAccountClassDescription.getDescription()); 121 } 122 } 123 124 @Override 125 public void canUpdate(GlAccountClass glAccountClass) { 126 var glAccountClassName = edit.getGlAccountClassName(); 127 var duplicateGlAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 128 129 if(duplicateGlAccountClass == null || glAccountClass.equals(duplicateGlAccountClass)) { 130 var parentGlAccountClassName = edit.getParentGlAccountClassName(); 131 132 parentGlAccountClass = parentGlAccountClassName == null? null: accountingControl.getGlAccountClassByName(parentGlAccountClassName); 133 134 if(parentGlAccountClassName == null || parentGlAccountClass != null) { 135 if(!accountingControl.isParentGlAccountClassSafe(glAccountClass, parentGlAccountClass)) { 136 addExecutionError(ExecutionErrors.InvalidParentGlAccountClass.name()); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownParentGlAccountClassName.name(), parentGlAccountClassName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.DuplicateGlAccountClassName.name(), glAccountClassName); 143 } 144 } 145 146 @Override 147 public void doUpdate(GlAccountClass glAccountClass) { 148 var partyPK = getPartyPK(); 149 var glAccountClassDetailValue = accountingControl.getGlAccountClassDetailValueForUpdate(glAccountClass); 150 var glAccountClassDescription = accountingControl.getGlAccountClassDescriptionForUpdate(glAccountClass, getPreferredLanguage()); 151 var description = edit.getDescription(); 152 153 glAccountClassDetailValue.setGlAccountClassName(edit.getGlAccountClassName()); 154 glAccountClassDetailValue.setParentGlAccountClassPK(parentGlAccountClass == null? null: parentGlAccountClass.getPrimaryKey()); 155 glAccountClassDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 156 glAccountClassDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 157 158 accountingControl.updateGlAccountClassFromValue(glAccountClassDetailValue, partyPK); 159 160 if(glAccountClassDescription == null && description != null) { 161 accountingControl.createGlAccountClassDescription(glAccountClass, getPreferredLanguage(), description, partyPK); 162 } else if(glAccountClassDescription != null && description == null) { 163 accountingControl.deleteGlAccountClassDescription(glAccountClassDescription, partyPK); 164 } else if(glAccountClassDescription != null && description != null) { 165 var glAccountClassDescriptionValue = accountingControl.getGlAccountClassDescriptionValue(glAccountClassDescription); 166 167 glAccountClassDescriptionValue.setDescription(description); 168 accountingControl.updateGlAccountClassDescriptionFromValue(glAccountClassDescriptionValue, partyPK); 169 } 170 } 171 172}