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.content.server.graphql;
018
019import com.echothree.model.control.content.server.control.ContentControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.util.BaseGraphQl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.content.server.entity.ContentWebAddress;
024import com.echothree.model.data.content.server.entity.ContentWebAddressDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("content web address object")
033@GraphQLName("ContentWebAddress")
034public class ContentWebAddressObject
035        extends BaseEntityInstanceObject {
036    
037    private final ContentWebAddress contentWebAddress; // Always Present
038    
039    public ContentWebAddressObject(ContentWebAddress contentWebAddress) {
040        super(contentWebAddress.getPrimaryKey());
041        
042        this.contentWebAddress = contentWebAddress;
043    }
044
045    private ContentWebAddressDetail contentWebAddressDetail; // Optional, use getContentWebAddressDetail()
046    
047    private ContentWebAddressDetail getContentWebAddressDetail() {
048        if(contentWebAddressDetail == null) {
049            contentWebAddressDetail = contentWebAddress.getLastDetail();
050        }
051        
052        return contentWebAddressDetail;
053    }
054    
055    @GraphQLField
056    @GraphQLDescription("content collection name")
057    @GraphQLNonNull
058    public String getContentWebAddressName() {
059        return getContentWebAddressDetail().getContentWebAddressName();
060    }
061
062    @GraphQLField
063    @GraphQLDescription("content collection")
064    public ContentCollectionObject getContentCollection(final DataFetchingEnvironment env) {
065        return ContentSecurityUtils.getHasContentCollectionAccess(env) ? new ContentCollectionObject(getContentWebAddressDetail().getContentCollection()) : null;
066    }
067
068    @GraphQLField
069    @GraphQLDescription("description")
070    @GraphQLNonNull
071    public String getDescription(final DataFetchingEnvironment env) {
072        var contentControl = Session.getModelController(ContentControl.class);
073        var userControl = Session.getModelController(UserControl.class);
074
075        return contentControl.getBestContentWebAddressDescription(contentWebAddress, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
076    }
077    
078}