# ItemsBean_H.ejb Sample

This topic inludes the source code for the ItemsBean_H.ejb Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/EJBs/homeMethods/

## Sample Source Code

`001 package homeMethods; 002 003 import java.text.DecimalFormat; 004 import java.util.Collection; 005 import java.util.Iterator; 006 import javax.ejb.CreateException; 007 import javax.ejb.FinderException; 008 import weblogic.ejb.GenericEntityBean; 009 010 /** 011 * @ejbgen:entity default-transaction="Supports" concurrency-strategy="ReadOnly" 012 * ejb-name="ItemsBean_H" 013 * data-source-name = "cgSampleDataSource" 014 * table-name="EJB_ITEMS" 015 * prim-key-class = "java.lang.Integer" 016 * 017 * @ejbgen:jndi-name 018 * local="ejb.ItemsBean_HHome" 019 * 020 * @ejbgen:file-generation local-class = "true" local-class-name="Items_H" local-home = "true" local-home-name="Items_HHome" remote-class = "false" remote-class-name = "ItemsRemote" remote-home = "false" remote-home-name = "ItemsRemoteHome" value-class = "false" value-class-name = "ItemsValue" pk-class = "true" 021 */ 022 023 public abstract class ItemsBean_H extends GenericEntityBean 024 { 025 026 027 /** 028 * @ejbgen:cmp-field column = "ITEMNUMBER" 029 * primkey-field="true" 030 * @ejbgen:local-method 031 */ 032 public abstract Integer getItemnumber(); 033 /** 034 * @ejbgen:local-method 035 */ 036 public abstract void setItemnumber(Integer val); 037 038 /** 039 * @ejbgen:cmp-field column = "ITEMNAME" 040 * @ejbgen:local-method 041 */ 042 public abstract String getItemname(); 043 /** 044 * @ejbgen:local-method 045 */ 046 public abstract void setItemname(String val); 047 048 /** 049 * @ejbgen:cmp-field column = "PRICE" 050 * @ejbgen:local-method 051 */ 052 public abstract double getPrice(); 053 /** 054 * @ejbgen:local-method 055 */ 056 public abstract void setPrice(double val); 057 058 /** 059 * @ejbgen:cmp-field column = "QUANTITYAVAILABLE" 060 * @ejbgen:local-method 061 */ 062 public abstract int getQuantityavailable(); 063 /** 064 * @ejbgen:local-method 065 */ 066 public abstract void setQuantityavailable(int val); 067 068 069 /** 070 * @ejbgen:local-home-method 071 */ 072 public int ejbHomeGetNumberOfExpensiveItems(double price) 073 { 074 try { 075 Collection expensiveItems = ejbSelectExpensiveItems(price); 076 return expensiveItems.size(); 077 } 078 catch(FinderException fe) { 079 return -1; 080 } 081 } 082 083 /** 084 * @ejbgen:local-home-method 085 */ 086 public String ejbHomeGetAverageExpensiveItems(double price) 087 { 088 try { 089 Collection expensiveItems = ejbSelectExpensiveItems(price); 090 Iterator expensiveIterator = expensiveItems.iterator(); 091 double sum = 0; 092 while (expensiveIterator.hasNext()) { 093 Items_H anItem = (Items_H)expensiveIterator.next(); 094 sum += anItem.getPrice(); 095 } 096 // Format average to two digits after decimal 097 DecimalFormat myFormatter = new DecimalFormat("##.##"); 098 return myFormatter.format((sum/expensiveItems.size())); 099 } 100 catch(FinderException fe) { 101 return "-1.0"; 102 } 103 } 104 105 106 /** 107 * @ejbgen:local-home-method 108 */ 109 public boolean ejbHomeGetExpensiveItemsInStock(double price) 110 { 111 try { 112 Collection expensiveItems = ejbSelectExpensiveItems(price); 113 Iterator expensiveIterator = expensiveItems.iterator(); 114 double sum = 0; 115 while (expensiveIterator.hasNext()) { 116 Items_H anItem = (Items_H)expensiveIterator.next(); 117 if(anItem.getQuantityavailable() > 0) 118 return true; 119 } 120 return false; 121 } 122 catch(FinderException fe) { 123 return false; 124 } 125 } 126 127 /** 128 * @ejbgen:select ejb-ql="SELECT OBJECT(o) from ItemsBean_H as o WHERE o.price > ?1" 129 */ 130 public abstract java.util.Collection ejbSelectExpensiveItems(double price) throws FinderException; 131 132 133 public java.lang.Integer ejbCreate(java.lang.Integer Itemnumber) 134 { 135 setItemnumber(Itemnumber); 136 137 return null; // FIXME return PK value 138 } 139 140 public void ejbPostCreate(java.lang.Integer Itemnumber) 141 { 142 } 143 }`