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