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.item.server.command;
018
019import com.echothree.control.user.item.common.form.CreateItemVolumeForm;
020import com.echothree.model.control.item.server.control.ItemControl;
021import com.echothree.model.control.uom.common.UomConstants;
022import com.echothree.model.control.uom.server.control.UomControl;
023import com.echothree.model.control.uom.server.util.Conversion;
024import com.echothree.model.data.item.server.entity.Item;
025import com.echothree.model.data.item.server.entity.ItemUnitOfMeasureType;
026import com.echothree.model.data.item.server.entity.ItemVolume;
027import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
028import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039
040public class CreateItemVolumeCommand
041        extends BaseSimpleCommand<CreateItemVolumeForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
047                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
051                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
053                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
055                ));
056    }
057    
058    /** Creates a new instance of CreateItemVolumeCommand */
059    public CreateItemVolumeCommand(UserVisitPK userVisitPK, CreateItemVolumeForm form) {
060        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
061    }
062    
063    @Override
064    protected BaseResult execute() {
065        var itemControl = Session.getModelController(ItemControl.class);
066        String itemName = form.getItemName();
067        Item item = itemControl.getItemByName(itemName);
068        
069        if(item != null) {
070            var uomControl = Session.getModelController(UomControl.class);
071            String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
072            UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(),
073                    unitOfMeasureTypeName);
074            
075            if(unitOfMeasureType != null) {
076                ItemUnitOfMeasureType itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType);
077                
078                if(itemUnitOfMeasureType != null) {
079                    ItemVolume itemVolume = itemControl.getItemVolume(item, unitOfMeasureType);
080                    
081                    if(itemVolume == null) {
082                        UnitOfMeasureKind volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
083                        
084                        if(volumeUnitOfMeasureKind != null) {
085                            String heightUnitOfMeasureTypeName = form.getHeightUnitOfMeasureTypeName();
086                            UnitOfMeasureType heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
087                                    heightUnitOfMeasureTypeName);
088                            
089                            if(heightUnitOfMeasureType != null) {
090                                Long height = Long.valueOf(form.getHeight());
091                                
092                                if(height > 0) {
093                                    String widthUnitOfMeasureTypeName = form.getWidthUnitOfMeasureTypeName();
094                                    UnitOfMeasureType widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
095                                            widthUnitOfMeasureTypeName);
096                                    
097                                    if(widthUnitOfMeasureType != null) {
098                                        Long width = Long.valueOf(form.getWidth());
099                                        
100                                        if(width > 0) {
101                                            String depthUnitOfMeasureTypeName = form.getDepthUnitOfMeasureTypeName();
102                                            UnitOfMeasureType depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
103                                                    depthUnitOfMeasureTypeName);
104                                            
105                                            if(depthUnitOfMeasureType != null) {
106                                                Long depth = Long.valueOf(form.getDepth());
107                                                
108                                                if(depth > 0) {
109                                                    Conversion heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
110                                                    Conversion widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
111                                                    Conversion depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
112                                                    
113                                                    itemControl.createItemVolume(item, unitOfMeasureType, heightConversion.getQuantity(),
114                                                            widthConversion.getQuantity(), depthConversion.getQuantity(), getPartyPK());
115                                                } else {
116                                                    addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
117                                                }
118                                            } else {
119                                                addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
120                                            }
121                                        } else {
122                                            addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
123                                        }
124                                    } else {
125                                        addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
126                                    }
127                                } else {
128                                    addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
129                                }
130                            } else {
131                                addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
132                            }
133                        } else {
134                            addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
135                        }
136                    } else {
137                        addExecutionError(ExecutionErrors.DuplicateItemVolume.name());
138                    }
139                } else {
140                    addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName);
141                }
142            } else {
143                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
144            }
145        } else {
146            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
147        }
148        
149        return null;
150    }
151    
152}