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