KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Frame JavaDoc;
17 import java.math.BigDecimal JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import org.compiere.apps.AEnv;
23 import org.compiere.apps.ALayout;
24 import org.compiere.apps.ALayoutConstraint;
25 import org.compiere.grid.ed.VCheckBox;
26 import org.compiere.minigrid.IDColumn;
27 import org.compiere.model.MQuery;
28 import org.compiere.plaf.CompierePLAF;
29 import org.compiere.swing.CLabel;
30 import org.compiere.swing.CTextField;
31 import org.compiere.util.Env;
32 import org.compiere.util.KeyNamePair;
33 import org.compiere.util.Log;
34 import org.compiere.util.Msg;
35
36 /**
37  * Search Business Partner and return selection
38  *
39  * @author Jorg Janke
40  * @version $Id: InfoBPartner.java,v 1.20 2003/10/17 06:15:56 jjanke Exp $
41  */

42 public final class InfoBPartner extends Info
43 {
44     /**
45      * Standard Constructor
46      * @param frame frame
47      * @param modal modal
48      * @param WindowNo WindowNo
49      * @param value Query value Name or Value if contains numbers
50      * @param isSOTrx if false, query vendors only
51      * @param multiSelection multiple selection
52      * @param whereClause where clause
53      */

54     public InfoBPartner(Frame JavaDoc frame, boolean modal, int WindowNo,
55         String JavaDoc value, boolean isSOTrx,
56         boolean multiSelection, String JavaDoc whereClause)
57     {
58         super(frame, modal, WindowNo, "bp", "C_BPartner_ID", multiSelection, whereClause);
59         Log.trace(Log.l1_User, "InfoBPartner", value);
60         setTitle(Msg.getMsg(Env.getCtx(), "InfoBPartner"));
61         m_isSOTrx = isSOTrx;
62         //
63
statInit();
64         initInfo (value, whereClause);
65         //
66
int no = p_table.getRowCount();
67         setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false);
68         setStatusDB(Integer.toString(no));
69         // AutoQuery
70
if (value != null && value.length() > 0)
71             executeQuery();
72         p_loadedOK = true;
73         // Focus
74
fieldValue.requestFocus();
75
76         AEnv.positionCenterWindow(frame, this);
77     } // InfoBPartner
78

79     /** SalesOrder Trx */
80     private boolean m_isSOTrx = false;
81
82     private int m_AD_User_ID_index = -1;
83     private int m_C_BPartner_Location_ID_index = -1;
84
85     /** From Clause */
86     private static String JavaDoc s_partnerFROM = "C_BPartner bp"
87         + " LEFT OUTER JOIN AD_User c ON (bp.C_BPartner_ID=c.C_BPartner_ID AND c.IsActive='Y')"
88         + " LEFT OUTER JOIN C_BPartner_Location l ON (bp.C_BPartner_ID=l.C_BPartner_ID AND l.IsActive='Y')"
89         + " LEFT OUTER JOIN C_Location a ON (l.C_Location_ID=a.C_Location_ID)";
90
91     /** Array of Column Info */
92     private static Info_Column[] s_partnerLayout = {
93         new Info_Column(" ", "bp.C_BPartner_ID", IDColumn.class),
94         new Info_Column(Msg.translate(Env.getCtx(), "Value"), "bp.Value", String JavaDoc.class),
95         new Info_Column(Msg.translate(Env.getCtx(), "Name"), "bp.Name", String JavaDoc.class),
96         new Info_Column(Msg.translate(Env.getCtx(), "Contact"), "c.Name AS Contact", KeyNamePair.class, "c.AD_User_ID"),
97         new Info_Column(Msg.translate(Env.getCtx(), "SO_CreditAvailable"), "bp.SO_CreditLimit-bp.SO_CreditUsed AS SO_CreditAvailable", BigDecimal JavaDoc.class, true, true, null),
98         new Info_Column(Msg.translate(Env.getCtx(), "SO_CreditUsed"), "bp.SO_CreditUsed", BigDecimal JavaDoc.class),
99         new Info_Column(Msg.translate(Env.getCtx(), "Phone"), "c.Phone", String JavaDoc.class),
100         new Info_Column(Msg.translate(Env.getCtx(), "Postal"), "a.Postal", KeyNamePair.class, "l.C_BPartner_Location_ID"),
101         new Info_Column(Msg.translate(Env.getCtx(), "City"), "a.City", String JavaDoc.class),
102         new Info_Column(Msg.translate(Env.getCtx(), "Revenue"), "bp.ActualLifetimeValue", BigDecimal JavaDoc.class)
103     };
104
105     //
106
private CLabel labelValue = new CLabel();
107     private CTextField fieldValue = new CTextField(10);
108     private CLabel labelName = new CLabel();
109     private CTextField fieldName = new CTextField(10);
110     private CLabel labelContact = new CLabel();
111     private CTextField fieldContact = new CTextField(10);
112     private CLabel labelEMail = new CLabel();
113     private CTextField fieldEMail = new CTextField(10);
114     private CLabel labelPostal = new CLabel();
115     private CTextField fieldPostal = new CTextField(10);
116     private CLabel labelPhone = new CLabel();
117     private CTextField fieldPhone = new CTextField(10);
118     private VCheckBox checkAND = new VCheckBox();
119     private VCheckBox checkCustomer = new VCheckBox();
120
121     /**
122      * Static Setup - add fields to parameterPanel
123      */

