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