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.associate.server.command;
018
019import com.echothree.control.user.associate.common.form.CreateAssociateForm;
020import com.echothree.control.user.associate.common.result.AssociateResultFactory;
021import com.echothree.model.control.associate.server.control.AssociateControl;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.associate.server.entity.Associate;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class CreateAssociateCommand
036        extends BaseSimpleCommand<CreateAssociateForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("AssociateProgramName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("AssociateName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
046                new FieldDefinition("SummaryMimeTypeName", FieldType.MIME_TYPE, true, null, null),
047                new FieldDefinition("Summary", FieldType.STRING, true, null, null)
048        );
049    }
050
051    @Inject
052    AssociateControl associateControl;
053
054    @Inject
055    PartyControl partyControl;
056
057    
058    /** Creates a new instance of CreateAssociateCommand */
059    public CreateAssociateCommand() {
060        super(null, FORM_FIELD_DEFINITIONS, false);
061    }
062    
063    @Override
064    protected BaseResult execute() {
065        var result = AssociateResultFactory.getCreateAssociateResult();
066        Associate associate = null;
067        var associateProgramName = form.getAssociateProgramName();
068        var associateProgram = associateControl.getAssociateProgramByName(associateProgramName);
069        
070        if(associateProgram != null) {
071            var associateName = form.getAssociateName();
072
073            associate = associateControl.getAssociateByName(associateProgram, associateName);
074            
075            if(associate == null) {
076                var partyName = form.getPartyName();
077                var party = partyControl.getPartyByName(partyName);
078                
079                if(party != null) {
080                    var summaryMimeTypeName = form.getSummaryMimeTypeName();
081                    var summaryMimeType = mimeTypeControl.getMimeTypeByName(summaryMimeTypeName);
082                    
083                    if(summaryMimeType != null) {
084                        var description = form.getDescription();
085                        var summary = form.getSummary();
086                        
087                        associate = associateControl.createAssociate(associateProgram, associateName, party, description, summaryMimeType,
088                                summary, getPartyPK());
089                    } else {
090                        addExecutionError(ExecutionErrors.UnknownSummaryMimeTypeName.name(), summaryMimeTypeName);
091                    }
092                } else {
093                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
094                }
095            } else {
096                addExecutionError(ExecutionErrors.DuplicateAssociateName.name(), associateName);
097            }
098        } else {
099            addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName);
100        }
101        
102        if(associate != null) {
103            result.setEntityRef(associate.getPrimaryKey().getEntityRef());
104            result.setAssociateProgramName(associate.getLastDetail().getAssociateProgram().getLastDetail().getAssociateProgramName());
105            result.setAssociateName(associate.getLastDetail().getAssociateName());
106        }
107
108        return result;
109    }
110    
111}