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