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