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.ItemAccountingCategoryEdit; 021import com.echothree.control.user.accounting.common.form.EditItemAccountingCategoryForm; 022import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 023import com.echothree.control.user.accounting.common.result.EditItemAccountingCategoryResult; 024import com.echothree.control.user.accounting.common.spec.ItemAccountingCategoryUniversalSpec; 025import com.echothree.model.control.accounting.server.control.AccountingControl; 026import com.echothree.model.control.accounting.server.logic.ItemAccountingCategoryLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.accounting.server.entity.ItemAccountingCategory; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 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.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditItemAccountingCategoryCommand 045 extends BaseAbstractEditCommand<ItemAccountingCategoryUniversalSpec, ItemAccountingCategoryEdit, EditItemAccountingCategoryResult, ItemAccountingCategory, ItemAccountingCategory> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.ItemAccountingCategory.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("ParentItemAccountingCategoryName", 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 EditItemAccountingCategoryCommand */ 075 public EditItemAccountingCategoryCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditItemAccountingCategoryResult getResult() { 081 return AccountingResultFactory.getEditItemAccountingCategoryResult(); 082 } 083 084 @Override 085 public ItemAccountingCategoryEdit getEdit() { 086 return AccountingEditFactory.getItemAccountingCategoryEdit(); 087 } 088 089 @Override 090 public ItemAccountingCategory getEntity(EditItemAccountingCategoryResult result) { 091 return ItemAccountingCategoryLogic.getInstance().getItemAccountingCategoryByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 092 } 093 094 @Override 095 public ItemAccountingCategory getLockEntity(ItemAccountingCategory itemAccountingCategory) { 096 return itemAccountingCategory; 097 } 098 099 @Override 100 public void fillInResult(EditItemAccountingCategoryResult result, ItemAccountingCategory itemAccountingCategory) { 101 var accountingControl = Session.getModelController(AccountingControl.class); 102 103 result.setItemAccountingCategory(accountingControl.getItemAccountingCategoryTransfer(getUserVisit(), itemAccountingCategory)); 104 } 105 106 ItemAccountingCategory parentItemAccountingCategory = null; 107 108 @Override 109 public void doLock(ItemAccountingCategoryEdit edit, ItemAccountingCategory itemAccountingCategory) { 110 var accountingControl = Session.getModelController(AccountingControl.class); 111 var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage()); 112 var itemAccountingCategoryDetail = itemAccountingCategory.getLastDetail(); 113 114 parentItemAccountingCategory = itemAccountingCategoryDetail.getParentItemAccountingCategory(); 115 116 edit.setItemAccountingCategoryName(itemAccountingCategoryDetail.getItemAccountingCategoryName()); 117 edit.setParentItemAccountingCategoryName(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getLastDetail().getItemAccountingCategoryName()); 118 edit.setIsDefault(itemAccountingCategoryDetail.getIsDefault().toString()); 119 edit.setSortOrder(itemAccountingCategoryDetail.getSortOrder().toString()); 120 121 if(itemAccountingCategoryDescription != null) { 122 edit.setDescription(itemAccountingCategoryDescription.getDescription()); 123 } 124 } 125 126 @Override 127 public void canUpdate(ItemAccountingCategory itemAccountingCategory) { 128 var accountingControl = Session.getModelController(AccountingControl.class); 129 var itemAccountingCategoryName = edit.getItemAccountingCategoryName(); 130 var duplicateItemAccountingCategory = accountingControl.getItemAccountingCategoryByName(itemAccountingCategoryName); 131 132 if(duplicateItemAccountingCategory == null || itemAccountingCategory.equals(duplicateItemAccountingCategory)) { 133 var parentItemAccountingCategoryName = edit.getParentItemAccountingCategoryName(); 134 135 parentItemAccountingCategory = parentItemAccountingCategoryName == null? null: accountingControl.getItemAccountingCategoryByName(parentItemAccountingCategoryName); 136 137 if(parentItemAccountingCategoryName == null || parentItemAccountingCategory != null) { 138 if(parentItemAccountingCategory != null) { 139 if(!accountingControl.isParentItemAccountingCategorySafe(itemAccountingCategory, parentItemAccountingCategory)) { 140 addExecutionError(ExecutionErrors.InvalidParentItemAccountingCategory.name()); 141 } 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownParentItemAccountingCategoryName.name(), parentItemAccountingCategoryName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.DuplicateItemAccountingCategoryName.name(), itemAccountingCategoryName); 148 } 149 } 150 151 @Override 152 public void doUpdate(ItemAccountingCategory itemAccountingCategory) { 153 var accountingControl = Session.getModelController(AccountingControl.class); 154 var partyPK = getPartyPK(); 155 var itemAccountingCategoryDetailValue = accountingControl.getItemAccountingCategoryDetailValueForUpdate(itemAccountingCategory); 156 var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescriptionForUpdate(itemAccountingCategory, getPreferredLanguage()); 157 var description = edit.getDescription(); 158 159 itemAccountingCategoryDetailValue.setItemAccountingCategoryName(edit.getItemAccountingCategoryName()); 160 itemAccountingCategoryDetailValue.setParentItemAccountingCategoryPK(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getPrimaryKey()); 161 itemAccountingCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 162 itemAccountingCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 163 164 ItemAccountingCategoryLogic.getInstance().updateItemAccountingCategoryFromValue(itemAccountingCategoryDetailValue, partyPK); 165 166 if(itemAccountingCategoryDescription == null && description != null) { 167 accountingControl.createItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage(), description, partyPK); 168 } else if(itemAccountingCategoryDescription != null && description == null) { 169 accountingControl.deleteItemAccountingCategoryDescription(itemAccountingCategoryDescription, partyPK); 170 } else if(itemAccountingCategoryDescription != null && description != null) { 171 var itemAccountingCategoryDescriptionValue = accountingControl.getItemAccountingCategoryDescriptionValue(itemAccountingCategoryDescription); 172 173 itemAccountingCategoryDescriptionValue.setDescription(description); 174 accountingControl.updateItemAccountingCategoryDescriptionFromValue(itemAccountingCategoryDescriptionValue, partyPK); 175 } 176 } 177 178}