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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchSalesOrdersForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.order.common.OrderTypes;
022import com.echothree.model.control.order.server.logic.OrderAliasLogic;
023import com.echothree.model.control.order.server.logic.OrderLogic;
024import com.echothree.model.control.sales.common.workflow.SalesOrderStatusConstants;
025import com.echothree.model.control.sales.server.search.SalesOrderSearchEvaluator;
026import com.echothree.model.control.search.common.SearchKinds;
027import com.echothree.model.control.search.server.logic.SearchLogic;
028import com.echothree.model.control.workflow.server.control.WorkflowControl;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.google.common.base.Splitter;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class SearchSalesOrdersCommand
042        extends BaseSimpleCommand<SearchSalesOrdersForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045
046    static {
047        FORM_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, false, null, null),
052                new FieldDefinition("SalesOrderStatusChoice", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
056                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
057                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
058        );
059    }
060
061    @Inject
062    WorkflowControl workflowControl;
063
064    @Inject
065    OrderAliasLogic orderAliasLogic;
066
067    @Inject
068    OrderLogic orderLogic;
069
070    @Inject
071    SearchLogic searchLogic;
072
073    /** Creates a new instance of SearchSalesOrdersCommand */
074    public SearchSalesOrdersCommand() {
075        super(null, FORM_FIELD_DEFINITIONS, false);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var result = SearchResultFactory.getSearchSalesOrdersResult();
081        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.SALES_ORDER.name());
082
083        if(!hasExecutionErrors()) {
084            var searchTypeName = form.getSearchTypeName();
085            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
086
087            if(!hasExecutionErrors()) {
088                var salesOrderStatusChoice = form.getSalesOrderStatusChoice();
089                var salesOrderStatusWorkflowStep = salesOrderStatusChoice == null ? null :
090                    workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(SalesOrderStatusConstants.Workflow_SALES_ORDER_STATUS), salesOrderStatusChoice);
091
092                if(salesOrderStatusChoice == null || salesOrderStatusChoice != null) {
093                    var orderType = orderLogic.getOrderTypeByName(this, OrderTypes.SALES_ORDER.name());
094
095                    if(!hasExecutionErrors()) {
096                        var orderAliasTypeName = form.getOrderAliasTypeName();
097                        var orderAliasType = orderAliasTypeName == null ? null : orderAliasLogic.getOrderAliasTypeByName(this, orderType, orderAliasTypeName);
098
099                        if(!hasExecutionErrors()) {
100                            var salesOrderSearchEvaluator = new SalesOrderSearchEvaluator(getUserVisit(), searchType,
101                                    searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind),
102                                    searchLogic.getDefaultSearchSortDirection(null));
103                            var createdSince = form.getCreatedSince();
104                            var modifiedSince = form.getModifiedSince();
105                            var fields = form.getFields();
106
107                            salesOrderSearchEvaluator.setOrderStatusWorkflowStep(salesOrderStatusWorkflowStep);
108                            salesOrderSearchEvaluator.setOrderAliasType(orderAliasType);
109                            salesOrderSearchEvaluator.setAlias(form.getAlias());
110                            salesOrderSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
111                            salesOrderSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
112                            salesOrderSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
113
114                            result.setCount(salesOrderSearchEvaluator.execute(this));
115                        }
116                    }
117                } else {
118                    addExecutionError(ExecutionErrors.UnknownSalesOrderStatusChoice.name(), salesOrderStatusChoice);
119                }                        
120            }
121        }
122        
123        return result;
124    }
125}