124     private void statInit()
125     {
126         labelValue.setText(Msg.getMsg(Env.getCtx(), "Value"));
127         fieldValue.setBackground(CompierePLAF.getInfoBackground());
128         fieldValue.addActionListener(this);
129
130         labelName.setText(Msg.getMsg(Env.getCtx(), "Name"));
131         fieldName.setBackground(CompierePLAF.getInfoBackground());
132         fieldName.addActionListener(this);
133
134         labelContact.setText(Msg.getMsg(Env.getCtx(), "Contact"));
135         fieldContact.setBackground(CompierePLAF.getInfoBackground());
136         fieldContact.addActionListener(this);
137         
138         labelEMail.setText(Msg.getMsg(Env.getCtx(), "EMail"));
139         fieldEMail.setBackground(CompierePLAF.getInfoBackground());
140         fieldEMail.addActionListener(this);
141
142         labelPostal.setText(Msg.getMsg(Env.getCtx(), "Postal"));
143         fieldPostal.setBackground(CompierePLAF.getInfoBackground());
144         fieldPostal.addActionListener(this);
145
146         labelPhone.setText(Msg.translate(Env.getCtx(), "Phone"));
147         fieldPhone.setBackground(CompierePLAF.getInfoBackground());
148         fieldPhone.addActionListener(this);
149
150         checkAND.setText(Msg.getMsg(Env.getCtx(), "SearchAND"));
151         checkAND.setToolTipText(Msg.getMsg(Env.getCtx(), "SearchANDInfo"));
152         checkAND.setSelected(true);
153         checkAND.addActionListener(this);
154         if (m_isSOTrx)
155             checkCustomer.setText(Msg.getMsg(Env.getCtx(), "OnlyCustomers"));
156         else
157             checkCustomer.setText(Msg.getMsg(Env.getCtx(), "OnlyVendors"));
158         checkCustomer.setSelected(true);
159         checkCustomer.setFocusable(false);
160         checkCustomer.setRequestFocusEnabled(false);
161         checkCustomer.addActionListener(this);
162         //
163
parameterPanel.setLayout(new ALayout());
164         //
165
parameterPanel.add(labelValue, new ALayoutConstraint(0,0));
166         parameterPanel.add(fieldValue, null);
167         parameterPanel.add(labelContact, null);
168         parameterPanel.add(fieldContact, null);
169         parameterPanel.add(labelPhone, null);
170         parameterPanel.add(fieldPhone, null);
171         parameterPanel.add(checkCustomer, null);
172         //
173
parameterPanel.add(labelName, new ALayoutConstraint(1,0));
174         parameterPanel.add(fieldName, null);
175         parameterPanel.add(labelEMail, null);
176         parameterPanel.add(fieldEMail, null);
177         parameterPanel.add(labelPostal, null);
178         parameterPanel.add(fieldPostal, null);
179         parameterPanel.add(checkAND, null);
180     } // statInit
181

182     /**
183      * Dynamic Init
184      * @param value value
185      * @param whereClause where clause
186      */

