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