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.google.common.base.Splitter; 035import java.util.List; 036import javax.enterprise.context.Dependent; 037import javax.inject.Inject; 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 @Inject 061 WorkflowControl workflowControl; 062 063 @Inject 064 OrderLogic orderLogic; 065 066 @Inject 067 SearchLogic searchLogic; 068 069 /** Creates a new instance of SearchSalesOrdersCommand */ 070 public SearchSalesOrdersCommand() { 071 super(null, FORM_FIELD_DEFINITIONS, false); 072 } 073 074 @Override 075 protected BaseResult execute() { 076 var result = SearchResultFactory.getSearchSalesOrdersResult(); 077 var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.SALES_ORDER.name()); 078 079 if(!hasExecutionErrors()) { 080 var searchTypeName = form.getSearchTypeName(); 081 var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName); 082 083 if(!hasExecutionErrors()) { 084 var salesOrderStatusChoice = form.getSalesOrderStatusChoice(); 085 var salesOrderStatusWorkflowStep = salesOrderStatusChoice == null ? null : 086 workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(SalesOrderStatusConstants.Workflow_SALES_ORDER_STATUS), salesOrderStatusChoice); 087 088 if(salesOrderStatusChoice == null || salesOrderStatusChoice != null) { 089 var orderType = orderLogic.getOrderTypeByName(this, OrderTypes.SALES_ORDER.name()); 090 091 if(!hasExecutionErrors()) { 092 var orderAliasTypeName = form.getOrderAliasTypeName(); 093 var orderAliasType = orderAliasTypeName == null ? null : orderLogic.getOrderAliasTypeByName(this, orderType, orderAliasTypeName); 094 095 if(!hasExecutionErrors()) { 096 var salesOrderSearchEvaluator = new SalesOrderSearchEvaluator(getUserVisit(), searchType, 097 searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), 098 searchLogic.getDefaultSearchSortDirection(null)); 099 var createdSince = form.getCreatedSince(); 100 var modifiedSince = form.getModifiedSince(); 101 var fields = form.getFields(); 102 103 salesOrderSearchEvaluator.setOrderStatusWorkflowStep(salesOrderStatusWorkflowStep); 104 salesOrderSearchEvaluator.setOrderAliasType(orderAliasType); 105 salesOrderSearchEvaluator.setAlias(form.getAlias()); 106 salesOrderSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 107 salesOrderSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 108 salesOrderSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 109 110 result.setCount(salesOrderSearchEvaluator.execute(this)); 111 } 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownSalesOrderStatusChoice.name(), salesOrderStatusChoice); 115 } 116 } 117 } 118 119 return result; 120 } 121}