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.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 com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.ApplicationScoped;
028import javax.enterprise.inject.spi.CDI;
029
030@ApplicationScoped
031public class PeriodLogic {
032
033    protected PeriodLogic() {
034        super();
035    }
036
037    public static PeriodLogic getInstance() {
038        return CDI.current().select(PeriodLogic.class).get();
039    }
040    
041    public Period createPeriod(final PeriodKind periodKind, final String periodName, final Period parentPeriod,
042            final PeriodType periodType, final Long startTime, final Long endTime, final PartyPK createdBy) {
043        var periodControl = Session.getModelController(PeriodControl.class);
044        var period = periodControl.createPeriod(periodKind, periodName, parentPeriod, periodType, startTime, endTime, createdBy);
045        var periodTypeDetail = periodType.getLastDetail();
046        var workflowEntrance = periodTypeDetail.getWorkflowEntrance();
047        
048        if(workflowEntrance == null) {
049            workflowEntrance = periodTypeDetail.getPeriodKind().getLastDetail().getWorkflowEntrance();
050        }
051        
052        if(workflowEntrance != null) {
053            var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
054            var workflowControl = Session.getModelController(WorkflowControl.class);
055            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(period.getPrimaryKey());
056
057            workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy);
058        }
059        
060        return period;
061    }
062    
063}