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.GetItemDescriptionsForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.item.server.logic.ItemLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.item.server.entity.Item;
029import com.echothree.model.data.item.server.entity.ItemDescription;
030import com.echothree.model.data.item.server.factory.ItemDescriptionFactory;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.ArrayList;
042import java.util.Collection;
043import java.util.List;
044
045public class GetItemDescriptionsCommand
046        extends BaseMultipleEntitiesCommand<ItemDescription, GetItemDescriptionsForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.ItemDescription.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("ItemDescriptionTypeUseTypeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null)
063        );
064    }
065    
066    /** Creates a new instance of GetItemDescriptionsCommand */
067    public GetItemDescriptionsCommand(UserVisitPK userVisitPK, GetItemDescriptionsForm form) {
068        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
069    }
070
071    Item item;
072
073    @Override
074    protected Collection<ItemDescription> getEntities() {
075        var itemControl = Session.getModelController(ItemControl.class);
076        Collection<ItemDescription> entities = null;
077        var itemName = form.getItemName();
078
079        item = ItemLogic.getInstance().getItemByName(this, itemName);
080
081        if(!hasExecutionErrors()) {
082            var itemDescriptionTypeUseTypeName = form.getItemDescriptionTypeUseTypeName();
083            var itemDescriptionTypeUseType = itemDescriptionTypeUseTypeName == null ? null : itemControl.getItemDescriptionTypeUseTypeByName(itemDescriptionTypeUseTypeName);
084
085            if(itemDescriptionTypeUseTypeName == null || itemDescriptionTypeUseType != null) {
086                if(itemDescriptionTypeUseType == null) {
087                    if(form.getLanguageIsoName() == null) {
088                        entities = itemControl.getItemDescriptionsByItem(item);
089                    } else {
090                        addExecutionError(ExecutionErrors.InvalidParameterCombination.name());
091                    }
092                } else {
093                    var partyControl = Session.getModelController(PartyControl.class);
094                    var languageIsoName = form.getLanguageIsoName();
095                    var language = languageIsoName == null ? getPreferredLanguage() : partyControl.getLanguageByIsoName(languageIsoName);
096
097                    if(languageIsoName == null || language != null) {
098                        var itemDescriptionTypeUses = itemControl.getItemDescriptionTypeUsesByItemDescriptionTypeUseType(itemDescriptionTypeUseType);
099
100                        entities = new ArrayList<>();
101
102                        for(var itemDescriptionTypeUse : itemDescriptionTypeUses) {
103                            var itemDescription = itemControl.getBestItemDescription(itemDescriptionTypeUse.getItemDescriptionType(), item, language);
104
105                            if(itemDescription != null) {
106                                entities.add(itemDescription);
107                            }
108                        }
109                    } else {
110                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
111                    }
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownItemDescriptionTypeUseTypeName.name(), itemDescriptionTypeUseTypeName);
115            }
116        }
117
118        return entities;
119    }
120
121    @Override
122    protected BaseResult getResult(Collection<ItemDescription> entities) {
123        var result = ItemResultFactory.getGetItemDescriptionsResult();
124
125        if(entities != null) {
126            var itemControl = Session.getModelController(ItemControl.class);
127            var userVisit = getUserVisit();
128
129            if(session.hasLimit(ItemDescriptionFactory.class)) {
130                result.setItemDescriptionCount(itemControl.countItemDescriptionsByItem(item));
131            }
132
133            result.setItem(itemControl.getItemTransfer(userVisit, item));
134            result.setItemDescriptions(itemControl.getItemDescriptionTransfers(userVisit, entities));
135        }
136
137        return result;
138    }
139
140}