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.term.server.logic; 018 019import com.echothree.control.user.term.common.spec.TermUniversalSpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.term.common.exception.DuplicateTermNameException; 025import com.echothree.model.control.term.common.exception.UnknownDefaultTermException; 026import com.echothree.model.control.term.common.exception.UnknownPartyTermException; 027import com.echothree.model.control.term.common.exception.UnknownTermNameException; 028import com.echothree.model.control.term.server.control.TermControl; 029import com.echothree.model.data.party.server.entity.Language; 030import com.echothree.model.data.party.server.entity.Party; 031import com.echothree.model.data.term.server.entity.PartyTerm; 032import com.echothree.model.data.term.server.entity.Term; 033import com.echothree.model.data.term.server.entity.TermType; 034import com.echothree.model.data.term.server.value.TermDetailValue; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.persistence.BasePK; 037import com.echothree.util.server.control.BaseLogic; 038import com.echothree.util.server.message.ExecutionErrorAccumulator; 039import com.echothree.util.server.persistence.EntityPermission; 040import javax.enterprise.context.ApplicationScoped; 041import javax.inject.Inject; 042 043@ApplicationScoped 044public class TermLogic 045 extends BaseLogic { 046 047 @Inject 048 TermControl termControl; 049 050 @Inject 051 EntityInstanceLogic entityInstanceLogic; 052 053 protected TermLogic() { 054 super(); 055 } 056 057 public Term createTerm(final ExecutionErrorAccumulator eea, final String termName, final TermType termType, 058 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 059 final BasePK createdBy) { 060 var term = termControl.getTermByName(termName); 061 062 if(term == null) { 063 term = termControl.createTerm(termName, termType, isDefault, sortOrder, createdBy); 064 065 if(description != null) { 066 termControl.createTermDescription(term, language, description, createdBy); 067 } 068 } else { 069 handleExecutionError(DuplicateTermNameException.class, eea, ExecutionErrors.DuplicateTermName.name(), termName); 070 } 071 072 return term; 073 } 074 075 public Term getTermByName(final ExecutionErrorAccumulator eea, final String termName, 076 final EntityPermission entityPermission) { 077 var term = termControl.getTermByName(termName, entityPermission); 078 079 if(term == null) { 080 handleExecutionError(UnknownTermNameException.class, eea, ExecutionErrors.UnknownTermName.name(), termName); 081 } 082 083 return term; 084 } 085 086 public Term getTermByName(final ExecutionErrorAccumulator eea, final String termName) { 087 return getTermByName(eea, termName, EntityPermission.READ_ONLY); 088 } 089 090 public Term getTermByNameForUpdate(final ExecutionErrorAccumulator eea, final String termName) { 091 return getTermByName(eea, termName, EntityPermission.READ_WRITE); 092 } 093 094 public Term getTermByUniversalSpec(final ExecutionErrorAccumulator eea, 095 final TermUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 096 Term term = null; 097 var termName = universalSpec.getTermName(); 098 var parameterCount = (termName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 099 100 switch(parameterCount) { 101 case 0 -> { 102 if(allowDefault) { 103 term = termControl.getDefaultTerm(entityPermission); 104 105 if(term == null) { 106 handleExecutionError(UnknownDefaultTermException.class, eea, ExecutionErrors.UnknownDefaultTerm.name()); 107 } 108 } else { 109 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 110 } 111 } 112 case 1 -> { 113 if(termName == null) { 114 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 115 ComponentVendors.ECHO_THREE.name(), EntityTypes.Term.name()); 116 117 if(eea == null || !eea.hasExecutionErrors()) { 118 term = termControl.getTermByEntityInstance(entityInstance, entityPermission); 119 } 120 } else { 121 term = getTermByName(eea, termName, entityPermission); 122 } 123 } 124 default -> 125 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 126 } 127 128 return term; 129 } 130 131 public Term getTermByUniversalSpec(final ExecutionErrorAccumulator eea, 132 final TermUniversalSpec universalSpec, boolean allowDefault) { 133 return getTermByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 134 } 135 136 public Term getTermByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 137 final TermUniversalSpec universalSpec, boolean allowDefault) { 138 return getTermByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 139 } 140 141 public PartyTerm getPartyTerm(final ExecutionErrorAccumulator eea, final Party party) { 142 var partyTerm = termControl.getPartyTerm(party); 143 144 if(partyTerm == null) { 145 handleExecutionError(UnknownPartyTermException.class, eea, ExecutionErrors.UnknownPartyTerm.name(), party.getLastDetail().getPartyName()); 146 } 147 148 return partyTerm; 149 } 150 151 public Term getDefaultTerm(final ExecutionErrorAccumulator eea) { 152 var term = termControl.getDefaultTerm(); 153 154 if(term == null) { 155 handleExecutionError(UnknownDefaultTermException.class, eea, ExecutionErrors.UnknownDefaultTerm.name()); 156 } 157 158 return term; 159 } 160 161 public void updateTermFromValue(final ExecutionErrorAccumulator eea, TermDetailValue termDetailValue, BasePK updatedBy) { 162 termControl.updateTermFromValue(termDetailValue, updatedBy); 163 } 164 165 public void deleteTerm(final ExecutionErrorAccumulator eea, Term term, BasePK deletedBy) { 166 termControl.deleteTerm(term, deletedBy); 167 } 168 169}