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.selector.server.command;
018
019import com.echothree.control.user.selector.common.form.GetSelectorPartiesForm;
020import com.echothree.control.user.selector.common.result.SelectorResultFactory;
021import com.echothree.model.control.selector.server.control.SelectorControl;
022import com.echothree.model.control.selector.server.logic.SelectorKindLogic;
023import com.echothree.model.control.selector.server.logic.SelectorLogic;
024import com.echothree.model.control.selector.server.logic.SelectorTypeLogic;
025import com.echothree.model.data.selector.server.entity.Selector;
026import com.echothree.model.data.selector.server.entity.SelectorParty;
027import com.echothree.model.data.selector.server.factory.SelectorPartyFactory;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
032import java.util.Collection;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class GetSelectorPartiesCommand
039        extends BasePaginatedMultipleEntitiesCommand<SelectorParty, GetSelectorPartiesForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045            new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
046            new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
047            new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null)
048        );
049    }
050
051    @Inject
052    SelectorControl selectorControl;
053
054    @Inject
055    SelectorKindLogic selectorKindLogic;
056
057    @Inject
058    SelectorLogic selectorLogic;
059
060    @Inject
061    SelectorTypeLogic selectorTypeLogic;
062    
063    /** Creates a new instance of GetSelectorPartiesCommand */
064    public GetSelectorPartiesCommand() {
065        super(null, FORM_FIELD_DEFINITIONS, true);
066    }
067
068    Selector selector;
069
070    @Override
071    protected void handleForm() {
072        var selectorType = selectorTypeLogic.getSelectorTypeByName(this, form.getSelectorKindName(), form.getSelectorTypeName());
073
074        if(!hasExecutionErrors()) {
075            selector = selectorLogic.getSelectorByName(this, selectorType, form.getSelectorName());
076        }
077    }
078
079    @Override
080    protected Long getTotalEntities() {
081        return hasExecutionErrors() ? null : selectorControl.countSelectorPartiesBySelector(selector);
082    }
083
084    @Override
085    protected Collection<SelectorParty> getEntities() {
086        return hasExecutionErrors() ? null : selectorControl.getSelectorPartiesBySelector(selector);
087    }
088
089    @Override
090    protected BaseResult getResult(Collection<SelectorParty> entities) {
091        var result = SelectorResultFactory.getGetSelectorPartiesResult();
092
093        if(entities != null) {
094            var userVisit = getUserVisit();
095
096            result.setSelector(selectorControl.getSelectorTransfer(userVisit, selector));
097
098            if(session.hasLimit(SelectorPartyFactory.class)) {
099                result.setSelectorPartyCount(getTotalEntities());
100            }
101
102            result.setSelectorParties(selectorControl.getSelectorPartyTransfers(userVisit, entities));
103        }
104
105        return result;
106    }
107    
108}