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.selector.server.command;
018
019import com.echothree.control.user.selector.common.form.GetSelectorForm;
020import com.echothree.control.user.selector.common.result.SelectorResultFactory;
021import com.echothree.control.user.selector.common.form.GetSelectorForm;
022import com.echothree.control.user.selector.common.result.GetSelectorResult;
023import com.echothree.control.user.selector.common.result.SelectorResultFactory;
024import com.echothree.model.control.core.common.EventTypes;
025import com.echothree.model.control.selector.server.control.SelectorControl;
026import com.echothree.model.control.selector.server.logic.SelectorLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.control.selector.server.control.SelectorControl;
031import com.echothree.model.data.selector.server.entity.Selector;
032import com.echothree.model.data.selector.server.entity.SelectorKind;
033import com.echothree.model.data.selector.server.entity.Selector;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.BaseSingleEntityCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.Arrays;
046import java.util.Collections;
047import java.util.List;
048
049public class GetSelectorCommand
050        extends BaseSingleEntityCommand<Selector, GetSelectorForm> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
054
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
059                        new SecurityRoleDefinition(SecurityRoleGroups.Selector.name(), SecurityRoles.Review.name())
060                ))
061        ));
062
063        FORM_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
068                new FieldDefinition("Key", FieldType.KEY, false, null, null),
069                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
070                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
071        );
072    }
073
074    /** Creates a new instance of GetSelectorCommand */
075    public GetSelectorCommand(UserVisitPK userVisitPK, GetSelectorForm form) {
076        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
077    }
078
079    @Override
080    protected Selector getEntity() {
081        var selector = SelectorLogic.getInstance().getSelectorByUniversalSpec(this, form, true);
082
083        if(selector != null) {
084            sendEvent(selector.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
085        }
086
087        return selector;
088    }
089
090    @Override
091    protected BaseResult getResult(Selector selector) {
092        var selectorControl = Session.getModelController(SelectorControl.class);
093        var result = SelectorResultFactory.getGetSelectorResult();
094
095        if(selector != null) {
096            result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
097        }
098
099        return result;
100    }
101
102}