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.core.server.graphql;
018
019import com.echothree.model.control.core.server.control.CoreControl;
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.core.server.entity.Appearance;
024import com.echothree.model.data.core.server.entity.AppearanceDetail;
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;
031import java.util.ArrayList;
032import java.util.List;
033
034@GraphQLDescription("appearance object")
035@GraphQLName("Appearance")
036public class AppearanceObject
037        extends BaseEntityInstanceObject {
038    
039    private final Appearance appearance; // Always Present
040    
041    public AppearanceObject(Appearance appearance) {
042        super(appearance.getPrimaryKey());
043        
044        this.appearance = appearance;
045    }
046
047    private AppearanceDetail appearanceDetail; // Optional, use getAppearanceDetail()
048    
049    private AppearanceDetail getAppearanceDetail() {
050        if(appearanceDetail == null) {
051            appearanceDetail = appearance.getLastDetail();
052        }
053        
054        return appearanceDetail;
055    }
056    
057    @GraphQLField
058    @GraphQLDescription("appearance name")
059    @GraphQLNonNull
060    public String getAppearanceName() {
061        return getAppearanceDetail().getAppearanceName();
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("text color")
066    public ColorObject getTextColor() {
067        var textColor = getAppearanceDetail().getTextColor();
068        
069        return textColor == null ? null : new ColorObject(textColor);
070    }
071    
072    @GraphQLField
073    @GraphQLDescription("background color")
074    public ColorObject getBackgroundColor() {
075        var backgroundColor = getAppearanceDetail().getBackgroundColor();
076        
077        return backgroundColor == null ? null : new ColorObject(backgroundColor);
078    }
079    
080    @GraphQLField
081    @GraphQLDescription("font style")
082    public FontStyleObject getFontStyle() {
083        var fontStyle = getAppearanceDetail().getFontStyle();
084        
085        return fontStyle == null ? null : new FontStyleObject(fontStyle);
086    }
087    
088    @GraphQLField
089    @GraphQLDescription("font weight")
090    public FontWeightObject getFontWeight() {
091        var fontWeight = getAppearanceDetail().getFontWeight();
092        
093        return fontWeight == null ? null : new FontWeightObject(fontWeight);
094    }
095    
096    @GraphQLField
097    @GraphQLDescription("appearance text decorations")
098    @GraphQLNonNull
099    public List<AppearanceTextDecorationObject> getAppearanceTextDecorations() {
100        var coreControl = Session.getModelController(CoreControl.class);
101        var entities = coreControl.getAppearanceTextDecorationsByAppearance(appearance);
102        var objects = new ArrayList<AppearanceTextDecorationObject>(entities.size());
103        
104        entities.forEach((entity) -> {
105            objects.add(new AppearanceTextDecorationObject(entity));
106        });
107        
108        return objects;
109    }
110    
111    @GraphQLField
112    @GraphQLDescription("appearance text transformations")
113    @GraphQLNonNull
114    public List<AppearanceTextTransformationObject> getAppearanceTextTransformations() {
115        var coreControl = Session.getModelController(CoreControl.class);
116        var entities = coreControl.getAppearanceTextTransformationsByAppearance(appearance);
117        var objects = new ArrayList<AppearanceTextTransformationObject>(entities.size());
118        
119        entities.forEach((entity) -> {
120            objects.add(new AppearanceTextTransformationObject(entity));
121        });
122        
123        return objects;
124    }
125    
126    @GraphQLField
127    @GraphQLDescription("is default")
128    @GraphQLNonNull
129    public boolean getIsDefault() {
130        return getAppearanceDetail().getIsDefault();
131    }
132    
133    @GraphQLField
134    @GraphQLDescription("sort order")
135    @GraphQLNonNull
136    public int getSortOrder() {
137        return getAppearanceDetail().getSortOrder();
138    }
139    
140    @GraphQLField
141    @GraphQLDescription("description")
142    @GraphQLNonNull
143    public String getDescription(final DataFetchingEnvironment env) {
144        var coreControl = Session.getModelController(CoreControl.class);
145        var userControl = Session.getModelController(UserControl.class);
146
147        return coreControl.getBestAppearanceDescription(appearance, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
148    }
149    
150}