r7061: initial property settings
[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$
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 :accessor first-name
22                :value-constraint stringp :null-allowed nil)
23    (last-name :value-type (varchar 30) :initarg :last-name :accessor 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 :accessor dob :print-formatter format-date
28         :value-constraint integerp :input-filter convert-to-date)
29    (resume :value-type string :initarg :resume :accessor resume
30            :value-constraint stringp)
31 ;;   (addresses :value-type (list-of subobject) :initarg :addresses :accessor addresses))
32    (addresses :subobject t :initarg :addresses :accessor addresses))
33   (:metaclass hyperobject-class)
34   (:default-initargs :first-name "" :last-name "" :dob 0 :resume nil) 
35   (:default-print-slots first-name last-name dob resume)
36   (:user-name "Person")
37   (:user-name-plural "Persons")
38   (:description "A Person")
39   (:direct-rules
40    (:rule-1 (:dependants (last-name first-name) :volatile full-name)
41               (setf full-name (concatenate 'string first-name " " last-name)))))
42
43 (defun format-date (ut)
44   (when (typep ut 'integer)
45       (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
46           (decode-universal-time ut)
47         (declare (ignore daylight-p zone))
48         (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" 
49                 dow
50                 day
51                 (1- mon)
52                 year
53                 hr min sec))))
54
55 (defclass address (hyperobject)
56   ((title :value-type (varchar 20) :initarg :title :accessor title
57           :value-constraint stringp)
58    (street :value-type (varchar 30) :initarg :street :accessor street
59            :value-constraint stringp)
60    (phones :subobject t :initarg :phones :accessor phones))
61   (:metaclass hyperobject-class)
62   (:default-initargs :title nil :street nil) 
63   (:user-name "Address" "Addresses")
64   (:default-print-slots title street)
65   (:description "An address"))
66
67 (defclass phone (hyperobject)
68   ((title :value-type (varchar 20) :initarg :title :accessor title
69           :value-constraint stringp)
70    (phone-number :value-type (varchar 16) :initarg :phone-number :accessor phone-number
71                  :value-constraint stringp))
72   (:metaclass hyperobject-class)
73   (:user-name "Phone Number")
74   (:user-name-plural "Phone Numbers")
75   (:default-initargs :title nil :phone-number nil)
76   (:default-print-slots title phone-number)
77   (:description "A phone number"))
78
79
80 (defparameter home-phone-1 (make-instance 'phone :title "Voice" :phone-number "367-9812"))
81 (defparameter home-phone-2 (make-instance 'phone :title "Fax" :phone-number "367-9813"))
82
83 (defparameter office-phone-1 (make-instance 'phone :title "Main line" :phone-number "123-0001"))
84 (defparameter office-phone-2 (make-instance 'phone :title "Staff line" :phone-number "123-0002"))
85 (defparameter office-phone-3 (make-instance 'phone :title "Fax" :phone-number "123-0005"))
86
87 (defparameter home (make-instance 'address :title "Home" :street "321 Shady Lane"
88                                   :phones (list home-phone-1 home-phone-2)))
89
90 (defparameter office (make-instance 'address :title "Office" :street "113 Main St."
91                                     :phones (list office-phone-1 office-phone-2 office-phone-3)))
92
93                               
94 (defparameter mary (make-instance 'person :first-name "Mary" :last-name "Jackson"
95                             :dob (get-universal-time)
96                             :addresses (list home office)
97                             :resume "Style & Grace"))
98
99
100 (format t "~&Text Format~%")
101 (view mary :subobjects t)
102
103 (format t "~&XML Format with field labels and hyperlinks~%")
104 (view mary :subobjects t :category :xml-link-labels)