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.SearchSalesOrdersForm; 020import com.echothree.control.user.search.common.result.SearchResultFactory; 021import com.echothree.control.user.search.common.result.SearchSalesOrdersResult; 022import com.echothree.model.control.order.common.OrderTypes; 023import com.echothree.model.control.order.server.logic.OrderLogic; 024import com.echothree.model.control.sales.common.workflow.SalesOrderStatusConstants; 025import com.echothree.model.control.sales.server.search.SalesOrderSearchEvaluator; 026import com.echothree.model.control.search.common.SearchKinds; 027import com.echothree.model.control.search.server.logic.SearchLogic; 028import com.echothree.model.control.workflow.server.control.WorkflowControl; 029import com.echothree.model.data.order.server.entity.OrderAliasType; 030import com.echothree.model.data.order.server.entity.OrderType; 031import com.echothree.model.data.search.server.entity.SearchKind; 032import com.echothree.model.data.search.server.entity.SearchType; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.model.data.workflow.server.entity.WorkflowStep; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseSimpleCommand; 040import com.echothree.util.server.persistence.Session; 041import com.google.common.base.Splitter; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045 046public class SearchSalesOrdersCommand 047 extends BaseSimpleCommand<SearchSalesOrdersForm> { 048 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 053 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("OrderName", FieldType.ENTITY_NAME, false, null, null), 055 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 056 new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("SalesOrderStatusChoice", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 061 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 062 new FieldDefinition("Fields", FieldType.STRING, false, null, null) 063 )); 064 } 065 066 /** Creates a new instance of SearchSalesOrdersCommand */ 067 public SearchSalesOrdersCommand(UserVisitPK userVisitPK, SearchSalesOrdersForm form) { 068 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected BaseResult execute() { 073 SearchLogic searchLogic = SearchLogic.getInstance(); 074 SearchSalesOrdersResult result = SearchResultFactory.getSearchSalesOrdersResult(); 075 SearchKind searchKind = searchLogic.getSearchKindByName(null, SearchKinds.SALES_ORDER.name()); 076 077 if(!hasExecutionErrors()) { 078 String searchTypeName = form.getSearchTypeName(); 079 SearchType searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName); 080 081 if(!hasExecutionErrors()) { 082 var workflowControl = Session.getModelController(WorkflowControl.class); 083 String salesOrderStatusChoice = form.getSalesOrderStatusChoice(); 084 WorkflowStep salesOrderStatusWorkflowStep = salesOrderStatusChoice == null ? null : 085 workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(SalesOrderStatusConstants.Workflow_SALES_ORDER_STATUS), salesOrderStatusChoice); 086 087 if(salesOrderStatusChoice == null || salesOrderStatusChoice != null) { 088 OrderLogic orderLogic = OrderLogic.getInstance(); 089 OrderType orderType = orderLogic.getOrderTypeByName(this, OrderTypes.SALES_ORDER.name()); 090 091 if(!hasExecutionErrors()) { 092 String orderAliasTypeName = form.getOrderAliasTypeName(); 093 OrderAliasType orderAliasType = orderAliasTypeName == null ? null : orderLogic.getOrderAliasTypeByName(this, orderType, orderAliasTypeName); 094 095 if(!hasExecutionErrors()) { 096 SalesOrderSearchEvaluator salesOrderSearchEvaluator = new SalesOrderSearchEvaluator(getUserVisit(), searchType, 097 searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), 098 searchLogic.getDefaultSearchSortDirection(null)); 099 String createdSince = form.getCreatedSince(); 100 String modifiedSince = form.getModifiedSince(); 101 String 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}