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.GetContentCatalogItemForm;
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.ContentCatalogItem;
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 GetContentCatalogItemCommand
042        extends BaseSingleEntityCommand<ContentCatalogItem, GetContentCatalogItemForm> {
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("ItemName", FieldType.ENTITY_NAME, true, null, null),
053                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
057                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
058                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
059                );
060    }
061    
062    /** Creates a new instance of GetContentCatalogItemCommand */
063    public GetContentCatalogItemCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    @Override
068    protected ContentCatalogItem getEntity() {
069        var contentWebAddressName = form.getContentWebAddressName();
070        var contentCollectionName = form.getContentCollectionName();
071        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
072        ContentCatalogItem contentCatalogItem = null;
073
074        if(parameterCount == 1) {
075            var contentControl = Session.getModelController(ContentControl.class);
076            ContentCollection contentCollection = null;
077
078            if(contentWebAddressName != null) {
079                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
080
081                if(contentWebAddress != null) {
082                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
085                }
086            } else {
087                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
088
089                if(contentCollection == null) {
090                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
091                }
092            }
093
094            if(!hasExecutionErrors()) {
095                var itemControl = Session.getModelController(ItemControl.class);
096                var itemName = form.getItemName();
097                var item = itemControl.getItemByName(itemName);
098
099                if(item != null) {
100                    var inventoryControl = Session.getModelController(InventoryControl.class);
101                    var inventoryConditionName = form.getInventoryConditionName();
102                    var inventoryCondition = inventoryConditionName == null ? inventoryControl.getDefaultInventoryCondition()
103                            : inventoryControl.getInventoryConditionByName(inventoryConditionName);
104
105                    if(inventoryCondition != null) {
106                        var uomControl = Session.getModelController(UomControl.class);
107                        var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
108                        var itemDetail = item.getLastDetail();
109                        var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
110                        var unitOfMeasureType = unitOfMeasureTypeName == null ? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind)
111                                : uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
112
113                        if(unitOfMeasureType != null) {
114                            var accountingControl = Session.getModelController(AccountingControl.class);
115                            var currencyIsoName = form.getCurrencyIsoName();
116                            var currency = currencyIsoName == null ? accountingControl.getDefaultCurrency()
117                                    : accountingControl.getCurrencyByIsoName(currencyIsoName);
118
119                            if(currency != null) {
120                                var contentCatalogName = form.getContentCatalogName();
121                                var partyPK = getPartyPK();
122                                var userVisit = getUserVisitForUpdate();
123
124                                var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
125                                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
126
127                                if(contentCatalog != null) {
128                                    contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, item, inventoryCondition,
129                                            unitOfMeasureType, currency);
130
131                                    if(contentCatalogItem != null) {
132                                        AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, userVisit, contentCatalog.getPrimaryKey(), partyPK);
133
134                                        if(!hasExecutionErrors()) {
135                                            sendEvent(contentCatalog.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
136                                        }
137                                    } else {
138                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollection.getLastDetail().getContentCollectionName(),
139                                                contentCatalog.getLastDetail().getContentCatalogName(), itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName);
140                                    }
141                                } else {
142                                    if(contentCatalogName == null) {
143                                        addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), contentCollection.getLastDetail().getContentCollectionName());
144                                    } else {
145                                        addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollection.getLastDetail().getContentCollectionName(),
146                                                contentCatalogName);
147                                    }
148                                }
149                            } else {
150                                if(currencyIsoName == null) {
151                                    addExecutionError(ExecutionErrors.UnknownDefaultCurrency.name());
152                                } else {
153                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
154                                }
155                            }
156                        } else {
157                            if(unitOfMeasureTypeName == null) {
158                                addExecutionError(ExecutionErrors.UnknownDefaultUnitOfMeasureType.name(),
159                                        unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName());
160                            } else {
161                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
162                                        unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
163                            }
164                        }
165                    } else {
166                        if(inventoryConditionName == null) {
167                            addExecutionError(ExecutionErrors.UnknownDefaultInventoryCondition.name());
168                        } else {
169                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
170                        }
171                    }
172                } else {
173                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
174                }
175            }
176        } else {
177            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
178        }
179
180        return contentCatalogItem;
181    }
182    
183    @Override
184    protected BaseResult getResult(ContentCatalogItem contentCatalogItem) {
185        var result = ContentResultFactory.getGetContentCatalogItemResult();
186
187        if (contentCatalogItem != null) {
188            var contentControl = Session.getModelController(ContentControl.class);
189
190            result.setContentCatalogItem(contentControl.getContentCatalogItemTransfer(getUserVisit(), contentCatalogItem));
191        }
192
193        return result;
194    }
195    
196}