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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetPartyApplicationEditorUsesForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyApplicationEditorUseControl;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.party.server.logic.PartyLogic;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.model.data.party.server.entity.PartyApplicationEditorUse;
029import com.echothree.model.data.party.server.factory.PartyApplicationEditorUseFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetPartyApplicationEditorUsesCommand
045        extends BasePaginatedMultipleEntitiesCommand<PartyApplicationEditorUse, GetPartyApplicationEditorUsesForm> {
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.PartyApplicationEditorUse.name(), SecurityRoles.List.name())
055                ))
056        ));
057        
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
060        );
061    }
062    
063    @Inject
064    PartyApplicationEditorUseControl partyApplicationEditorUseControl;
065
066    @Inject
067    PartyControl partyControl;
068
069    @Inject
070    PartyLogic partyLogic;
071
072    /** Creates a new instance of GetPartyApplicationEditorUsesCommand */
073    public GetPartyApplicationEditorUsesCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076    
077    Party party;
078
079    @Override
080    protected void handleForm() {
081        var partyName = form.getPartyName();
082
083        party = partyName == null ? getParty() : partyLogic.getPartyByName(this, partyName);
084    }
085
086    @Override
087    protected Long getTotalEntities() {
088        return hasExecutionErrors() ? null : partyApplicationEditorUseControl.countPartyApplicationEditorUsesByParty(party);
089    }
090
091    @Override
092    protected Collection<PartyApplicationEditorUse> getEntities() {
093        return hasExecutionErrors() ? null : partyApplicationEditorUseControl.getPartyApplicationEditorUsesByParty(party);
094    }
095
096    @Override
097    protected BaseResult getResult(Collection<PartyApplicationEditorUse> entities) {
098        var result = PartyResultFactory.getGetPartyApplicationEditorUsesResult();
099
100        if(entities != null) {
101            result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
102
103            if(session.hasLimit(PartyApplicationEditorUseFactory.class)) {
104                result.setPartyApplicationEditorUseCount(getTotalEntities());
105            }
106
107            result.setPartyApplicationEditorUses(partyApplicationEditorUseControl.getPartyApplicationEditorUseTransfers(new ArrayList<>(entities), getUserVisit()));
108        }
109
110        return result;
111    }
112
113}