1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.compiere.util.*; 20 21 27 public class MAttribute extends X_M_Attribute 28 { 29 34 public MAttribute (Properties ctx, int M_Attribute_ID) 35 { 36 super (ctx, M_Attribute_ID); 37 45 } 47 52 public MAttribute (Properties ctx, ResultSet rs) 53 { 54 super (ctx, rs); 55 } 57 58 private MAttributeValue[] m_values = null; 59 60 64 public MAttributeValue[] getMAttributeValues() 65 { 66 if (m_values == null && isList()) 67 { 68 ArrayList list = new ArrayList(); 69 if (!isMandatory()) 70 list.add (null); 71 String sql = "SELECT * FROM M_AttributeValue " 73 + "WHERE M_Attribute_ID=? " 74 + "ORDER BY Value"; 75 PreparedStatement pstmt = null; 76 try 77 { 78 pstmt = DB.prepareStatement(sql); 79 pstmt.setInt(1, getM_Attribute_ID()); 80 ResultSet rs = pstmt.executeQuery(); 81 while (rs.next()) 82 list.add(new MAttributeValue (getCtx(), rs)); 83 rs.close(); 84 pstmt.close(); 85 pstmt = null; 86 } 87 catch (SQLException ex) 88 { 89 log.error("getValues", ex); 90 } 91 try 92 { 93 if (pstmt != null) 94 pstmt.close(); 95 } 96 catch (SQLException ex1) 97 { 98 } 99 pstmt = null; 100 m_values = new MAttributeValue[list.size()]; 101 list.toArray(m_values); 102 } 103 return m_values; 104 } 106 107 108 113 public MAttributeInstance getMAttributeInstance (int M_AttributeSetInstance_ID) 114 { 115 MAttributeInstance retValue = null; 116 String sql = "SELECT * " 117 + "FROM M_AttributeInstance " 118 + "WHERE M_Attribute_ID=? AND M_AttributeSetInstance_ID=?"; 119 PreparedStatement pstmt = null; 120 try 121 { 122 pstmt = DB.prepareStatement (sql); 123 pstmt.setInt (1, getM_Attribute_ID()); 124 pstmt.setInt(2, M_AttributeSetInstance_ID); 125 ResultSet rs = pstmt.executeQuery (); 126 if (rs.next ()) 127 retValue = new MAttributeInstance (getCtx(), rs); 128 rs.close (); 129 pstmt.close (); 130 pstmt = null; 131 } 132 catch (SQLException ex) 133 { 134 log.error ("getAttributeInstance", ex); 135 } 136 try 137 { 138 if (pstmt != null) 139 pstmt.close (); 140 } 141 catch (SQLException ex1) 142 { 143 } 144 pstmt = null; 145 146 return retValue; 147 } 149 154 public void setMAttributeInstance (int M_AttributeSetInstance_ID, MAttributeValue value) 155 { 156 MAttributeInstance instance = getMAttributeInstance(M_AttributeSetInstance_ID); 157 if (instance == null) 158 { 159 if (value != null) 160 instance = new MAttributeInstance (getCtx (), getM_Attribute_ID (), 161 M_AttributeSetInstance_ID, value.getM_AttributeValue_ID (), 162 value.getName ()); else 164 instance = new MAttributeInstance (getCtx(), getM_Attribute_ID(), 165 M_AttributeSetInstance_ID, 0, null); 166 } 167 else 168 { 169 if (value != null) 170 { 171 instance.setM_AttributeValue_ID (value.getM_AttributeValue_ID ()); 172 instance.setValue (value.getName()); } 174 else 175 { 176 instance.setM_AttributeValue_ID (0); 177 instance.setValue (null); 178 } 179 } 180 instance.save(); 181 } 183 188 public void setMAttributeInstance (int M_AttributeSetInstance_ID, String value) 189 { 190 MAttributeInstance instance = getMAttributeInstance(M_AttributeSetInstance_ID); 191 if (instance == null) 192 instance = new MAttributeInstance (getCtx(), getM_Attribute_ID(), M_AttributeSetInstance_ID, value); 193 else 194 instance.setValue(value); 195 instance.save(); 196 } 198 } | Popular Tags |