5 (defparameter *default-type
* :ucw
)
7 ;;; maps meta-model slot-types to slot-presentation
8 (defparameter *slot-type-map
* '(number ucw
:currency
))
10 ;;; an alist of model-class-name . attributes
11 ;;; should really be a hash-table.
12 (defvar *attribute-map
* (list))
14 ;;; some utilities for merging plists
16 (defun plist-nunion (new-props plist
)
17 (loop for cons on new-props
18 for i from
1 to
(length new-props
)
20 do
(setf (getf plist
(first cons
)) (second cons
))
21 finally
(return plist
)))
23 (defun plist-union (new-props plist
)
24 "Non-destructive version of plist-nunion"
25 (plist-nunion new-props
(copy-list plist
)))
27 (defun gen-ptype (type)
28 (or (getf *slot-type-map
* type
) type
))
30 (defun gen-presentation-slots (instance)
31 (mapcar #'(lambda (x) (gen-pslot (cadr x
)
34 (list-slot-types instance
)))
37 (defun gen-pslot (type label slot-name
)
38 (copy-list `(,(gen-ptype type
)
40 :slot-name
,slot-name
)))
42 (defun gen-presentation-args (instance args
)
43 (declare (ignore instance
))
47 (defun find-or-create-attributes (class-name)
48 "return an exisiting class attribute map or create one.
50 A map is a cons of class-name . attributes.
51 attributes is an alist keyed on the attribute nreeame."
52 (or (assoc class-name
*attribute-map
*)
54 (setf *attribute-map
* (acons class-name
(list (list)) *attribute-map
*))
55 (assoc class-name
*attribute-map
*))))
57 (defgeneric find-class-attributes
(class))
59 (defmethod find-class-attributes ((model t
))
60 (find-or-create-attributes (class-name (class-of model
))))
62 (defmethod find-class-attributes ((model symbol
))
63 (find-or-create-attributes model
))
65 (defmethod add-attribute ((model t
) name def
)
66 (let ((map (find-class-attributes model
)))
67 (setf (cdr map
) (acons name def
(cdr map
)))))
69 (defmethod find-attribute ((model t
) name
)
70 (assoc name
(cdr (find-class-attributes model
))))
72 (defmethod (setf find-attribute
) ((def list
) (model t
) name
)
73 (let ((attr (find-attribute model name
)))
79 (add-attribute model name def
)
80 (find-attribute model name
)))))
82 (defmethod set-attribute ((model t
) name definition
&key
(inherit t
))
83 (setf (find-attribute model name
)
85 (cons (car definition
)
86 (plist-union (cdr definition
)
87 (cddr (find-attribute model name
))))
91 (defgeneric attributes-getter
(model))
99 :accessor attributes-getter
100 :initform
#'get-attributes
101 :initarg
:attributes-getter
)
103 :initarg
:global-properties
104 :accessor global-properties
110 (use-instance-class-p
111 :initarg
:use-instance-class-p
112 :accessor use-instance-class-p
114 (initializedp :initform nil
)
115 (modifiedp :accessor modifiedp
:initform nil
)))
117 (defcomponent mewa-object-presentation
(mewa object-presentation
) ())
119 (defcomponent mewa-one-line-presentation
(mewa one-line-presentation
)
121 (:default-initargs
:attributes-getter
#'one-line-attributes-getter
))
123 (defmethod attributes :around
((self mewa
))
124 (let ((a (call-next-method)))
125 (or a
(funcall (attributes-getter self
) self
))))
127 (defmethod get-attributes ((self mewa
))
129 (append (meta-model:list-slots
(instance self
))
130 (meta-model:list-has-many
(instance self
)))
133 (defmethod one-line-attributes-getter ((self mewa
))
134 (or (meta-model:list-keys
(instance self
))))
138 (defmethod find-instance-classes ((self mewa
))
140 (it.bese.arnesi.mopp
:compute-class-precedence-list
(class-of (instance self
)))))
142 (defmethod find-all-attributes ((self mewa
))
144 (mapcar #'(lambda (x)
145 (cdr (find-class-attributes x
)))
148 (defun make-attribute (&rest props
&key type
&allow-other-keys
)
150 (cons (gensym) (cons type props
)))
153 (defmethod find-applicable-attributes ((self mewa
))
154 (let ((all-attributes (find-all-attributes self
)))
155 (flet ((gen-att (x) (let ((att (assoc x all-attributes
)))
157 (setf (cddr att
) (plist-union (global-properties self
) (cddr att
)))
159 (if (attributes self
)
161 (mapcar #'(lambda (x)
166 ;;if the car is a keyword then this is an inline def
167 ((and (listp x
) (keywordp (car x
)))
168 (let ((att (apply #'make-attribute x
)))
170 (plist-union (cddr att
) (global-properties self
)))
172 ;; if the plist has a :type
173 ((and (listp x
) (getf (cdr x
) :type
))
174 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
175 (def (gen-att (car x
))))
176 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
177 (cons (car def
) new
)))
178 ;;finally if we are just overiding the props
179 ((and (listp x
) (symbolp (car x
)))
180 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
181 (def (gen-att (car x
))))
182 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
183 (cons (car def
) (cons (second def
) (cdr new
)))))
191 (defmethod find-slot-presentations ((self mewa
))
192 (mapcar #'(lambda (s)
193 (let ((class-name (or (gethash (second s
) ucw
::*slot-type-mapping
*) 'mewa-object-presentation
)))
194 (apply #'make-instance
196 (append (cddr s
) (list :parent self
)))))
197 (find-applicable-attributes self
)))
199 (defmethod default-attributes ((model t
))
200 (append (mapcar #'(lambda (s) (cons (car s
) (gen-pslot (if (meta-model:foreign-key-p model
(car s
))
203 (string (car s
)) (car s
))))
204 (meta-model:list-slot-types model
))
205 (mapcar #'(lambda (s) (cons s
(append (gen-pslot 'ucw
::has-many
(string s
) s
) `(:presentation
(make-presentation ,model
:type
:one-line
)))))
206 (meta-model:list-has-many model
))))
208 (defmethod set-default-attributes ((model t
))
209 (mapcar #'(lambda (x)
210 (setf (find-attribute model
(car x
)) (cdr x
)))
211 (default-attributes model
)))
214 (defcomponent mewa-object-presentation
(mewa ucw
:object-presentation
) ())
216 (defcomponent mewa-list-presentation
(mewa ucw
:list-presentation
)
217 ((it.bese.ucw
::instances
:accessor instances
:initarg
:instances
:initform nil
)
218 (instance :accessor instance
))) ;to make make-presentation happy
220 (defmethod get-all-instances ((self mewa-list-presentation
))
226 (defmethod initialize-slots ((self mewa
))
227 (when (use-instance-class-p self
)
229 (append (find-instance-classes self
)
231 (setf (slots self
) (find-slot-presentations self
)))
234 (defmethod render-on :around
((res response
) (self mewa
))
235 (unless (slot-value self
'initializedp
)
236 (initialize-slots self
))
237 (setf (slot-value self
'initializedp
) t
)
241 (defmethod make-presentation ((object t
) &key
(type :viewer
) (initargs nil
))
242 (let* ((p (make-instance 'mewa-object-presentation
))
243 (a (progn (setf (slot-value p
'instance
) object
)
245 (assoc type
(find-all-attributes p
))))
247 (i (apply #'make-instance
(second a
) (plist-union initargs
(cddr a
)))))
248 (setf (slot-value i
'instance
) object
)
251 (defmethod call-component :before
((from standard-component
) (to mewa
))
252 (unless (slot-value to
'initializedp
)
253 (initialize-slots to
))
254 (setf (slot-value to
'initializedp
) t
)
255 (setf (slots to
) (mapcar #'(lambda (x) (prog2
256 (setf (component.place x
) (component.place from
))
260 (defmacro call-presentation
(object &rest args
)
261 `(present-object ,object
:presentation
(make-presentation ,object
,@args
)))