KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > manentia > kasai > Group


1 package org.manentia.kasai;
2
3 import java.io.StringReader JavaDoc;
4 import java.sql.ResultSet JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.logging.Level JavaDoc;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10
11 import org.apache.commons.lang.exception.ExceptionUtils;
12 import org.manentia.kasai.util.Constants;
13 import org.manentia.kasai.exceptions.InvalidAttributesException;
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16 import org.xml.sax.InputSource JavaDoc;
17
18 import com.koala.commons.log.Log;
19 import com.koala.commons.xml.exceptions.XMLException;
20
21 /**
22  *
23  * @author fpena
24  *
25  */

26
27 public class Group{
28     private String JavaDoc id;
29
30     private boolean blocked;
31
32     private String JavaDoc description;
33
34     private Document JavaDoc data;
35
36     private String JavaDoc dataStr;
37
38     private boolean system;
39     
40     
41     public Group() {
42     }
43
44     public Group (ResultSet JavaDoc rs) throws SQLException JavaDoc{
45         id = rs.getString("id");
46         blocked = (rs.getInt("blocked")!=0);
47         description = rs.getString("description");
48         system = (rs.getInt("system")!=0);
49     }
50     
51     public boolean getBlocked() {
52         return this.blocked;
53     }
54
55     public void setBlocked(boolean blocked) {
56         this.blocked = blocked;
57     }
58
59     public String JavaDoc getDescription() {
60         return this.description;
61     }
62
63     public void setDescription(String JavaDoc description) {
64         this.description = description;
65     }
66
67     public String JavaDoc getId() {
68         return this.id;
69     }
70
71     public void setId(String JavaDoc id) {
72         this.id = id;
73     }
74
75     public void setValue(String JavaDoc key, String JavaDoc value) throws XMLException {
76         DocumentBuilder JavaDoc docBuilder;
77         Element JavaDoc root;
78
79         try {
80             loadData();
81
82             if ((key != null) && (value != null)) {
83                 this.data.getDocumentElement().setAttribute(key, value);
84                 this.dataStr = this.data.getDocumentElement().toString();
85             }
86         } catch (Exception JavaDoc e) {
87             Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(), "setValue",
88                 "Error modifing value (" + ExceptionUtils.getStackTrace(e) +
89                 ")", java.util.logging.Level.SEVERE);
90             throw new XMLException(KasaiFacade.class.getName() + ".xmlError", e);
91         }
92     }
93
94     public String JavaDoc getValue(String JavaDoc key) throws XMLException {
95         String JavaDoc value = null;
96         DocumentBuilder JavaDoc docBuilder;
97         Element JavaDoc root;
98
99         try {
100             if (key != null) {
101                 loadData();
102                 value = this.data.getDocumentElement().getAttribute(key);
103             }
104         } catch (Exception JavaDoc e) {
105             Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(), "getValue",
106                 "Error getting value (" + ExceptionUtils.getStackTrace(e) +
107                 ")", java.util.logging.Level.SEVERE);
108
109             throw new XMLException(KasaiFacade.class.getName() + ".xmlError", e);
110         }
111
112         return value;
113     }
114
115     public String JavaDoc getDataStr() {
116         return this.dataStr;
117     }
118
119     public void setDataStr(String JavaDoc dataStr) {
120         this.dataStr = dataStr;
121     }
122
123     private void loadData() throws XMLException {
124         DocumentBuilder JavaDoc docBuilder;
125         Element JavaDoc root;
126
127         try {
128             if ((this.dataStr == null) || (this.dataStr.length()<=0)) {
129                 docBuilder = DocumentBuilderFactory.newInstance()
130                                                    .newDocumentBuilder();
131                 data = docBuilder.newDocument();
132                 root = data.createElement("data");
133                 data.appendChild(root);
134             } else {
135                 if (data == null) {
136                     try {
137                         docBuilder = DocumentBuilderFactory.newInstance()
138                                                            .newDocumentBuilder();
139                         this.data = docBuilder.parse(new InputSource JavaDoc(
140                                     new StringReader JavaDoc(this.dataStr)));
141                     } catch (Exception JavaDoc e) {
142                         Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(),
143                             "loadData",
144                             "Error parsing xml document(" +
145                             ExceptionUtils.getStackTrace(e) + ")",
146                             java.util.logging.Level.SEVERE);
147
148                         throw new XMLException(KasaiFacade.class.getName() + ".xmlError", e);
149                     }
150                 }
151             }
152         } catch (Exception JavaDoc e) {
153             Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(), "loadData",
154                 "Error loading data (" + ExceptionUtils.getStackTrace(e) +
155                 ")", java.util.logging.Level.SEVERE);
156
157             throw new XMLException("Internal error", e);
158         }
159     }
160     
161     public void validate() throws InvalidAttributesException{
162         Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(),"validate","Enter",Level.INFO);
163         
164         if ((this.getId() == null) || (this.getId().length()==0)){
165             Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(), "validate","Id was not specified",Level.WARNING);
166             throw new InvalidAttributesException(Group.class.getName() + ".emptyId");
167         }
168         
169         Log.getInstance(Constants.PROPERTY_FILE).write(Group.class.getName(),"validate","Exit",Level.INFO);
170     }
171     
172     public boolean equals (java.lang.Object JavaDoc obj){
173         boolean result = false;
174         
175         try{
176             if (obj instanceof Group){
177                 if (((Group)obj).getId().equals (this.id)){
178                     result = true;
179                 }
180             }
181         }
182         catch (Exception JavaDoc e){
183             result = false;
184         }
185         return result;
186     }
187     
188     public boolean getSystem() {
189         return this.system;
190     }
191     
192     public void setSystem(boolean system) {
193         this.system = system;
194     }
195     
196 }
Popular Tags