1 14 package org.compiere.apps.search; 15 16 import javax.swing.*; 17 import java.awt.*; 18 import java.awt.event.*; 19 import java.util.*; 20 import java.sql.*; 21 22 import org.compiere.apps.*; 23 import org.compiere.grid.ed.*; 24 import org.compiere.swing.*; 25 import org.compiere.plaf.*; 26 import org.compiere.util.*; 27 import org.compiere.model.*; 28 29 import com.sun.crypto.provider.DESCipher; 30 31 37 public class InfoPAttribute extends CDialog 38 { 39 43 public InfoPAttribute (JDialog parent) 44 { 45 super (parent, Msg.getMsg(Env.getCtx(), "InfoPAttribute"), true); 46 try 47 { 48 jbInit(); 49 dynInit(); 50 } 51 catch (Exception e) 52 { 53 Log.error("InfoPAttribute", e); 54 } 55 AEnv.showCenterWindow(parent, this); 56 } 58 59 private String m_query = ""; 60 61 private ArrayList m_productEditors = new ArrayList(); 62 63 private ArrayList m_instanceEditors = new ArrayList(); 64 65 66 private CPanel mainPanel = new CPanel(); 67 private BorderLayout mainLayout = new BorderLayout(); 68 private CPanel centerPanel = new CPanel(); 69 private ConfirmPanel confirmPanel = new ConfirmPanel(true); 70 private CLabel serNoLabel = new CLabel(Msg.translate(Env.getCtx(), "SerNo")); 72 private VString serNoField = new VString("SerNo", false, false, true, 10, 20, null); 73 private CLabel lotLabel = new CLabel(Msg.translate(Env.getCtx(), "Lot")); 74 private VString lotField = new VString("Lot", false, false, true, 10, 20, null); 75 private VComboBox guaranteeDateSelection = null; 76 private VDate guaranteeDateField = new VDate ("GuaranteeDate", false, false, true, DisplayType.Date, Msg.translate(Env.getCtx(), "GuaranteeDate")); 77 private CLabel lotLabel2 = new CLabel(Msg.translate(Env.getCtx(), "M_Lot_ID")); 78 private VComboBox lotSelection = null; 79 81 85 private void jbInit() throws Exception 86 { 87 this.getContentPane().add(mainPanel, BorderLayout.CENTER); 88 mainPanel.setLayout(mainLayout); 89 mainPanel.add(centerPanel, BorderLayout.CENTER); 90 centerPanel.setLayout(new ALayout()); 91 confirmPanel.addActionListener(this); 93 mainPanel.add(confirmPanel, BorderLayout.SOUTH); 94 } 96 99 private void dynInit() 100 { 101 int row = addAttributes(); 102 String s = Msg.translate(Env.getCtx(), "GuaranteeDate"); 104 guaranteeDateSelection = new VComboBox (new Object [] 105 {s + " <", s + " =", s + " >"}); 106 initLotSelection(); 108 centerPanel.add(serNoLabel, new ALayoutConstraint(row++, 0)); 110 centerPanel.add(serNoField, null); 111 centerPanel.add(lotLabel, new ALayoutConstraint(row++, 0)); 112 centerPanel.add(lotField, null); 113 centerPanel.add(lotLabel2, new ALayoutConstraint(row++, 0)); 114 centerPanel.add(lotSelection, null); 115 centerPanel.add(guaranteeDateSelection, new ALayoutConstraint(row++, 0)); 116 centerPanel.add(guaranteeDateField, null); 117 Dimension d = centerPanel.getPreferredSize(); 119 d.width = 400; 120 centerPanel.setPreferredSize(d); 121 } 123 private int addAttributes() 124 { 125 int row = 0; 126 PreparedStatement pstmt = null; 127 String sql = MRole.getDefault().addAccessSQL( 128 "SELECT M_Attribute_ID, Name, Description, IsList, IsInstanceAttribute " 129 + "FROM M_Attribute " 130 + "WHERE IsActive='Y' " 131 + "ORDER BY IsInstanceAttribute, Name", 132 "M_Attribute", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); 133 try 134 { 135 pstmt = DB.prepareCall(sql); 136 ResultSet rs = pstmt.executeQuery(); 137 boolean instanceLine = false; 138 while (rs.next()) 139 { 140 int attribute_ID = rs.getInt(1); 141 String name = rs.getString(2); 142 String description = rs.getString(3); 143 boolean isList = "Y".equals(rs.getString(4)); 144 boolean isInstanceAttribute = "Y".equals(rs.getString(5)); 145 if (!instanceLine && isInstanceAttribute) 147 { 148 CPanel group = new CPanel(); 149 group.setBorder(new VLine(Msg.translate(Env.getCtx(), "IsInstanceAttribute"))); 150 group.add(Box.createVerticalStrut(VLine.SPACE)); 151 centerPanel.add(group, new ALayoutConstraint(row++, 0)); 152 instanceLine = true; 153 } 154 CLabel label = new CLabel(name); 156 if (description != null && description.length() > 0) 157 label.setToolTipText(description); 158 centerPanel.add(label, new ALayoutConstraint(row++, 0)); 159 Component field = null; 160 if (isList) 161 field = new VComboBox(getAttributeList(attribute_ID)); 162 else 163 field = new VString(name, false, false, true, 10, 40, null); 164 label.setLabelFor(field); 165 centerPanel.add(field, null); 166 field.setName(String.valueOf(attribute_ID)); 168 if (isInstanceAttribute) 169 m_instanceEditors.add(field); 170 else 171 m_productEditors.add(field); 172 } 173 rs.close(); 174 pstmt.close(); 175 pstmt = null; 176 } 177 catch (Exception e) 178 { 179 Log.error("addProductAttributes - " + sql, e); 180 } 181 try 182 { 183 if (pstmt != null) 184 pstmt.close(); 185 pstmt = null; 186 } 187 catch (Exception e) 188 { 189 pstmt = null; 190 } 191 return row; 192 } 194 199 private KeyNamePair[] getAttributeList(int M_Attribute_ID) 200 { 201 ArrayList list = new ArrayList(); 202 list.add(new KeyNamePair(-1, "")); 203 204 PreparedStatement pstmt = null; 205 String sql = MRole.getDefault().addAccessSQL( 206 "SELECT M_AttributeValue_ID, Value, Name " 207 + "FROM M_AttributeValue " 208 + "WHERE M_Attribute_ID=? " 209 + "ORDER BY 2", 210 "M_AttributeValue", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); 211 try 212 { 213 pstmt = DB.prepareCall(sql); 214 pstmt.setInt(1, M_Attribute_ID); 215 ResultSet rs = pstmt.executeQuery(); 216 while (rs.next()) 217 list.add(new KeyNamePair(rs.getInt(1), rs.getString(3))); 218 rs.close(); 219 pstmt.close(); 220 pstmt = null; 221 } 222 catch (Exception e) 223 { 224 Log.error("getAttributeList", e); 225 } 226 try 227 { 228 if (pstmt != null) 229 pstmt.close(); 230 pstmt = null; 231 } 232 catch (Exception e) 233 { 234 pstmt = null; 235 } 236 KeyNamePair[] retValue = new KeyNamePair[list.size()]; 237 list.toArray(retValue); 238 return retValue; 239 } 241 242 245 private void initLotSelection() 246 { 247 ArrayList list = new ArrayList(); 248 list.add(new KeyNamePair(-1, "")); 249 250 String sql = MRole.getDefault().addAccessSQL( 251 "SELECT M_Lot_ID, Name FROM M_Lot WHERE IsActive='Y' ORDER BY 2", 252 "M_Lot", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); 253 PreparedStatement pstmt = null; 254 try 255 { 256 pstmt = DB.prepareCall(sql); 257 ResultSet rs = pstmt.executeQuery(); 258 while (rs.next()) 259 list.add(new KeyNamePair(rs.getInt(1), rs.getString(2))); 260 rs.close(); 261 pstmt.close(); 262 pstmt = null; 263 } 264 catch (Exception e) 265 { 266 Log.error("initLotSelection", e); 267 } 268 try 269 { 270 if (pstmt != null) 271 pstmt.close(); 272 pstmt = null; 273 } 274 catch (Exception e) 275 { 276 pstmt = null; 277 } 278 KeyNamePair[] items = new KeyNamePair[list.size()]; 280 list.toArray(items); 281 lotSelection = new VComboBox(items); 282 } 284 285 286 290 public void actionPerformed(ActionEvent e) 291 { 292 if (e.getActionCommand().equals(ConfirmPanel.A_OK)) 293 { 294 createQuery(); 295 dispose(); 296 } 297 else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) 298 { 299 m_query = null; 300 dispose(); 301 } 302 } 304 314 private String createQuery() 315 { 316 323 324 325 StringBuffer sb = new StringBuffer (); 326 String s = serNoField.getText(); 328 if (s != null && s.length() > 0) 329 { 330 sb.append(" AND asi.SerNo"); 331 if (s.indexOf('%') == -1 && s.indexOf('_') == 1) 332 sb.append("="); 333 else 334 sb.append(" LIKE "); 335 sb.append(DB.TO_STRING(s)); 336 } 337 s = lotField.getText(); 339 if (s != null && s.length() > 0) 340 { 341 sb.append(" AND asi.Lot"); 342 if (s.indexOf('%') == -1 && s.indexOf('_') == 1) 343 sb.append("="); 344 else 345 sb.append(" LIKE "); 346 sb.append(DB.TO_STRING(s)); 347 } 348 KeyNamePair pp = (KeyNamePair)lotSelection.getSelectedItem(); 350 if (pp != null && pp.getKey() != -1) 351 { 352 int ID = pp.getKey(); 353 sb.append(" AND asi.M_Lot_ID=").append(ID); 354 } 355 356 Timestamp ts = (Timestamp)guaranteeDateField.getValue(); 358 if (ts != null) 359 { 360 sb.append(" AND TRUNC(asi.GuaranteeDate)"); 361 int index = guaranteeDateSelection.getSelectedIndex(); if (index == 0) 363 sb.append("<"); 364 else if (index == 1) 365 sb.append("="); 366 else 367 sb.append(">"); 368 sb.append(DB.TO_DATE(ts,true)); 369 } 370 371 StringBuffer iAttr = new StringBuffer (); 373 for (int i = 0; i < m_instanceEditors.size(); i++) 374 { 375 Component c = (Component)m_instanceEditors.get(i); 376 int M_Attribute_ID = Integer.parseInt(c.getName()); 377 if (c instanceof VComboBox) 378 { 379 VComboBox field = (VComboBox)c; 380 pp = (KeyNamePair)field.getSelectedItem(); 381 if (pp != null && pp.getKey() != -1) 382 { 383 if (iAttr.length() > 0) 384 iAttr.append(" AND "); 385 iAttr.append("(M_Attribute_ID=").append(M_Attribute_ID) 386 .append(" AND M_AttributeValue_ID=").append(pp.getKey()).append(")"); 387 } 388 } 389 else 390 { 391 VString field = (VString)c; 392 String value = field.getText(); 393 if (value != null && value.length() > 0) 394 { 395 if (iAttr.length() > 0) 396 iAttr.append(" AND "); 397 iAttr.append("(M_Attribute_ID=").append(M_Attribute_ID) 398 .append(" AND Value"); 399 if (value.indexOf('%') == -1 && value.indexOf('_') == 1) 400 iAttr.append("="); 401 else 402 iAttr.append(" LIKE "); 403 iAttr.append(DB.TO_STRING(value)).append(")"); 404 } 405 } 406 } 407 if (iAttr.length() > 0) 408 { 409 iAttr.insert(0, " AND asi.M_AttributeSetInstance_ID IN " 410 + "(SELECT M_AttributeSetInstance_ID FROM M_AttributeInstance " 411 + "WHERE "); 412 iAttr.append(")"); 413 } 414 415 if (sb.length() > 0 || iAttr.length() > 0) 417 { 418 sb.insert(0, " AND EXISTS (SELECT * FROM M_Storage s" 419 + " INNER JOIN M_AttributeSetInstance asi ON (s.M_AttributeSetInstance_ID=asi.M_AttributeSetInstance_ID) " 420 + "WHERE s.M_Product_ID=p.M_Product_ID"); 421 sb.append(iAttr).append(")"); 422 } 423 424 425 StringBuffer pAttr = new StringBuffer (); 427 for (int i = 0; i < m_productEditors.size(); i++) 428 { 429 Component c = (Component)m_productEditors.get(i); 430 int M_Attribute_ID = Integer.parseInt(c.getName()); 431 if (c instanceof VComboBox) 432 { 433 VComboBox field = (VComboBox)c; 434 pp = (KeyNamePair)field.getSelectedItem(); 435 if (pp != null && pp.getKey() != -1) 436 { 437 if (pAttr.length() > 0) 438 pAttr.append(" AND "); 439 pAttr.append("(M_Attribute_ID=").append(M_Attribute_ID) 440 .append(" AND M_AttributeValue_ID=").append(pp.getKey()).append(")"); 441 } 442 } 443 else 444 { 445 VString field = (VString)c; 446 String value = field.getText(); 447 if (value != null && value.length() > 0) 448 { 449 if (pAttr.length() > 0) 450 pAttr.append(" AND "); 451 pAttr.append("(M_Attribute_ID=").append(M_Attribute_ID) 452 .append(" AND Value"); 453 if (value.indexOf('%') == -1 && value.indexOf('_') == 1) 454 pAttr.append("="); 455 else 456 pAttr.append(" LIKE "); 457 pAttr.append(DB.TO_STRING(value)).append(")"); 458 } 459 } 460 } 461 if (pAttr.length() > 0) 462 { 463 pAttr.insert(0, " AND p.M_AttributeSetInstance_ID IN " 464 + "(SELECT M_AttributeSetInstance_ID " 465 + "FROM M_AttributeInstance WHERE "); 466 pAttr.append(")"); 467 sb.append(pAttr); 468 } 469 m_query = null; 471 if (sb.length() > 0) 472 m_query = sb.toString(); 473 Log.trace(Log.l3_Util, "InfoPAttribute.createQuery", m_query); 474 return m_query; 475 } 477 481 public String getWhereClause() 482 { 483 return m_query; 484 } 486 } | Popular Tags |