001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.control.user.content.common.result.GetContentCatalogItemResult;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
024import com.echothree.model.control.content.server.control.ContentControl;
025import com.echothree.model.control.core.common.EventTypes;
026import com.echothree.model.control.inventory.server.control.InventoryControl;
027import com.echothree.model.control.item.server.control.ItemControl;
028import com.echothree.model.control.uom.server.control.UomControl;
029import com.echothree.model.data.accounting.server.entity.Currency;
030import com.echothree.model.data.content.server.entity.ContentCatalog;
031import com.echothree.model.data.content.server.entity.ContentCatalogItem;
032import com.echothree.model.data.content.server.entity.ContentCollection;
033import com.echothree.model.data.content.server.entity.ContentWebAddress;
034import com.echothree.model.data.inventory.server.entity.InventoryCondition;
035import com.echothree.model.data.item.server.entity.Item;
036import com.echothree.model.data.item.server.entity.ItemDetail;
037import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
038import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
039import com.echothree.model.data.user.common.pk.UserVisitPK;
040import com.echothree.model.data.user.server.entity.UserVisit;
041import com.echothree.util.common.message.ExecutionErrors;
042import com.echothree.util.common.validation.FieldDefinition;
043import com.echothree.util.common.validation.FieldType;
044import com.echothree.util.common.command.BaseResult;
045import com.echothree.util.server.control.BaseSingleEntityCommand;
046import com.echothree.util.server.persistence.Session;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.List;
050
051public class GetContentCatalogItemCommand
052        extends BaseSingleEntityCommand<ContentCatalogItem, GetContentCatalogItemForm> {
053    
054    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
055    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
056    
057    static {
058        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
059                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
060                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
067                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
068                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
069                ));
070    }
071    
072    /** Creates a new instance of GetContentCatalogItemCommand */
073    public GetContentCatalogItemCommand(UserVisitPK userVisitPK, GetContentCatalogItemForm form) {
074        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
075    }
076    
077    @Override
078    protected ContentCatalogItem getEntity() {
079        String contentWebAddressName = form.getContentWebAddressName();
080        String contentCollectionName = form.getContentCollectionName();
081        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
082        ContentCatalogItem contentCatalogItem = null;
083
084        if(parameterCount == 1) {
085            var contentControl = Session.getModelController(ContentControl.class);
086            ContentCollection contentCollection = null;
087
088            if(contentWebAddressName != null) {
089                ContentWebAddress contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
090
091                if(contentWebAddress != null) {
092                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
093                } else {
094                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
095                }
096            } else {
097                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
098
099                if(contentCollection == null) {
100                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
101                }
102            }
103
104            if(!hasExecutionErrors()) {
105                var itemControl = Session.getModelController(ItemControl.class);
106                String itemName = form.getItemName();
107                Item item = itemControl.getItemByName(itemName);
108
109                if(item != null) {
110                    var inventoryControl = Session.getModelController(InventoryControl.class);
111                    String inventoryConditionName = form.getInventoryConditionName();
112                    InventoryCondition inventoryCondition = inventoryConditionName == null ? inventoryControl.getDefaultInventoryCondition()
113                            : inventoryControl.getInventoryConditionByName(inventoryConditionName);
114
115                    if(inventoryCondition != null) {
116                        var uomControl = Session.getModelController(UomControl.class);
117                        String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
118                        ItemDetail itemDetail = item.getLastDetail();
119                        UnitOfMeasureKind unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
120                        UnitOfMeasureType unitOfMeasureType = unitOfMeasureTypeName == null ? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind)
121                                : uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
122
123                        if(unitOfMeasureType != null) {
124                            var accountingControl = Session.getModelController(AccountingControl.class);
125                            String currencyIsoName = form.getCurrencyIsoName();
126                            Currency currency = currencyIsoName == null ? accountingControl.getDefaultCurrency()
127                                    : accountingControl.getCurrencyByIsoName(currencyIsoName);
128
129                            if(currency != null) {
130                                String contentCatalogName = form.getContentCatalogName();
131                                var partyPK = getPartyPK();
132                                UserVisit userVisit = getUserVisitForUpdate();
133
134                                ContentCatalog contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
135                                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
136
137                                if(contentCatalog != null) {
138                                    contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, item, inventoryCondition,
139                                            unitOfMeasureType, currency);
140
141                                    if(contentCatalogItem != null) {
142                                        AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, userVisit, contentCatalog.getPrimaryKey(), partyPK);
143
144                                        if(!hasExecutionErrors()) {
145                                            sendEvent(contentCatalog.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
146                                        }
147                                    } else {
148                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollection.getLastDetail().getContentCollectionName(),
149                                                contentCatalog.getLastDetail().getContentCatalogName(), itemName, inventoryConditionName, unitOfMeasureTypeName, currencyIsoName);
150                                    }
151                                } else {
152                                    if(contentCatalogName == null) {
153                                        addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), contentCollection.getLastDetail().getContentCollectionName());
154                                    } else {
155                                        addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollection.getLastDetail().getContentCollectionName(),
156                                                contentCatalogName);
157                                    }
158                                }
159                            } else {
160                                if(currencyIsoName == null) {
161                                    addExecutionError(ExecutionErrors.UnknownDefaultCurrency.name());
162                                } else {
163                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
164                                }
165                            }
166                        } else {
167                            if(unitOfMeasureTypeName == null) {
168                                addExecutionError(ExecutionErrors.UnknownDefaultUnitOfMeasureType.name(),
169                                        unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName());
170                            } else {
171                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
172                                        unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
173                            }
174                        }
175                    } else {
176                        if(inventoryConditionName == null) {
177                            addExecutionError(ExecutionErrors.UnknownDefaultInventoryCondition.name());
178                        } else {
179                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
180                        }
181                    }
182                } else {
183                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
184                }
185            }
186        } else {
187            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
188        }
189
190        return contentCatalogItem;
191    }
192    
193    @Override
194    protected BaseResult getResult(ContentCatalogItem contentCatalogItem) {
195        GetContentCatalogItemResult result = ContentResultFactory.getGetContentCatalogItemResult();
196
197        if (contentCatalogItem != null) {
198            var contentControl = Session.getModelController(ContentControl.class);
199
200            result.setContentCatalogItem(contentControl.getContentCatalogItemTransfer(getUserVisit(), contentCatalogItem));
201        }
202
203        return result;
204    }
205    
206}