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.vendor.server.command; 018 019import com.echothree.control.user.vendor.common.form.GetVendorItemsForm; 020import com.echothree.control.user.vendor.common.result.VendorResultFactory; 021import com.echothree.model.control.item.server.control.ItemControl; 022import com.echothree.model.control.item.server.logic.ItemLogic; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.control.vendor.server.control.VendorControl; 027import com.echothree.model.control.vendor.server.logic.VendorLogic; 028import com.echothree.model.data.item.server.entity.Item; 029import com.echothree.model.data.vendor.server.entity.Vendor; 030import com.echothree.model.data.vendor.server.entity.VendorItem; 031import com.echothree.model.data.vendor.server.factory.VendorItemFactory; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.Collection; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class GetVendorItemsCommand 047 extends BasePaginatedMultipleEntitiesCommand<VendorItem, GetVendorItemsForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.VendorItem.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null) 063 ); 064 } 065 066 @Inject 067 ItemControl itemControl; 068 069 @Inject 070 VendorControl vendorControl; 071 072 @Inject 073 ItemLogic itemLogic; 074 075 @Inject 076 VendorLogic vendorLogic; 077 078 079 /** Creates a new instance of GetVendorItemsCommand */ 080 public GetVendorItemsCommand() { 081 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 082 } 083 084 private Vendor vendor; 085 private Item item; 086 087 @Override 088 protected void handleForm() { 089 var vendorName = form.getVendorName(); 090 var itemName = form.getItemName(); 091 var parameterCount = (vendorName == null ? 0 : 1) + (itemName == null ? 0 : 1); 092 093 if(parameterCount == 1) { 094 if(vendorName != null) { 095 vendor = vendorLogic.getVendorByName(this, vendorName, null, null); 096 } else { 097 item = itemLogic.getItemByNameThenAlias(this, itemName); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 101 } 102 } 103 104 @Override 105 protected Long getTotalEntities() { 106 Long total = null; 107 108 if(!hasExecutionErrors()) { 109 if(vendor != null) { 110 total = vendorControl.countVendorItemsByVendorParty(vendor.getParty()); 111 } else { 112 total = vendorControl.countVendorItemsByItem(item); 113 } 114 } 115 116 return total; 117 } 118 119 @Override 120 protected Collection<VendorItem> getEntities() { 121 Collection<VendorItem> entities = null; 122 123 if(!hasExecutionErrors()) { 124 if(vendor != null) { 125 entities = vendorControl.getVendorItemsByVendorParty(vendor.getParty()); 126 } else { 127 entities = vendorControl.getVendorItemsByItem(item); 128 } 129 } 130 131 return entities; 132 } 133 134 @Override 135 protected BaseResult getResult(Collection<VendorItem> entities) { 136 var result = VendorResultFactory.getGetVendorItemsResult(); 137 138 if(entities != null) { 139 var userVisit = getUserVisit(); 140 141 if(vendor != null) { 142 result.setVendor(vendorControl.getVendorTransfer(userVisit, vendor)); 143 } else if(item != null) { 144 result.setItem(itemControl.getItemTransfer(userVisit, item)); 145 } 146 147 if(session.hasLimit(VendorItemFactory.class)) { 148 result.setVendorItemCount(getTotalEntities()); 149 } 150 151 result.setVendorItems(vendorControl.getVendorItemTransfers(userVisit, entities)); 152 } 153 154 return result; 155 } 156 157}