92d96d2dd41482c0e3e5b28c20aa2d0ec1a31ff1
[plcapi.git] / PLC / NodeTagTypes.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 # $Revision: 9423 $
5 #
6 from types import StringTypes
7
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Roles import Role, Roles
13
14 class NodeTagType (Row):
15
16     """
17     Representation of a row in the node_tag_types table.
18     """
19
20     table_name = 'node_tag_types'
21     primary_key = 'node_tag_type_id'
22     join_tables = ['node_tag']
23     fields = {
24         'node_tag_type_id': Parameter(int, "Node tag type identifier"),
25         'tagname': Parameter(str, "Node tag type name", max = 100),
26         'description': Parameter(str, "Node tag type description", max = 254),
27         'category' : Parameter (str, "Node tag category", max=64, optional=True),
28         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
29         }
30
31     def validate_name(self, name):
32         if not len(name):
33             raise PLCInvalidArgument, "node tag type name must be set"
34
35         conflicts = NodeTagTypes(self.api, [name])
36         for tag_type in conflicts:
37             if 'node_tag_type_id' not in self or \
38                    self['node_tag_type_id'] != tag_type['node_tag_type_id']:
39                 raise PLCInvalidArgument, "node tag type name already in use"
40
41         return name
42
43     def validate_min_role_id(self, role_id):
44         roles = [row['role_id'] for row in Roles(self.api)]
45         if role_id not in roles:
46             raise PLCInvalidArgument, "Invalid role"
47
48         return role_id
49
50 class NodeTagTypes(Table):
51     """
52     Representation of row(s) from the node_tag_types table
53     in the database.
54     """
55
56     def __init__(self, api, node_tag_type_filter = None, columns = None):
57         Table.__init__(self, api, NodeTagType, columns)
58
59         sql = "SELECT %s FROM node_tag_types WHERE True" % \
60               ", ".join(self.columns)
61
62         if node_tag_type_filter is not None:
63             if isinstance(node_tag_type_filter, (list, tuple, set)):
64                 # Separate the list into integers and strings
65                 ints = filter(lambda x: isinstance(x, (int, long)), node_tag_type_filter)
66                 strs = filter(lambda x: isinstance(x, StringTypes), node_tag_type_filter)
67                 node_tag_type_filter = Filter(NodeTagType.fields, {'node_tag_type_id': ints, 'tagname': strs})
68                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "OR")
69             elif isinstance(node_tag_type_filter, dict):
70                 node_tag_type_filter = Filter(NodeTagType.fields, node_tag_type_filter)
71                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
72             elif isinstance (node_tag_type_filter, StringTypes):
73                 node_tag_type_filter = Filter(NodeTagType.fields, {'tagname':[node_tag_type_filter]})
74                 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
75             else:
76                 raise PLCInvalidArgument, "Wrong node tag type filter %r"%node_tag_type_filter
77
78         self.selectall(sql)