KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > search > InfoPAttribute


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
32  * Search by Product Attribute.
33  *
34  * @author Jorg Janke
35  * @version $Id: InfoPAttribute.java,v 1.5 2003/10/26 05:54:06 jjanke Exp $
36  */

37 public class InfoPAttribute extends CDialog
38 {
39     /**
40      * Constructor
41      * @param parent
42      */

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 JavaDoc e)
52         {
53             Log.error("InfoPAttribute", e);
54         }
55         AEnv.showCenterWindow(parent, this);
56     } // InfoPAttribute
57

58     /** Resulting Query */
59     private String JavaDoc m_query = "";
60     /** Product Attribure Editors */
61     private ArrayList m_productEditors = new ArrayList();
62     /** Instance Attribute Editors */
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     //
71
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     //
80

81     /**
82      * Static Init
83      * @throws Exception
84      */

85     private void jbInit() throws Exception JavaDoc
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
92
confirmPanel.addActionListener(this);
93         mainPanel.add(confirmPanel, BorderLayout.SOUTH);
94     } // jbInit
95

96     /**
97      * Dynamic Init of the Center Panel
98      */

99     private void dynInit()
100     {
101         int row = addAttributes();
102         //
103
String JavaDoc s = Msg.translate(Env.getCtx(), "GuaranteeDate");
104         guaranteeDateSelection = new VComboBox (new Object JavaDoc[]
105             {s + " <", s + " =", s + " >"});
106     // guaranteeDateSelection.setPreferredSize();
107
initLotSelection();
108         // Fixed Instance Selection Fields
109
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         //
118
Dimension d = centerPanel.getPreferredSize();
119         d.width = 400;
120         centerPanel.setPreferredSize(d);
121     } // dynInit
122

123     private int addAttributes()
124     {
125         int row = 0;
126         PreparedStatement pstmt = null;
127         String JavaDoc 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 JavaDoc name = rs.getString(2);
142                 String JavaDoc description = rs.getString(3);
143                 boolean isList = "Y".equals(rs.getString(4));
144                 boolean isInstanceAttribute = "Y".equals(rs.getString(5));
145                 // Instance
146
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                 //
155
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                 //
167
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 JavaDoc 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 JavaDoc e)
188         {
189             pstmt = null;
190         }
191         return row;
192     } // addProductAttributes
193

194     /**
195      * Get Attribute List
196      * @param M_Attribute_ID attribure
197      * @return array
198      */

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 JavaDoc 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 JavaDoc 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 JavaDoc e)
233         {
234             pstmt = null;
235         }
236         KeyNamePair[] retValue = new KeyNamePair[list.size()];
237         list.toArray(retValue);
238         return retValue;
239     } // getAttributeList
240

241
242     /**
243      * Initialize Lot Selection
244      */

245     private void initLotSelection()
246     {
247         ArrayList list = new ArrayList();
248         list.add(new KeyNamePair(-1, ""));
249         
250         String JavaDoc 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 JavaDoc 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 JavaDoc e)
275         {
276             pstmt = null;
277         }
278         // Create List
279
KeyNamePair[] items = new KeyNamePair[list.size()];
280         list.toArray(items);
281         lotSelection = new VComboBox(items);
282     } // initLotSelection
283

284
285     
286     /**
287      * Action Listener
288      * @param e event
289      */

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     } // actionPerformed
303

304     /**
305      * Create Query
306      * <code>
307      * Available synonyms:
308      * M_Product p
309      * M_ProductPrice pr
310      * M_AttributeSet pa
311      * </code>
312      * @return query
313      */

314     private String JavaDoc createQuery()
315     {
316         /** Base Query
317         SELECT *
318         FROM M_Product p
319          INNER JOIN M_ProductPrice pr ON (p.M_Product_ID=pr.M_Product_ID)
320          LEFT OUTER JOIN M_AttributeSet pa ON (p.M_AttributeSet_ID=pa.M_AttributeSet_ID)
321         WHERE
322         **/

323         
324         /*** Instance Attributes */
325         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
326         // Serial No
327
String JavaDoc 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         // Lot Number
338
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         // Lot ID
349
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         // Guarantee Date
357
Timestamp ts = (Timestamp)guaranteeDateField.getValue();
358         if (ts != null)
359         {
360             sb.append(" AND TRUNC(asi.GuaranteeDate)");
361             int index = guaranteeDateSelection.getSelectedIndex(); // < = >
362
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         // Instance Editors
372
StringBuffer JavaDoc iAttr = new StringBuffer JavaDoc();
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 JavaDoc 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         // finish Instance Attributes
416
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         // Product Attributes
426
StringBuffer JavaDoc pAttr = new StringBuffer JavaDoc();
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 JavaDoc 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         //
470
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     } // createQuery
476

477     /**
478      * Get resulting Query WHERE
479      * @return query or null
480      */

481     public String JavaDoc getWhereClause()
482     {
483         return m_query;
484     } // getQuery
485

486 } // InfoPAttribute
487
Popular Tags