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