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.model.control.search.server.graphql;
018
019import com.echothree.control.user.search.common.form.GetWarehouseResultsForm;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.graphql.server.graphql.count.Connections;
023import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
024import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
025import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
027import com.echothree.model.control.search.common.SearchKinds;
028import com.echothree.model.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.control.warehouse.server.graphql.WarehouseObject;
030import com.echothree.model.data.search.common.SearchResultConstants;
031import com.echothree.util.server.persistence.Session;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLName;
035import graphql.annotations.annotationTypes.GraphQLNonNull;
036import graphql.annotations.connection.GraphQLConnection;
037import graphql.schema.DataFetchingEnvironment;
038
039@GraphQLDescription("warehouse results object")
040@GraphQLName("WarehouseResults")
041public class WarehouseResultsObject
042        extends BaseResultsObject<GetWarehouseResultsForm> {
043
044    public WarehouseResultsObject(GetWarehouseResultsForm form) {
045        super(ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name(), SearchKinds.WAREHOUSE.name(), form);
046    }
047
048    @GraphQLField
049    @GraphQLDescription("warehouses")
050    @GraphQLNonNull
051    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
052    public CountingPaginatedData<WarehouseObject> getWarehouses(final DataFetchingEnvironment env) {
053        var userVisitSearch = getUserVisitSearch(env);
054
055        if(userVisitSearch == null) {
056            return Connections.emptyConnection();
057        } else {
058            var totalCount = getTotalCount(env);
059
060            try(var objectLimiter = new ObjectLimiter(env, SearchResultConstants.COMPONENT_VENDOR_NAME, SearchResultConstants.ENTITY_TYPE_NAME, totalCount)) {
061                var warehouseControl = Session.getModelController(WarehouseControl.class);
062                var warehouses = warehouseControl.getWarehouseObjectsFromUserVisitSearch(userVisitSearch);
063
064                return new CountedObjects<>(objectLimiter, warehouses);
065            }
066        }
067    }
068    
069}