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