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