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.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.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditItemAccountingCategoryCommand 047 extends BaseAbstractEditCommand<ItemAccountingCategoryUniversalSpec, ItemAccountingCategoryEdit, EditItemAccountingCategoryResult, ItemAccountingCategory, ItemAccountingCategory> { 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.ItemAccountingCategory.name(), SecurityRoles.Edit.name()) 058 ))) 059 ))); 060 061 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("ParentItemAccountingCategoryName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 071 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of EditItemAccountingCategoryCommand */ 077 public EditItemAccountingCategoryCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditItemAccountingCategoryResult getResult() { 083 return AccountingResultFactory.getEditItemAccountingCategoryResult(); 084 } 085 086 @Override 087 public ItemAccountingCategoryEdit getEdit() { 088 return AccountingEditFactory.getItemAccountingCategoryEdit(); 089 } 090 091 @Override 092 public ItemAccountingCategory getEntity(EditItemAccountingCategoryResult result) { 093 return ItemAccountingCategoryLogic.getInstance().getItemAccountingCategoryByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 094 } 095 096 @Override 097 public ItemAccountingCategory getLockEntity(ItemAccountingCategory itemAccountingCategory) { 098 return itemAccountingCategory; 099 } 100 101 @Override 102 public void fillInResult(EditItemAccountingCategoryResult result, ItemAccountingCategory itemAccountingCategory) { 103 var accountingControl = Session.getModelController(AccountingControl.class); 104 105 result.setItemAccountingCategory(accountingControl.getItemAccountingCategoryTransfer(getUserVisit(), itemAccountingCategory)); 106 } 107 108 ItemAccountingCategory parentItemAccountingCategory = null; 109 110 @Override 111 public void doLock(ItemAccountingCategoryEdit edit, ItemAccountingCategory itemAccountingCategory) { 112 var accountingControl = Session.getModelController(AccountingControl.class); 113 var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage()); 114 var itemAccountingCategoryDetail = itemAccountingCategory.getLastDetail(); 115 116 parentItemAccountingCategory = itemAccountingCategoryDetail.getParentItemAccountingCategory(); 117 118 edit.setItemAccountingCategoryName(itemAccountingCategoryDetail.getItemAccountingCategoryName()); 119 edit.setParentItemAccountingCategoryName(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getLastDetail().getItemAccountingCategoryName()); 120 edit.setIsDefault(itemAccountingCategoryDetail.getIsDefault().toString()); 121 edit.setSortOrder(itemAccountingCategoryDetail.getSortOrder().toString()); 122 123 if(itemAccountingCategoryDescription != null) { 124 edit.setDescription(itemAccountingCategoryDescription.getDescription()); 125 } 126 } 127 128 @Override 129 public void canUpdate(ItemAccountingCategory itemAccountingCategory) { 130 var accountingControl = Session.getModelController(AccountingControl.class); 131 var itemAccountingCategoryName = edit.getItemAccountingCategoryName(); 132 var duplicateItemAccountingCategory = accountingControl.getItemAccountingCategoryByName(itemAccountingCategoryName); 133 134 if(duplicateItemAccountingCategory == null || itemAccountingCategory.equals(duplicateItemAccountingCategory)) { 135 var parentItemAccountingCategoryName = edit.getParentItemAccountingCategoryName(); 136 137 parentItemAccountingCategory = parentItemAccountingCategoryName == null? null: accountingControl.getItemAccountingCategoryByName(parentItemAccountingCategoryName); 138 139 if(parentItemAccountingCategoryName == null || parentItemAccountingCategory != null) { 140 if(parentItemAccountingCategory != null) { 141 if(!accountingControl.isParentItemAccountingCategorySafe(itemAccountingCategory, parentItemAccountingCategory)) { 142 addExecutionError(ExecutionErrors.InvalidParentItemAccountingCategory.name()); 143 } 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownParentItemAccountingCategoryName.name(), parentItemAccountingCategoryName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.DuplicateItemAccountingCategoryName.name(), itemAccountingCategoryName); 150 } 151 } 152 153 @Override 154 public void doUpdate(ItemAccountingCategory itemAccountingCategory) { 155 var accountingControl = Session.getModelController(AccountingControl.class); 156 var partyPK = getPartyPK(); 157 var itemAccountingCategoryDetailValue = accountingControl.getItemAccountingCategoryDetailValueForUpdate(itemAccountingCategory); 158 var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescriptionForUpdate(itemAccountingCategory, getPreferredLanguage()); 159 var description = edit.getDescription(); 160 161 itemAccountingCategoryDetailValue.setItemAccountingCategoryName(edit.getItemAccountingCategoryName()); 162 itemAccountingCategoryDetailValue.setParentItemAccountingCategoryPK(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getPrimaryKey()); 163 itemAccountingCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 164 itemAccountingCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 165 166 ItemAccountingCategoryLogic.getInstance().updateItemAccountingCategoryFromValue(itemAccountingCategoryDetailValue, partyPK); 167 168 if(itemAccountingCategoryDescription == null && description != null) { 169 accountingControl.createItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage(), description, partyPK); 170 } else if(itemAccountingCategoryDescription != null && description == null) { 171 accountingControl.deleteItemAccountingCategoryDescription(itemAccountingCategoryDescription, partyPK); 172 } else if(itemAccountingCategoryDescription != null && description != null) { 173 var itemAccountingCategoryDescriptionValue = accountingControl.getItemAccountingCategoryDescriptionValue(itemAccountingCategoryDescription); 174 175 itemAccountingCategoryDescriptionValue.setDescription(description); 176 accountingControl.updateItemAccountingCategoryDescriptionFromValue(itemAccountingCategoryDescriptionValue, partyPK); 177 } 178 } 179 180}