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.term.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.term.common.TermTypes; 022import com.echothree.model.control.term.server.control.TermControl; 023import com.echothree.model.control.user.server.control.UserControl; 024import com.echothree.model.data.term.server.entity.Term; 025import com.echothree.model.data.term.server.entity.TermDetail; 026import com.echothree.util.server.persistence.Session; 027import graphql.annotations.annotationTypes.GraphQLDescription; 028import graphql.annotations.annotationTypes.GraphQLField; 029import graphql.annotations.annotationTypes.GraphQLName; 030import graphql.annotations.annotationTypes.GraphQLNonNull; 031import graphql.schema.DataFetchingEnvironment; 032 033@GraphQLDescription("term object") 034@GraphQLName("Term") 035public class TermObject 036 extends BaseEntityInstanceObject { 037 038 private final Term term; // Always Present 039 040 public TermObject(Term term) { 041 super(term.getPrimaryKey()); 042 043 this.term = term; 044 } 045 046 private TermDetail termDetail; // Optional, use getTermDetail() 047 048 private TermDetail getTermDetail() { 049 if(termDetail == null) { 050 termDetail = term.getLastDetail(); 051 } 052 053 return termDetail; 054 } 055 056 @GraphQLField 057 @GraphQLDescription("term name") 058 @GraphQLNonNull 059 public String getTermName() { 060 return getTermDetail().getTermName(); 061 } 062 063 @GraphQLField 064 @GraphQLDescription("term type") 065 public TermTypeObject getTermType(final DataFetchingEnvironment env) { 066 return TermSecurityUtils.getHasTermTypeAccess(env) ? new TermTypeObject(getTermDetail().getTermType()) : null; 067 } 068 069 @GraphQLField 070 @GraphQLDescription("is default") 071 @GraphQLNonNull 072 public boolean getIsDefault() { 073 return getTermDetail().getIsDefault(); 074 } 075 076 @GraphQLField 077 @GraphQLDescription("sort order") 078 @GraphQLNonNull 079 public int getSortOrder() { 080 return getTermDetail().getSortOrder(); 081 } 082 083 @GraphQLField 084 @GraphQLDescription("term") 085 public TermInterface getTerm(final DataFetchingEnvironment env) { 086 var termType = TermTypes.valueOf(getTermDetail().getTermType().getTermTypeName()); 087 var termControl = Session.getModelController(TermControl.class); 088 TermInterface termInterface = null; 089 090 switch(termType) { 091 case DATE_DRIVEN -> termInterface = new DateDrivenTermObject(termControl.getDateDrivenTerm(term)); 092 case PREPAID -> {} 093 case STANDARD -> termInterface = new StandardTermObject(termControl.getStandardTerm(term)); 094 } 095 096 return termInterface; 097 } 098 099 @GraphQLField 100 @GraphQLDescription("description") 101 @GraphQLNonNull 102 public String getDescription(final DataFetchingEnvironment env) { 103 var termControl = Session.getModelController(TermControl.class); 104 var userControl = Session.getModelController(UserControl.class); 105 106 return termControl.getBestTermDescription(term, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 107 } 108 109}