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.inject.Inject;
034
035@ApplicationScoped
036public class LanguageLogic
037        extends BaseLogic {
038
039    @Inject
040    PartyControl partyControl;
041
042    @Inject
043    EntityInstanceLogic entityInstanceLogic;
044
045    protected LanguageLogic() {
046        super();
047    }
048    
049    public Language getLanguageByName(final ExecutionErrorAccumulator eea, final String languageIsoName,
050            final EntityPermission entityPermission) {
051        var language = partyControl.getLanguageByIsoName(languageIsoName, entityPermission);
052
053        if(language == null) {
054            handleExecutionError(UnknownLanguageIsoNameException.class, eea, ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
055        }
056
057        return language;
058    }
059    
060    public Language getLanguageByName(final ExecutionErrorAccumulator eea, final String name) {
061        return getLanguageByName(eea, name, EntityPermission.READ_ONLY);
062    }
063    
064    public Language getLanguageByNameForUpdate(final ExecutionErrorAccumulator eea, final String name) {
065        return getLanguageByName(eea, name, EntityPermission.READ_WRITE);
066    }
067    
068    public Language getLanguageByUuid(final ExecutionErrorAccumulator eea, final String uuid, final EntityPermission entityPermission) {
069        Language projectLanguage = null;
070        
071        var entityInstance = entityInstanceLogic.getEntityInstance(eea, (String)null, uuid,
072                ComponentVendors.ECHO_THREE.name(), EntityTypes.Language.name());
073
074        if(eea == null || !eea.hasExecutionErrors()) {
075            projectLanguage = partyControl.getLanguageByEntityInstance(entityInstance, entityPermission);
076        }
077
078        return projectLanguage;
079    }
080    
081    public Language getLanguageByUuid(final ExecutionErrorAccumulator eea, final String uuid) {
082        return getLanguageByUuid(eea, uuid, EntityPermission.READ_ONLY);
083    }
084    
085    public Language getLanguageByUuidForUpdate(final ExecutionErrorAccumulator eea, final String uuid) {
086        return getLanguageByUuid(eea, uuid, EntityPermission.READ_WRITE);
087    }
088    
089    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid,
090            final EntityPermission entityPermission) {
091        Language language = null;
092        var languageIsoName = spec.getLanguageIsoName();
093        var languageUuid = uuid.getLanguageUuid();
094        var parameterCount = (languageIsoName == null ? 0 : 1) + (languageUuid == null ? 0 : 1);
095
096        if (parameterCount == 1) {
097            language = languageIsoName == null
098                    ? getLanguageByUuid(eea, languageUuid, entityPermission)
099                    : getLanguageByName(eea, languageIsoName, entityPermission);
100        } else {
101            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
102        }
103        
104        return language;
105    }
106    
107    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid) {
108        return getLanguage(eea, spec, uuid, EntityPermission.READ_ONLY);
109    }
110    
111    public Language getLanguageForUpdate(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUuid uuid) {
112        return getLanguage(eea, spec, uuid, EntityPermission.READ_WRITE);
113    }
114    
115}