BrightSide Workbench Full Report + Source Code
ContactGroup.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 org.turro.contacts.Contact;
21 import org.turro.contacts.db.ContactsPU;
22 import org.turro.elephant.context.Application;
23 import org.zkoss.lang.Strings;
24 
29 public class ContactGroup implements java.io.Serializable {
30 
31  private static final long serialVersionUID = -1;
32 
33  private Application app = Application.getApplication();
34 
35  private String value;
36  private boolean role = true;
37 
38  public ContactGroup(Object object) {
39  setObject(object);
40  }
41 
42  public boolean isRole() {
43  return role;
44  }
45 
46  public void setRole(boolean role) {
47  this.role = role;
48  }
49 
50  public String getRealValue() {
51  if(Strings.isEmpty(value)) return null;
52  return (isRole() ? "*R*" : "") + value;
53  }
54 
55  public ContactGroup setRealValue(String value) {
56  role = false;
57  if(value != null && value.startsWith("*R*")) {
58  role = true;
59  value = value.substring(3);
60  }
61  this.value = value;
62  return this;
63  }
64 
65  public Object getObject() {
66  if(isRole()) {
67  return (String) value;
68  }
69  else {
70  if(value == null) return null;
71  return new ContactsPU().find(Contact.class, value);
72  }
73  }
74 
75  public void setObject(Object value) {
76  if(value instanceof Contact) {
77  this.value = ((Contact) value).getId();
78  setRole(false);
79  }
80  else {
81  this.value = (String) value;
82  setRole(true);
83  }
84  }
85 
86  public String getObjectString() {
87  if(isRole()) {
88  return (String) value;
89  }
90  else {
91  Contact o = (Contact) getObject();
92  if(o == null) return null;
93  return o.getName();
94  }
95  }
96 
97  public Object getValue() {
98  return getObjectString();
99  }
100 
101 }
ContactGroup setRealValue(String value)