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.item.server.command; 018 019import com.echothree.control.user.item.common.edit.ItemCategoryEdit; 020import com.echothree.control.user.item.common.edit.ItemEditFactory; 021import com.echothree.control.user.item.common.form.EditItemCategoryForm; 022import com.echothree.control.user.item.common.result.EditItemCategoryResult; 023import com.echothree.control.user.item.common.result.ItemResultFactory; 024import com.echothree.model.control.core.common.EntityTypes; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.control.item.server.control.ItemControl; 027import com.echothree.model.control.item.server.logic.ItemCategoryLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.item.server.entity.ItemCategory; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseAbstractEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import com.echothree.control.user.item.common.spec.ItemCategoryUniversalSpec; 042import com.echothree.model.control.core.common.ComponentVendors; 043import javax.enterprise.context.Dependent; 044import javax.inject.Inject; 045 046@Dependent 047public class EditItemCategoryCommand 048 extends BaseAbstractEditCommand<ItemCategoryUniversalSpec, ItemCategoryEdit, EditItemCategoryResult, ItemCategory, ItemCategory> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 052 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.ItemCategory.name(), SecurityRoles.Edit.name()) 059 )) 060 )); 061 062 SPEC_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 065 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("ParentItemCategoryName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 072 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 073 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 074 ); 075 } 076 077 @Inject 078 ItemControl itemControl; 079 080 @Inject 081 EntityInstanceLogic entityInstanceLogic; 082 083 @Inject 084 ItemCategoryLogic itemCategoryLogic; 085 086 087 /** Creates a new instance of EditItemCategoryCommand */ 088 public EditItemCategoryCommand() { 089 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 090 } 091 092 @Override 093 public EditItemCategoryResult getResult() { 094 return ItemResultFactory.getEditItemCategoryResult(); 095 } 096 097 @Override 098 public ItemCategoryEdit getEdit() { 099 return ItemEditFactory.getItemCategoryEdit(); 100 } 101 102 @Override 103 public ItemCategory getEntity(EditItemCategoryResult result) { 104 ItemCategory itemCategory = null; 105 var itemCategoryName = spec.getItemCategoryName(); 106 var parameterCount = (itemCategoryName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(spec); 107 108 if(parameterCount == 1) { 109 if(itemCategoryName == null) { 110 var entityInstance = entityInstanceLogic.getEntityInstance(this, spec, ComponentVendors.ECHO_THREE.name(), 111 EntityTypes.ItemCategory.name()); 112 113 if(!hasExecutionErrors()) { 114 itemCategory = itemControl.getItemCategoryByEntityInstance(entityInstance, editModeToEntityPermission(editMode)); 115 } 116 } else { 117 itemCategory = itemCategoryLogic.getItemCategoryByName(this, itemCategoryName, editModeToEntityPermission(editMode)); 118 } 119 120 if(itemCategory != null) { 121 result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory)); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 125 } 126 127 return itemCategory; 128 } 129 130 @Override 131 public ItemCategory getLockEntity(ItemCategory itemCategory) { 132 return itemCategory; 133 } 134 135 @Override 136 public void fillInResult(EditItemCategoryResult result, ItemCategory itemCategory) { 137 result.setItemCategory(itemControl.getItemCategoryTransfer(getUserVisit(), itemCategory)); 138 } 139 140 ItemCategory parentItemCategory = null; 141 142 @Override 143 public void doLock(ItemCategoryEdit edit, ItemCategory itemCategory) { 144 var itemCategoryDescription = itemControl.getItemCategoryDescription(itemCategory, getPreferredLanguage()); 145 var itemCategoryDetail = itemCategory.getLastDetail(); 146 147 parentItemCategory = itemCategoryDetail.getParentItemCategory(); 148 149 edit.setItemCategoryName(itemCategoryDetail.getItemCategoryName()); 150 edit.setParentItemCategoryName(parentItemCategory == null? null: parentItemCategory.getLastDetail().getItemCategoryName()); 151 edit.setIsDefault(itemCategoryDetail.getIsDefault().toString()); 152 edit.setSortOrder(itemCategoryDetail.getSortOrder().toString()); 153 154 if(itemCategoryDescription != null) { 155 edit.setDescription(itemCategoryDescription.getDescription()); 156 } 157 } 158 159 @Override 160 public void canUpdate(ItemCategory itemCategory) { 161 var itemCategoryName = edit.getItemCategoryName(); 162 var duplicateItemCategory = itemControl.getItemCategoryByName(itemCategoryName); 163 164 if(duplicateItemCategory == null || itemCategory.equals(duplicateItemCategory)) { 165 var parentItemCategoryName = edit.getParentItemCategoryName(); 166 167 parentItemCategory = parentItemCategoryName == null? null: itemControl.getItemCategoryByName(parentItemCategoryName); 168 169 if(parentItemCategoryName == null || parentItemCategory != null) { 170 if(parentItemCategory != null) { 171 if(!itemControl.isParentItemCategorySafe(itemCategory, parentItemCategory)) { 172 addExecutionError(ExecutionErrors.InvalidParentItemCategory.name()); 173 } 174 } 175 } else { 176 addExecutionError(ExecutionErrors.UnknownParentItemCategoryName.name(), parentItemCategoryName); 177 } 178 } else { 179 addExecutionError(ExecutionErrors.DuplicateItemCategoryName.name(), itemCategoryName); 180 } 181 } 182 183 @Override 184 public void doUpdate(ItemCategory itemCategory) { 185 var partyPK = getPartyPK(); 186 var itemCategoryDetailValue = itemControl.getItemCategoryDetailValueForUpdate(itemCategory); 187 var itemCategoryDescription = itemControl.getItemCategoryDescriptionForUpdate(itemCategory, getPreferredLanguage()); 188 var description = edit.getDescription(); 189 190 itemCategoryDetailValue.setItemCategoryName(edit.getItemCategoryName()); 191 itemCategoryDetailValue.setParentItemCategoryPK(parentItemCategory == null? null: parentItemCategory.getPrimaryKey()); 192 itemCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 193 itemCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 194 195 itemControl.updateItemCategoryFromValue(itemCategoryDetailValue, partyPK); 196 197 if(itemCategoryDescription == null && description != null) { 198 itemControl.createItemCategoryDescription(itemCategory, getPreferredLanguage(), description, partyPK); 199 } else if(itemCategoryDescription != null && description == null) { 200 itemControl.deleteItemCategoryDescription(itemCategoryDescription, partyPK); 201 } else if(itemCategoryDescription != null && description != null) { 202 var itemCategoryDescriptionValue = itemControl.getItemCategoryDescriptionValue(itemCategoryDescription); 203 204 itemCategoryDescriptionValue.setDescription(description); 205 itemControl.updateItemCategoryDescriptionFromValue(itemCategoryDescriptionValue, partyPK); 206 } 207 } 208 209}