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.period.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.period.server.control.PeriodControl;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.control.workflow.server.graphql.WorkflowEntranceObject;
029import com.echothree.model.control.workflow.server.graphql.WorkflowSecurityUtils;
030import com.echothree.model.data.period.common.PeriodTypeConstants;
031import com.echothree.model.data.period.server.entity.PeriodKind;
032import com.echothree.model.data.period.server.entity.PeriodKindDetail;
033import com.echothree.util.server.persistence.Session;
034import graphql.annotations.annotationTypes.GraphQLDescription;
035import graphql.annotations.annotationTypes.GraphQLField;
036import graphql.annotations.annotationTypes.GraphQLName;
037import graphql.annotations.annotationTypes.GraphQLNonNull;
038import graphql.annotations.connection.GraphQLConnection;
039import graphql.schema.DataFetchingEnvironment;
040import java.util.ArrayList;
041import java.util.stream.Collectors;
042
043@GraphQLDescription("period kind object")
044@GraphQLName("PeriodKind")
045public class PeriodKindObject
046        extends BaseEntityInstanceObject {
047    
048    private final PeriodKind periodKind; // Always Present
049    
050    public PeriodKindObject(PeriodKind periodKind) {
051        super(periodKind.getPrimaryKey());
052        
053        this.periodKind = periodKind;
054    }
055
056    private PeriodKindDetail periodKindDetail; // Optional, use getPeriodKindDetail()
057    
058    private PeriodKindDetail getPeriodKindDetail() {
059        if(periodKindDetail == null) {
060            periodKindDetail = periodKind.getLastDetail();
061        }
062        
063        return periodKindDetail;
064    }
065    
066    @GraphQLField
067    @GraphQLDescription("period kind name")
068    @GraphQLNonNull
069    public String getPeriodKindName() {
070        return getPeriodKindDetail().getPeriodKindName();
071    }
072
073    @GraphQLField
074    @GraphQLDescription("workflow entrance")
075    public WorkflowEntranceObject getWorkflowEntrance(final DataFetchingEnvironment env) {
076        if(WorkflowSecurityUtils.getHasWorkflowEntranceAccess(env)) {
077            var workflowEntrance = getPeriodKindDetail().getWorkflowEntrance();
078
079            return workflowEntrance == null ? null : new WorkflowEntranceObject(workflowEntrance);
080        } else {
081            return null;
082        }
083    }
084
085    @GraphQLField
086    @GraphQLDescription("is default")
087    @GraphQLNonNull
088    public boolean getIsDefault() {
089        return getPeriodKindDetail().getIsDefault();
090    }
091    
092    @GraphQLField
093    @GraphQLDescription("sort order")
094    @GraphQLNonNull
095    public int getSortOrder() {
096        return getPeriodKindDetail().getSortOrder();
097    }
098    
099    @GraphQLField
100    @GraphQLDescription("description")
101    @GraphQLNonNull
102    public String getDescription(final DataFetchingEnvironment env) {
103        var periodControl = Session.getModelController(PeriodControl.class);
104        var userControl = Session.getModelController(UserControl.class);
105
106        return periodControl.getBestPeriodKindDescription(periodKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
107    }
108
109    @GraphQLField
110    @GraphQLDescription("period types")
111    @GraphQLNonNull
112    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
113    public CountingPaginatedData<PeriodTypeObject> getPeriodTypes(final DataFetchingEnvironment env) {
114        if(PeriodSecurityUtils.getHasPeriodTypesAccess(env)) {
115            var periodControl = Session.getModelController(PeriodControl.class);
116            var totalCount = periodControl.countPeriodTypes(periodKind);
117
118            try(var objectLimiter = new ObjectLimiter(env, PeriodTypeConstants.COMPONENT_VENDOR_NAME, PeriodTypeConstants.ENTITY_TYPE_NAME, totalCount)) {
119                var entities = periodControl.getPeriodTypes(periodKind);
120                var periodTypes = entities.stream().map(PeriodTypeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
121
122                return new CountedObjects<>(objectLimiter, periodTypes);
123            }
124        } else {
125            return Connections.emptyConnection();
126        }
127    }
128
129    @GraphQLField
130    @GraphQLDescription("periods")
131    @GraphQLNonNull
132    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
133    public CountingPaginatedData<PeriodInterface> getPeriods(final DataFetchingEnvironment env,
134            @GraphQLName("parentPeriodName") final String parentPeriodName,
135            @GraphQLName("periodTypeName") final String periodTypeName) {
136        return PeriodObject.getPeriods(env, getPeriodKindName(), parentPeriodName, periodTypeName);
137    }
138
139}