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