BrightSide Workbench Full Report + Source Code
bserp-www/src/main/java/org/turro/erp/humanres/HumanResourceCombobox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.erp.humanres;
19 
20 import java.util.LinkedList;
21 import java.util.List;
22 import javax.persistence.EntityManager;
23 import javax.persistence.Query;
24 import org.turro.elephant.db.SQLHelper;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.erp.db.ErpPU;
27 import org.turro.erp.entity.HumanResource;
28 import org.turro.hr.humanres.HumanResources;
29 import org.turro.zkoss.input.GenericCombobox;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zk.ui.ext.AfterCompose;
33 
38 public class HumanResourceCombobox extends GenericCombobox<HumanResource> implements AfterCompose {
39 
40  private boolean onlyActive = true;
41  private boolean selectCurrent = false;
42 
43  public boolean isOnlyActive() {
44  return onlyActive;
45  }
46 
47  public void setOnlyActive(boolean onlyActive) {
48  this.onlyActive = onlyActive;
49  }
50 
51  public boolean isSelectCurrent() {
52  return selectCurrent;
53  }
54 
55  public void setSelectCurrent(boolean selectCurrent) {
56  this.selectCurrent = selectCurrent;
57  }
58 
59  @Override
60  public void populateList(String value, LinkedList list, int nRows) {
61  EntityManager em = new ErpPU().getEntityManager();
62  try {
63  WhereClause wc = SQLHelper.getWhereClause(new String[]{
64  "res.name"
65  }, value);
66  Query q = em.createQuery(
67  "select res from HumanResource res " +
68  "where active = :active " +
69  wc.getClause() +
70  " order by res.name"
71  );
72  wc.addNamedValue("active", onlyActive);
73  q.setMaxResults(nRows);
74  wc.setNamedParameters(q);
75  List<HumanResource> l = q.getResultList();
76  for(Object o : l) {
77  list.add(((HumanResource) o).getName());
78  }
79  } finally {
80  em.close();
81  }
82  }
83 
84  @Override
85  public String getTextFromObject(HumanResource value) {
86  return value.getName();
87  }
88 
89  @Override
90  public void afterCompose() {
91  if(selectCurrent) {
93  Events.postEvent(new Event(Events.ON_CHANGE, this));
94  }
95  }
96 
97 }
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
void addNamedValue(String name, Object value)
EntityManager getEntityManager()
Definition: Dao.java:202