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.control.user.item.server.command; 018 019import com.echothree.control.user.item.common.form.GetItemDescriptionForm; 020import com.echothree.control.user.item.common.result.ItemResultFactory; 021import com.echothree.model.control.content.server.logic.ContentLogic; 022import com.echothree.model.control.core.common.ComponentVendors; 023import com.echothree.model.control.core.common.EntityTypes; 024import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 025import com.echothree.model.control.item.server.control.ItemControl; 026import com.echothree.model.control.item.server.logic.ItemDescriptionLogic; 027import com.echothree.model.control.item.server.logic.ItemDescriptionTypeLogic; 028import com.echothree.model.control.item.server.logic.ItemLogic; 029import com.echothree.model.control.party.server.control.PartyControl; 030import com.echothree.model.data.item.server.entity.ItemDescription; 031import com.echothree.model.data.item.server.entity.ItemDescriptionType; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSingleEntityCommand; 038import com.echothree.util.server.validation.ParameterUtils; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class GetItemDescriptionCommand 045 extends BaseSingleEntityCommand<ItemDescription, GetItemDescriptionForm> { 046 047 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 FORM_FIELD_DEFINITIONS = List.of( 052 new FieldDefinition("ItemDescriptionTypeName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null), 054 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 055 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 056 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 057 new FieldDefinition("Referrer", FieldType.URL, false, null, null) 058 ); 059 } 060 061 @Inject 062 ItemControl itemControl; 063 064 @Inject 065 PartyControl partyControl; 066 067 @Inject 068 ContentLogic contentLogic; 069 070 @Inject 071 EntityInstanceLogic entityInstanceLogic; 072 073 @Inject 074 ItemDescriptionLogic itemDescriptionLogic; 075 076 @Inject 077 ItemDescriptionTypeLogic itemDescriptionTypeLogic; 078 079 @Inject 080 ItemLogic itemLogic; 081 082 083 /** Creates a new instance of GetItemDescriptionCommand */ 084 public GetItemDescriptionCommand() { 085 super(null, FORM_FIELD_DEFINITIONS, false); 086 } 087 088 private void checkReferrer(final ItemDescriptionType itemDescriptionType) { 089 if(itemDescriptionType.getLastDetail().getCheckContentWebAddress()) { 090 contentLogic.checkReferrer(this, form.getReferrer()); 091 } 092 } 093 094 @Override 095 protected ItemDescription getEntity() { 096 ItemDescription itemDescription = null; 097 var itemName = form.getItemName(); 098 var itemDescriptionTypeName = form.getItemDescriptionTypeName(); 099 var traditionalParameterCount = ParameterUtils.getInstance().countNonNullParameters(itemName, itemDescriptionTypeName); 100 101 if(traditionalParameterCount == 0 || traditionalParameterCount == 2) { 102 var possibleEntitySpecsCount = entityInstanceLogic.countPossibleEntitySpecs(form); 103 104 // checkReferrer(...) is called separately in the two paths since the first one can be short circuited if 105 // the referrer check fails. 106 if(traditionalParameterCount == 2 && possibleEntitySpecsCount == 0) { 107 var item = itemLogic.getItemByName(this, itemName); 108 var itemDescriptionType = itemDescriptionTypeLogic.getItemDescriptionTypeByName(this, itemDescriptionTypeName); 109 110 if(!hasExecutionErrors()) { 111 checkReferrer(itemDescriptionType); 112 113 if(!hasExecutionErrors()) { 114 var languageIsoName = form.getLanguageIsoName(); 115 var language = languageIsoName == null ? getPreferredLanguage() : partyControl.getLanguageByIsoName(languageIsoName); 116 117 if(languageIsoName == null || language != null) { 118 itemDescription = itemControl.getItemDescription(itemDescriptionType, item, language); 119 120 if(itemDescription == null) { 121 itemDescription = itemDescriptionLogic.searchForItemDescription(itemDescriptionType, 122 item, language, getPartyPK()); 123 } 124 125 if(itemDescription == null) { 126 addExecutionError(ExecutionErrors.UnknownItemDescription.name(), itemDescriptionTypeName, 127 itemName, languageIsoName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 131 } 132 } 133 } 134 } else if(traditionalParameterCount == 0 && possibleEntitySpecsCount == 1) { 135 var entityInstance = entityInstanceLogic.getEntityInstance(this, form, 136 ComponentVendors.ECHO_THREE.name(), EntityTypes.ItemDescription.name()); 137 138 if(!hasExecutionErrors()) { 139 itemDescription = itemControl.getItemDescriptionByEntityInstance(entityInstance); 140 141 checkReferrer(itemDescription.getLastDetail().getItemDescriptionType()); 142 143 if(hasExecutionErrors()) { 144 itemDescription = null; // pretend that didn't happen. 145 } 146 } 147 } else { 148 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 149 } 150 } else { 151 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 152 } 153 154 return itemDescription; 155 } 156 157 @Override 158 protected BaseResult getResult(ItemDescription itemDescription) { 159 var result = ItemResultFactory.getGetItemDescriptionResult(); 160 161 if(itemDescription != null) { 162 result.setItemDescription(itemControl.getItemDescriptionTransfer(getUserVisit(), itemDescription)); 163 } 164 165 return result; 166 } 167 168 169}