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.content.server.command; 018 019import com.echothree.control.user.content.common.form.SetDefaultContentCategoryItemForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.content.server.control.ContentControl; 022import com.echothree.model.control.content.server.logic.ContentLogic; 023import com.echothree.model.control.inventory.server.control.InventoryControl; 024import com.echothree.model.control.item.server.control.ItemControl; 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.control.uom.server.control.UomControl; 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.server.control.BaseSimpleCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041 042@Dependent 043public class SetDefaultContentCategoryItemCommand 044 extends BaseSimpleCommand<SetDefaultContentCategoryItemForm> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Edit.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null) 064 ); 065 } 066 067 /** Creates a new instance of SetDefaultContentCategoryItemCommand */ 068 public SetDefaultContentCategoryItemCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 070 } 071 072 @Override 073 protected BaseResult execute() { 074 var contentControl = Session.getModelController(ContentControl.class); 075 var contentCollectionName = form.getContentCollectionName(); 076 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 077 078 if(contentCollection != null) { 079 var contentCatalogName = form.getContentCatalogName(); 080 var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 081 082 if(contentCatalog != null) { 083 var contentCategoryName = form.getContentCategoryName(); 084 var contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 085 086 if(contentCategory != null) { 087 var itemControl = Session.getModelController(ItemControl.class); 088 var itemName = form.getItemName(); 089 var item = itemControl.getItemByName(itemName); 090 091 if(item != null) { 092 var inventoryControl = Session.getModelController(InventoryControl.class); 093 var inventoryConditionName = form.getInventoryConditionName(); 094 var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 095 096 if(inventoryCondition != null) { 097 var uomControl = Session.getModelController(UomControl.class); 098 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 099 var itemDetail = item.getLastDetail(); 100 var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind(); 101 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 102 103 if(unitOfMeasureType != null) { 104 var accountingControl = Session.getModelController(AccountingControl.class); 105 var currencyIsoName = form.getCurrencyIsoName(); 106 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 107 108 if(currency != null) { 109 var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, 110 item, inventoryCondition, unitOfMeasureType, currency); 111 112 if(contentCatalogItem != null) { 113 var contentCategoryItemValue = contentControl.getContentCategoryItemValueForUpdate(contentCategory, 114 contentCatalogItem); 115 116 if(contentCategoryItemValue != null) { 117 contentCategoryItemValue.setIsDefault(true); 118 119 ContentLogic.getInstance().updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK()); 120 } else { 121 addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(), 122 contentCollection.getLastDetail().getContentCollectionName(), 123 contentCatalog.getLastDetail().getContentCatalogName(), 124 contentCategory.getLastDetail().getContentCategoryName(), 125 item.getLastDetail().getItemName(), 126 inventoryCondition.getLastDetail().getInventoryConditionName(), 127 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 128 currency.getCurrencyIsoName()); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), 132 contentCollection.getLastDetail().getContentCollectionName(), 133 contentCatalog.getLastDetail().getContentCatalogName(), 134 item.getLastDetail().getItemName(), 135 inventoryCondition.getLastDetail().getInventoryConditionName(), 136 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 137 currency.getCurrencyIsoName()); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), 153 contentCollection.getLastDetail().getContentCollectionName(), 154 contentCatalog.getLastDetail().getContentCatalogName(), contentCategoryName); 155 } 156 } else { 157 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 158 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 162 } 163 164 return null; 165 } 166 167}