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.user.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.user.server.control.UserControl;
022import com.echothree.model.data.user.server.entity.RecoveryQuestion;
023import com.echothree.model.data.user.server.entity.RecoveryQuestionDetail;
024import com.echothree.util.server.persistence.Session;
025import graphql.annotations.annotationTypes.GraphQLDescription;
026import graphql.annotations.annotationTypes.GraphQLField;
027import graphql.annotations.annotationTypes.GraphQLName;
028import graphql.annotations.annotationTypes.GraphQLNonNull;
029import graphql.schema.DataFetchingEnvironment;
030
031@GraphQLDescription("recovery question object")
032@GraphQLName("RecoveryQuestion")
033public class RecoveryQuestionObject
034        extends BaseEntityInstanceObject {
035    
036    private final RecoveryQuestion recoveryQuestion; // Always Present
037    
038    public RecoveryQuestionObject(RecoveryQuestion recoveryQuestion) {
039        super(recoveryQuestion.getPrimaryKey());
040        
041        this.recoveryQuestion = recoveryQuestion;
042    }
043
044    private RecoveryQuestionDetail recoveryQuestionDetail; // Optional, use getRecoveryQuestionDetail()
045    
046    private RecoveryQuestionDetail getRecoveryQuestionDetail() {
047        if(recoveryQuestionDetail == null) {
048            recoveryQuestionDetail = recoveryQuestion.getLastDetail();
049        }
050        
051        return recoveryQuestionDetail;
052    }
053    
054    @GraphQLField
055    @GraphQLDescription("recovery question name")
056    @GraphQLNonNull
057    public String getRecoveryQuestionName() {
058        return getRecoveryQuestionDetail().getRecoveryQuestionName();
059    }
060
061    @GraphQLField
062    @GraphQLDescription("is default")
063    @GraphQLNonNull
064    public boolean getIsDefault() {
065        return getRecoveryQuestionDetail().getIsDefault();
066    }
067    
068    @GraphQLField
069    @GraphQLDescription("sort order")
070    @GraphQLNonNull
071    public int getSortOrder() {
072        return getRecoveryQuestionDetail().getSortOrder();
073    }
074    
075    @GraphQLField
076    @GraphQLDescription("description")
077    @GraphQLNonNull
078    public String getDescription(final DataFetchingEnvironment env) {
079        var userControl = Session.getModelController(UserControl.class);
080
081        return userControl.getBestRecoveryQuestionDescription(recoveryQuestion, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
082    }
083    
084}