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