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