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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchSalesOrderBatchesForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.accounting.server.logic.CurrencyLogic;
022import com.echothree.model.control.batch.common.BatchConstants;
023import com.echothree.model.control.batch.server.logic.BatchLogic;
024import com.echothree.model.control.payment.server.logic.PaymentMethodLogic;
025import com.echothree.model.control.sales.server.search.SalesOrderBatchSearchEvaluator;
026import com.echothree.model.control.search.common.SearchKinds;
027import com.echothree.model.control.search.server.logic.SearchLogic;
028import com.echothree.model.control.sales.common.workflow.SalesOrderBatchStatusConstants;
029import com.echothree.model.control.workflow.server.control.WorkflowControl;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.persistence.Session;
037import com.google.common.base.Splitter;
038import java.util.Arrays;
039import java.util.Collections;
040import java.util.List;
041import javax.enterprise.context.RequestScoped;
042
043@RequestScoped
044public class SearchSalesOrderBatchesCommand
045        extends BaseSimpleCommand<SearchSalesOrderBatchesForm> {
046    
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048
049    static {
050        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
051                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("BatchName", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("SalesOrderBatchStatusChoice", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("BatchAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
057                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null),
058                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
059                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
060                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
061                ));
062    }
063
064    /** Creates a new instance of SearchSalesOrderBatchesCommand */
065    public SearchSalesOrderBatchesCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var searchLogic = SearchLogic.getInstance();
072        var result = SearchResultFactory.getSearchSalesOrderBatchesResult();
073        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.SALES_ORDER_BATCH.name());
074
075        if(!hasExecutionErrors()) {
076            var searchTypeName = form.getSearchTypeName();
077            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
078
079            if(!hasExecutionErrors()) {
080                var currencyIsoName = form.getCurrencyIsoName();
081                var currency = currencyIsoName == null ? null : CurrencyLogic.getInstance().getCurrencyByName(this, currencyIsoName);
082
083                if(!hasExecutionErrors()) {
084                    var paymentMethodName = form.getPaymentMethodName();
085                    var paymentMethod = paymentMethodName == null ? null : PaymentMethodLogic.getInstance().getPaymentMethodByName(this, paymentMethodName);
086
087                    if(!hasExecutionErrors()) {
088                        var workflowControl = Session.getModelController(WorkflowControl.class);
089                        var salesOrderBatchStatusChoice = form.getSalesOrderBatchStatusChoice();
090                        var salesOrderBatchStatusWorkflowStep = salesOrderBatchStatusChoice == null ? null :
091                            workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(SalesOrderBatchStatusConstants.Workflow_SALES_ORDER_BATCH_STATUS), salesOrderBatchStatusChoice);
092
093                        if(salesOrderBatchStatusChoice == null || salesOrderBatchStatusChoice != null) {
094                            var batchLogic = BatchLogic.getInstance();
095                            var batchType = batchLogic.getBatchTypeByName(this, BatchConstants.BatchType_SALES_ORDER);
096
097                            if(!hasExecutionErrors()) {
098                                var batchAliasTypeName = form.getBatchAliasTypeName();
099                                var batchAliasType = batchAliasTypeName == null ? null : batchLogic.getBatchAliasTypeByName(this, batchType, batchAliasTypeName);
100
101                                if(!hasExecutionErrors()) {
102                                    var salesOrderBatchSearchEvaluator = new SalesOrderBatchSearchEvaluator(getUserVisit(), searchType, searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), searchLogic.getDefaultSearchSortDirection(null));
103                                    var createdSince = form.getCreatedSince();
104                                    var modifiedSince = form.getModifiedSince();
105                                    var fields = form.getFields();
106
107                                    salesOrderBatchSearchEvaluator.setCurrency(currency);
108                                    salesOrderBatchSearchEvaluator.setPaymentMethod(paymentMethod);
109                                    salesOrderBatchSearchEvaluator.setBatchStatusWorkflowStep(salesOrderBatchStatusWorkflowStep);
110                                    salesOrderBatchSearchEvaluator.setBatchAliasType(batchAliasType);
111                                    salesOrderBatchSearchEvaluator.setAlias(form.getAlias());
112                                    salesOrderBatchSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
113                                    salesOrderBatchSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
114                                    salesOrderBatchSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
115
116                                    result.setCount(salesOrderBatchSearchEvaluator.execute(this));
117                                }
118                            }
119                        } else {
120                            addExecutionError(ExecutionErrors.UnknownSalesOrderBatchStatusChoice.name(), salesOrderBatchStatusChoice);
121                        }                        
122                    }
123                }
124            }
125        }
126        
127        return result;
128    }
129}