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