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