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.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 javax.enterprise.context.ApplicationScoped;
034import javax.inject.Inject;
035
036@ApplicationScoped
037public class OfferUseLogic
038        extends BaseLogic {
039
040    @Inject
041    ContentControl contentControl;
042
043    @Inject
044    CustomerControl customerControl;
045
046    @Inject
047    OfferUseControl offerUseControl;
048
049    @Inject
050    SalesOrderControl salesOrderControl;
051
052    @Inject
053    UserControl userControl;
054
055    @Inject
056    WishlistControl wishlistControl;
057
058    @Inject
059    OfferLogic offerLogic;
060
061    @Inject
062    UseLogic useLogic;
063
064    protected OfferUseLogic() {
065        super();
066    }
067    
068    public OfferUse getOfferUseByName(final ExecutionErrorAccumulator eea, final String offerName, final String useName,
069            final EntityPermission entityPermission) {
070        var offer = offerLogic.getOfferByName(eea, offerName);
071        var use = useLogic.getUseByName(eea, useName);
072        OfferUse offerUse = null;
073
074        if(eea == null || !eea.hasExecutionErrors()) {
075            offerUse = offerUseControl.getOfferUse(offer, use, entityPermission);
076
077            if(offerUse == null) {
078                handleExecutionError(UnknownOfferUseException.class, eea, ExecutionErrors.UnknownOfferUse.name(),
079                        offerName, useName);
080            }
081        }
082
083        return offerUse;
084    }
085
086    public OfferUse getOfferUseByName(final ExecutionErrorAccumulator eea, final String offerName, final String useName) {
087        return getOfferUseByName(eea, offerName, useName, EntityPermission.READ_ONLY);
088    }
089
090    public OfferUse getOfferUseByNameForUpdate(final ExecutionErrorAccumulator eea, final String offerName, final String useName) {
091        return getOfferUseByName(eea, offerName, useName, EntityPermission.READ_WRITE);
092    }
093
094    public void deleteOfferUse(final ExecutionErrorAccumulator eea, final OfferUse offerUse, final BasePK deletedBy) {
095        if(contentControl.countContentCollectionsByDefaultOfferUse(offerUse) == 0
096                && contentControl.countContentCatalogsByDefaultOfferUse(offerUse) == 0
097                && contentControl.countContentCategoriesByDefaultOfferUse(offerUse) == 0
098                && customerControl.countCustomerTypesByDefaultOfferUse(offerUse) == 0
099                && customerControl.countCustomersByInitialOfferUse(offerUse) == 0
100                && salesOrderControl.countSalesOrdersByOfferUse(offerUse) == 0
101                && salesOrderControl.countSalesOrderLinesByOfferUse(offerUse) == 0
102                && userControl.countUserVisitsByOfferUse(offerUse) == 0
103                && wishlistControl.countWishlistsByOfferUse(offerUse) == 0
104                && wishlistControl.countWishlistLinesByOfferUse(offerUse) == 0) {
105            offerUseControl.deleteOfferUse(offerUse, deletedBy);
106        } else {
107            handleExecutionError(CannotDeleteOfferUseInUseException.class, eea, ExecutionErrors.CannotDeleteOfferUseInUse.name());
108        }
109    }
110
111}