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