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