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.common.exception.UnknownPeriodNameException; 021import com.echothree.model.control.period.server.control.PeriodControl; 022import com.echothree.model.control.period.server.control.PeriodControl.PeriodSelection; 023import com.echothree.model.control.workflow.server.control.WorkflowControl; 024import com.echothree.model.data.party.common.pk.PartyPK; 025import com.echothree.model.data.period.server.entity.Period; 026import com.echothree.model.data.period.server.entity.PeriodKind; 027import com.echothree.model.data.period.server.entity.PeriodType; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.server.control.BaseLogic; 030import com.echothree.util.server.message.ExecutionErrorAccumulator; 031import com.echothree.util.server.persistence.EntityPermission; 032import javax.enterprise.context.ApplicationScoped; 033import javax.inject.Inject; 034 035@ApplicationScoped 036public class PeriodLogic 037 extends BaseLogic { 038 039 @Inject 040 EntityInstanceControl entityInstanceControl; 041 042 @Inject 043 PeriodControl periodControl; 044 045 @Inject 046 WorkflowControl workflowControl; 047 048 @Inject 049 PeriodTypeLogic periodTypeLogic; 050 051 protected PeriodLogic() { 052 super(); 053 } 054 055 public Period createPeriod(final PeriodKind periodKind, final String periodName, final Period parentPeriod, 056 final PeriodType periodType, final Long startTime, final Long endTime, final PartyPK createdBy) { 057 var period = periodControl.createPeriod(periodKind, periodName, parentPeriod, periodType, startTime, endTime, createdBy); 058 var periodTypeDetail = periodType.getLastDetail(); 059 var workflowEntrance = periodTypeDetail.getWorkflowEntrance(); 060 061 if(workflowEntrance == null) { 062 workflowEntrance = periodTypeDetail.getPeriodKind().getLastDetail().getWorkflowEntrance(); 063 } 064 065 if(workflowEntrance != null) { 066 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(period.getPrimaryKey()); 067 068 workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy); 069 } 070 071 return period; 072 } 073 074 public Period getPeriodByName(final ExecutionErrorAccumulator eea, final PeriodKind periodKind, 075 final String periodName, final EntityPermission entityPermission) { 076 var period = periodControl.getPeriodByName(periodKind, periodName, entityPermission); 077 078 if(period == null) { 079 handleExecutionError(UnknownPeriodNameException.class, eea, ExecutionErrors.UnknownPeriodName.name(), 080 periodKind.getLastDetail().getPeriodKindName(), periodName); 081 } 082 083 return period; 084 } 085 086 public Period getPeriodByName(final ExecutionErrorAccumulator eea, final PeriodKind periodKind, final String periodName) { 087 return getPeriodByName(eea, periodKind, periodName, EntityPermission.READ_ONLY); 088 } 089 090 public PeriodSelection getPeriodSelection(final ExecutionErrorAccumulator executionErrorAccumulator, final PeriodKind periodKind, 091 final String parentPeriodName, final String periodTypeName) { 092 Period parentPeriod = null; 093 PeriodType periodType = null; 094 095 if(!hasExecutionErrors(executionErrorAccumulator) && parentPeriodName != null) { 096 parentPeriod = getPeriodByName(executionErrorAccumulator, periodKind, parentPeriodName); 097 } 098 099 if(!hasExecutionErrors(executionErrorAccumulator) && periodTypeName != null) { 100 periodType = periodTypeLogic.getPeriodTypeByName(executionErrorAccumulator, periodKind, periodTypeName); 101 } 102 103 return hasExecutionErrors(executionErrorAccumulator) ? null : new PeriodSelection(periodKind, parentPeriod, periodType); 104 } 105 106}