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