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.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 com.echothree.util.server.persistence.Session;
039import java.util.List;
040
041public class CreateSourceCommand
042        extends BaseSimpleCommand<CreateSourceForm> {
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.Source.name(), SecurityRoles.Create.name())
052                ))
053        ));
054
055        FORM_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L),
057                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
060                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
061        );
062    }
063    
064    /** Creates a new instance of CreateSourceCommand */
065    public CreateSourceCommand(UserVisitPK userVisitPK, CreateSourceForm form) {
066        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var offerControl = Session.getModelController(OfferControl.class);
072        var result = OfferResultFactory.getCreateSourceResult();
073        Source source = null;
074        var offerName = form.getOfferName();
075        var offer = offerControl.getOfferByName(offerName);
076        
077        if(offer != null) {
078            var useControl = Session.getModelController(UseControl.class);
079            var useName = form.getUseName();
080            var use = useControl.getUseByName(useName);
081            
082            if(use != null) {
083                var offerUseControl = Session.getModelController(OfferUseControl.class);
084                var offerUse = offerUseControl.getOfferUse(offer, use);
085                
086                if(offerUse != null) {
087                    var sourceControl = Session.getModelController(SourceControl.class);
088                    var sourceName = form.getSourceName();
089
090                    source = sourceControl.getSourceByName(sourceName);
091                    
092                    if(source == null) {
093                        var isDefault = Boolean.valueOf(form.getIsDefault());
094                        var sortOrder = Integer.valueOf(form.getSortOrder());
095                        
096                        source = sourceControl.createSource(sourceName, offerUse, isDefault, sortOrder, getPartyPK());
097                    } else {
098                        addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName);
099                    }
100                } else {
101                    addExecutionError(ExecutionErrors.UnknownOfferUse.name(), offerName, useName);
102                }
103            } else {
104                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
105            }
106        } else {
107            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
108        }
109
110        if(source != null) {
111            result.setSourceName(source.getLastDetail().getSourceName());
112            result.setEntityRef(source.getPrimaryKey().getEntityRef());
113        }
114
115        return result;
116    }
117    
118}