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.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.user.common.pk.UserVisitPK; 030import com.echothree.model.data.vendor.server.entity.Vendor; 031import com.echothree.model.data.vendor.server.entity.VendorItem; 032import com.echothree.model.data.vendor.server.factory.VendorItemFactory; 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.BaseMultipleEntitiesCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.Arrays; 043import java.util.Collection; 044import java.util.Collections; 045import java.util.List; 046 047public class GetVendorItemsCommand 048 extends BaseMultipleEntitiesCommand<VendorItem, GetVendorItemsForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.VendorItem.name(), SecurityRoles.List.name()) 058 ))) 059 ))); 060 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null) 064 )); 065 } 066 067 /** Creates a new instance of GetVendorItemsCommand */ 068 public GetVendorItemsCommand(UserVisitPK userVisitPK, GetVendorItemsForm form) { 069 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 070 } 071 072 Vendor vendor; 073 Item item; 074 075 @Override 076 protected Collection<VendorItem> getEntities() { 077 var vendorControl = Session.getModelController(VendorControl.class); 078 var vendorName = form.getVendorName(); 079 var itemName = form.getItemName(); 080 var parameterCount = (vendorName == null ? 0 : 1) + (itemName == null ? 0 : 1); 081 Collection<VendorItem> entities = null; 082 083 if(parameterCount == 1) { 084 if(vendorName != null) { 085 vendor = VendorLogic.getInstance().getVendorByName(this, vendorName, null, null); 086 087 if(!hasExecutionErrors()) { 088 var vendorParty = vendor.getParty(); 089 090 entities = vendorControl.getVendorItemsByVendorParty(vendorParty); 091 } 092 } else if(itemName != null) { 093 item = ItemLogic.getInstance().getItemByNameThenAlias(this, itemName); 094 095 if(!hasExecutionErrors()) { 096 entities = vendorControl.getVendorItemsByItem(item); 097 } 098 } 099 } else { 100 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 101 } 102 103 return entities; 104 } 105 106 @Override 107 protected BaseResult getResult(Collection<VendorItem> entities) { 108 var result = VendorResultFactory.getGetVendorItemsResult(); 109 110 if(entities != null) { 111 var vendorControl = Session.getModelController(VendorControl.class); 112 var userVisit = getUserVisit(); 113 114 result.setVendorItems(vendorControl.getVendorItemTransfers(userVisit, entities)); 115 116 if(vendor != null) { 117 var vendorParty = vendor.getParty(); 118 119 result.setVendor(vendorControl.getVendorTransfer(userVisit, vendor)); 120 121 if(session.hasLimit(VendorItemFactory.class)) { 122 result.setVendorItemCount(vendorControl.countVendorItemsByVendorParty(vendorParty)); 123 } 124 } else if(item != null) { 125 var itemControl = Session.getModelController(ItemControl.class); 126 127 result.setItem(itemControl.getItemTransfer(userVisit, item)); 128 129 if(session.hasLimit(VendorItemFactory.class)) { 130 result.setVendorItemCount(vendorControl.countVendorItemsByItem(item)); 131 } 132 } 133 } 134 135 return result; 136 } 137 138}