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.server.control.ContactControl;
022import com.echothree.model.data.contact.server.entity.PostalAddressFormat;
023import com.echothree.model.data.contact.server.entity.PostalAddressFormatDetail;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.transfer.ListWrapper;
026import java.util.Set;
027
028public class PostalAddressFormatTransferCache
029        extends BaseContactTransferCache<PostalAddressFormat, PostalAddressFormatTransfer> {
030    
031    boolean includeLines;
032    
033    /** Creates a new instance of PostalAddressFormatTransferCache */
034    public PostalAddressFormatTransferCache(UserVisit userVisit, ContactControl contactControl) {
035        super(userVisit, contactControl);
036        
037        var options = session.getOptions();
038        if(options != null) {
039            includeLines = options.contains(ContactOptions.PostalAddressFormatIncludeLines);
040        }
041        
042        setIncludeEntityInstance(true);
043    }
044    
045    public PostalAddressFormatTransfer getPostalAddressFormatTransfer(PostalAddressFormat postalAddressFormat) {
046        PostalAddressFormatTransfer postalAddressFormatTransfer = get(postalAddressFormat);
047        
048        if(postalAddressFormatTransfer == null) {
049            PostalAddressFormatDetail postalAddressFormatDetail = postalAddressFormat.getLastDetail();
050            String postalAddressFormatName = postalAddressFormatDetail.getPostalAddressFormatName();
051            Boolean isDefault = postalAddressFormatDetail.getIsDefault();
052            Integer sortOrder = postalAddressFormatDetail.getSortOrder();
053            String description = contactControl.getBestPostalAddressFormatDescription(postalAddressFormat, getLanguage());
054            
055            postalAddressFormatTransfer = new PostalAddressFormatTransfer(postalAddressFormatName, isDefault, sortOrder, description);
056            put(postalAddressFormat, postalAddressFormatTransfer);
057            
058            if(includeLines) {
059                postalAddressFormatTransfer.setPostalAddressLines(new ListWrapper<>(contactControl.getPostalAddressLineTransfersByPostalAddressFormat(userVisit, postalAddressFormat)));
060            }
061        }
062        
063        return postalAddressFormatTransfer;
064    }
065    
066}