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    
064    /** Creates a new instance of GetSelectorPartiesCommand */
065    public GetSelectorPartiesCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    Selector selector;
070
071    @Override
072    protected void handleForm() {
073        var selectorType = selectorTypeLogic.getSelectorTypeByName(this, form.getSelectorKindName(), form.getSelectorTypeName());
074
075        if(!hasExecutionErrors()) {
076            selector = selectorLogic.getSelectorByName(this, selectorType, form.getSelectorName());
077        }
078    }
079
080    @Override
081    protected Long getTotalEntities() {
082        return hasExecutionErrors() ? null : selectorControl.countSelectorPartiesBySelector(selector);
083    }
084
085    @Override
086    protected Collection<SelectorParty> getEntities() {
087        return hasExecutionErrors() ? null : selectorControl.getSelectorPartiesBySelector(selector);
088    }
089
090    @Override
091    protected BaseResult getResult(Collection<SelectorParty> entities) {
092        var result = SelectorResultFactory.getGetSelectorPartiesResult();
093
094        if(entities != null) {
095            var userVisit = getUserVisit();
096
097            result.setSelector(selectorControl.getSelectorTransfer(userVisit, selector));
098
099            if(session.hasLimit(SelectorPartyFactory.class)) {
100                result.setSelectorPartyCount(getTotalEntities());
101            }
102
103            result.setSelectorParties(selectorControl.getSelectorPartyTransfers(userVisit, entities));
104        }
105
106        return result;
107    }
108    
109}