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.party.server.logic;
018
019import com.echothree.control.user.party.common.spec.LanguageSpec;
020import com.echothree.control.user.party.common.spec.LanguageUlid;
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.core.server.entity.EntityInstance;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.util.common.message.ExecutionErrors;
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 LanguageLogic
036        extends BaseLogic {
037
038    private LanguageLogic() {
039        super();
040    }
041
042    private static class LanguageLogicHolder {
043        static LanguageLogic instance = new LanguageLogic();
044    }
045
046    public static LanguageLogic getInstance() {
047        return LanguageLogicHolder.instance;
048    }
049    
050    public Language getLanguageByName(final ExecutionErrorAccumulator eea, final String languageIsoName,
051            final EntityPermission entityPermission) {
052        var partyControl = Session.getModelController(PartyControl.class);
053        Language 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 getLanguageByUlid(final ExecutionErrorAccumulator eea, final String ulid, final EntityPermission entityPermission) {
071        Language projectLanguage = null;
072        
073        var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, (String)null, null, null, ulid,
074                ComponentVendors.ECHO_THREE.name(), EntityTypes.Language.name());
075
076        if(eea == null || !eea.hasExecutionErrors()) {
077            var partyControl = Session.getModelController(PartyControl.class);
078            
079            projectLanguage = partyControl.getLanguageByEntityInstance(entityInstance, entityPermission);
080        }
081
082        return projectLanguage;
083    }
084    
085    public Language getLanguageByUlid(final ExecutionErrorAccumulator eea, final String ulid) {
086        return getLanguageByUlid(eea, ulid, EntityPermission.READ_ONLY);
087    }
088    
089    public Language getLanguageByUlidForUpdate(final ExecutionErrorAccumulator eea, final String ulid) {
090        return getLanguageByUlid(eea, ulid, EntityPermission.READ_WRITE);
091    }
092    
093    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUlid ulid,
094            final EntityPermission entityPermission) {
095        Language language = null;
096        String languageIsoName = spec.getLanguageIsoName();
097        String languageUlid = ulid.getLanguageUlid();
098        var parameterCount = (languageIsoName == null ? 0 : 1) + (languageUlid == null ? 0 : 1);
099
100        if (parameterCount == 1) {
101            language = languageIsoName == null
102                    ? getLanguageByUlid(eea, languageUlid, entityPermission)
103                    : getLanguageByName(eea, languageIsoName, entityPermission);
104        } else {
105            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
106        }
107        
108        return language;
109    }
110    
111    public Language getLanguage(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUlid ulid) {
112        return getLanguage(eea, spec, ulid, EntityPermission.READ_ONLY);
113    }
114    
115    public Language getLanguageForUpdate(final ExecutionErrorAccumulator eea, final LanguageSpec spec, final LanguageUlid ulid) {
116        return getLanguage(eea, spec, ulid, EntityPermission.READ_WRITE);
117    }
118    
119}