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.model.control.content.server.control.ContentControl; 020import com.echothree.model.control.customer.server.control.CustomerControl; 021import com.echothree.model.control.offer.common.exception.CannotDeleteOfferUseInUseException; 022import com.echothree.model.control.offer.common.exception.UnknownOfferUseException; 023import com.echothree.model.control.offer.server.control.OfferUseControl; 024import com.echothree.model.control.sales.server.control.SalesOrderControl; 025import com.echothree.model.control.user.server.control.UserControl; 026import com.echothree.model.control.wishlist.server.control.WishlistControl; 027import com.echothree.model.data.offer.server.entity.OfferUse; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.persistence.BasePK; 030import com.echothree.util.server.control.BaseLogic; 031import com.echothree.util.server.message.ExecutionErrorAccumulator; 032import com.echothree.util.server.persistence.EntityPermission; 033import com.echothree.util.server.persistence.Session; 034import javax.enterprise.context.ApplicationScoped; 035import javax.enterprise.inject.spi.CDI; 036 037@ApplicationScoped 038public class OfferUseLogic 039 extends BaseLogic { 040 041 protected OfferUseLogic() { 042 super(); 043 } 044 045 public static OfferUseLogic getInstance() { 046 return CDI.current().select(OfferUseLogic.class).get(); 047 } 048 049 public OfferUse getOfferUseByName(final ExecutionErrorAccumulator eea, final String offerName, final String useName, 050 final EntityPermission entityPermission) { 051 var offer = OfferLogic.getInstance().getOfferByName(eea, offerName); 052 var use = UseLogic.getInstance().getUseByName(eea, useName); 053 OfferUse offerUse = null; 054 055 if(!eea.hasExecutionErrors()) { 056 var offerUseControl = Session.getModelController(OfferUseControl.class); 057 058 offerUse = offerUseControl.getOfferUse(offer, use, entityPermission); 059 060 if(offerUse == null) { 061 handleExecutionError(UnknownOfferUseException.class, eea, ExecutionErrors.UnknownOfferUse.name(), 062 offerName, useName); 063 } 064 } 065 066 return offerUse; 067 } 068 069 public OfferUse getOfferUseByName(final ExecutionErrorAccumulator eea, final String offerName, final String useName) { 070 return getOfferUseByName(eea, offerName, useName, EntityPermission.READ_ONLY); 071 } 072 073 public OfferUse getOfferUseByNameForUpdate(final ExecutionErrorAccumulator eea, final String offerName, final String useName) { 074 return getOfferUseByName(eea, offerName, useName, EntityPermission.READ_WRITE); 075 } 076 077 public void deleteOfferUse(final ExecutionErrorAccumulator eea, final OfferUse offerUse, final BasePK deletedBy) { 078 var contentControl = Session.getModelController(ContentControl.class); 079 var customerControl = Session.getModelController(CustomerControl.class); 080 var salesOrderControl = Session.getModelController(SalesOrderControl.class); 081 var userControl = Session.getModelController(UserControl.class); 082 var wishlistControl = Session.getModelController(WishlistControl.class); 083 084 if(contentControl.countContentCollectionsByDefaultOfferUse(offerUse) == 0 085 && contentControl.countContentCatalogsByDefaultOfferUse(offerUse) == 0 086 && contentControl.countContentCategoriesByDefaultOfferUse(offerUse) == 0 087 && customerControl.countCustomerTypesByDefaultOfferUse(offerUse) == 0 088 && customerControl.countCustomersByInitialOfferUse(offerUse) == 0 089 && salesOrderControl.countSalesOrdersByOfferUse(offerUse) == 0 090 && salesOrderControl.countSalesOrderLinesByOfferUse(offerUse) == 0 091 && userControl.countUserVisitsByOfferUse(offerUse) == 0 092 && wishlistControl.countWishlistsByOfferUse(offerUse) == 0 093 && wishlistControl.countWishlistLinesByOfferUse(offerUse) == 0) { 094 var offerUseControl = Session.getModelController(OfferUseControl.class); 095 096 offerUseControl.deleteOfferUse(offerUse, deletedBy); 097 } else { 098 handleExecutionError(CannotDeleteOfferUseInUseException.class, eea, ExecutionErrors.CannotDeleteOfferUseInUse.name()); 099 } 100 } 101 102}