00001 package edu.tum.cs.srldb; 00002 import java.io.Serializable; 00003 import java.sql.ResultSet; 00004 import java.sql.ResultSetMetaData; 00005 import java.sql.SQLException; 00006 import java.util.HashMap; 00007 import java.util.Iterator; 00008 import java.util.Map; 00009 import java.util.Set; 00010 import java.util.Map.Entry; 00011 00012 import edu.tum.cs.srldb.datadict.DDAttribute; 00013 import edu.tum.cs.srldb.datadict.DDException; 00014 import edu.tum.cs.srldb.datadict.domain.AutomaticDomain; 00015 import edu.tum.cs.srldb.datadict.domain.BooleanDomain; 00016 import edu.tum.cs.srldb.datadict.domain.Domain; 00017 00018 public abstract class Item implements Serializable { 00019 protected HashMap<String,String> attribs; 00020 protected static int GUID = 1; 00021 protected int id; 00022 protected Database database; 00024 protected boolean immutable = false; 00025 00026 public Item(Database database) { 00027 this.attribs = new HashMap<String, String>(); 00028 this.id = GUID++; 00029 this.database = database; 00030 } 00031 00032 public void addAttribsFromResultSet(ResultSet rs, boolean callNext) throws Exception { 00033 checkMutable(); 00034 boolean erroneous = false; 00035 try { 00036 ResultSetMetaData rsmd = rs.getMetaData(); 00037 int numCols = rsmd.getColumnCount(); 00038 if(callNext) 00039 if(!rs.next()) 00040 return; 00041 for(int i = 1; i <= numCols; i++) { 00042 String val = rs.getString(i); 00043 if(val == null) 00044 erroneous = true; 00045 addAttribute(rsmd.getColumnName(i), val); 00046 } 00047 } 00048 catch(SQLException e) { 00049 e.printStackTrace(); 00050 } 00051 if(erroneous) 00052 throw new Exception("Result set contains null entry!"); 00053 } 00054 00062 public void addAttribute(String attribute, String value) throws DDException { 00063 checkMutable(); 00064 DDAttribute attrib = database.getDataDictionary().getAttribute(attribute); 00065 if(attrib != null) { 00066 Domain domain = attrib.getDomain(); 00067 if(domain instanceof AutomaticDomain) 00068 ((AutomaticDomain)domain).addValue(value); 00069 } 00070 attribs.put(attribute, value); 00071 } 00072 00077 public void addAttributes(Map<String,String> attributes) throws DDException { 00078 checkMutable(); 00079 for(Entry<String,String> entry : attributes.entrySet()) { 00080 addAttribute(entry.getKey(), entry.getValue()); 00081 } 00082 } 00083 00084 protected void checkMutable() throws DDException { 00085 if(immutable) 00086 throw new DDException("This object can no longer be modified, probably because it has already been committed."); 00087 } 00088 00092 public void print() { 00093 Set<String> keys = attribs.keySet(); 00094 for(Iterator<String> i = keys.iterator(); i.hasNext();) { 00095 String key = i.next(); 00096 String value = attribs.get(key); 00097 System.out.println(key + ": " + value); 00098 } 00099 } 00100 00101 public int getInt(String attrName) { 00102 return Integer.parseInt(attribs.get(attrName)); 00103 } 00104 00105 public double getDouble(String attrName) { 00106 return Double.parseDouble(attribs.get(attrName)); 00107 } 00108 00109 public String getString(String attrName) { 00110 return attribs.get(attrName); 00111 } 00112 00113 public Database getDatabase() { 00114 return database; 00115 } 00116 00117 public Map<String,String> getAttributes() { 00118 return attribs; 00119 } 00120 00121 public Set<String> getAttributeNames() { 00122 return attribs.keySet(); 00123 } 00124 00125 public String getAttributeValue(String attribName) { 00126 return getString(attribName); 00127 } 00128 00129 public boolean getBoolean(String attribName) { 00130 return BooleanDomain.getInstance().isTrue(getString(attribName)); 00131 } 00132 00133 public boolean hasAttribute(String attribName) { 00134 return attribs.containsKey(attribName); 00135 } 00136 00137 public int getGUID() { 00138 return this.id; 00139 } 00140 }