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.party.server.transfer;
018
019import com.echothree.model.control.party.common.PartyProperties;
020import com.echothree.model.control.party.common.transfer.LanguageTransfer;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.party.server.entity.Language;
023import com.echothree.model.data.user.server.entity.UserVisit;
024import com.echothree.util.common.form.TransferProperties;
025import java.util.Set;
026
027public class LanguageTransferCache
028        extends BasePartyTransferCache<Language, LanguageTransfer> {
029    
030    TransferProperties transferProperties;
031    boolean filterLanguageIsoName;
032    boolean filterisDefault;
033    boolean filterSortOrder;
034    boolean filterDescription;
035    
036    /** Creates a new instance of LanguageTransferCache */
037    public LanguageTransferCache(UserVisit userVisit, PartyControl partyControl) {
038        super(userVisit, partyControl);
039
040        transferProperties = session.getTransferProperties();
041        if(transferProperties != null) {
042            var properties = transferProperties.getProperties(LanguageTransfer.class);
043            
044            if(properties != null) {
045                filterLanguageIsoName = !properties.contains(PartyProperties.LANGUAGE_ISO_NAME);
046                filterisDefault = !properties.contains(PartyProperties.IS_DEFAULT);
047                filterSortOrder = !properties.contains(PartyProperties.SORT_ORDER);
048                filterDescription = !properties.contains(PartyProperties.DESCRIPTION);
049            }
050        }
051    }
052    
053    public LanguageTransfer getLanguageTransfer(Language language) {
054        LanguageTransfer languageTransfer = get(language);
055        
056        if(languageTransfer == null) {
057            String languageIsoName = filterLanguageIsoName ? null : language.getLanguageIsoName();
058            Boolean isDefault = filterisDefault ? null : language.getIsDefault();
059            Integer sortOrder = filterSortOrder ? null : language.getSortOrder();
060            String description = filterDescription ? null : partyControl.getBestLanguageDescription(language, getLanguage());
061            
062            languageTransfer = new LanguageTransfer(languageIsoName, isDefault, sortOrder, description);
063            put(language, languageTransfer);
064        }
065        
066        return languageTransfer;
067    }
068    
069}