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