187     private void initInfo(String JavaDoc value, String JavaDoc whereClause)
188     {
189         /** From
190             C_BPartner bp
191              LEFT OUTER JOIN AD_User c ON (bp.C_BPartner_ID=c.C_BPartner_ID AND c.IsActive='Y')
192              LEFT OUTER JOIN C_BPartner_Location l ON (bp.C_BPartner_ID=l.C_BPartner_ID AND l.IsActive='Y')
193              LEFT OUTER JOIN C_Location a ON (l.C_Location_ID=a.C_Location_ID)
194         **/

195
196         // Create Grid
197
StringBuffer JavaDoc where = new StringBuffer JavaDoc();
198         where.append("bp.IsSummary='N' AND bp.IsActive='Y'");
199         if (whereClause != null && whereClause.length() > 0)
200             where.append(" AND ").append(whereClause);
201         //
202
prepareTable(s_partnerLayout, s_partnerFROM,
203             where.toString(),
204             "bp.Value");
205
206         // Get indexes
207
for (int i = 0; i < p_layout.length; i++)
208         {
209             if (p_layout[i].getIDcolSQL().indexOf("AD_User_ID") != -1)
210                 m_AD_User_ID_index = i;
211             if (p_layout[i].getIDcolSQL().indexOf("C_BPartner_Location_ID") != -1)
212                 m_C_BPartner_Location_ID_index = i;
213         }
214
215         // Set Value
216
if (value == null)
217             value = "%";
218         if (!value.endsWith("%"))
219             value += "%";
220
221         // Put query string in Name if not numeric
222
if (value.equals("%"))
223             fieldName.setText(value);
224         // No Numbers entered
225
else if ((value.indexOf("0")+value.indexOf("1")+value.indexOf("2")+value.indexOf("3")+value.indexOf("4") +value.indexOf("5")
226             +value.indexOf("6")+value.indexOf("7")+value.indexOf("8")+value.indexOf("9")) == -10)
227         {
228             if (value.startsWith("%"))
229                 fieldName.setText(value);
230             else
231                 fieldName.setText("%" + value);
232         }
233         // Number entered
234
else
235             fieldValue.setText(value);
236     } // initInfo
237

238     /*************************************************************************/
239
240     /**
241      * Construct SQL Where Clause and define parameters.
242      * (setParameters needs to set parameters)
243      * Includes first AND
244      * @return WHERE clause
245      */

246     String JavaDoc getSQLWhere()
247     {
248         ArrayList JavaDoc list = new ArrayList JavaDoc();
249         // => Value
250
String JavaDoc value = fieldValue.getText().toUpperCase();
251         if (!(value.equals("") || value.equals("%")))
252             list.add ("UPPER(bp.Value) LIKE ?");
253         // => Name
254
String JavaDoc name = fieldName.getText().toUpperCase();
255         if (!(name.equals("") || name.equals("%")))
256             list.add ("UPPER(bp.Name) LIKE ?");
257         // => Contact
258
String JavaDoc contact = fieldContact.getText().toUpperCase();
259         if (!(contact.equals("") || contact.equals("%")))
260             list.add ("UPPER(c.Name) LIKE ?");
261         // => EMail
262
String JavaDoc email = fieldEMail.getText().toUpperCase();
263         if (!(email.equals("") || email.equals("%")))
264             list.add ("UPPER(c.EMail) LIKE ?");
265         // => Phone
266
String JavaDoc phone = fieldPhone.getText().toUpperCase();
267         if (!(phone.equals("") || phone.equals("%")))
268             list.add ("UPPER(c.Phone) LIKE ?");
269         // => Postal
270
String JavaDoc postal = fieldPostal.getText().toUpperCase();
271         if (!(postal.equals("") || postal.equals("%")))
272             list.add ("UPPER(a.Postal) LIKE ?");
273
274         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
275         int size = list.size();
276         // Just one
277
if (size == 1)
278             sql.append(" AND ").append(list.get(0));
279         else if (size > 1)
280         {
281             boolean AND = checkAND.isSelected();
282             sql.append(" AND ");
283             if (!AND)
284                 sql.append("(");
285             for (int i = 0; i < size; i++)
286             {
287                 if (i > 0)
288                     sql.append(AND ? " AND " : " OR ");
289                 sql.append(list.get(i));
290             }
291             if (!AND)
292                 sql.append(")");
293         }
294
295         // Static SQL
296
if (checkCustomer.isSelected())
297         {
298             sql.append(" AND ");
299             if (m_isSOTrx)
300                 sql.append ("bp.IsCustomer='Y'");
301             else
302                 sql.append ("bp.IsVendor='Y'");
303         }
304         return sql.toString();
305     } // getSQLWhere
306

307     /**
308      * Set Parameters for Query.
309      * (as defined in getSQLWhere)
310      * @param pstmt pstmt
311      * @throws SQLException
312      */

