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