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.BaseGetResultsFacetsForm;
020import com.echothree.control.user.search.common.result.BaseGetResultsFacetsResult;
021import com.echothree.model.control.core.common.EntityAttributeTypes;
022import com.echothree.model.control.core.server.logic.EntityTypeLogic;
023import com.echothree.model.control.search.common.transfer.UserVisitSearchFacetTransfer;
024import com.echothree.model.control.search.server.control.SearchControl;
025import com.echothree.model.control.search.server.logic.SearchLogic;
026import com.echothree.model.control.search.server.logic.UserVisitSearchFacetLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.persistence.Session;
035import java.util.LinkedHashMap;
036import java.util.List;
037
038public abstract class BaseGetResultsFacetsCommand<F extends BaseGetResultsFacetsForm, R extends BaseGetResultsFacetsResult>
039        extends BaseSimpleCommand<F> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null)
046        );
047    }
048
049    /** Creates a new instance of BaseGetResultsFacetsCommand */
050    protected BaseGetResultsFacetsCommand(UserVisitPK userVisitPK, F form, CommandSecurityDefinition COMMAND_SECURITY_DEFINITION) {
051        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
052    }
053
054    // Substantial portions of this are duplicated in BaseResultsObject.
055    protected BaseResult execute(final String componentVendorName, final String entityTypeName, final String searchKindName,
056            final BaseGetResultsFacetsResult result) {
057        var entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, componentVendorName, entityTypeName);
058        var searchType = SearchLogic.getInstance().getSearchTypeByName(this, searchKindName, form.getSearchTypeName());
059
060        if(!hasExecutionErrors()) {
061            var searchControl = Session.getModelController(SearchControl.class);
062            var userVisitSearch = searchControl.getUserVisitSearch(getUserVisit(), searchType);
063
064            if(userVisitSearch != null) {
065                var coreControl = getCoreControl();
066                var entityAttributes = coreControl.getEntityAttributesByEntityType(entityType);
067                var userVisitSearchFacets = new LinkedHashMap<String, UserVisitSearchFacetTransfer>();
068
069                entityAttributes.forEach((entityAttribute) -> {
070                    var entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
071
072                    if (entityAttributeTypeName.equals(EntityAttributeTypes.LISTITEM.name())
073                            || entityAttributeTypeName.equals(EntityAttributeTypes.MULTIPLELISTITEM.name())
074                            || entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())
075                            || entityAttributeTypeName.equals(EntityAttributeTypes.LONG.name())) {
076                        var userVisitSearchFacet = UserVisitSearchFacetLogic.getInstance().getUserVisitSearchFacetTransfer(this, userVisitSearch, entityAttribute);
077
078                        if ((userVisitSearchFacet.getUserVisitSearchFacetListItems() != null && userVisitSearchFacet.getUserVisitSearchFacetListItems().getSize() > 0)
079                                || (userVisitSearchFacet.getUserVisitSearchFacetIntegers() != null && userVisitSearchFacet.getUserVisitSearchFacetIntegers().getSize() > 0)
080                                || (userVisitSearchFacet.getUserVisitSearchFacetLongs() != null && userVisitSearchFacet.getUserVisitSearchFacetLongs().getSize() > 0)) {
081                            userVisitSearchFacets.put(entityAttribute.getLastDetail().getEntityAttributeName(), userVisitSearchFacet);
082                        }
083                    }
084                });
085
086                if(!hasExecutionErrors()) {
087                    result.setUserVisitSearchFacets(userVisitSearchFacets);
088                }
089            } else {
090                addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name(), searchType.getLastDetail().getSearchTypeName());
091            }
092        }
093        
094        return result;
095    }
096
097    @Override
098    protected abstract BaseResult execute();
099    
100}