1 package org.manentia.kasai; 2 3 import java.io.StringReader ; 4 import java.sql.ResultSet ; 5 import java.sql.SQLException ; 6 import java.util.logging.Level ; 7 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 15 import org.w3c.dom.Element ; 16 import org.xml.sax.InputSource ; 17 18 import com.koala.commons.log.Log; 19 import com.koala.commons.xml.exceptions.XMLException; 20 21 26 27 public class Group{ 28 private String id; 29 30 private boolean blocked; 31 32 private String description; 33 34 private Document data; 35 36 private String dataStr; 37 38 private boolean system; 39 40 41 public Group() { 42 } 43 44 public Group (ResultSet rs) throws SQLException { 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 getDescription() { 60 return this.description; 61 } 62 63 public void setDescription(String description) { 64 this.description = description; 65 } 66 67 public String getId() { 68 return this.id; 69 } 70 71 public void setId(String id) { 72 this.id = id; 73 } 74 75 public void setValue(String key, String value) throws XMLException { 76 DocumentBuilder docBuilder; 77 Element 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 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 getValue(String key) throws XMLException { 95 String value = null; 96 DocumentBuilder docBuilder; 97 Element root; 98 99 try { 100 if (key != null) { 101 loadData(); 102 value = this.data.getDocumentElement().getAttribute(key); 103 } 104 } catch (Exception 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 getDataStr() { 116 return this.dataStr; 117 } 118 119 public void setDataStr(String dataStr) { 120 this.dataStr = dataStr; 121 } 122 123 private void loadData() throws XMLException { 124 DocumentBuilder docBuilder; 125 Element 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 ( 140 new StringReader (this.dataStr))); 141 } catch (Exception 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 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 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 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 |