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.offer.server.command; 018 019import com.echothree.control.user.offer.common.form.CreateOfferUseForm; 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.control.sequence.common.SequenceTypes; 029import com.echothree.model.control.sequence.server.control.SequenceControl; 030import com.echothree.model.data.offer.server.entity.OfferUse; 031import com.echothree.model.data.sequence.server.entity.Sequence; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.persistence.BasePK; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.server.control.BaseSimpleCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class CreateOfferUseCommand 048 extends BaseSimpleCommand<CreateOfferUseForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.Create.name()) 058 )) 059 )); 060 061 FORM_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L), 063 new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null) 065 ); 066 } 067 068 /** Creates a new instance of CreateOfferUseCommand */ 069 public CreateOfferUseCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 071 } 072 073 @Override 074 protected BaseResult execute() { 075 var offerControl = Session.getModelController(OfferControl.class); 076 var result = OfferResultFactory.getCreateOfferUseResult(); 077 OfferUse offerUse = null; 078 var offerName = form.getOfferName(); 079 var offer = offerControl.getOfferByName(offerName); 080 081 if(offer != null) { 082 var useControl = Session.getModelController(UseControl.class); 083 var useName = form.getUseName(); 084 var use = useControl.getUseByName(useName); 085 086 if(use != null) { 087 var offerUseControl = Session.getModelController(OfferUseControl.class); 088 089 offerUse = offerUseControl.getOfferUse(offer, use); 090 091 if(offerUse == null) { 092 var salesOrderSequenceName = form.getSalesOrderSequenceName(); 093 Sequence salesOrderSequence = null; 094 095 if(salesOrderSequenceName != null) { 096 var sequenceControl = Session.getModelController(SequenceControl.class); 097 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name()); 098 099 if(sequenceType != null) { 100 salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName); 101 } else { 102 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name()); 103 } 104 } 105 106 if(salesOrderSequenceName == null || salesOrderSequence != null) { 107 var sourceControl = Session.getModelController(SourceControl.class); 108 var sourceName = offerName + useName; 109 var source = sourceControl.getSourceByName(sourceName); 110 111 if(source == null) { 112 BasePK partyPK = getPartyPK(); 113 114 offerUse = offerUseControl.createOfferUse(offer, use, salesOrderSequence, partyPK); 115 sourceControl.createSource(sourceName, offerUse, false, 1, partyPK); 116 } else { 117 addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.DuplicateOfferUse.name()); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownUseName.name(), useName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 130 } 131 132 if(offerUse != null) { 133 var offerUseDetail = offerUse.getLastDetail(); 134 135 result.setOfferName(offerUseDetail.getOffer().getLastDetail().getOfferName()); 136 result.setUseName(offerUseDetail.getUse().getLastDetail().getUseName()); 137 result.setEntityRef(offerUseDetail.getPrimaryKey().getEntityRef()); 138 } 139 140 return result; 141 } 142 143}