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.SearchContentCatalogItemsForm;
020import com.echothree.control.user.search.common.result.SearchContentCatalogItemsResult;
021import com.echothree.control.user.search.common.result.SearchResultFactory;
022import com.echothree.model.control.content.server.search.ContentCatalogItemSearchEvaluator;
023import com.echothree.model.control.party.server.logic.LanguageLogic;
024import com.echothree.model.control.search.common.SearchKinds;
025import com.echothree.model.control.search.server.control.SearchControl;
026import com.echothree.model.control.search.server.logic.SearchLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.persistence.Session;
032import com.google.common.base.Splitter;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035
036@Dependent
037public class SearchContentCatalogItemsCommand
038        extends BaseSearchCommand<SearchContentCatalogItemsForm, SearchContentCatalogItemsResult> {
039
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("SearchSortDirectionName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("Q", FieldType.STRING, false, null, null),
050                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
051                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
052                new FieldDefinition("Fields", FieldType.STRING, false, null, null),
053                new FieldDefinition("RememberPreferences", FieldType.BOOLEAN, false, null, null),
054                new FieldDefinition("SearchUseTypeName", FieldType.ENTITY_NAME, false, null, null)
055                );
056    }
057
058    /** Creates a new instance of SearchContentCatalogItemsCommand */
059    public SearchContentCatalogItemsCommand() {
060        super(null, FORM_FIELD_DEFINITIONS, false);
061    }
062    
063    @Override
064    protected BaseResult execute() {
065        var result = SearchResultFactory.getSearchContentCatalogItemsResult();
066        var searchLogic = SearchLogic.getInstance();
067        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.CONTENT_CATALOG_ITEM.name());
068
069        if(!hasExecutionErrors()) {
070            var searchTypeName = form.getSearchTypeName();
071            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
072
073            if(!hasExecutionErrors()) {
074                var languageIsoName = form.getLanguageIsoName();
075                var language = languageIsoName == null ? null : LanguageLogic.getInstance().getLanguageByName(this, languageIsoName);
076                
077                if(!hasExecutionErrors()) {
078                    var searchControl = Session.getModelController(SearchControl.class);
079                    var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType);
080                    var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail();
081                    boolean rememberPreferences = Boolean.valueOf(form.getRememberPreferences());
082                    var searchDefaultOperatorName = form.getSearchDefaultOperatorName();
083                    var searchDefaultOperator = searchDefaultOperatorName == null
084                            ? getDefaultSearchDefaultOperator(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
085                            : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName);
086
087                    if(!hasExecutionErrors()) {
088                        var searchSortOrderName = form.getSearchSortOrderName();
089                        var searchSortOrder = searchSortOrderName == null
090                                ? getDefaultSearchSortOrder(searchLogic, rememberPreferences, searchKind, partySearchTypePreferenceDetail)
091                                : searchLogic.getSearchSortOrderByName(this, searchKind, searchSortOrderName);
092
093                        if(!hasExecutionErrors()) {
094                            var searchSortDirectionName = form.getSearchSortDirectionName();
095                            var searchSortDirection = searchSortDirectionName == null
096                                    ? getDefaultSearchSortDirection(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
097                                    : searchLogic.getSearchSortDirectionByName(this, searchSortDirectionName);
098
099                            if(!hasExecutionErrors()) {
100                                var searchUseTypeName = form.getSearchUseTypeName();
101                                var searchUseType = searchUseTypeName == null ? null : SearchLogic.getInstance().getSearchUseTypeByName(this, searchUseTypeName);
102
103                                if(!hasExecutionErrors()) {
104                                    var userVisit = getUserVisit();
105                                    var createdSince = form.getCreatedSince();
106                                    var modifiedSince = form.getModifiedSince();
107                                    var fields = form.getFields();
108
109                                    if(rememberPreferences) {
110                                        var party = getParty();
111
112                                        if(party != null) {
113                                            updatePartySearchTypePreferences(searchControl, searchType, partySearchTypePreference, searchDefaultOperator,
114                                                    searchSortOrder, searchSortDirection, party);
115                                        }
116                                    }
117
118                                    var contentCatalogItemSearchEvaluator = new ContentCatalogItemSearchEvaluator(userVisit, language,
119                                            searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, searchUseType);
120
121                                    contentCatalogItemSearchEvaluator.setQ(this, form.getQ());
122                                    contentCatalogItemSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
123                                    contentCatalogItemSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
124                                    contentCatalogItemSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
125
126                                    if(!hasExecutionErrors()) {
127                                        result.setCount(contentCatalogItemSearchEvaluator.execute(this));
128                                    }
129                                }
130                            }
131                        }
132                    }
133                }
134            }
135        }
136        
137        return result;
138    }
139}