001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.form.EditGlAccountClassForm; 022import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 023import com.echothree.control.user.accounting.common.spec.GlAccountClassSpec; 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.GlAccountClass; 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 com.echothree.util.server.persistence.Session; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditGlAccountClassCommand 047 extends BaseEditCommand<GlAccountClassSpec, GlAccountClassEdit> { 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(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.GlAccountClass.name(), SecurityRoles.Edit.name()) 058 ))) 059 ))); 060 061 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null) 063 )); 064 065 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("ParentGlAccountClassName", 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 /** Creates a new instance of EditGlAccountClassCommand */ 075 public EditGlAccountClassCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 protected BaseResult execute() { 081 var accountingControl = Session.getModelController(AccountingControl.class); 082 var result = AccountingResultFactory.getEditGlAccountClassResult(); 083 084 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 085 var glAccountClassName = spec.getGlAccountClassName(); 086 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 087 088 if(glAccountClass != null) { 089 if(editMode.equals(EditMode.LOCK)) { 090 if(lockEntity(glAccountClass)) { 091 var glAccountClassDescription = accountingControl.getGlAccountClassDescription(glAccountClass, getPreferredLanguage()); 092 var edit = AccountingEditFactory.getGlAccountClassEdit(); 093 var glAccountClassDetail = glAccountClass.getLastDetail(); 094 var parentGlAccountClass = glAccountClassDetail.getParentGlAccountClass(); 095 096 result.setGlAccountClass(accountingControl.getGlAccountClassTransfer(getUserVisit(), glAccountClass)); 097 098 result.setEdit(edit); 099 edit.setGlAccountClassName(glAccountClassDetail.getGlAccountClassName()); 100 edit.setParentGlAccountClassName(parentGlAccountClass == null? null: parentGlAccountClass.getLastDetail().getGlAccountClassName()); 101 edit.setIsDefault(glAccountClassDetail.getIsDefault().toString()); 102 edit.setSortOrder(glAccountClassDetail.getSortOrder().toString()); 103 104 if(glAccountClassDescription != null) { 105 edit.setDescription(glAccountClassDescription.getDescription()); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 109 } 110 111 result.setEntityLock(getEntityLockTransfer(glAccountClass)); 112 } else { // EditMode.ABANDON 113 unlockEntity(glAccountClass); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 117 } 118 } else if(editMode.equals(EditMode.UPDATE)) { 119 var glAccountClassName = spec.getGlAccountClassName(); 120 var glAccountClass = accountingControl.getGlAccountClassByNameForUpdate(glAccountClassName); 121 122 if(glAccountClass != null) { 123 glAccountClassName = edit.getGlAccountClassName(); 124 var duplicateGlAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 125 126 if(duplicateGlAccountClass == null || glAccountClass.equals(duplicateGlAccountClass)) { 127 var parentGlAccountClassName = edit.getParentGlAccountClassName(); 128 GlAccountClass parentGlAccountClass = null; 129 130 if(parentGlAccountClassName != null) { 131 parentGlAccountClass = accountingControl.getGlAccountClassByName(parentGlAccountClassName); 132 } 133 134 if(parentGlAccountClassName == null || parentGlAccountClass != null) { 135 if(accountingControl.isParentGlAccountClassSafe(glAccountClass, parentGlAccountClass)) { 136 if(lockEntityForUpdate(glAccountClass)) { 137 try { 138 var partyPK = getPartyPK(); 139 var glAccountClassDetailValue = accountingControl.getGlAccountClassDetailValueForUpdate(glAccountClass); 140 var glAccountClassDescription = accountingControl.getGlAccountClassDescriptionForUpdate(glAccountClass, getPreferredLanguage()); 141 var description = edit.getDescription(); 142 143 glAccountClassDetailValue.setGlAccountClassName(edit.getGlAccountClassName()); 144 glAccountClassDetailValue.setParentGlAccountClassPK(parentGlAccountClass == null? null: parentGlAccountClass.getPrimaryKey()); 145 glAccountClassDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 146 glAccountClassDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 147 148 accountingControl.updateGlAccountClassFromValue(glAccountClassDetailValue, partyPK); 149 150 if(glAccountClassDescription == null && description != null) { 151 accountingControl.createGlAccountClassDescription(glAccountClass, getPreferredLanguage(), description, partyPK); 152 } else if(glAccountClassDescription != null && description == null) { 153 accountingControl.deleteGlAccountClassDescription(glAccountClassDescription, partyPK); 154 } else if(glAccountClassDescription != null && description != null) { 155 var glAccountClassDescriptionValue = accountingControl.getGlAccountClassDescriptionValue(glAccountClassDescription); 156 157 glAccountClassDescriptionValue.setDescription(description); 158 accountingControl.updateGlAccountClassDescriptionFromValue(glAccountClassDescriptionValue, partyPK); 159 } 160 } finally { 161 unlockEntity(glAccountClass); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.EntityLockStale.name()); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.InvalidParentGlAccountClass.name()); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.UnknownParentGlAccountClassName.name(), parentGlAccountClassName); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.DuplicateGlAccountClassName.name(), glAccountClassName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 177 } 178 179 if(hasExecutionErrors()) { 180 result.setGlAccountClass(accountingControl.getGlAccountClassTransfer(getUserVisit(), glAccountClass)); 181 result.setEntityLock(getEntityLockTransfer(glAccountClass)); 182 } 183 } 184 185 return result; 186 } 187 188}