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