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.letter.server.transfer;
018
019import com.echothree.model.control.contact.common.transfer.PartyContactMechanismTransfer;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.letter.common.transfer.LetterSourceTransfer;
022import com.echothree.model.control.letter.server.control.LetterControl;
023import com.echothree.model.control.party.common.transfer.CompanyTransfer;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.letter.server.entity.LetterSource;
026import com.echothree.model.data.letter.server.entity.LetterSourceDetail;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.server.persistence.Session;
029
030public class LetterSourceTransferCache
031        extends BaseLetterTransferCache<LetterSource, LetterSourceTransfer> {
032    
033    ContactControl contactControl = Session.getModelController(ContactControl.class);
034    PartyControl partyControl = Session.getModelController(PartyControl.class);
035    
036    /** Creates a new instance of LetterSourceTransferCache */
037    public LetterSourceTransferCache(UserVisit userVisit, LetterControl letterControl) {
038        super(userVisit, letterControl);
039        
040        setIncludeEntityInstance(true);
041    }
042    
043    public LetterSourceTransfer getLetterSourceTransfer(LetterSource letterSource) {
044        LetterSourceTransfer letterSourceTransfer = get(letterSource);
045        
046        if(letterSourceTransfer == null) {
047            LetterSourceDetail letterSourceDetail = letterSource.getLastDetail();
048            String letterSourceName = letterSourceDetail.getLetterSourceName();
049            CompanyTransfer companyTransfer = partyControl.getCompanyTransfer(userVisit, letterSourceDetail.getCompanyParty());
050            PartyContactMechanismTransfer emailAddressPartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getEmailAddressPartyContactMechanism());
051            PartyContactMechanismTransfer postalAddressPartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getPostalAddressPartyContactMechanism());
052            PartyContactMechanismTransfer letterSourcePartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getLetterSourcePartyContactMechanism());
053            Boolean isDefault = letterSourceDetail.getIsDefault();
054            Integer sortOrder = letterSourceDetail.getSortOrder();
055            String description = letterControl.getBestLetterSourceDescription(letterSource, getLanguage());
056            
057            letterSourceTransfer = new LetterSourceTransfer(letterSourceName, companyTransfer,
058                    emailAddressPartyContactMechanismTransfer, postalAddressPartyContactMechanismTransfer,
059                    letterSourcePartyContactMechanismTransfer, isDefault, sortOrder, description);
060            put(letterSource, letterSourceTransfer);
061        }
062        
063        return letterSourceTransfer;
064    }
065    
066}