001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.EntityInstanceControl;
020import com.echothree.model.control.period.server.control.PeriodControl;
021import com.echothree.model.control.workflow.server.control.WorkflowControl;
022import com.echothree.model.data.party.common.pk.PartyPK;
023import com.echothree.model.data.period.server.entity.Period;
024import com.echothree.model.data.period.server.entity.PeriodKind;
025import com.echothree.model.data.period.server.entity.PeriodType;
026import javax.enterprise.context.ApplicationScoped;
027import javax.inject.Inject;
028
029@ApplicationScoped
030public class PeriodLogic {
031
032    @Inject
033    EntityInstanceControl entityInstanceControl;
034
035    @Inject
036    PeriodControl periodControl;
037
038    @Inject
039    WorkflowControl workflowControl;
040
041    protected PeriodLogic() {
042        super();
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 period = periodControl.createPeriod(periodKind, periodName, parentPeriod, periodType, startTime, endTime, createdBy);
048        var periodTypeDetail = periodType.getLastDetail();
049        var workflowEntrance = periodTypeDetail.getWorkflowEntrance();
050        
051        if(workflowEntrance == null) {
052            workflowEntrance = periodTypeDetail.getPeriodKind().getLastDetail().getWorkflowEntrance();
053        }
054        
055        if(workflowEntrance != null) {
056            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(period.getPrimaryKey());
057
058            workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy);
059        }
060        
061        return period;
062    }
063    
064}