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