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.user.server.transfer;
018
019import com.echothree.model.control.accounting.common.transfer.CurrencyTransfer;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.associate.common.transfer.AssociateReferralTransfer;
022import com.echothree.model.control.associate.server.control.AssociateControl;
023import com.echothree.model.control.campaign.server.control.CampaignControl;
024import com.echothree.model.control.offer.common.transfer.OfferUseTransfer;
025import com.echothree.model.control.offer.server.control.OfferUseControl;
026import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
027import com.echothree.model.control.party.common.transfer.LanguageTransfer;
028import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
029import com.echothree.model.control.party.server.control.PartyControl;
030import com.echothree.model.control.user.common.UserOptions;
031import com.echothree.model.control.user.common.transfer.UserKeyTransfer;
032import com.echothree.model.control.user.common.transfer.UserVisitTransfer;
033import com.echothree.model.control.user.server.control.UserControl;
034import com.echothree.model.data.accounting.server.entity.Currency;
035import com.echothree.model.data.associate.server.entity.AssociateReferral;
036import com.echothree.model.data.offer.server.entity.OfferUse;
037import com.echothree.model.data.party.server.entity.DateTimeFormat;
038import com.echothree.model.data.party.server.entity.Language;
039import com.echothree.model.data.party.server.entity.TimeZone;
040import com.echothree.model.data.user.server.entity.UserKey;
041import com.echothree.model.data.user.server.entity.UserVisit;
042import com.echothree.util.common.transfer.ListWrapper;
043import com.echothree.util.server.persistence.Session;
044import java.util.Set;
045
046public class UserVisitTransferCache
047        extends BaseUserTransferCache<UserVisit, UserVisitTransfer> {
048    
049    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
050    AssociateControl associateControl = Session.getModelController(AssociateControl.class);
051    CampaignControl campaignControl = Session.getModelController(CampaignControl.class);
052    OfferUseControl offerUseControl = Session.getModelController(OfferUseControl.class);
053    PartyControl partyControl = Session.getModelController(PartyControl.class);
054    
055    boolean includeUserVisitCampaigns;
056    
057    /** Creates a new instance of UserVisitTransferCache */
058    public UserVisitTransferCache(UserVisit userVisit, UserControl userControl) {
059        super(userVisit, userControl);
060
061        var options = session.getOptions();
062        if(options != null) {
063            includeUserVisitCampaigns = options.contains(UserOptions.UserVisitIncludeUserVisitCampaigns);
064        }
065    }
066    
067    public UserVisitTransfer getUserVisitTransfer(UserVisit userVisitEntity) {
068        UserVisitTransfer userVisitTransfer = get(userVisit);
069        
070        if(userVisitTransfer == null) {
071            UserKey userKey = userVisitEntity.getUserKey();
072            UserKeyTransfer userKeyTransfer = userKey == null ? null : userControl.getUserKeyTransfer(userVisit, userKey);
073            Language preferredLanguage = userVisitEntity.getPreferredLanguage();
074            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
075            Currency preferredCurrency = userVisitEntity.getPreferredCurrency();
076            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
077            TimeZone preferredTimeZone = userVisitEntity.getPreferredTimeZone();
078            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
079            DateTimeFormat preferredDateTimeFormat = userVisitEntity.getPreferredDateTimeFormat();
080            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
081            Long unformattedLastCommandTime = userVisitEntity.getLastCommandTime();
082            String lastCommandTime = formatTypicalDateTime(unformattedLastCommandTime);
083            OfferUse offerUse = userVisitEntity.getOfferUse();
084            OfferUseTransfer offerUseTransfer = offerUse == null ? null : offerUseControl.getOfferUseTransfer(userVisit, offerUse);
085            AssociateReferral associateReferral = userVisitEntity.getAssociateReferral();
086            AssociateReferralTransfer associateReferralTransfer = associateReferral == null ? null : associateControl.getAssociateReferralTransfer(userVisit, associateReferral);
087            Long unformattedRetainUntilTime = userVisitEntity.getRetainUntilTime();
088            String retainUntilTime = formatTypicalDateTime(unformattedRetainUntilTime);
089
090            userVisitTransfer = new UserVisitTransfer(userKeyTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer,
091                    preferredDateTimeFormatTransfer, unformattedLastCommandTime, lastCommandTime, offerUseTransfer, associateReferralTransfer,
092                    unformattedRetainUntilTime, retainUntilTime);
093            put(userVisitEntity, userVisitTransfer);
094            
095            if(includeUserVisitCampaigns) {
096                userVisitTransfer.setUserVisitCampaigns(new ListWrapper<>(campaignControl.getUserVisitCampaignTransfersByUserVisit(userVisit, userVisitEntity)));
097            }
098        }
099
100        return userVisitTransfer;
101    }
102    
103}