BrightSide Workbench Full Report + Source Code
SaleProspectCombobox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.crm.zul.sale;
20 
21 import java.util.LinkedList;
22 import org.turro.string.Strings;
23 import org.turro.crm.db.CrmPU;
24 import org.turro.crm.entity.SaleProspect;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.util.Chars;
27 import org.turro.zkoss.input.GenericCombobox;
28 
33 public class SaleProspectCombobox extends GenericCombobox<SaleProspect> {
34 
35  @Override
36  public void populateList(String value, LinkedList list, int nRows) {
37  WhereClause wc = new WhereClause();
38  wc.addClause("select distinct sp from SaleProspect as sp");
39  wc.addClause("where 1=1");
40  long id = getIdFromText(value);
41  if(id > 0) {
42  wc.addClause("and sp.id = :id");
43  wc.addNamedValue("id", id);
44  }
45  if(!Strings.isBlank(value)) {
46  wc.addClause("and (");
47  wc.setPrefix("");
48  wc.addLikeFields(new String[] {"sp.description"}, getDescriptionFromText(value));
49  wc.setPrefix("or");
50  wc.addLikeFields(new String[] {"sp.customer.name"}, getNameFromText(value));
51  wc.setPrefix(null);
52  wc.addClause(")");
53  }
54  list.addAll(new CrmPU().getResultList(wc, nRows));
55  }
56 
57  @Override
58  public String getTextFromObject(SaleProspect value) {
59  return Strings.truncateAndWarn(value.getDescription(), 50) +
60  Chars.forward().spaced().toString() +
61  value.getCustomer().getName() +
62  Chars.forward().spaced().toString() +
63  value.getId();
64  }
65 
66  public String getDescriptionFromText(String text) {
67  String[] s = text.split(Chars.forward().regexp().spaced().toString());
68  if(s.length > 0) {
69  return s[0];
70  }
71  return null;
72  }
73 
74  public String getNameFromText(String text) {
75  String[] s = text.split(Chars.forward().regexp().spaced().toString());
76  if(s.length > 1) {
77  return s[1];
78  } else if(s.length > 0) {
79  return s[0];
80  }
81  return null;
82  }
83 
84  public long getIdFromText(String text) {
85  long id = 0;
86  int p = text.lastIndexOf(Chars.forward().spaced().toString());
87  try {
88  if(p > -1) {
89  id = Long.valueOf(text.substring(p + 1));
90  } else {
91  id = Long.valueOf(text);
92  }
93  } catch(Exception ex) {}
94  return id;
95  }
96 
97 }
void populateList(String value, LinkedList list, int nRows)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)