313     void setParameters(PreparedStatement JavaDoc pstmt) throws SQLException JavaDoc
314     {
315         int index = 1;
316         // => Value
317
String JavaDoc value = fieldValue.getText().toUpperCase();
318         if (!(value.equals("") || value.equals("%")))
319         {
320             if (!value.endsWith("%"))
321                 value += "%";
322             pstmt.setString(index++, value);
323             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "Value: " + value);
324         }
325         // => Name
326
String JavaDoc name = fieldName.getText().toUpperCase();
327         if (!(name.equals("") || name.equals("%")))
328         {
329             if (!name.endsWith("%"))
330                 name += "%";
331             pstmt.setString(index++, name);
332             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "Name: " + name);
333         }
334         // => Contact
335
String JavaDoc contact = fieldContact.getText().toUpperCase();
336         if (!(contact.equals("") || contact.equals("%")))
337         {
338             if (!contact.endsWith("%"))
339                 contact += "%";
340             pstmt.setString(index++, contact);
341             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "Contact: " + contact);
342         }
343         // => EMail
344
String JavaDoc email = fieldEMail.getText().toUpperCase();
345         if (!(email.equals("") || email.equals("%")))
346         {
347             if (!email.endsWith("%"))
348                 email += "%";
349             pstmt.setString(index++, email);
350             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "EMail: " + email);
351         }
352         // => Phone
353
String JavaDoc phone = fieldPhone.getText().toUpperCase();
354         if (!(phone.equals("") || phone.equals("%")))
355         {
356             if (!phone.endsWith("%"))
357                 phone += "%";
358             pstmt.setString(index++, phone);
359             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "Phone: " + phone);
360         }
361         // => Postal
362
String JavaDoc postal = fieldPostal.getText().toUpperCase();
363         if (!(postal.equals("") || postal.equals("%")))
364         {
365             if (!postal.endsWith("%"))
366                 postal += "%";
367             pstmt.setString(index++, postal);
368             Log.trace(Log.l5_DData, "InfoBPartner.setParameters", "Postal: " + postal);
369         }
370     } // setParameters
371

372     /*************************************************************************/
373
374     /**
375      * Save Selection Details
376      * Get Location/Partner Info
377      */

378     public void saveSelectionDetail()
379     {
380         int row = p_table.getSelectedRow();
381         if (row == -1)
382             return;
383
384         int AD_User_ID = 0;
385         int C_BPartner_Location_ID = 0;
386
387         if (m_AD_User_ID_index != -1)
388         {
389             Object JavaDoc data = p_table.getModel().getValueAt(row, m_AD_User_ID_index);
390             if (data instanceof KeyNamePair)
391                 AD_User_ID = ((KeyNamePair)data).getKey();
392         }
393         if (m_C_BPartner_Location_ID_index != -1)
394         {
395             Object JavaDoc data = p_table.getModel().getValueAt(row, m_C_BPartner_Location_ID_index);
396             if (data instanceof KeyNamePair)
397                 C_BPartner_Location_ID = ((KeyNamePair)data).getKey();
398         }
399         // publish for Callout to read
400
Integer JavaDoc ID = getSelectedRowKey();
401         Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID", ID == null ? "0" : ID.toString());
402         Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "AD_User_ID", String.valueOf(AD_User_ID));
403         Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_Location_ID", String.valueOf(C_BPartner_Location_ID));
404     } // saveSelectionDetail
405

406
407     /*************************************************************************/
408
409     /**
410      * Show History
411      */

412     void showHistory()
413     {
414         Log.trace(Log.l1_User, "InfoBPartner.showHistory");
415         Integer JavaDoc C_BPartner_ID = getSelectedRowKey();
416         if (C_BPartner_ID == null)
417             return;
418         InvoiceHistory ih = new InvoiceHistory (this, C_BPartner_ID.intValue(), 0);
419         ih.setVisible(true);
420         ih = null;
421     } // showHistory
422

423     /**
424      * Has History
425      * @return true
426      */

427     boolean hasHistory()
428     {
429         return true;
430     } // hasHistory
431

432     /**
433      * Zoom
434      */

435     void zoom()
436     {
437         Log.trace(Log.l1_User, "InfoBPartner.zoom");
438         Integer JavaDoc C_BPartner_ID = getSelectedRowKey();
439         if (C_BPartner_ID == null)
440             return;
441         MQuery query = new MQuery("C_BPartner");
442         query.addRestriction("C_BPartner_ID", MQuery.EQUAL, C_BPartner_ID);
443         zoom (123, query);
444     } // zoom
445

446     /**
447      * Has Zoom
448      * @return true
449      */

450     boolean hasZoom()
451     {
452         return true;
453     } // hasZoom
454

455     /**
456      * Customize
457      */

458     void customize()
459     {
460         Log.trace(Log.l1_User, "InfoBPartner.customize");
461     } // customize
462

463     /**
464      * Has Customize
465      * @return false
466      */

467     boolean hasCustomize()
468     {
469         return false; // for now
470
} // hasCustomize
471

472 } // InfoBPartner
473
Popular Tags