1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.text.DateFormat ; 19 20 import org.compiere.util.*; 21 22 28 public class MAttributeSetInstance extends X_M_AttributeSetInstance 29 { 30 37 public static MAttributeSetInstance get (Properties ctx, int M_AttributeSetInstance_ID, int M_Product_ID) 38 { 39 MAttributeSetInstance retValue = null; 40 if (M_AttributeSetInstance_ID != 0) 42 { 43 s_log.debug("get - from M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID); 44 return new MAttributeSetInstance (ctx, M_AttributeSetInstance_ID); 45 } 46 s_log.debug("get - from M_Product_ID=" + M_Product_ID); 48 if (M_Product_ID == 0) 49 return null; 50 String sql = "SELECT M_AttributeSet_ID, M_AttributeSetInstance_ID " 51 + "FROM M_Product " 52 + "WHERE M_Product_ID=?"; 53 PreparedStatement pstmt = null; 54 try 55 { 56 pstmt = DB.prepareStatement(sql); 57 pstmt.setInt(1, M_Product_ID); 58 ResultSet rs = pstmt.executeQuery(); 59 if (rs.next()) 60 { 61 int M_AttributeSet_ID = rs.getInt(1); 62 retValue = new MAttributeSetInstance (ctx, 0, M_AttributeSet_ID); 65 } 66 rs.close(); 67 pstmt.close(); 68 pstmt = null; 69 } 70 catch (SQLException ex) 71 { 72 s_log.error("get", ex); 73 } 74 try 75 { 76 if (pstmt != null) 77 pstmt.close(); 78 } 79 catch (SQLException ex1) 80 { 81 } 82 pstmt = null; 83 return retValue; 85 } 87 private static Logger s_log = Logger.getCLogger (MAttributeSetInstance.class); 88 89 90 91 96 public MAttributeSetInstance (Properties ctx, int M_AttributeSetInstance_ID) 97 { 98 super (ctx, M_AttributeSetInstance_ID); 99 } 101 106 public MAttributeSetInstance (Properties ctx, ResultSet rs) 107 { 108 super (ctx, rs); 109 } 111 117 public MAttributeSetInstance (Properties ctx, int M_AttributeSetInstance_ID, int M_AttributeSet_ID) 118 { 119 super (ctx, M_AttributeSetInstance_ID); 120 setM_AttributeSet_ID(M_AttributeSet_ID); 121 } 123 124 private MAttributeSet m_mas = null; 125 126 private DateFormat m_dateFormat = DisplayType.getDateFormat(DisplayType.Date); 127 128 132 public void setMAttributeSet (MAttributeSet mas) 133 { 134 m_mas = mas; 135 setM_AttributeSet_ID(mas.getM_AttributeSet_ID()); 136 } 138 142 public MAttributeSet getMAttributeSet() 143 { 144 if (m_mas == null && getM_AttributeSet_ID() != 0) 145 m_mas = new MAttributeSet (getCtx(), getM_AttributeSet_ID()); 146 return m_mas; 147 } 149 157 public void setDescription() 158 { 159 getMAttributeSet(); 161 if (m_mas == null) 162 { 163 setDescription ("??"); 164 return; 165 } 166 StringBuffer sb = new StringBuffer (); 167 if (m_mas.isSerNo() && getSerNo() != null) 169 sb.append("#").append(getSerNo()); 170 if (m_mas.isLot() && getLot() != null) 172 { 173 if (sb.length() > 0) 174 sb.append("_"); 175 sb.append("\u013D").append(getLot ()); 176 } 177 if (m_mas.isGuaranteeDate() && getGuaranteeDate() != null) 179 { 180 if (sb.length() > 0) 181 sb.append("_"); 182 sb.append (m_dateFormat.format (getGuaranteeDate())); 183 } 184 MAttribute[] attributes = m_mas.getMAttributes(false); 186 for (int i = 0; i < attributes.length; i++) 187 { 188 if (sb.length() > 0 && i == 0) 189 sb.append("_"); 190 if (i != 0) 191 sb.append("-"); 192 MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID()); 193 if (mai != null && mai.getValue() != null) 194 sb.append(mai.getValue()); 195 } 196 attributes = m_mas.getMAttributes(true); 198 for (int i = 0; i < attributes.length; i++) 199 { 200 if (sb.length() > 0 && i == 0) 201 sb.append("_"); 202 if (i != 0) 203 sb.append("-"); 204 MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID()); 205 if (mai != null && mai.getValue() != null) 206 sb.append(mai.getValue()); 207 } 208 setDescription (sb.toString()); 211 } 213 214 215 220 public Timestamp getGuaranteeDate(boolean getNew) 221 { 222 if (getNew) 223 { 224 int days = getMAttributeSet().getGuaranteeDays(); 225 Timestamp ts = TimeUtil.addDays(new Timestamp(System.currentTimeMillis()), days); 226 setGuaranteeDate(ts); 227 } 228 return getGuaranteeDate(); 229 } 231 237 public String getLot (boolean getNew, int M_Product_ID) 238 { 239 if (getNew) 240 { 241 int M_LotCtl_ID = getMAttributeSet().getM_LotCtl_ID(); 242 if (M_LotCtl_ID != 0) 243 { 244 MLotCtl ctl = new MLotCtl (getCtx(), M_LotCtl_ID); 245 MLot lot = ctl.createLot(M_Product_ID); 246 setM_Lot_ID (lot.getM_Lot_ID()); 247 setLot (lot.getName()); 248 } 249 } 250 return getLot(); 251 } 253 258 public String getSerNo(boolean getNew) 259 { 260 if (getNew) 261 { 262 int M_SerNoCtl_ID = getMAttributeSet().getM_SerNoCtl_ID(); 263 if (M_SerNoCtl_ID != 0) 264 { 265 MSerNoCtl ctl = new MSerNoCtl (getCtx(), M_SerNoCtl_ID); 266 setSerNo(ctl.createSerNo()); 267 } 268 } 269 return getSerNo(); 270 } 272 } | Popular Tags |