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.period.server.transfer;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.period.common.PeriodConstants;
021import com.echothree.model.control.period.common.transfer.PeriodKindTransfer;
022import com.echothree.model.control.period.common.transfer.PeriodTransfer;
023import com.echothree.model.control.period.common.transfer.PeriodTypeTransfer;
024import com.echothree.model.control.period.server.control.PeriodControl;
025import com.echothree.model.control.accounting.common.workflow.FiscalPeriodStatusConstants;
026import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
027import com.echothree.model.control.workflow.server.control.WorkflowControl;
028import com.echothree.model.data.core.server.entity.EntityInstance;
029import com.echothree.model.data.period.server.entity.Period;
030import com.echothree.model.data.period.server.entity.PeriodDetail;
031import com.echothree.model.data.period.server.entity.PeriodType;
032import com.echothree.model.data.user.server.entity.UserVisit;
033import com.echothree.util.server.persistence.Session;
034
035public class PeriodTransferCache
036        extends BasePeriodTransferCache<Period, PeriodTransfer> {
037    
038    CoreControl coreControl = Session.getModelController(CoreControl.class);
039    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
040    
041    /** Creates a new instance of PeriodTransferCache */
042    public PeriodTransferCache(UserVisit userVisit, PeriodControl periodControl) {
043        super(userVisit, periodControl);
044        
045        setIncludeEntityInstance(true);
046    }
047    
048    public PeriodTransfer getPeriodTransfer(Period period) {
049        PeriodTransfer periodTransfer = get(period);
050        
051        if(periodTransfer == null) {
052            PeriodDetail periodDetail = period.getLastDetail();
053            PeriodKindTransfer periodKindTransfer = periodControl.getPeriodKindTransfer(userVisit, periodDetail.getPeriodKind());
054            String periodName = periodDetail.getPeriodName();
055            Period parentPeriod = periodDetail.getParentPeriod();
056            PeriodTransfer parentPeriodTransfer = parentPeriod == null? null: periodControl.getPeriodTransfer(userVisit, parentPeriod);
057            PeriodType periodType = periodDetail.getPeriodType();
058            PeriodTypeTransfer periodTypeTransfer = periodType == null? null: periodControl.getPeriodTypeTransfer(userVisit, periodType);
059            Long unformattedStartTime = periodDetail.getStartTime();
060            String startTime = formatTypicalDateTime(unformattedStartTime);
061            Long unformattedEndTime = periodDetail.getEndTime();
062            String endTime = formatTypicalDateTime(unformattedEndTime);
063            String description = periodControl.getBestPeriodDescription(period, getLanguage());
064            WorkflowEntityStatusTransfer status = null;
065            
066            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(period.getPrimaryKey());
067            String periodKindName = periodTypeTransfer.getPeriodKind().getPeriodKindName();
068            if(periodKindName.equals(PeriodConstants.PeriodKind_FISCAL)) {
069                status = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
070                        FiscalPeriodStatusConstants.Workflow_FISCAL_PERIOD_STATUS, entityInstance);
071            }
072            
073            periodTransfer = new PeriodTransfer(periodKindTransfer, periodName, parentPeriodTransfer, periodTypeTransfer, unformattedStartTime, startTime, unformattedEndTime,
074                    endTime, description, status);
075            put(period, periodTransfer);
076        }
077        
078        return periodTransfer;
079    }
080    
081}