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.GetItemDescriptionsForm; 020import com.echothree.control.user.item.common.result.ItemResultFactory; 021import com.echothree.model.control.item.server.control.ItemControl; 022import com.echothree.model.control.item.server.logic.ItemDescriptionTypeUseTypeLogic; 023import com.echothree.model.control.item.server.logic.ItemLogic; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.control.party.server.logic.LanguageLogic; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.item.server.entity.Item; 030import com.echothree.model.data.item.server.entity.ItemDescription; 031import com.echothree.model.data.item.server.entity.ItemDescriptionTypeUseType; 032import com.echothree.model.data.item.server.factory.ItemDescriptionFactory; 033import com.echothree.model.data.party.server.entity.Language; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import java.util.ArrayList; 043import java.util.Collection; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046import javax.inject.Inject; 047 048@Dependent 049public class GetItemDescriptionsCommand 050 extends BasePaginatedMultipleEntitiesCommand<ItemDescription, GetItemDescriptionsForm> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(SecurityRoleGroups.ItemDescription.name(), SecurityRoles.List.name()) 060 )) 061 )); 062 063 FORM_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("ItemDescriptionTypeUseTypeName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null) 067 ); 068 } 069 070 @Inject 071 ItemControl itemControl; 072 073 @Inject 074 PartyControl partyControl; 075 076 @Inject 077 ItemDescriptionTypeUseTypeLogic itemDescriptionTypeUseTypeLogic; 078 079 @Inject 080 ItemLogic itemLogic; 081 082 @Inject 083 LanguageLogic languageLogic; 084 085 086 /** Creates a new instance of GetItemDescriptionsCommand */ 087 public GetItemDescriptionsCommand() { 088 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 089 } 090 091 Item item; 092 ItemDescriptionTypeUseType itemDescriptionTypeUseType; 093 Language language; 094 095 Collection<ItemDescription> entities; 096 097 @Override 098 protected void handleForm() { 099 var itemName = form.getItemName(); 100 101 item = itemLogic.getItemByName(this, itemName); 102 103 // If an Item was found... 104 if(!hasExecutionErrors()) { 105 var itemDescriptionTypeUseTypeName = form.getItemDescriptionTypeUseTypeName(); 106 107 itemDescriptionTypeUseType = itemDescriptionTypeUseTypeName == null ? null : itemDescriptionTypeUseTypeLogic.getItemDescriptionTypeUseTypeByName(this, itemDescriptionTypeUseTypeName); 108 109 if(!hasExecutionErrors()) { 110 var languageIsoName = form.getLanguageIsoName(); 111 112 if(itemDescriptionTypeUseType == null) { 113 // If no ItemDescriptionTypeUseTypeName was specified, a LanguageIsoName must not be specified either... 114 if(languageIsoName != null) { 115 addExecutionError(ExecutionErrors.InvalidParameterCombination.name()); 116 } 117 } else { 118 // If an ItemDescriptionTypeUseTypeName was specified, a Language must either be specific or pulled from the default... 119 language = languageIsoName == null ? getPreferredLanguage() : languageLogic.getLanguageByName(this, languageIsoName); 120 } 121 } 122 } 123 } 124 125 @Override 126 protected Long getTotalEntities() { 127 Long totalEntities = null; 128 129 if(!hasExecutionErrors()) { 130 if(itemDescriptionTypeUseType == null) { 131 totalEntities = itemControl.countItemDescriptionsByItem(item); 132 } else { 133 // Get all ItemDescriptionTypeUses for the given ItemDescriptionTypeUseType... 134 var itemDescriptionTypeUses = itemControl.getItemDescriptionTypeUsesByItemDescriptionTypeUseType(itemDescriptionTypeUseType); 135 136 entities = new ArrayList<>(); 137 138 // And now try to get the best possible Item Description Type for each ItemDescriptionTypeUse... 139 for(var itemDescriptionTypeUse : itemDescriptionTypeUses) { 140 // There are further comments in ItemControl on the algorithm for this. 141 var itemDescription = itemControl.getBestItemDescription(itemDescriptionTypeUse.getItemDescriptionType(), item, language); 142 143 if(itemDescription != null) { 144 entities.add(itemDescription); 145 } 146 } 147 148 totalEntities = (long)entities.size(); 149 } 150 } 151 152 153 return totalEntities; 154 } 155 156 @Override 157 protected Collection<ItemDescription> getEntities() { 158 // If an Item was found... 159 if(!hasExecutionErrors()) { 160 if(itemDescriptionTypeUseType == null) { 161 // If no ItemDescriptionTypeUseTypeName was specified, a LanguageIsoName must not be specified either... 162 if(language == null) { 163 // Get all ItemDescriptions for the Item regardless of Language... 164 entities = itemControl.getItemDescriptionsByItem(item); 165 } 166 } else { 167 getTotalEntities(); // Populates entities in this case. 168 } 169 } 170 171 return entities; 172 } 173 174 @Override 175 protected BaseResult getResult(Collection<ItemDescription> entities) { 176 var result = ItemResultFactory.getGetItemDescriptionsResult(); 177 178 if(entities != null) { 179 var userVisit = getUserVisit(); 180 181 result.setItem(itemControl.getItemTransfer(userVisit, item)); 182 183 if(itemDescriptionTypeUseType != null) { 184 result.setItemDescriptionTypeUseType(itemControl.getItemDescriptionTypeUseTypeTransfer(userVisit, itemDescriptionTypeUseType)); 185 } 186 187 if(language != null) { 188 result.setLanguage(partyControl.getLanguageTransfer(userVisit, language)); 189 } 190 191 if(session.hasLimit(ItemDescriptionFactory.class)) { 192 result.setItemDescriptionCount(getTotalEntities()); 193 } 194 195 result.setItemDescriptions(itemControl.getItemDescriptionTransfers(userVisit, entities)); 196 } 197 198 return result; 199 } 200 201}