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.CreateOfferForm; 020import com.echothree.control.user.offer.common.result.OfferResultFactory; 021import com.echothree.model.control.filter.common.FilterKinds; 022import com.echothree.model.control.filter.common.FilterTypes; 023import com.echothree.model.control.filter.server.control.FilterControl; 024import com.echothree.model.control.filter.server.control.FilterKindControl; 025import com.echothree.model.control.filter.server.control.FilterTypeControl; 026import com.echothree.model.control.offer.server.logic.OfferLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.control.selector.common.SelectorKinds; 032import com.echothree.model.control.selector.common.SelectorTypes; 033import com.echothree.model.control.selector.server.control.SelectorControl; 034import com.echothree.model.control.sequence.common.SequenceTypes; 035import com.echothree.model.control.sequence.server.control.SequenceControl; 036import com.echothree.model.data.filter.server.entity.Filter; 037import com.echothree.model.data.offer.server.entity.Offer; 038import com.echothree.model.data.selector.server.entity.Selector; 039import com.echothree.model.data.sequence.server.entity.Sequence; 040import com.echothree.model.data.user.common.pk.UserVisitPK; 041import com.echothree.util.common.command.BaseResult; 042import com.echothree.util.common.message.ExecutionErrors; 043import com.echothree.util.common.validation.FieldDefinition; 044import com.echothree.util.common.validation.FieldType; 045import com.echothree.util.server.control.BaseSimpleCommand; 046import com.echothree.util.server.control.CommandSecurityDefinition; 047import com.echothree.util.server.control.PartyTypeDefinition; 048import com.echothree.util.server.control.SecurityRoleDefinition; 049import java.util.List; 050import javax.enterprise.context.Dependent; 051import javax.inject.Inject; 052 053@Dependent 054public class CreateOfferCommand 055 extends BaseSimpleCommand<CreateOfferForm> { 056 057 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 058 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 059 060 static { 061 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 064 new SecurityRoleDefinition(SecurityRoleGroups.Offer.name(), SecurityRoles.Create.name()) 065 )) 066 )); 067 068 FORM_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L), 070 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("DivisionName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("OfferItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("OfferItemPriceFilterName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 077 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 078 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 079 ); 080 } 081 082 @Inject 083 FilterControl filterControl; 084 085 @Inject 086 FilterKindControl filterKindControl; 087 088 @Inject 089 FilterTypeControl filterTypeControl; 090 091 @Inject 092 PartyControl partyControl; 093 094 @Inject 095 SelectorControl selectorControl; 096 097 @Inject 098 SequenceControl sequenceControl; 099 100 @Inject 101 OfferLogic offerLogic; 102 103 104 /** Creates a new instance of CreateOfferCommand */ 105 public CreateOfferCommand() { 106 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 107 } 108 109 @Override 110 protected BaseResult execute() { 111 var result = OfferResultFactory.getCreateOfferResult(); 112 Offer offer = null; 113 var salesOrderSequenceName = form.getSalesOrderSequenceName(); 114 Sequence salesOrderSequence = null; 115 116 if(salesOrderSequenceName != null) { 117 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name()); 118 119 if(sequenceType != null) { 120 salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName); 121 } else { 122 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name()); 123 } 124 } 125 126 if(salesOrderSequenceName == null || salesOrderSequence != null) { 127 var companyName = form.getCompanyName(); 128 var partyCompany = companyName == null? partyControl.getDefaultPartyCompany(): 129 partyControl.getPartyCompanyByName(companyName); 130 var partyCompanyParty = partyCompany == null? null: partyCompany.getParty(); 131 132 if(companyName == null || partyCompanyParty != null) { 133 var divisionName = form.getDivisionName(); 134 var partyDivision = divisionName == null? partyControl.getDefaultPartyDivision(partyCompanyParty): 135 partyControl.getPartyDivisionByName(partyCompanyParty, divisionName); 136 var partyDivisionParty = partyDivision == null? null: partyDivision.getParty(); 137 138 if(divisionName == null || partyDivisionParty != null) { 139 var departmentName = form.getDepartmentName(); 140 var partyDepartment = departmentName == null? partyControl.getDefaultPartyDepartment(partyDivisionParty): 141 partyControl.getPartyDepartmentByName(partyDivisionParty, departmentName); 142 var partyDepartmentParty = partyDepartment == null? null: partyDepartment.getParty(); 143 144 if(departmentName == null || partyDepartmentParty != null) { 145 var offerItemSelectorName = form.getOfferItemSelectorName(); 146 Selector offerItemSelector = null; 147 148 if(offerItemSelectorName != null) { 149 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 150 151 if(selectorKind != null) { 152 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 153 SelectorTypes.OFFER.name()); 154 155 if(selectorType != null) { 156 offerItemSelector = selectorControl.getSelectorByName(selectorType, offerItemSelectorName); 157 } else { 158 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.OFFER.name()); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 162 } 163 } 164 165 if(offerItemSelectorName == null || offerItemSelector != null) { 166 var offerItemPriceFilterName = form.getOfferItemPriceFilterName(); 167 Filter offerItemPriceFilter = null; 168 169 if(offerItemPriceFilterName != null) { 170 var filterKind = filterKindControl.getFilterKindByName(FilterKinds.PRICE.name()); 171 var filterType = filterTypeControl.getFilterTypeByName(filterKind, FilterTypes.OFFER_ITEM_PRICE.name()); 172 173 if(filterType != null) { 174 offerItemPriceFilter = filterControl.getFilterByName(filterType, offerItemPriceFilterName); 175 } 176 } 177 178 if(offerItemPriceFilterName == null || offerItemPriceFilter != null) { 179 var offerName = form.getOfferName(); 180 var isDefault = Boolean.valueOf(form.getIsDefault()); 181 var sortOrder = Integer.valueOf(form.getSortOrder()); 182 var description = form.getDescription(); 183 184 offer = offerLogic.createOffer(this, offerName, salesOrderSequence, 185 partyDepartmentParty, offerItemSelector, offerItemPriceFilter, isDefault, sortOrder, 186 getPreferredLanguage(), description, getPartyPK()); 187 } else { 188 addExecutionError(ExecutionErrors.UnknownOfferItemPriceFilterName.name(), offerItemPriceFilterName); 189 } 190 } else { 191 addExecutionError(ExecutionErrors.UnknownOfferItemSelectorName.name(), offerItemSelectorName); 192 } 193 } else { 194 addExecutionError(ExecutionErrors.UnknownDepartmentName.name(), departmentName); 195 } 196 } else { 197 addExecutionError(ExecutionErrors.UnknownDivisionName.name(), divisionName); 198 } 199 } else { 200 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 201 } 202 } else { 203 addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName); 204 } 205 206 if(offer != null) { 207 result.setOfferName(offer.getLastDetail().getOfferName()); 208 result.setEntityRef(offer.getPrimaryKey().getEntityRef()); 209 } 210 211 return result; 212 } 213 214}