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