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.offer.server.logic; 018 019import com.echothree.control.user.offer.common.spec.UseUniversalSpec; 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.offer.common.exception.CannotDeleteUseInUseException; 025import com.echothree.model.control.offer.common.exception.DuplicateUseNameException; 026import com.echothree.model.control.offer.common.exception.UnknownDefaultUseException; 027import com.echothree.model.control.offer.common.exception.UnknownUseNameException; 028import com.echothree.model.control.offer.server.control.OfferUseControl; 029import com.echothree.model.control.offer.server.control.UseControl; 030import com.echothree.model.data.offer.server.entity.Use; 031import com.echothree.model.data.offer.server.entity.UseType; 032import com.echothree.model.data.offer.server.value.UseDetailValue; 033import com.echothree.model.data.party.server.entity.Language; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.persistence.BasePK; 036import com.echothree.util.server.control.BaseLogic; 037import com.echothree.util.server.message.ExecutionErrorAccumulator; 038import com.echothree.util.server.persistence.EntityPermission; 039import com.echothree.util.server.persistence.Session; 040import javax.enterprise.context.ApplicationScoped; 041import javax.enterprise.inject.spi.CDI; 042 043@ApplicationScoped 044public class UseLogic 045 extends BaseLogic { 046 047 protected UseLogic() { 048 super(); 049 } 050 051 public static UseLogic getInstance() { 052 return CDI.current().select(UseLogic.class).get(); 053 } 054 055 public Use createUse(final ExecutionErrorAccumulator eea, final String useName, final UseType useType, 056 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 057 final BasePK createdBy) { 058 var useControl = Session.getModelController(UseControl.class); 059 var use = useControl.getUseByName(useName); 060 061 if(use == null) { 062 use = useControl.createUse(useName, useType, isDefault, sortOrder, createdBy); 063 064 if(description != null) { 065 useControl.createUseDescription(use, language, description, createdBy); 066 } 067 } else { 068 handleExecutionError(DuplicateUseNameException.class, eea, ExecutionErrors.DuplicateUseName.name(), useName); 069 } 070 071 return use; 072 } 073 074 public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName, 075 final EntityPermission entityPermission) { 076 var useControl = Session.getModelController(UseControl.class); 077 var use = useControl.getUseByName(useName, entityPermission); 078 079 if(use == null) { 080 handleExecutionError(UnknownUseNameException.class, eea, ExecutionErrors.UnknownUseName.name(), useName); 081 } 082 083 return use; 084 } 085 086 public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName) { 087 return getUseByName(eea, useName, EntityPermission.READ_ONLY); 088 } 089 090 public Use getUseByNameForUpdate(final ExecutionErrorAccumulator eea, final String useName) { 091 return getUseByName(eea, useName, EntityPermission.READ_WRITE); 092 } 093 094 public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea, 095 final UseUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 096 var useControl = Session.getModelController(UseControl.class); 097 var useName = universalSpec.getUseName(); 098 var parameterCount = (useName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 099 Use use = null; 100 101 switch(parameterCount) { 102 case 0 -> { 103 if(allowDefault) { 104 use = useControl.getDefaultUse(entityPermission); 105 106 if(use == null) { 107 handleExecutionError(UnknownDefaultUseException.class, eea, ExecutionErrors.UnknownDefaultUse.name()); 108 } 109 } else { 110 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 111 } 112 } 113 case 1 -> { 114 if(useName == null) { 115 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 116 ComponentVendors.ECHO_THREE.name(), EntityTypes.Use.name()); 117 118 if(!eea.hasExecutionErrors()) { 119 use = useControl.getUseByEntityInstance(entityInstance, entityPermission); 120 } 121 } else { 122 use = getUseByName(eea, useName, entityPermission); 123 } 124 } 125 default -> 126 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 127 } 128 129 return use; 130 } 131 132 public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea, 133 final UseUniversalSpec universalSpec, boolean allowDefault) { 134 return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 135 } 136 137 public Use getUseByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 138 final UseUniversalSpec universalSpec, boolean allowDefault) { 139 return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 140 } 141 142 public void updateUseFromValue(UseDetailValue useDetailValue, BasePK updatedBy) { 143 var useControl = Session.getModelController(UseControl.class); 144 145 useControl.updateUseFromValue(useDetailValue, updatedBy); 146 } 147 148 public void deleteUse(final ExecutionErrorAccumulator eea, final Use use, final BasePK deletedBy) { 149 var offerUseControl = Session.getModelController(OfferUseControl.class); 150 151 if(offerUseControl.countOfferUsesByUse(use) == 0) { 152 var useControl = Session.getModelController(UseControl.class); 153 154 useControl.deleteUse(use, deletedBy); 155 } else { 156 handleExecutionError(CannotDeleteUseInUseException.class, eea, ExecutionErrors.CannotDeleteUseInUse.name()); 157 } 158 } 159 160}