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.vendor.server.command;
018
019import com.echothree.control.user.core.common.spec.UniversalEntitySpec;
020import com.echothree.control.user.vendor.common.form.GetVendorForm;
021import com.echothree.control.user.vendor.common.result.VendorResultFactory;
022import com.echothree.model.control.core.common.EventTypes;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.logic.PartyLogic;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.vendor.server.control.VendorControl;
029import com.echothree.model.control.vendor.server.logic.VendorLogic;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.model.data.vendor.server.entity.Vendor;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.SecurityResult;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseSingleEntityCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042
043public class GetVendorCommand
044        extends BaseSingleEntityCommand<Vendor, GetVendorForm> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.Vendor.name(), SecurityRoles.Review.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Key", FieldType.KEY, false, null, null),
063                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
064                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
065        );
066    }
067
068    /** Creates a new instance of GetVendorCommand */
069    public GetVendorCommand(UserVisitPK userVisitPK, GetVendorForm form) {
070        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
071    }
072
073    String vendorName;
074    String partyName;
075    UniversalEntitySpec universalEntitySpec;
076    int parameterCount;
077
078    @Override
079    public SecurityResult security() {
080        var securityResult = super.security();
081
082        vendorName = form.getVendorName();
083        partyName = form.getPartyName();
084        universalEntitySpec = form;
085        parameterCount = (vendorName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
086                EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
087
088        if(!canSpecifyParty() && parameterCount != 0) {
089            securityResult = getInsufficientSecurityResult();
090        }
091
092        return securityResult;
093    }
094
095    @Override
096    protected Vendor getEntity() {
097        Vendor vendor = null;
098
099        if(parameterCount == 0) {
100            var party = getParty();
101
102            PartyLogic.getInstance().checkPartyType(this, party, PartyTypes.VENDOR.name());
103
104            if(!hasExecutionErrors()) {
105                var vendorControl = Session.getModelController(VendorControl.class);
106
107                vendor = vendorControl.getVendor(party);
108            }
109        } else {
110            vendor = VendorLogic.getInstance().getVendorByName(this, vendorName, partyName, universalEntitySpec);
111        }
112
113        if(vendor != null) {
114            sendEvent(vendor.getPartyPK(), EventTypes.READ, null, null, getPartyPK());
115        }
116
117        return vendor;
118    }
119
120    @Override
121    protected BaseResult getResult(Vendor vendor) {
122        var result = VendorResultFactory.getGetVendorResult();
123
124        if(vendor != null) {
125            var vendorControl = Session.getModelController(VendorControl.class);
126
127            result.setVendor(vendorControl.getVendorTransfer(getUserVisit(), vendor));
128        }
129
130        return result;
131    }
132
133}