# -*- coding: UTF-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Version: $Id$

from sqlalchemy import *
from gnuviechadmin import gva_cfg, exceptions

meta = BoundMetaData(gva_cfg.GVA_DBE)
client_table = Table('clients', meta,
                     Column('clientid', Integer, primary_key=True),
                     Column('title', String(10)),
                     Column('firstname', String(64), nullable=False),
                     Column('lastname', String(64), nullable=False),
                     Column('address1', String(64), nullable=False),
                     Column('address2', String(64)),
                     Column('zip', String(7), nullable=False),
                     Column('city', String(64), nullable=False),
                     Column('country', String(5), nullable=False),
                     Column('phone', String(32), nullable=False),
                     Column('mobile', String(32)),
                     Column('fax', String(32)),
                     Column('email', String(64), unique=True, nullable=False))
client_table.create(checkfirst=True)

class Client(object):
    mandatory = ('firstname', 'lastname', 'address1', 'zip', 'city',
                 'country', 'phone', 'email')

    """This class provides a client representation"""
    def __init__(self, **kwargs):
        self.clientid = None
        self.country = 'de'
        self.title = None
        self.address2 = None
        self.mobile = None
        self.fax = None
        self.organisation = None
        for key in kwargs.keys():
            self.__setattr__(key, kwargs[key])
        self.validate()

    def validate(self):
        missingfields = []
        for key in self.mandatory:
            if self.__getattribute__(key) is None:
                missingfields.append(key)
        if missingfields:
            raise exceptions.MissingFieldsError(missingfields)

    def __repr__(self):
         return """Client (Id %(clientid)d):
 Name:    %(title)s %(firstname)s %(lastname)s
 Address: %(address1)s
          %(address2)s
          %(zip)s %(city)s (%(country)s)
 Phone:   %(phone)s
 Mobile:  %(mobile)s
 Fax:     %(fax)s
 Email:   %(email)s""" % ({'clientid'  : self.clientid,
                           'title'     : self.title,
                           'firstname' : self.firstname,
                           'lastname'  : self.lastname,
                           'address1'  : self.address1,
                           'address2'  : self.address2,
                           'zip'       : self.zip,
                           'city'      : self.city,
                           'country'   : self.country,
                           'phone'     : self.phone,
                           'mobile'    : self.mobile,
                           'fax'       : self.fax,
                           'email'     : self.email})

clientmapper = mapper(Client, client_table)