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.model.control.warehouse.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.graphql.count.Connections;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.inventory.server.control.InventoryLocationControl;
027import com.echothree.model.control.inventory.server.graphql.InventoryLocationGroupObject;
028import com.echothree.model.control.inventory.server.graphql.InventoryLocationObject;
029import com.echothree.model.control.inventory.server.graphql.InventorySecurityUtils;
030import com.echothree.model.control.item.common.workflow.ItemStatusConstants;
031import com.echothree.model.control.user.server.control.UserControl;
032import com.echothree.model.control.warehouse.common.workflow.LocationStatusConstants;
033import com.echothree.model.control.warehouse.server.control.WarehouseControl;
034import com.echothree.model.control.workflow.server.graphql.WorkflowEntityStatusObject;
035import com.echothree.model.data.inventory.common.InventoryLocationConstants;
036import com.echothree.model.data.warehouse.server.entity.Location;
037import com.echothree.model.data.warehouse.server.entity.LocationDetail;
038import com.echothree.util.server.persistence.Session;
039import java.util.ArrayList;
040import java.util.stream.Collectors;
041import graphql.annotations.annotationTypes.GraphQLDescription;
042import graphql.annotations.annotationTypes.GraphQLField;
043import graphql.annotations.annotationTypes.GraphQLName;
044import graphql.annotations.annotationTypes.GraphQLNonNull;
045import graphql.annotations.connection.GraphQLConnection;
046import graphql.schema.DataFetchingEnvironment;
047
048@GraphQLDescription("location type object")
049@GraphQLName("Location")
050@SuppressWarnings("StringConcatToTextBlock")
051public class LocationObject
052        extends BaseEntityInstanceObject {
053    
054    private final Location location; // Always Present
055    
056    public LocationObject(Location location) {
057        super(location.getPrimaryKey());
058        
059        this.location = location;
060    }
061
062    private LocationDetail locationDetail; // Optional, use getLocationDetail()
063    
064    private LocationDetail getLocationDetail() {
065        if(locationDetail == null) {
066            locationDetail = location.getLastDetail();
067        }
068        
069        return locationDetail;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("warehouse")
074    public WarehouseObject getWarehouse(final DataFetchingEnvironment env) {
075        return WarehouseSecurityUtils.getHasWarehouseAccess(env) ?
076                new WarehouseObject(getLocationDetail().getWarehouseParty()) : null;
077    }
078
079    @GraphQLField
080    @GraphQLDescription("location name")
081    @GraphQLNonNull
082    public String getLocationName() {
083        return getLocationDetail().getLocationName();
084    }
085
086    @GraphQLField
087    @GraphQLDescription("location type")
088    public LocationTypeObject getLocationType(final DataFetchingEnvironment env) {
089        return WarehouseSecurityUtils.getHasLocationTypeAccess(env) ?
090                new LocationTypeObject(getLocationDetail().getLocationType()) : null;
091    }
092
093    @GraphQLField
094    @GraphQLDescription("location use type")
095    public LocationUseTypeObject getLocationUseType(final DataFetchingEnvironment env) {
096        return WarehouseSecurityUtils.getHasLocationUseTypeAccess(env) ?
097                new LocationUseTypeObject(getLocationDetail().getLocationUseType()) : null;
098    }
099
100    @GraphQLField
101    @GraphQLDescription("velocity")
102    @GraphQLNonNull
103    public int getVelocity() {
104        return getLocationDetail().getVelocity();
105    }
106
107    @GraphQLField
108    @GraphQLDescription("inventory location group")
109    public InventoryLocationGroupObject getInventoryLocationGroup(final DataFetchingEnvironment env) {
110        return InventorySecurityUtils.getHasInventoryLocationGroupAccess(env) ?
111                new InventoryLocationGroupObject(getLocationDetail().getInventoryLocationGroup()) : null;
112    }
113
114    @GraphQLField
115    @GraphQLDescription("description")
116    @GraphQLNonNull
117    public String getDescription(final DataFetchingEnvironment env) {
118        var warehouseControl = Session.getModelController(WarehouseControl.class);
119        var userControl = Session.getModelController(UserControl.class);
120
121        return warehouseControl.getBestLocationDescription(location, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
122    }
123
124    @GraphQLField
125    @GraphQLDescription("location status")
126    public WorkflowEntityStatusObject getLocationStatus(final DataFetchingEnvironment env) {
127        return getWorkflowEntityStatusObject(env, LocationStatusConstants.Workflow_LOCATION_STATUS);
128    }
129
130    @GraphQLField
131    @GraphQLDescription("inventory locations")
132    @GraphQLNonNull
133    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
134    public CountingPaginatedData<InventoryLocationObject> getInventoryLocations(final DataFetchingEnvironment env) {
135        if(InventorySecurityUtils.getHasInventoryLocationsAccess(env)) {
136            var inventoryLocationControl = Session.getModelController(InventoryLocationControl.class);
137            var totalCount = inventoryLocationControl.countInventoryLocationsByLocation(location);
138
139            try(var objectLimiter = new ObjectLimiter(env, InventoryLocationConstants.COMPONENT_VENDOR_NAME,
140                    InventoryLocationConstants.ENTITY_TYPE_NAME, totalCount)) {
141                var entities = inventoryLocationControl.getInventoryLocationsByLocation(location);
142                var inventoryLocations = entities.stream()
143                        .map(InventoryLocationObject::new)
144                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
145
146                return new CountedObjects<>(objectLimiter, inventoryLocations);
147            }
148        } else {
149            return Connections.emptyConnection();
150        }
151    }
152
153}