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