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