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