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.GetSalesOrderResultsForm;
020import com.echothree.control.user.search.common.result.GetSalesOrderResultsResult;
021import com.echothree.control.user.search.common.result.SearchResultFactory;
022import com.echothree.model.control.sales.server.control.SalesOrderControl;
023import com.echothree.model.control.search.common.SearchKinds;
024import com.echothree.model.control.search.server.control.SearchControl;
025import com.echothree.model.control.search.server.logic.SearchLogic;
026import com.echothree.model.data.search.server.entity.SearchKind;
027import com.echothree.model.data.search.server.entity.SearchType;
028import com.echothree.model.data.search.server.entity.UserVisitSearch;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.persistence.Session;
037import java.util.Arrays;
038import java.util.Collections;
039import java.util.List;
040
041public class GetSalesOrderResultsCommand
042        extends BaseSimpleCommand<GetSalesOrderResultsForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null)
049                ));
050    }
051
052    /** Creates a new instance of GetSalesOrderResultsCommand */
053    public GetSalesOrderResultsCommand(UserVisitPK userVisitPK, GetSalesOrderResultsForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        GetSalesOrderResultsResult result = SearchResultFactory.getGetSalesOrderResultsResult();
060        var searchControl = Session.getModelController(SearchControl.class);
061        SearchKind searchKind = searchControl.getSearchKindByName(SearchKinds.SALES_ORDER.name());
062        
063        if(searchKind != null) {
064            String searchTypeName = form.getSearchTypeName();
065            SearchType searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName);
066            
067            if(searchType != null) {
068                UserVisit userVisit = getUserVisit();
069                UserVisitSearch userVisitSearch = searchControl.getUserVisitSearch(userVisit, searchType);
070                
071                if(userVisitSearch != null) {
072                    var salesOrderControl = Session.getModelController(SalesOrderControl.class);
073
074                    if(session.hasLimit(com.echothree.model.data.search.server.factory.SearchResultFactory.class)) {
075                        result.setSalesOrderResultCount(SearchLogic.getInstance().countSearchResults(userVisitSearch.getSearch()));
076                    }
077
078                    result.setSalesOrderResults(salesOrderControl.getSalesOrderResultTransfers(userVisit, userVisitSearch));
079                } else {
080                    addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name());
081                }
082            } else {
083                addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.SALES_ORDER.name(), searchTypeName);
084            }
085        } else {
086            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.SALES_ORDER.name());
087        }
088        
089        return result;
090    }
091}