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.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 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 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 /** Creates a new instance of EditGlAccountCategoryCommand */ 075 public EditGlAccountCategoryCommand() { 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.getEditGlAccountCategoryResult(); 083 084 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 085 var glAccountCategoryName = spec.getGlAccountCategoryName(); 086 var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 087 088 if(glAccountCategory != null) { 089 if(editMode.equals(EditMode.LOCK)) { 090 if(lockEntity(glAccountCategory)) { 091 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage()); 092 var edit = AccountingEditFactory.getGlAccountCategoryEdit(); 093 var glAccountCategoryDetail = glAccountCategory.getLastDetail(); 094 var parentGlAccountCategory = glAccountCategoryDetail.getParentGlAccountCategory(); 095 096 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory)); 097 098 result.setEdit(edit); 099 edit.setGlAccountCategoryName(glAccountCategoryDetail.getGlAccountCategoryName()); 100 edit.setParentGlAccountCategoryName(parentGlAccountCategory == null? null: parentGlAccountCategory.getLastDetail().getGlAccountCategoryName()); 101 edit.setIsDefault(glAccountCategoryDetail.getIsDefault().toString()); 102 edit.setSortOrder(glAccountCategoryDetail.getSortOrder().toString()); 103 104 if(glAccountCategoryDescription != null) { 105 edit.setDescription(glAccountCategoryDescription.getDescription()); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 109 } 110 111 result.setEntityLock(getEntityLockTransfer(glAccountCategory)); 112 } else { // EditMode.ABANDON 113 unlockEntity(glAccountCategory); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 117 } 118 } else if(editMode.equals(EditMode.UPDATE)) { 119 var glAccountCategoryName = spec.getGlAccountCategoryName(); 120 var glAccountCategory = accountingControl.getGlAccountCategoryByNameForUpdate(glAccountCategoryName); 121 122 if(glAccountCategory != null) { 123 glAccountCategoryName = edit.getGlAccountCategoryName(); 124 var duplicateGlAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 125 126 if(duplicateGlAccountCategory == null || glAccountCategory.equals(duplicateGlAccountCategory)) { 127 var parentGlAccountCategoryName = edit.getParentGlAccountCategoryName(); 128 GlAccountCategory parentGlAccountCategory = null; 129 130 if(parentGlAccountCategoryName != null) { 131 parentGlAccountCategory = accountingControl.getGlAccountCategoryByName(parentGlAccountCategoryName); 132 } 133 134 if(parentGlAccountCategoryName == null || parentGlAccountCategory != null) { 135 if(accountingControl.isParentGlAccountCategorySafe(glAccountCategory, parentGlAccountCategory)) { 136 if(lockEntityForUpdate(glAccountCategory)) { 137 try { 138 var partyPK = getPartyPK(); 139 var glAccountCategoryDetailValue = accountingControl.getGlAccountCategoryDetailValueForUpdate(glAccountCategory); 140 var glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescriptionForUpdate(glAccountCategory, getPreferredLanguage()); 141 var description = edit.getDescription(); 142 143 glAccountCategoryDetailValue.setGlAccountCategoryName(edit.getGlAccountCategoryName()); 144 glAccountCategoryDetailValue.setParentGlAccountCategoryPK(parentGlAccountCategory == null? null: parentGlAccountCategory.getPrimaryKey()); 145 glAccountCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 146 glAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 147 148 accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, partyPK); 149 150 if(glAccountCategoryDescription == null && description != null) { 151 accountingControl.createGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage(), description, partyPK); 152 } else if(glAccountCategoryDescription != null && description == null) { 153 accountingControl.deleteGlAccountCategoryDescription(glAccountCategoryDescription, partyPK); 154 } else if(glAccountCategoryDescription != null && description != null) { 155 var glAccountCategoryDescriptionValue = accountingControl.getGlAccountCategoryDescriptionValue(glAccountCategoryDescription); 156 157 glAccountCategoryDescriptionValue.setDescription(description); 158 accountingControl.updateGlAccountCategoryDescriptionFromValue(glAccountCategoryDescriptionValue, partyPK); 159 } 160 } finally { 161 unlockEntity(glAccountCategory); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.EntityLockStale.name()); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.InvalidParentGlAccountCategory.name()); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.UnknownParentGlAccountCategoryName.name(), parentGlAccountCategoryName); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.DuplicateGlAccountCategoryName.name(), glAccountCategoryName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 177 } 178 179 if(hasExecutionErrors()) { 180 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory)); 181 result.setEntityLock(getEntityLockTransfer(glAccountCategory)); 182 } 183 } 184 185 return result; 186 } 187 188}