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