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.logic;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.period.server.control.PeriodControl;
021import com.echothree.model.control.workflow.server.control.WorkflowControl;
022import com.echothree.model.data.core.server.entity.EntityInstance;
023import com.echothree.model.data.party.common.pk.PartyPK;
024import com.echothree.model.data.period.server.entity.Period;
025import com.echothree.model.data.period.server.entity.PeriodKind;
026import com.echothree.model.data.period.server.entity.PeriodType;
027import com.echothree.model.data.period.server.entity.PeriodTypeDetail;
028import com.echothree.model.data.workflow.server.entity.WorkflowEntrance;
029import com.echothree.util.server.persistence.Session;
030
031public class PeriodLogic {
032
033    private PeriodLogic() {
034        super();
035    }
036
037    private static class PeriodLogicHolder {
038        static PeriodLogic instance = new PeriodLogic();
039    }
040
041    public static PeriodLogic getInstance() {
042        return PeriodLogicHolder.instance;
043    }
044    
045    public Period createPeriod(final PeriodKind periodKind, final String periodName, final Period parentPeriod,
046            final PeriodType periodType, final Long startTime, final Long endTime, final PartyPK createdBy) {
047        var periodControl = Session.getModelController(PeriodControl.class);
048        Period period = periodControl.createPeriod(periodKind, periodName, parentPeriod, periodType, startTime, endTime, createdBy);
049        PeriodTypeDetail periodTypeDetail = periodType.getLastDetail();
050        WorkflowEntrance workflowEntrance = periodTypeDetail.getWorkflowEntrance();
051        
052        if(workflowEntrance == null) {
053            workflowEntrance = periodTypeDetail.getPeriodKind().getLastDetail().getWorkflowEntrance();
054        }
055        
056        if(workflowEntrance != null) {
057            var coreControl = Session.getModelController(CoreControl.class);
058            var workflowControl = Session.getModelController(WorkflowControl.class);
059            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(period.getPrimaryKey());
060
061            workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy);
062        }
063        
064        return period;
065    }
066    
067}