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