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.InventoryControl; 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 com.echothree.util.server.persistence.Session; 037import java.util.List; 038import javax.enterprise.context.Dependent; 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 /** Creates a new instance of GetContentCategoryItemCommand */ 064 public GetContentCategoryItemCommand() { 065 super(null, FORM_FIELD_DEFINITIONS, true); 066 } 067 068 @Override 069 protected ContentCategoryItem getEntity() { 070 var contentWebAddressName = form.getContentWebAddressName(); 071 var contentCollectionName = form.getContentCollectionName(); 072 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 073 ContentCategoryItem contentCategoryItem = null; 074 075 if(parameterCount == 1) { 076 var contentControl = Session.getModelController(ContentControl.class); 077 ContentCollection contentCollection = null; 078 079 if(contentWebAddressName != null) { 080 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 081 082 if(contentWebAddress != null) { 083 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 084 } else { 085 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 086 } 087 } else { 088 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 089 090 if(contentCollection == null) { 091 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 092 } 093 } 094 095 if(!hasExecutionErrors()) { 096 var itemControl = Session.getModelController(ItemControl.class); 097 var itemName = form.getItemName(); 098 var item = itemControl.getItemByName(itemName); 099 100 if(item != null) { 101 var inventoryControl = Session.getModelController(InventoryControl.class); 102 var inventoryConditionName = form.getInventoryConditionName(); 103 var inventoryCondition = inventoryConditionName == null ? inventoryControl.getDefaultInventoryCondition() 104 : inventoryControl.getInventoryConditionByName(inventoryConditionName); 105 106 if(inventoryCondition != null) { 107 var uomControl = Session.getModelController(UomControl.class); 108 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 109 var itemDetail = item.getLastDetail(); 110 var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind(); 111 var unitOfMeasureType = unitOfMeasureTypeName == null ? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind) 112 : uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 113 114 if(unitOfMeasureType != null) { 115 var accountingControl = Session.getModelController(AccountingControl.class); 116 var currencyIsoName = form.getCurrencyIsoName(); 117 var currency = currencyIsoName == null ? accountingControl.getDefaultCurrency() 118 : accountingControl.getCurrencyByIsoName(currencyIsoName); 119 120 if(currency != null) { 121 var contentCatalogName = form.getContentCatalogName(); 122 var partyPK = getPartyPK(); 123 var userVisit = getUserVisitForUpdate(); 124 125 var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection) 126 : contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 127 128 if(contentCatalog != null) { 129 var contentCategoryName = form.getContentCategoryName(); 130 var contentCategory = contentCategoryName == null ? null 131 : contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 132 133 if(contentCategoryName == null || contentCategory != null) { 134 var contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, item, 135 inventoryCondition, unitOfMeasureType, currency); 136 137 if(contentCatalogItem != null) { 138 // If contentCategory is null, we'll attempt to find the item in a default ContentCategory 139 // that it had been placed in. Always direct the user to the item if possible in a category 140 // if we can. 141 contentCategoryItem = contentCategory == null ? contentControl.getDefaultContentCategoryItem(contentCatalogItem) 142 : contentControl.getContentCategoryItem(contentCategory, contentCatalogItem); 143 144 if(contentCategoryItem != null) { 145 if(contentCategory == null) { 146 contentCategory = contentCategoryItem.getContentCategory(); 147 } 148 149 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, userVisit, contentCategory.getPrimaryKey(), partyPK); 150 151 if(!hasExecutionErrors()) { 152 sendEvent(contentCategory.getPrimaryKey(), EventTypes.READ, null, null, partyPK); 153 } 154 } else { 155 if(contentCategoryName == null) { 156 addExecutionError(ExecutionErrors.UnknownDefaultContentCategoryItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 157 contentCatalogName, itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName); 158 } else { 159 addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 160 contentCatalogName, contentCategoryName, itemName, inventoryConditionName, unitOfMeasureTypeName, 161 currencyIsoName); 162 } 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollection.getLastDetail().getContentCollectionName(), 166 contentCatalog.getLastDetail().getContentCatalogName(), itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName); 167 } 168 } else { 169 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCollection.getLastDetail().getContentCollectionName(), 170 contentCatalog.getLastDetail().getContentCatalogName(), contentCategoryName); 171 } 172 } else { 173 if(contentCatalogName == null) { 174 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), contentCollection.getLastDetail().getContentCollectionName()); 175 } else { 176 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollection.getLastDetail().getContentCollectionName(), 177 contentCatalogName); 178 } 179 } 180 } else { 181 if(currencyIsoName == null) { 182 addExecutionError(ExecutionErrors.UnknownDefaultCurrency.name()); 183 } else { 184 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 185 } 186 } 187 } else { 188 if(unitOfMeasureTypeName == null) { 189 addExecutionError(ExecutionErrors.UnknownDefaultUnitOfMeasureType.name(), 190 unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName()); 191 } else { 192 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), 193 unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName); 194 } 195 } 196 } else { 197 if(inventoryConditionName == null) { 198 addExecutionError(ExecutionErrors.UnknownDefaultInventoryCondition.name()); 199 } else { 200 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 201 } 202 } 203 } else { 204 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 205 } 206 } 207 } else { 208 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 209 } 210 211 return contentCategoryItem; 212 } 213 214 @Override 215 protected BaseResult getResult(ContentCategoryItem contentCategoryItem) { 216 var result = ContentResultFactory.getGetContentCategoryItemResult(); 217 218 if (contentCategoryItem != null) { 219 var contentControl = Session.getModelController(ContentControl.class); 220 221 result.setContentCategoryItem(contentControl.getContentCategoryItemTransfer(getUserVisit(), contentCategoryItem)); 222 } 223 224 return result; 225 } 226 227}