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.offer.server.command;
018
019import com.echothree.control.user.offer.common.form.CreateSourceForm;
020import com.echothree.control.user.offer.common.result.OfferResultFactory;
021import com.echothree.model.control.offer.server.control.OfferControl;
022import com.echothree.model.control.offer.server.control.OfferUseControl;
023import com.echothree.model.control.offer.server.control.SourceControl;
024import com.echothree.model.control.offer.server.control.UseControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.offer.server.entity.Source;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class CreateSourceCommand
044        extends BaseSimpleCommand<CreateSourceForm> {
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.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.Source.name(), SecurityRoles.Create.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L),
059                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
063        );
064    }
065
066    @Inject
067    OfferControl offerControl;
068
069    @Inject
070    OfferUseControl offerUseControl;
071
072    @Inject
073    SourceControl sourceControl;
074
075    @Inject
076    UseControl useControl;
077
078    
079    /** Creates a new instance of CreateSourceCommand */
080    public CreateSourceCommand() {
081        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
082    }
083    
084    @Override
085    protected BaseResult execute() {
086        var result = OfferResultFactory.getCreateSourceResult();
087        Source source = null;
088        var offerName = form.getOfferName();
089        var offer = offerControl.getOfferByName(offerName);
090        
091        if(offer != null) {
092            var useName = form.getUseName();
093            var use = useControl.getUseByName(useName);
094            
095            if(use != null) {
096                var offerUse = offerUseControl.getOfferUse(offer, use);
097                
098                if(offerUse != null) {
099                    var sourceName = form.getSourceName();
100
101                    source = sourceControl.getSourceByName(sourceName);
102                    
103                    if(source == null) {
104                        var isDefault = Boolean.valueOf(form.getIsDefault());
105                        var sortOrder = Integer.valueOf(form.getSortOrder());
106                        
107                        source = sourceControl.createSource(sourceName, offerUse, isDefault, sortOrder, getPartyPK());
108                    } else {
109                        addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName);
110                    }
111                } else {
112                    addExecutionError(ExecutionErrors.UnknownOfferUse.name(), offerName, useName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
119        }
120
121        if(source != null) {
122            result.setSourceName(source.getLastDetail().getSourceName());
123            result.setEntityRef(source.getPrimaryKey().getEntityRef());
124        }
125
126        return result;
127    }
128    
129}