001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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;
038
039public class GetSecurityRoleGroupsCommand
040        extends BasePaginatedMultipleEntitiesCommand<SecurityRoleGroup, GetSecurityRoleGroupsForm> {
041
042    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
047                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
048                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
049                        new SecurityRoleDefinition(SecurityRoleGroups.SecurityRoleGroup.name(), SecurityRoles.List.name())
050                ))
051        ));
052        
053        FORM_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("ParentSecurityRoleGroupName", FieldType.ENTITY_NAME, false, null, null)
055        );
056    }
057    
058    /** Creates a new instance of GetSecurityRoleGroupsCommand */
059    public GetSecurityRoleGroupsCommand(UserVisitPK userVisitPK, GetSecurityRoleGroupsForm form) {
060        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
061    }
062
063    SecurityRoleGroup parentSecurityRoleGroup;
064
065    @Override
066    protected void handleForm() {
067        var securityControl = Session.getModelController(SecurityControl.class);
068        var parentSecurityRoleGroupName = form.getParentSecurityRoleGroupName();
069
070        parentSecurityRoleGroup = parentSecurityRoleGroupName == null ? null : securityControl.getSecurityRoleGroupByName(parentSecurityRoleGroupName);
071
072        if(parentSecurityRoleGroupName != null && parentSecurityRoleGroup == null) {
073            addExecutionError(ExecutionErrors.UnknownParentSecurityRoleGroupName.name(), parentSecurityRoleGroupName);
074        }
075    }
076
077    @Override
078    protected Long getTotalEntities() {
079        var securityControl = Session.getModelController(SecurityControl.class);
080
081        return hasExecutionErrors() ? null :
082                parentSecurityRoleGroup == null ?
083                        securityControl.countSecurityRoleGroups() :
084                        securityControl.countSecurityRoleGroupsByParentSecurityRoleGroup(parentSecurityRoleGroup);
085    }
086
087    @Override
088    protected Collection<SecurityRoleGroup> getEntities() {
089        var securityControl = Session.getModelController(SecurityControl.class);
090        Collection<SecurityRoleGroup> entities = null;
091
092        if(!hasExecutionErrors()) {
093            entities = parentSecurityRoleGroup == null ?
094                    securityControl.getSecurityRoleGroups():
095                    securityControl.getSecurityRoleGroupsByParentSecurityRoleGroup(parentSecurityRoleGroup);
096        }
097
098        return entities;
099    }
100
101    @Override
102    protected BaseResult getResult(Collection<SecurityRoleGroup> entities) {
103        var result = SecurityResultFactory.getGetSecurityRoleGroupsResult();
104        var securityControl = Session.getModelController(SecurityControl.class);
105        var userVisit = getUserVisit();
106
107        result.setParentSecurityRoleGroup(parentSecurityRoleGroup == null ? null : securityControl.getSecurityRoleGroupTransfer(userVisit, parentSecurityRoleGroup));
108
109        if(entities != null) {
110            result.setSecurityRoleGroups(securityControl.getSecurityRoleGroupTransfers(userVisit, entities));
111        }
112
113        return result;
114    }
115
116}