001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.GetRelatedItemForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.item.server.logic.RelatedItemTypeLogic;
023import com.echothree.model.data.item.server.entity.RelatedItem;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSingleEntityCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class GetRelatedItemCommand
036        extends BaseSingleEntityCommand<RelatedItem, GetRelatedItemForm> {
037
038    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("RelatedItemTypeName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("FromItemName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ToItemName", FieldType.ENTITY_NAME, true, null, null)
046        );
047    }
048
049    @Inject
050    ItemControl itemControl;
051
052    @Inject
053    RelatedItemTypeLogic relatedItemTypeLogic;
054
055    
056    /** Creates a new instance of GetRelatedItemCommand */
057    public GetRelatedItemCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060
061
062    @Override
063    protected RelatedItem getEntity() {
064        var relatedItemType = relatedItemTypeLogic.getRelatedItemTypeByName(this, form.getRelatedItemTypeName());
065        RelatedItem relatedItem = null;
066
067        if(relatedItemType != null) {
068            var fromItemName = form.getFromItemName();
069            var fromItem = itemControl.getItemByName(fromItemName);
070
071            if(fromItem != null) {
072                var toItemName = form.getToItemName();
073                var toItem = itemControl.getItemByName(toItemName);
074
075                if(toItem != null) {
076                    relatedItem = itemControl.getRelatedItem(relatedItemType, fromItem, toItem);
077
078                    if(relatedItem == null) {
079                        addExecutionError(ExecutionErrors.UnknownRelatedItem.name(),
080                                relatedItemType.getLastDetail().getRelatedItemTypeName(),
081                                fromItem.getLastDetail().getItemName(), toItem.getLastDetail().getItemName());
082                    }
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName);
085                }
086            } else {
087                addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName);
088            }
089        }
090
091        return relatedItem;
092    }
093
094    @Override
095    protected BaseResult getResult(RelatedItem relatedItem) {
096        var result = ItemResultFactory.getGetRelatedItemResult();
097
098        if(relatedItem != null) {
099            result.setRelatedItem(itemControl.getRelatedItemTransfer(getUserVisit(), relatedItem));
100        }
101
102        return result;
103    }
104
105}