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.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.Arrays;
049import java.util.Collections;
050import java.util.List;
051import javax.enterprise.context.RequestScoped;
052
053@RequestScoped
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(Collections.unmodifiableList(Arrays.asList(
062                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
063                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
064                        new SecurityRoleDefinition(SecurityRoleGroups.Offer.name(), SecurityRoles.Create.name())
065                        )))
066                )));
067        
068        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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    /** Creates a new instance of CreateOfferCommand */
083    public CreateOfferCommand() {
084        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var result = OfferResultFactory.getCreateOfferResult();
090        Offer offer = null;
091        var salesOrderSequenceName = form.getSalesOrderSequenceName();
092        Sequence salesOrderSequence = null;
093
094        if(salesOrderSequenceName != null) {
095            var sequenceControl = Session.getModelController(SequenceControl.class);
096            var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name());
097
098            if(sequenceType != null) {
099                salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName);
100            } else {
101                addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name());
102            }
103        }
104
105        if(salesOrderSequenceName == null || salesOrderSequence != null) {
106            var partyControl = Session.getModelController(PartyControl.class);
107            var companyName = form.getCompanyName();
108            var partyCompany = companyName == null? partyControl.getDefaultPartyCompany():
109                partyControl.getPartyCompanyByName(companyName);
110            var partyCompanyParty = partyCompany == null? null: partyCompany.getParty();
111
112            if(companyName == null || partyCompanyParty != null) {
113                var divisionName = form.getDivisionName();
114                var partyDivision = divisionName == null? partyControl.getDefaultPartyDivision(partyCompanyParty):
115                    partyControl.getPartyDivisionByName(partyCompanyParty, divisionName);
116                var partyDivisionParty = partyDivision == null? null: partyDivision.getParty();
117
118                if(divisionName == null || partyDivisionParty != null) {
119                    var departmentName = form.getDepartmentName();
120                    var partyDepartment = departmentName == null? partyControl.getDefaultPartyDepartment(partyDivisionParty):
121                        partyControl.getPartyDepartmentByName(partyDivisionParty, departmentName);
122                    var partyDepartmentParty = partyDepartment == null? null: partyDepartment.getParty();
123
124                    if(departmentName == null || partyDepartmentParty != null) {
125                        var offerItemSelectorName = form.getOfferItemSelectorName();
126                        Selector offerItemSelector = null;
127
128                        if(offerItemSelectorName != null) {
129                            var selectorControl = Session.getModelController(SelectorControl.class);
130                            var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
131
132                            if(selectorKind != null) {
133                                var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
134                                        SelectorTypes.OFFER.name());
135
136                                if(selectorType != null) {
137                                    offerItemSelector = selectorControl.getSelectorByName(selectorType, offerItemSelectorName);
138                                } else {
139                                    addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.OFFER.name());
140                                }
141                            } else {
142                                addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
143                            }
144                        }
145
146                        if(offerItemSelectorName == null || offerItemSelector != null) {
147                            var offerItemPriceFilterName = form.getOfferItemPriceFilterName();
148                            Filter offerItemPriceFilter = null;
149
150                            if(offerItemPriceFilterName != null) {
151                                var filterControl = Session.getModelController(FilterControl.class);
152                                var filterKind = filterControl.getFilterKindByName(FilterKinds.PRICE.name());
153                                var filterType = filterControl.getFilterTypeByName(filterKind, FilterTypes.OFFER_ITEM_PRICE.name());
154
155                                if(filterType != null) {
156                                    offerItemPriceFilter = filterControl.getFilterByName(filterType, offerItemPriceFilterName);
157                                }
158                            }
159
160                            if(offerItemPriceFilterName == null || offerItemPriceFilter != null) {
161                                var offerName = form.getOfferName();
162                                var isDefault = Boolean.valueOf(form.getIsDefault());
163                                var sortOrder = Integer.valueOf(form.getSortOrder());
164                                var description = form.getDescription();
165
166                                offer = OfferLogic.getInstance().createOffer(this, offerName, salesOrderSequence,
167                                        partyDepartmentParty, offerItemSelector, offerItemPriceFilter, isDefault, sortOrder,
168                                        getPreferredLanguage(), description, getPartyPK());
169                            } else {
170                                addExecutionError(ExecutionErrors.UnknownOfferItemPriceFilterName.name(), offerItemPriceFilterName);
171                            }
172                        } else {
173                            addExecutionError(ExecutionErrors.UnknownOfferItemSelectorName.name(), offerItemSelectorName);
174                        }
175                    } else {
176                        addExecutionError(ExecutionErrors.UnknownDepartmentName.name(), departmentName);
177                    }
178                } else {
179                    addExecutionError(ExecutionErrors.UnknownDivisionName.name(), divisionName);
180                }
181            } else {
182                addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
183            }
184        } else {
185            addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName);
186        }
187
188        if(offer != null) {
189            result.setOfferName(offer.getLastDetail().getOfferName());
190            result.setEntityRef(offer.getPrimaryKey().getEntityRef());
191        }
192        
193        return result;
194    }
195    
196}