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.SetDefaultContentCategoryItemForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.content.server.control.ContentControl;
022import com.echothree.model.control.content.server.logic.ContentLogic;
023import com.echothree.model.control.inventory.server.control.InventoryControl;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
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.ContentCollection;
034import com.echothree.model.data.content.server.value.ContentCategoryItemValue;
035import com.echothree.model.data.inventory.server.entity.InventoryCondition;
036import com.echothree.model.data.item.server.entity.Item;
037import com.echothree.model.data.item.server.entity.ItemDetail;
038import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
039import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
040import com.echothree.model.data.user.common.pk.UserVisitPK;
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.BaseSimpleCommand;
046import com.echothree.util.server.control.CommandSecurityDefinition;
047import com.echothree.util.server.control.PartyTypeDefinition;
048import com.echothree.util.server.control.SecurityRoleDefinition;
049import com.echothree.util.server.persistence.Session;
050import java.util.Arrays;
051import java.util.Collections;
052import java.util.List;
053
054public class SetDefaultContentCategoryItemCommand
055        extends BaseSimpleCommand<SetDefaultContentCategoryItemForm> {
056    
057    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
058    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
059    
060    static {
061        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
062                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
063                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Edit.name())
064                        )))
065                )));
066        
067        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
074                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null)
075                ));
076    }
077    
078    /** Creates a new instance of SetDefaultContentCategoryItemCommand */
079    public SetDefaultContentCategoryItemCommand(UserVisitPK userVisitPK, SetDefaultContentCategoryItemForm form) {
080        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var contentControl = Session.getModelController(ContentControl.class);
086        String contentCollectionName = form.getContentCollectionName();
087        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
088        
089        if(contentCollection != null) {
090            String contentCatalogName = form.getContentCatalogName();
091            ContentCatalog contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
092            
093            if(contentCatalog != null) {
094                String contentCategoryName = form.getContentCategoryName();
095                ContentCategory contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
096                
097                if(contentCategory != null) {
098                    var itemControl = Session.getModelController(ItemControl.class);
099                    String itemName = form.getItemName();
100                    Item item = itemControl.getItemByName(itemName);
101                    
102                    if(item != null) {
103                        var inventoryControl = Session.getModelController(InventoryControl.class);
104                        String inventoryConditionName = form.getInventoryConditionName();
105                        InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
106                        
107                        if(inventoryCondition != null) {
108                            var uomControl = Session.getModelController(UomControl.class);
109                            String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
110                            ItemDetail itemDetail = item.getLastDetail();
111                            UnitOfMeasureKind unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
112                            UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
113                            
114                            if(unitOfMeasureType != null) {
115                                var accountingControl = Session.getModelController(AccountingControl.class);
116                                String currencyIsoName = form.getCurrencyIsoName();
117                                Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
118                                
119                                if(currency != null) {
120                                    ContentCatalogItem contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog,
121                                            item, inventoryCondition, unitOfMeasureType, currency);
122                                    
123                                    if(contentCatalogItem != null) {
124                                        ContentCategoryItemValue contentCategoryItemValue = contentControl.getContentCategoryItemValueForUpdate(contentCategory,
125                                                contentCatalogItem);
126                                        
127                                        if(contentCategoryItemValue != null) {
128                                            contentCategoryItemValue.setIsDefault(Boolean.TRUE);
129                                            
130                                            ContentLogic.getInstance().updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK());
131                                        } else {
132                                            addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name(),
133                                                    contentCollection.getLastDetail().getContentCollectionName(),
134                                                    contentCatalog.getLastDetail().getContentCatalogName(),
135                                                    contentCategory.getLastDetail().getContentCategoryName(),
136                                                    item.getLastDetail().getItemName(),
137                                                    inventoryCondition.getLastDetail().getInventoryConditionName(),
138                                                    unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
139                                                    currency.getCurrencyIsoName());
140                                        }
141                                    } else {
142                                        addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(),
143                                                contentCollection.getLastDetail().getContentCollectionName(),
144                                                contentCatalog.getLastDetail().getContentCatalogName(),
145                                                item.getLastDetail().getItemName(),
146                                                inventoryCondition.getLastDetail().getInventoryConditionName(),
147                                                unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
148                                                currency.getCurrencyIsoName());
149                                    }
150                                } else {
151                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
152                                }
153                            } else {
154                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
155                            }
156                        } else {
157                            addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
158                        }
159                    } else {
160                        addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
161                    }
162                } else {
163                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
164                            contentCollection.getLastDetail().getContentCollectionName(),
165                            contentCatalog.getLastDetail().getContentCatalogName(), contentCategoryName);
166                }
167            } else {
168                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
169                        contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
170            }
171        } else {
172            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
173        }
174        
175        return null;
176    }
177    
178}