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