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.chain.server.command;
018
019import com.echothree.control.user.chain.common.form.GetChainsForm;
020import com.echothree.control.user.chain.common.result.ChainResultFactory;
021import com.echothree.model.control.chain.server.control.ChainControl;
022import com.echothree.model.control.chain.server.logic.ChainKindLogic;
023import com.echothree.model.control.chain.server.logic.ChainTypeLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.chain.server.entity.Chain;
028import com.echothree.model.data.chain.server.entity.ChainKind;
029import com.echothree.model.data.chain.server.entity.ChainType;
030import com.echothree.model.data.chain.server.factory.ChainFactory;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetChainsCommand
045        extends BasePaginatedMultipleEntitiesCommand<Chain, GetChainsForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.Chain.name(), SecurityRoles.List.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null)
061        );
062    }
063
064    @Inject
065    ChainControl chainControl;
066
067    @Inject
068    ChainKindLogic chainKindLogic;
069
070    @Inject
071    ChainTypeLogic chainTypeLogic;
072
073    /** Creates a new instance of GetChainsCommand */
074    public GetChainsCommand() {
075        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
076    }
077
078    protected ChainKind chainKind;
079    protected ChainType chainType;
080
081    @Override
082    protected void handleForm() {
083        chainKind = chainKindLogic.getChainKindByName(this, form.getChainKindName());
084
085        if(!hasExecutionErrors()) {
086            chainType = chainTypeLogic.getChainTypeByName(this, chainKind, form.getChainTypeName());
087        }
088    }
089
090    @Override
091    protected Long getTotalEntities() {
092        return hasExecutionErrors() ? null : chainControl.countChainsByChainType(chainType);
093    }
094
095    @Override
096    protected Collection<Chain> getEntities() {
097        return hasExecutionErrors() ? null : chainControl.getChainsByChainType(chainType);
098    }
099
100    @Override
101    protected BaseResult getResult(Collection<Chain> entities) {
102        var result = ChainResultFactory.getGetChainsResult();
103
104        if(entities != null) {
105            var userVisit = getUserVisit();
106
107            result.setChainType(chainControl.getChainTypeTransfer(userVisit, chainType));
108
109            if(session.hasLimit(ChainFactory.class)) {
110                result.setChainCount(getTotalEntities());
111            }
112
113            result.setChains(chainControl.getChainTransfersByChainType(userVisit, chainType));
114        }
115
116        return result;
117    }
118    
119}