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.contact.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.contact.common.ContactOptions;
021import com.echothree.model.control.contact.common.transfer.PostalAddressLineTransfer;
022import com.echothree.model.control.contact.server.control.ContactControl;
023import com.echothree.model.data.contact.server.entity.PostalAddressLine;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.transfer.ListWrapper;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class PostalAddressLineTransferCache
030        extends BaseContactTransferCache<PostalAddressLine, PostalAddressLineTransfer> {
031
032    @Inject
033    ContactControl contactControl;
034
035    boolean includeElements;
036    
037    /** Creates a new instance of PostalAddressLineTransferCache */
038    protected PostalAddressLineTransferCache() {
039        super();
040        
041        var options = session.getOptions();
042        if(options != null) {
043            includeElements = options.contains(ContactOptions.PostalAddressLineIncludeElements);
044        }
045    }
046    
047    public PostalAddressLineTransfer getPostalAddressLineTransfer(UserVisit userVisit, PostalAddressLine postalAddressLine) {
048        var postalAddressLineTransfer = get(postalAddressLine);
049        
050        if(postalAddressLineTransfer == null) {
051            var postalAddressLineDetail = postalAddressLine.getLastDetail();
052            var postalAddressFormat = contactControl.getPostalAddressFormatTransfer(userVisit, postalAddressLineDetail.getPostalAddressFormat());
053            var postalAddressLineSortOrder = postalAddressLineDetail.getPostalAddressLineSortOrder();
054            var prefix = postalAddressLineDetail.getPrefix();
055            var alwaysIncludePrefix = postalAddressLineDetail.getAlwaysIncludePrefix();
056            var suffix = postalAddressLineDetail.getSuffix();
057            var alwaysIncludeSuffix = postalAddressLineDetail.getAlwaysIncludeSuffix();
058            var collapseIfEmpty = postalAddressLineDetail.getCollapseIfEmpty();
059            
060            postalAddressLineTransfer = new PostalAddressLineTransfer(postalAddressFormat, postalAddressLineSortOrder, prefix,
061                    alwaysIncludePrefix, suffix, alwaysIncludeSuffix, collapseIfEmpty);
062            put(userVisit, postalAddressLine, postalAddressLineTransfer);
063            
064            if(includeElements) {
065                postalAddressLineTransfer.setPostalAddressLineElements(new ListWrapper<>(contactControl.getPostalAddressLineElementTransfersByPostalAddressLine(userVisit, postalAddressLine)));
066            }
067        }
068        
069        return postalAddressLineTransfer;
070    }
071    
072}