001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.security.server.command;
018
019import com.echothree.control.user.security.common.form.GetSecurityRoleGroupsForm;
020import com.echothree.control.user.security.common.result.SecurityResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.security.server.control.SecurityControl;
025import com.echothree.model.data.security.server.entity.SecurityRoleGroup;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import com.echothree.util.server.control.SecurityRoleDefinition;
035import com.echothree.util.server.persistence.Session;
036import java.util.Collection;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class GetSecurityRoleGroupsCommand
042        extends BasePaginatedMultipleEntitiesCommand<SecurityRoleGroup, GetSecurityRoleGroupsForm> {
043
044    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
049                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
050                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
051                        new SecurityRoleDefinition(SecurityRoleGroups.SecurityRoleGroup.name(), SecurityRoles.List.name())
052                ))
053        ));
054        
055        FORM_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("ParentSecurityRoleGroupName", FieldType.ENTITY_NAME, false, null, null)
057        );
058    }
059    
060    /** Creates a new instance of GetSecurityRoleGroupsCommand */
061    public GetSecurityRoleGroupsCommand() {
062        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
063    }
064
065    SecurityRoleGroup parentSecurityRoleGroup;
066
067    @Override
068    protected void handleForm() {
069        var securityControl = Session.getModelController(SecurityControl.class);
070        var parentSecurityRoleGroupName = form.getParentSecurityRoleGroupName();
071
072        parentSecurityRoleGroup = parentSecurityRoleGroupName == null ? null : securityControl.getSecurityRoleGroupByName(parentSecurityRoleGroupName);
073
074        if(parentSecurityRoleGroupName != null && parentSecurityRoleGroup == null) {
075            addExecutionError(ExecutionErrors.UnknownParentSecurityRoleGroupName.name(), parentSecurityRoleGroupName);
076        }
077    }
078
079    @Override
080    protected Long getTotalEntities() {
081        var securityControl = Session.getModelController(SecurityControl.class);
082
083        return hasExecutionErrors() ? null :
084                parentSecurityRoleGroup == null ?
085                        securityControl.countSecurityRoleGroups() :
086                        securityControl.countSecurityRoleGroupsByParentSecurityRoleGroup(parentSecurityRoleGroup);
087    }
088
089    @Override
090    protected Collection<SecurityRoleGroup> getEntities() {
091        var securityControl = Session.getModelController(SecurityControl.class);
092        Collection<SecurityRoleGroup> entities = null;
093
094        if(!hasExecutionErrors()) {
095            entities = parentSecurityRoleGroup == null ?
096                    securityControl.getSecurityRoleGroups():
097                    securityControl.getSecurityRoleGroupsByParentSecurityRoleGroup(parentSecurityRoleGroup);
098        }
099
100        return entities;
101    }
102
103    @Override
104    protected BaseResult getResult(Collection<SecurityRoleGroup> entities) {
105        var result = SecurityResultFactory.getGetSecurityRoleGroupsResult();
106        var securityControl = Session.getModelController(SecurityControl.class);
107        var userVisit = getUserVisit();
108
109        result.setParentSecurityRoleGroup(parentSecurityRoleGroup == null ? null : securityControl.getSecurityRoleGroupTransfer(userVisit, parentSecurityRoleGroup));
110
111        if(entities != null) {
112            result.setSecurityRoleGroups(securityControl.getSecurityRoleGroupTransfers(userVisit, entities));
113        }
114
115        return result;
116    }
117
118}