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.shipment.server.logic; 018 019import com.echothree.control.user.shipment.common.spec.FreeOnBoardUniversalSpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.shipment.common.exception.DuplicateFreeOnBoardNameException; 025import com.echothree.model.control.shipment.common.exception.UnknownDefaultFreeOnBoardException; 026import com.echothree.model.control.shipment.common.exception.UnknownFreeOnBoardNameException; 027import com.echothree.model.control.shipment.server.control.FreeOnBoardControl; 028import com.echothree.model.data.party.server.entity.Language; 029import com.echothree.model.data.shipment.server.entity.FreeOnBoard; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.persistence.BasePK; 032import com.echothree.util.server.control.BaseLogic; 033import com.echothree.util.server.message.ExecutionErrorAccumulator; 034import com.echothree.util.server.persistence.EntityPermission; 035import com.echothree.util.server.persistence.Session; 036import javax.enterprise.context.ApplicationScoped; 037import javax.enterprise.inject.spi.CDI; 038 039@ApplicationScoped 040public class FreeOnBoardLogic 041 extends BaseLogic { 042 043 protected FreeOnBoardLogic() { 044 super(); 045 } 046 047 public static FreeOnBoardLogic getInstance() { 048 return CDI.current().select(FreeOnBoardLogic.class).get(); 049 } 050 051 public FreeOnBoard createFreeOnBoard(final ExecutionErrorAccumulator eea, final String freeOnBoardName, 052 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 053 final BasePK createdBy) { 054 var freeOnBoardControl = Session.getModelController(FreeOnBoardControl.class); 055 var freeOnBoard = freeOnBoardControl.getFreeOnBoardByName(freeOnBoardName); 056 057 if(freeOnBoard == null) { 058 freeOnBoard = freeOnBoardControl.createFreeOnBoard(freeOnBoardName, isDefault, sortOrder, createdBy); 059 060 if(description != null) { 061 freeOnBoardControl.createFreeOnBoardDescription(freeOnBoard, language, description, createdBy); 062 } 063 } else { 064 handleExecutionError(DuplicateFreeOnBoardNameException.class, eea, ExecutionErrors.DuplicateFreeOnBoardName.name(), freeOnBoardName); 065 } 066 067 return freeOnBoard; 068 } 069 070 public FreeOnBoard getFreeOnBoardByName(final ExecutionErrorAccumulator eea, final String freeOnBoardName, 071 final EntityPermission entityPermission) { 072 var freeOnBoardControl = Session.getModelController(FreeOnBoardControl.class); 073 var freeOnBoard = freeOnBoardControl.getFreeOnBoardByName(freeOnBoardName, entityPermission); 074 075 if(freeOnBoard == null) { 076 handleExecutionError(UnknownFreeOnBoardNameException.class, eea, ExecutionErrors.UnknownFreeOnBoardName.name(), freeOnBoardName); 077 } 078 079 return freeOnBoard; 080 } 081 082 public FreeOnBoard getFreeOnBoardByName(final ExecutionErrorAccumulator eea, final String freeOnBoardName) { 083 return getFreeOnBoardByName(eea, freeOnBoardName, EntityPermission.READ_ONLY); 084 } 085 086 public FreeOnBoard getFreeOnBoardByNameForUpdate(final ExecutionErrorAccumulator eea, final String freeOnBoardName) { 087 return getFreeOnBoardByName(eea, freeOnBoardName, EntityPermission.READ_WRITE); 088 } 089 090 public FreeOnBoard getFreeOnBoardByUniversalSpec(final ExecutionErrorAccumulator eea, 091 final FreeOnBoardUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 092 FreeOnBoard freeOnBoard = null; 093 var freeOnBoardControl = Session.getModelController(FreeOnBoardControl.class); 094 var freeOnBoardName = universalSpec.getFreeOnBoardName(); 095 var parameterCount = (freeOnBoardName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 096 097 switch(parameterCount) { 098 case 0 -> { 099 if(allowDefault) { 100 freeOnBoard = freeOnBoardControl.getDefaultFreeOnBoard(entityPermission); 101 102 if(freeOnBoard == null) { 103 handleExecutionError(UnknownDefaultFreeOnBoardException.class, eea, ExecutionErrors.UnknownDefaultFreeOnBoard.name()); 104 } 105 } else { 106 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 107 } 108 } 109 case 1 -> { 110 if(freeOnBoardName == null) { 111 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 112 ComponentVendors.ECHO_THREE.name(), EntityTypes.FreeOnBoard.name()); 113 114 if(!eea.hasExecutionErrors()) { 115 freeOnBoard = freeOnBoardControl.getFreeOnBoardByEntityInstance(entityInstance, entityPermission); 116 } 117 } else { 118 freeOnBoard = getFreeOnBoardByName(eea, freeOnBoardName, entityPermission); 119 } 120 } 121 default -> 122 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 123 } 124 125 return freeOnBoard; 126 } 127 128 public FreeOnBoard getFreeOnBoardByUniversalSpec(final ExecutionErrorAccumulator eea, 129 final FreeOnBoardUniversalSpec universalSpec, boolean allowDefault) { 130 return getFreeOnBoardByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 131 } 132 133 public FreeOnBoard getFreeOnBoardByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 134 final FreeOnBoardUniversalSpec universalSpec, boolean allowDefault) { 135 return getFreeOnBoardByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 136 } 137 138 public FreeOnBoard getDefaultFreeOnBoard(final ExecutionErrorAccumulator eea) { 139 var freeOnBoardControl = Session.getModelController(FreeOnBoardControl.class); 140 var freeOnBoard = freeOnBoardControl.getDefaultFreeOnBoard(); 141 142 if(freeOnBoard == null) { 143 handleExecutionError(UnknownDefaultFreeOnBoardException.class, eea, ExecutionErrors.UnknownDefaultFreeOnBoard.name()); 144 } 145 146 return freeOnBoard; 147 } 148 149 public void deleteFreeOnBoard(final ExecutionErrorAccumulator eea, final FreeOnBoard freeOnBoard, 150 final BasePK deletedBy) { 151 var freeOnBoardControl = Session.getModelController(FreeOnBoardControl.class); 152 153 freeOnBoardControl.deleteFreeOnBoard(freeOnBoard, deletedBy); 154 } 155}