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.util.BaseGraphQl;
021import com.echothree.model.control.period.server.control.PeriodControl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.control.workflow.server.graphql.WorkflowEntranceObject;
024import com.echothree.model.control.workflow.server.graphql.WorkflowSecurityUtils;
025import com.echothree.model.data.period.server.entity.PeriodKind;
026import com.echothree.model.data.period.server.entity.PeriodKindDetail;
027import com.echothree.util.server.persistence.Session;
028import graphql.annotations.annotationTypes.GraphQLDescription;
029import graphql.annotations.annotationTypes.GraphQLField;
030import graphql.annotations.annotationTypes.GraphQLName;
031import graphql.annotations.annotationTypes.GraphQLNonNull;
032import graphql.schema.DataFetchingEnvironment;
033
034@GraphQLDescription("period kind object")
035@GraphQLName("PeriodKind")
036public class PeriodKindObject
037        extends BaseEntityInstanceObject {
038    
039    private final PeriodKind periodKind; // Always Present
040    
041    public PeriodKindObject(PeriodKind periodKind) {
042        super(periodKind.getPrimaryKey());
043        
044        this.periodKind = periodKind;
045    }
046
047    private PeriodKindDetail periodKindDetail; // Optional, use getPeriodKindDetail()
048    
049    private PeriodKindDetail getPeriodKindDetail() {
050        if(periodKindDetail == null) {
051            periodKindDetail = periodKind.getLastDetail();
052        }
053        
054        return periodKindDetail;
055    }
056    
057    @GraphQLField
058    @GraphQLDescription("period kind name")
059    @GraphQLNonNull
060    public String getPeriodKindName() {
061        return getPeriodKindDetail().getPeriodKindName();
062    }
063
064    @GraphQLField
065    @GraphQLDescription("workflow entrance")
066    public WorkflowEntranceObject getWorkflowEntrance(final DataFetchingEnvironment env) {
067        if(WorkflowSecurityUtils.getHasWorkflowEntranceAccess(env)) {
068            var workflowEntrance = getPeriodKindDetail().getWorkflowEntrance();
069
070            return workflowEntrance == null ? null : new WorkflowEntranceObject(workflowEntrance);
071        } else {
072            return null;
073        }
074    }
075
076    @GraphQLField
077    @GraphQLDescription("is default")
078    @GraphQLNonNull
079    public boolean getIsDefault() {
080        return getPeriodKindDetail().getIsDefault();
081    }
082    
083    @GraphQLField
084    @GraphQLDescription("sort order")
085    @GraphQLNonNull
086    public int getSortOrder() {
087        return getPeriodKindDetail().getSortOrder();
088    }
089    
090    @GraphQLField
091    @GraphQLDescription("description")
092    @GraphQLNonNull
093    public String getDescription(final DataFetchingEnvironment env) {
094        var periodControl = Session.getModelController(PeriodControl.class);
095        var userControl = Session.getModelController(UserControl.class);
096
097        return periodControl.getBestPeriodKindDescription(periodKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
098    }
099
100}