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