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.contact.server.logic;
018
019import com.echothree.model.control.contact.common.exception.UnknownPartyContactMechanismException;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.data.contact.server.entity.ContactMechanism;
022import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
023import com.echothree.model.data.party.server.entity.Party;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.server.control.BaseLogic;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027import com.echothree.util.server.persistence.EntityPermission;
028import com.echothree.util.server.persistence.Session;
029import javax.enterprise.context.ApplicationScoped;
030import javax.enterprise.inject.spi.CDI;
031
032@ApplicationScoped
033public class PartyContactMechanismLogic
034    extends BaseLogic {
035
036    protected PartyContactMechanismLogic() {
037        super();
038    }
039
040    public static PartyContactMechanismLogic getInstance() {
041        return CDI.current().select(PartyContactMechanismLogic.class).get();
042    }
043
044    public PartyContactMechanism getPartyContactMechanism(final ExecutionErrorAccumulator eea, final Party party,
045            final ContactMechanism contactMechanism, final EntityPermission entityPermission) {
046        var contactControl = Session.getModelController(ContactControl.class);
047        var partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism, entityPermission);
048
049        if(partyContactMechanism == null) {
050            handleExecutionError(UnknownPartyContactMechanismException.class, eea, ExecutionErrors.UnknownPartyContactMechanism.name(),
051                    party.getLastDetail().getPartyName(), contactMechanism.getLastDetail().getContactMechanismName());
052        }
053
054        return partyContactMechanism;
055    }
056    
057    public PartyContactMechanism getPartyContactMechanism(final ExecutionErrorAccumulator eea, final Party party,
058            final ContactMechanism contactMechanism) {
059        return getPartyContactMechanism(eea, party, contactMechanism, EntityPermission.READ_ONLY);
060    }
061
062    public PartyContactMechanism getPartyContactMechanismForUpdate(final ExecutionErrorAccumulator eea, final Party party,
063            final ContactMechanism contactMechanism) {
064        return getPartyContactMechanism(eea, party, contactMechanism, EntityPermission.READ_WRITE);
065    }
066
067}