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.GetContentCategoryItemForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.associate.server.logic.AssociateReferralLogic; 023import com.echothree.model.control.content.server.control.ContentControl; 024import com.echothree.model.control.core.common.EventTypes; 025import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 026import com.echothree.model.control.item.server.control.ItemControl; 027import com.echothree.model.control.uom.server.control.UomControl; 028import com.echothree.model.data.content.server.entity.ContentCategoryItem; 029import com.echothree.model.data.content.server.entity.ContentCollection; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.control.BaseSingleEntityCommand; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class GetContentCategoryItemCommand 042 extends BaseSingleEntityCommand<ContentCategoryItem, GetContentCategoryItemForm> { 043 044 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 FORM_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 050 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null), 055 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 056 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 058 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 059 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 060 ); 061 } 062 063 @Inject 064 AccountingControl accountingControl; 065 066 @Inject 067 ContentControl contentControl; 068 069 @Inject 070 InventoryConditionControl inventoryConditionControl; 071 072 @Inject 073 ItemControl itemControl; 074 075 @Inject 076 UomControl uomControl; 077 078 @Inject 079 AssociateReferralLogic associateReferralLogic; 080 081 082 /** Creates a new instance of GetContentCategoryItemCommand */ 083 public GetContentCategoryItemCommand() { 084 super(null, FORM_FIELD_DEFINITIONS, true); 085 } 086 087 @Override 088 protected ContentCategoryItem getEntity() { 089 var contentWebAddressName = form.getContentWebAddressName(); 090 var contentCollectionName = form.getContentCollectionName(); 091 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 092 ContentCategoryItem contentCategoryItem = null; 093 094 if(parameterCount == 1) { 095 ContentCollection contentCollection = null; 096 097 if(contentWebAddressName != null) { 098 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 099 100 if(contentWebAddress != null) { 101 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 102 } else { 103 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 104 } 105 } else { 106 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 107 108 if(contentCollection == null) { 109 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 110 } 111 } 112 113 if(!hasExecutionErrors()) { 114 var itemName = form.getItemName(); 115 var item = itemControl.getItemByName(itemName); 116 117 if(item != null) { 118 var inventoryConditionName = form.getInventoryConditionName(); 119 var inventoryCondition = inventoryConditionName == null ? inventoryConditionControl.getDefaultInventoryCondition() 120 : inventoryConditionControl.getInventoryConditionByName(inventoryConditionName); 121 122 if(inventoryCondition != null) { 123 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 124 var itemDetail = item.getLastDetail(); 125 var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind(); 126 var unitOfMeasureType = unitOfMeasureTypeName == null ? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind) 127 : uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 128 129 if(unitOfMeasureType != null) { 130 var currencyIsoName = form.getCurrencyIsoName(); 131 var currency = currencyIsoName == null ? accountingControl.getDefaultCurrency() 132 : accountingControl.getCurrencyByIsoName(currencyIsoName); 133 134 if(currency != null) { 135 var contentCatalogName = form.getContentCatalogName(); 136 var partyPK = getPartyPK(); 137 var userVisit = getUserVisitForUpdate(); 138 139 var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection) 140 : contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 141 142 if(contentCatalog != null) { 143 var contentCategoryName = form.getContentCategoryName(); 144 var contentCategory = contentCategoryName == null ? null 145 : contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 146 147 if(contentCategoryName == null || contentCategory != null) { 148 var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, item, 149 inventoryCondition, unitOfMeasureType, currency); 150 151 if(contentCatalogItem != null) { 152 // If contentCategory is null, we'll attempt to find the item in a default ContentCategory 153 // that it had been placed in. Always direct the user to the item if possible in a category 154 // if we can. 155 contentCategoryItem = contentCategory == null ? contentControl.getDefaultContentCategoryItem(contentCatalogItem) 156 : contentControl.getContentCategoryItem(contentCategory, contentCatalogItem); 157 158 if(contentCategoryItem != null) { 159 if(contentCategory == null) { 160 contentCategory = contentCategoryItem.getContentCategory(); 161 } 162 163 associateReferralLogic.handleAssociateReferral(session, this, form, userVisit, contentCategory.getPrimaryKey(), partyPK); 164 165 if(!hasExecutionErrors()) { 166 sendEvent(contentCategory.getPrimaryKey(), EventTypes.READ, null, null, partyPK); 167 } 168 } else { 169 if(contentCategoryName == null) { 170 addExecutionError(ExecutionErrors.UnknownDefaultContentCategoryItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 171 contentCatalogName, itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName); 172 } else { 173 addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 174 contentCatalogName, contentCategoryName, itemName, inventoryConditionName, unitOfMeasureTypeName, 175 currencyIsoName); 176 } 177 } 178 } else { 179 addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 180 contentCatalog.getLastDetail().getContentCatalogName(), itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName); 181 } 182 } else { 183 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCollection.getLastDetail().getContentCollectionName(), 184 contentCatalog.getLastDetail().getContentCatalogName(), contentCategoryName); 185 } 186 } else { 187 if(contentCatalogName == null) { 188 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), contentCollection.getLastDetail().getContentCollectionName()); 189 } else { 190 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollection.getLastDetail().getContentCollectionName(), 191 contentCatalogName); 192 } 193 } 194 } else { 195 if(currencyIsoName == null) { 196 addExecutionError(ExecutionErrors.UnknownDefaultCurrency.name()); 197 } else { 198 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 199 } 200 } 201 } else { 202 if(unitOfMeasureTypeName == null) { 203 addExecutionError(ExecutionErrors.UnknownDefaultUnitOfMeasureType.name(), 204 unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName()); 205 } else { 206 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), 207 unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName); 208 } 209 } 210 } else { 211 if(inventoryConditionName == null) { 212 addExecutionError(ExecutionErrors.UnknownDefaultInventoryCondition.name()); 213 } else { 214 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 215 } 216 } 217 } else { 218 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 219 } 220 } 221 } else { 222 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 223 } 224 225 return contentCategoryItem; 226 } 227 228 @Override 229 protected BaseResult getResult(ContentCategoryItem contentCategoryItem) { 230 var result = ContentResultFactory.getGetContentCategoryItemResult(); 231 232 if (contentCategoryItem != null) { 233 result.setContentCategoryItem(contentControl.getContentCategoryItemTransfer(getUserVisit(), contentCategoryItem)); 234 } 235 236 return result; 237 } 238 239}