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.club.server.transfer;
018
019import com.echothree.model.control.club.common.transfer.ClubItemTransfer;
020import com.echothree.model.control.club.server.control.ClubControl;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.uom.common.UomConstants;
023import com.echothree.model.control.uom.server.control.UomControl;
024import com.echothree.model.data.club.server.entity.ClubItem;
025import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class ClubItemTransferCache
032        extends BaseClubTransferCache<ClubItem, ClubItemTransfer> {
033
034    ClubControl clubControl = Session.getModelController(ClubControl.class);
035    ItemControl itemControl = Session.getModelController(ItemControl.class);
036    UomControl uomControl = Session.getModelController(UomControl.class);
037    UnitOfMeasureKind timeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_TIME);
038    
039    /** Creates a new instance of ClubItemTransferCache */
040    protected ClubItemTransferCache() {
041        super();
042    }
043    
044    public ClubItemTransfer getClubItemTransfer(UserVisit userVisit, ClubItem clubItem) {
045        var clubItemTransfer = get(clubItem);
046        
047        if(clubItemTransfer == null) {
048            var club = clubControl.getClubTransfer(userVisit, clubItem.getClub());
049            var clubItemType = clubControl.getClubItemTypeTransfer(userVisit, clubItem.getClubItemType());
050            var item = itemControl.getItemTransfer(userVisit, clubItem.getItem());
051            var unformattedSubscriptionTime = clubItem.getSubscriptionTime();
052            var subscriptionTime = formatUnitOfMeasure(userVisit, timeUnitOfMeasureKind, unformattedSubscriptionTime);
053            
054            clubItemTransfer = new ClubItemTransfer(club, clubItemType, item, unformattedSubscriptionTime, subscriptionTime);
055            put(userVisit, clubItem, clubItemTransfer);
056        }
057        return clubItemTransfer;
058    }
059    
060}