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