001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.transfer;
018
019import com.echothree.model.control.core.common.CoreOptions;
020import com.echothree.model.control.core.common.transfer.AppearanceTransfer;
021import com.echothree.model.control.core.server.control.AppearanceControl;
022import com.echothree.model.control.core.server.control.ColorControl;
023import com.echothree.model.control.core.server.control.FontControl;
024import com.echothree.model.data.core.server.entity.Appearance;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.transfer.ListWrapper;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class AppearanceTransferCache
032        extends BaseCoreTransferCache<Appearance, AppearanceTransfer> {
033
034    AppearanceControl appearanceControl = Session.getModelController(AppearanceControl.class);
035    ColorControl colorControl = Session.getModelController(ColorControl.class);
036    FontControl fontControl = Session.getModelController(FontControl.class);
037
038    boolean includeTextDecorations;
039    boolean includeTextTransformations;
040    
041    /** Creates a new instance of AppearanceTransferCache */
042    protected AppearanceTransferCache() {
043        super();
044        
045        var options = session.getOptions();
046        if(options != null) {
047            includeTextDecorations = options.contains(CoreOptions.AppearanceIncludeTextDecorations);
048            includeTextTransformations = options.contains(CoreOptions.AppearanceIncludeTextTransformations);
049        }
050        
051        setIncludeEntityInstance(true);
052    }
053
054    public AppearanceTransfer getAppearanceTransfer(UserVisit userVisit, Appearance appearance) {
055        var appearanceTransfer = get(appearance);
056
057        if(appearanceTransfer == null) {
058            var appearanceDetail = appearance.getLastDetail();
059            var appearanceName = appearanceDetail.getAppearanceName();
060            var textColor = appearanceDetail.getTextColor();
061            var textColorTransfer = textColor == null ? null : colorControl.getColorTransfer(userVisit, textColor);
062            var backgroundColor = appearanceDetail.getBackgroundColor();
063            var backgroundColorTransfer = backgroundColor == null ? null : colorControl.getColorTransfer(userVisit, backgroundColor);
064            var fontStyle = appearanceDetail.getFontStyle();
065            var fontStyleTransfer = fontStyle == null ? null : fontControl.getFontStyleTransfer(userVisit, fontStyle);
066            var fontWeight = appearanceDetail.getFontWeight();
067            var fontWeightTransfer = fontWeight == null ? null : fontControl.getFontWeightTransfer(userVisit, fontWeight);
068            var isDefault = appearanceDetail.getIsDefault();
069            var sortOrder = appearanceDetail.getSortOrder();
070            var description = appearanceControl.getBestAppearanceDescription(appearance, getLanguage(userVisit));
071
072            appearanceTransfer = new AppearanceTransfer(appearanceName, textColorTransfer, backgroundColorTransfer, fontStyleTransfer, fontWeightTransfer,
073                    isDefault, sortOrder, description);
074            put(userVisit, appearance, appearanceTransfer);
075            
076            if(includeTextDecorations) {
077                appearanceTransfer.setAppearanceTextDecorations(new ListWrapper<>(appearanceControl.getAppearanceTextDecorationTransfersByAppearance(userVisit, appearance)));
078            }
079            
080            if(includeTextTransformations) {
081                appearanceTransfer.setAppearanceTextTransformations(new ListWrapper<>(appearanceControl.getAppearanceTextTransformationTransfersByAppearance(userVisit, appearance)));
082            }
083        }
084
085        return appearanceTransfer;
086    }
087
088}