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