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.offer.server.logic; 018 019import com.echothree.control.user.offer.common.spec.UseUniversalSpec; 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.offer.common.exception.CannotDeleteUseInUseException; 025import com.echothree.model.control.offer.common.exception.DuplicateUseNameException; 026import com.echothree.model.control.offer.common.exception.UnknownDefaultUseException; 027import com.echothree.model.control.offer.common.exception.UnknownUseNameException; 028import com.echothree.model.control.offer.server.control.OfferUseControl; 029import com.echothree.model.control.offer.server.control.UseControl; 030import com.echothree.model.data.core.server.entity.EntityInstance; 031import com.echothree.model.data.offer.server.entity.Use; 032import com.echothree.model.data.offer.server.entity.UseType; 033import com.echothree.model.data.offer.server.value.UseDetailValue; 034import com.echothree.model.data.party.server.entity.Language; 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 com.echothree.util.server.persistence.Session; 041 042public class UseLogic 043 extends BaseLogic { 044 045 private UseLogic() { 046 super(); 047 } 048 049 private static class UseLogicHolder { 050 static UseLogic instance = new UseLogic(); 051 } 052 053 public static UseLogic getInstance() { 054 return UseLogicHolder.instance; 055 } 056 057 public Use createUse(final ExecutionErrorAccumulator eea, final String useName, final UseType useType, 058 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 059 final BasePK createdBy) { 060 var useControl = Session.getModelController(UseControl.class); 061 var use = useControl.getUseByName(useName); 062 063 if(use == null) { 064 use = useControl.createUse(useName, useType, isDefault, sortOrder, createdBy); 065 066 if(description != null) { 067 useControl.createUseDescription(use, language, description, createdBy); 068 } 069 } else { 070 handleExecutionError(DuplicateUseNameException.class, eea, ExecutionErrors.DuplicateUseName.name(), useName); 071 } 072 073 return use; 074 } 075 076 public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName, 077 final EntityPermission entityPermission) { 078 var useControl = Session.getModelController(UseControl.class); 079 var use = useControl.getUseByName(useName, entityPermission); 080 081 if(use == null) { 082 handleExecutionError(UnknownUseNameException.class, eea, ExecutionErrors.UnknownUseName.name(), useName); 083 } 084 085 return use; 086 } 087 088 public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName) { 089 return getUseByName(eea, useName, EntityPermission.READ_ONLY); 090 } 091 092 public Use getUseByNameForUpdate(final ExecutionErrorAccumulator eea, final String useName) { 093 return getUseByName(eea, useName, EntityPermission.READ_WRITE); 094 } 095 096 public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea, 097 final UseUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 098 var useControl = Session.getModelController(UseControl.class); 099 var useName = universalSpec.getUseName(); 100 var parameterCount = (useName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 101 Use use = null; 102 103 switch(parameterCount) { 104 case 0: 105 if(allowDefault) { 106 use = useControl.getDefaultUse(entityPermission); 107 108 if(use == null) { 109 handleExecutionError(UnknownDefaultUseException.class, eea, ExecutionErrors.UnknownDefaultUse.name()); 110 } 111 } else { 112 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 113 } 114 break; 115 case 1: 116 if(useName == null) { 117 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 118 ComponentVendors.ECHO_THREE.name(), EntityTypes.Use.name()); 119 120 if(!eea.hasExecutionErrors()) { 121 use = useControl.getUseByEntityInstance(entityInstance, entityPermission); 122 } 123 } else { 124 use = getUseByName(eea, useName, entityPermission); 125 } 126 break; 127 default: 128 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 129 break; 130 } 131 132 return use; 133 } 134 135 public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea, 136 final UseUniversalSpec universalSpec, boolean allowDefault) { 137 return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 138 } 139 140 public Use getUseByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 141 final UseUniversalSpec universalSpec, boolean allowDefault) { 142 return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 143 } 144 145 public void updateUseFromValue(UseDetailValue useDetailValue, BasePK updatedBy) { 146 var useControl = Session.getModelController(UseControl.class); 147 148 useControl.updateUseFromValue(useDetailValue, updatedBy); 149 } 150 151 public void deleteUse(final ExecutionErrorAccumulator eea, final Use use, final BasePK deletedBy) { 152 var offerUseControl = Session.getModelController(OfferUseControl.class); 153 154 if(offerUseControl.countOfferUsesByUse(use) == 0) { 155 var useControl = Session.getModelController(UseControl.class); 156 157 useControl.deleteUse(use, deletedBy); 158 } else { 159 handleExecutionError(CannotDeleteUseInUseException.class, eea, ExecutionErrors.CannotDeleteUseInUse.name()); 160 } 161 } 162 163}