63a174b4f9eec111ab77d61c5a3fc3c54a166751
[hyperobject.git] / examples / person.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          hyperobject-example.lisp
6 ;;;; Purpose:       Hyperobject Example file
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Oct 2002
9 ;;;;
10 ;;;; A simple example file for hyperobjects
11 ;;;;
12 ;;;; $Id: person.lisp,v 1.3 2002/12/13 22:15:30 kevin Exp $
13 ;;;;
14 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
15 ;;;;
16 ;;;; *************************************************************************
17  
18 (in-package :hyperobject-user)
19
20 (defclass person (hyperobject)
21   ((first-name :value-type (varchar 20) :initarg :first-name :reader first-name
22                :value-constraint stringp :null-allowed nil)
23    (last-name :value-type (varchar 30) :initarg :last-name :reader last-name
24               :value-constraint stringp
25               :hyperlink find-person-by-last-name :null-allowed nil)
26    (full-name :value-type string :stored nil)
27    (dob :value-type integer :initarg :dob :reader dob :print-formatter format-date
28         :value-constraint integerp :input-filter convert-to-date)
29    (resume :value-type string :initarg :resume :reader resume
30            :value-constraint stringp)
31    (addresses :value-type (list-of subobject) :initarg :addresses :reader addresses))
32   (:metaclass hyperobject-class)
33   (:default-initargs :first-name "" :last-name "" :dob 0 :resume nil) 
34   (:default-print-slots first-name last-name dob resume)
35   (:user-name "Person")
36   (:description "A Person")
37   (:direct-rules
38    ((:rule-1 (:depends-on '(last-name first-name))
39               (setf full-name (concatentate 'string first-name " " last-name))))))
40
41 (defun format-date (ut)
42   (when (typep ut 'integer)
43       (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
44           (decode-universal-time ut)
45         (declare (ignore daylight-p zone))
46         (format nil "~[Mon~;Tue~;Wed~;Thu~;Fri~;Sat~;Sun~], ~d ~[Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~d ~2,'0d:~2,'0d:~2,'0d" 
47                 dow
48                 day
49                 (1- mon)
50                 year
51                 hr min sec))))
52
53 (defclass address (hyperobject)
54   ((title :value-type (varchar 20) :initarg :title :reader title
55           :value-constraint stringp)
56    (street :value-type (varchar 30) :initarg :street :reader street
57            :value-constraint stringp)
58    (phones :value-type (list-oj subobject) :initarg :phones :reader phones)
59   (:metaclass hyperobject-class)
60   (:default-initargs :title nil :street nil) 
61   (:user-name "Address")
62   (:default-print-slots title street)
63   (:description "An address"))
64
65 (defclass phone (hyperobject)
66   ((title :value-type (varchar 20) :initarg :title :reader title
67           :value-constraint stringp)
68    (phone-number :value-type (varchar 16) :initarg :phone-number :reader phone-number
69                  :value-constraint stringp))
70   (:metaclass hyperobject-class)
71   (:user-name "Phone Number")
72   (:default-initargs :title nil :phone-number nil)
73   (:default-print-slots title phone-number)
74   (:description "A phone number"))
75
76
77 (defparameter home-phone-1 (make-instance 'phone :title "Voice" :phone-number "367-9812"))
78 (defparameter home-phone-2 (make-instance 'phone :title "Fax" :phone-number "367-9813"))
79
80 (defparameter office-phone-1 (make-instance 'phone :title "Main line" :phone-number "123-0001"))
81 (defparameter office-phone-2 (make-instance 'phone :title "Staff line" :phone-number "123-0002"))
82 (defparameter office-phone-3 (make-instance 'phone :title "Fax" :phone-number "123-0005"))
83
84 (defparameter home (make-instance 'address :title "Home" :street "321 Shady Lane"
85                                   :phones (list home-phone-1 home-phone-2)))
86
87 (defparameter office (make-instance 'address :title "Office" :street "113 Main St."
88                                     :phones (list office-phone-1 office-phone-2 office-phone-3)))
89
90                               
91 (defparameter mary (make-instance 'person :first-name "Mary" :last-name "Jackson"
92                             :dob (get-universal-time)
93                             :addresses (list home office)
94                             :resume "Style & Grace"))
95
96
97 (format t "~&Text Format~%")
98 (view mary :subobjects t)
99
100 (format t "~&XML Format with field labels and hyperlinks~%")
101 (view mary :subobjects t :category :xml-link-labels)