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