BrightSide Workbench Full Report + Source Code
ContactComboModel.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.contacts.util;
19 
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.jpa.Dao;
29 import org.zkoss.lang.Strings;
30 import org.zkoss.zul.AbstractListModel;
31 import org.zkoss.zul.ListModel;
32 import org.zkoss.zul.ListSubModel;
33 import org.zkoss.zul.event.ListDataEvent;
34 import org.zkoss.zul.ext.Sortable;
35 
40 public class ContactComboModel extends AbstractListModel implements Sortable<Contact>, ListSubModel, java.io.Serializable {
41 
42  private static final long serialVersionUID = 20081029L;
43 
44  private Application app = Application.getApplication();
45 
46  private boolean onlyUsers = false;
47  private Object[] data;
48  private String roleIn;
49  private Object lastValue;
50 
51  public ContactComboModel(Object[] data, String roleIn) {
52  this.data = data;
53  this.roleIn = roleIn;
54  onlyUsers = !Strings.isEmpty(roleIn);
55  }
56 
57  public ContactComboModel(List data, String roleIn) {
58  this.data = data.toArray(new Object[data.size()]);
59  this.roleIn = roleIn;
60  onlyUsers = !Strings.isEmpty(roleIn);
61  }
62 
63  @Override
64  public Object getElementAt(int index) {
65  if(data == null || data.length <= index) return null;
66  return data[index];
67  }
68 
69  @Override
70  public int getSize() {
71  return data.length;
72  }
73 
74  @Override
75  public void sort(Comparator cmpr, boolean ascending) {
76  Arrays.sort(data, cmpr);
77  fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
78  }
79 
80  @Override
81  public String getSortDirection(Comparator<Contact> cmpr) {
82  return "natural";
83  }
84 
85  @Override
86  public ListModel getSubModel(Object value, int nRows) {
87  if(value != null && value.equals(lastValue)) return this;
88  lastValue = value;
89  LinkedList lData = new LinkedList();
90  Dao dao = new ContactsPU();
91  WhereClause wc = new WhereClause();
92  wc.addClause("select contact from Contact as contact");
93  wc.addClause("where 1=1");
94  if(onlyUsers) {
95  wc.addClause("and contact.login is not null");
96  }
97  if(roleIn != null) {
98  wc.addClause(roleIn);
99  }
100  wc.addLikeFields(new String[]{ "contact.name" }, value.toString());
101  wc.addClause("order by contact.name");
102  if(nRows < 1) nRows = 10;
103  for(Object o : dao.getResultList(wc, nRows)) {
104  Contact c = (Contact) o;
105  if(!onlyUsers || (onlyUsers && c.isWebUser())) {
106  lData.add(c.getName());
107  }
108  }
109  return new ContactComboModel(lData, roleIn);
110  }
111 
112  public boolean isOnlyUsers() {
113  return onlyUsers;
114  }
115 
116  public void setOnlyUsers(boolean onlyUsers) {
117  this.onlyUsers = onlyUsers;
118  }
119 
120 }
ContactComboModel(Object[] data, String roleIn)
void sort(Comparator cmpr, boolean ascending)
String getSortDirection(Comparator< Contact > cmpr)
ListModel getSubModel(Object value, int nRows)
void addLikeFields(String[] fields, String value)