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.party.server.logic;
018
019import com.echothree.control.user.party.common.spec.LanguageSpec;
020import com.echothree.control.user.party.common.spec.LanguageUuid;
021import com.echothree.model.control.core.common.ComponentVendors;
022import com.echothree.model.control.core.common.EntityTypes;
023import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
024import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
025import com.echothree.model.control.party.common.exception.UnknownLanguageIsoNameException;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.server.control.BaseLogic;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import com.echothree.util.server.persistence.EntityPermission;
032import javax.enterprise.context.ApplicationScoped;
033import javax.enterprise.inject.spi.CDI;
034import javax.inject.Inject;
035
036@ApplicationScoped
037public class LanguageLogic
038        extends BaseLogic {
039
040    @Inject
041    PartyControl partyControl;
042
043    @Inject
044    EntityInstanceLogic entityInstanceLogic;
045
046    protected LanguageLogic() {
047        super();
048    }
049
050    public static LanguageLogic getInstance() {
051        return CDI.current().select(LanguageLogic.class).get();
052    }
053    
054    public Language getLanguageByName(final ExecutionErrorAccumulator eea, final String languageIsoName,
055            final EntityPermission entityPermission) {
056        var language = partyControl.getLanguageByIsoName(languageIsoName, entityPermission);
057
058        if(language == null) {
059            handleExecutionError(UnknownLanguageIsoNameException.class, eea, ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
060        }
061
062        return language;
063    }
064    
065    public Language getLanguageByName(final ExecutionErrorAccumulator eea, final String name) {
066        return getLanguageByName(eea, name, EntityPermission.READ_ONLY);
067    }
068    
069    public Language getLanguageByNameForUpdate(final ExecutionErrorAccumulator eea, final String name) {
070        return getLanguageByName(eea, name, EntityPermission.READ_WRITE);
071    }
072    
073    public Language getLanguageByUuid(final ExecutionErrorAccumulator eea, final String uuid, final EntityPermission entityPermission) {
074        Language projectLanguage = null;
075        
076        var entityInstance = entityInstanceLogic.getEntityInstance(eea, (String)null, uuid,
077                ComponentVendors.ECHO_THREE.name(), EntityTypes.Language.name());
078
079        if(eea == null || !eea.hasExecutionErrors()) {
080            projectLanguage = partyControl.getLanguageByEntityInstance(entityInstance, entityPermission);
081        }
082
083        return projectLanguage;
084    }
085    
086    public Language getLanguageByUuid(final ExecutionErrorAccumulator eea, final String uuid) {
087        return getLanguageByUuid(eea, uuid, EntityPermission.READ_ONLY);
088    }
089    
090    public Language getLanguageByUuidForUpdate(final ExecutionErrorAccumulator eea, final String uuid) {
091        return getLanguageByUuid(eea, uuid, EntityPermission.READ_WRITE);
092    }
093    
094    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid,
095            final EntityPermission entityPermission) {
096        Language language = null;
097        var languageIsoName = spec.getLanguageIsoName();
098        var languageUuid = uuid.getLanguageUuid();
099        var parameterCount = (languageIsoName == null ? 0 : 1) + (languageUuid == null ? 0 : 1);
100
101        if (parameterCount == 1) {
102            language = languageIsoName == null
103                    ? getLanguageByUuid(eea, languageUuid, entityPermission)
104                    : getLanguageByName(eea, languageIsoName, entityPermission);
105        } else {
106            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
107        }
108        
109        return language;
110    }
111    
112    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid) {
113        return getLanguage(eea, spec, uuid, EntityPermission.READ_ONLY);
114    }
115    
116    public Language getLanguageForUpdate(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid) {
117        return getLanguage(eea, spec, uuid, EntityPermission.READ_WRITE);
118    }
119    
120}