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.GetRelatedItemsForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.data.item.server.entity.Item;
023import com.echothree.model.data.item.server.entity.RelatedItem;
024import com.echothree.model.data.item.server.entity.RelatedItemType;
025import com.echothree.model.data.item.server.factory.RelatedItemFactory;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
031import java.util.Collection;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetRelatedItemsCommand
038        extends BasePaginatedMultipleEntitiesCommand<RelatedItem, GetRelatedItemsForm> {
039
040    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("RelatedItemTypeName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("FromItemName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("ToItemName", FieldType.ENTITY_NAME, false, null, null)
048        );
049    }
050
051    @Inject
052    ItemControl itemControl;
053
054    
055    /** Creates a new instance of GetRelatedItemsCommand */
056    public GetRelatedItemsCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, true);
058    }
059
060    RelatedItemType relatedItemType;
061    Item fromItem;
062    Item toItem;
063
064    @Override
065    protected void handleForm() {
066        var fromItemName = form.getFromItemName();
067        var toItemName = form.getToItemName();
068        var parameterCount = (fromItemName == null ? 0 : 1) + (toItemName == null ? 0 : 1);
069
070        if(parameterCount == 1) {
071            var relatedItemTypeName = form.getRelatedItemTypeName();
072
073            relatedItemType = relatedItemTypeName == null ? null : itemControl.getRelatedItemTypeByName(relatedItemTypeName);
074
075            if(relatedItemTypeName == null || relatedItemType != null) {
076                fromItem = fromItemName == null ? null : itemControl.getItemByName(fromItemName);
077
078                if(fromItemName == null || fromItem != null) {
079                    toItem = toItemName == null ? null : itemControl.getItemByName(toItemName);
080
081                    if(toItemName != null && toItem == null) {
082                        addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName);
083                    }
084                } else {
085                    addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName);
086                }
087            } else {
088                addExecutionError(ExecutionErrors.UnknownRelatedItemTypeName.name(), relatedItemTypeName);
089            }
090        } else {
091            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
092        }
093    }
094
095    @Override
096    protected Long getTotalEntities() {
097        Long totalEntities = null;
098
099        if(!hasExecutionErrors()) {
100            if(relatedItemType == null) {
101                if(fromItem != null) {
102                    totalEntities = itemControl.countRelatedItemsByFromItem(fromItem);
103                } else {
104                    totalEntities = itemControl.countRelatedItemsByToItem(toItem);
105                }
106            } else {
107                if(fromItem != null) {
108                    totalEntities = itemControl.countRelatedItemsByRelatedItemTypeAndFromItem(relatedItemType, fromItem);
109                } else {
110                    totalEntities = itemControl.countRelatedItemsByRelatedItemTypeAndToItem(relatedItemType, toItem);
111                }
112            }
113        }
114
115        return totalEntities;
116    }
117
118    @Override
119    protected Collection<RelatedItem> getEntities() {
120        Collection<RelatedItem> entities = null;
121
122        if(!hasExecutionErrors()) {
123            if(relatedItemType == null) {
124                if(fromItem != null) {
125                    entities = itemControl.getRelatedItemsByFromItem(fromItem);
126                } else if(toItem != null) {
127                    entities = itemControl.getRelatedItemsByToItem(toItem);
128                }
129            } else {
130                if(fromItem != null) {
131                    entities = itemControl.getRelatedItemsByRelatedItemTypeAndFromItem(relatedItemType, fromItem);
132                } else if(toItem != null) {
133                    entities = itemControl.getRelatedItemsByRelatedItemTypeAndToItem(relatedItemType, toItem);
134                }
135            }
136        }
137
138        return entities;
139    }
140
141    @Override
142    protected BaseResult getResult(Collection<RelatedItem> entities) {
143        var result = ItemResultFactory.getGetRelatedItemsResult();
144
145        if(entities != null) {
146            var userVisit = getUserVisit();
147
148            if(session.hasLimit(RelatedItemFactory.class)) {
149                result.setRelatedItemCount(getTotalEntities());
150            }
151
152            if(relatedItemType != null) {
153                result.setRelatedItemType(itemControl.getRelatedItemTypeTransfer(userVisit, relatedItemType));
154            }
155
156            if(fromItem != null) {
157                result.setFromItem(itemControl.getItemTransfer(userVisit, fromItem));
158            }
159
160            if(toItem != null) {
161                result.setToItem(itemControl.getItemTransfer(userVisit, toItem));
162            }
163
164            result.setRelatedItems(itemControl.getRelatedItemTransfers(userVisit, entities));
165        }
166
167        return result;
168    }
169
170}