- implement interface to address_types table
[plcapi.git] / PLC / AddressTypes.py
index e94f086..4f6fe76 100644 (file)
@@ -4,24 +4,83 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id$
+# $Id: AddressTypes.py,v 1.1 2006/09/06 15:36:06 mlhuang Exp $
 #
 
+from types import StringTypes
+from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Table import Row, Table
 
-class AddressTypes(dict):
+class AddressType(Row):
     """
-    Representation of the address_types table in the database.
+    Representation of a row in the address_types table. To use,
+    instantiate with a dict of values.
     """
 
+    table_name = 'address_types'
+    primary_key = 'address_type_id'
     fields = {
         'address_type_id': Parameter(int, "Address type identifier"),
-        'name': Parameter(str, "Address type name"),
+        'name': Parameter(str, "Address type", max = 20),
+        'description': Parameter(str, "Address type description", max = 254),
         }
 
-    def __init__(self, api):
-        sql = "SELECT address_type_id, name FROM address_types"
+    def __init__(self, api, fields = {}):
+        Row.__init__(self, fields)
+        self.api = api
+
+    def validate_name(self, name):
+       # Remove leading and trailing spaces
+       name = name.strip()
+
+       # Make sure name is not blank after we removed the spaces
+        if not name:
+            raise PLCInvalidArgument, "Address type must be specified"
+       
+       # Make sure node group does not alredy exist
+       conflicts = AddressTypes(self.api, [name])
+       for address_type_id in conflicts:
+            if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
+               raise PLCInvalidArgument, "Address type name already in use"
+
+       return name
+
+    def delete(self, commit = True):
+        assert 'address_type_id' in self
+
+        # Clean up miscellaneous join tables
+        for table in ['address_address_type', 'address_types']:
+            self.api.db.do("DELETE FROM %s" \
+                           " WHERE address_type_id = %d" % \
+                           (table, self['address_type_id']), self)
+
+        if commit:
+            self.api.db.commit()
+        
+class AddressTypes(Table):
+    """
+    Representation of the address_types table in the database.
+    """
+
+    def __init__(self, api, address_type_id_or_name_list = None):
+        sql = "SELECT %s FROM address_types" % \
+              ", ".join(AddressType.fields)
+        
+        if address_type_id_or_name_list:
+            # Separate the list into integers and strings
+            address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)),
+                                   address_type_id_or_name_list)
+            names = filter(lambda name: isinstance(name, StringTypes),
+                           address_type_id_or_name_list)
+            sql += " WHERE (False"
+            if address_type_ids:
+                sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids))
+            if names:
+                sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
+            sql += ")"
+
+        rows = api.db.selectall(sql)
 
-        for row in api.db.selectall(sql):
-            self[row['address_type_id']] = row['name']
-            self[row['name']] = row['address_type_id']
+        for row in rows:
+            self[row['address_type_id']] = AddressType(api, row)