3 (defparameter *default-type
* :ucw
)
5 ;;; some utilities for merging plists
7 (defun plist-nunion (new-props plist
)
8 (loop for cons on new-props by
#'cddr
9 do
(setf (getf plist
(first cons
)) (second cons
))
10 finally
(return plist
)))
12 (defun plist-union (new-props plist
)
13 "Non-destructive version of plist-nunion"
14 (plist-nunion new-props
(copy-list plist
)))
17 ;;; an alist of model-class-name . attributes
18 ;;; should really be a hash-table.
19 (defvar *attribute-map
* (list))
21 (defun find-or-create-attributes (class-name)
22 "return an exisiting class attribute map or create one.
24 A map is a cons of class-name . attributes.
25 attributes is an alist keyed on the attribute name."
26 (or (assoc class-name
*attribute-map
*)
28 (setf *attribute-map
* (acons class-name
(list (list)) *attribute-map
*))
29 (assoc class-name
*attribute-map
*))))
31 (defgeneric find-class-attributes
(class))
33 (defmethod find-class-attributes ((model t
))
34 (find-or-create-attributes (class-name (class-of model
))))
36 (defmethod find-class-attributes ((model symbol
))
37 (find-or-create-attributes model
))
39 (defmethod clear-class-attributes ((model t
))
40 (setf (cdr (find-class-attributes model
)) nil
))
42 (defmethod add-attribute ((model t
) name def
)
43 (let ((map (find-class-attributes model
)))
44 (setf (cdr map
) (acons name def
(cdr map
)))))
46 (defmethod find-attribute ((model t
) name
)
47 (assoc name
(cdr (find-class-attributes model
))))
49 (defmethod (setf find-attribute
) ((def list
) (model t
) name
)
50 (let ((attr (find-attribute model name
)))
56 (add-attribute model name def
)
57 (find-attribute model name
)))))
59 (defmethod set-attribute ((model t
) name definition
&key
(inherit t
))
60 (setf (find-attribute model name
)
62 (cons (car definition
)
63 (plist-union (cdr definition
)
64 (cddr (find-attribute model name
))))
67 (defmethod perform-set-attributes ((model t
) definitions
)
68 (dolist (def definitions
)
69 (funcall #'set-attribute model
(first def
) (rest def
))))
71 (defmethod set-attribute-properties ((model t
) attribute properties
)
72 (let ((a (find-attribute model attribute
)))
74 (setf (cddr a
) (plist-nunion properties
(cddr a
)))
75 (error "Attribute ~A does not exist" attribute
) )))
77 (defmethod perform-set-attribute-properties ((model t
) definitions
)
78 (dolist (def definitions
)
79 (funcall #'set-attribute-properties model
(car def
) (cdr def
))))
81 (defmethod perform-define-attributes ((model t
) attributes
)
82 (loop for attribute in attributes
83 do
(destructuring-bind (name type
&rest args
)
86 ;;use the existing (default) type
87 (set-attribute-properties model name args
))
89 ;;set the type as well
90 (set-attribute model name
(cons type args
)))))))
92 (defmacro define-attributes
(models &body attribute-definitions
)
94 ,@(loop for model in models
95 collect
`(perform-define-attributes (quote ,model
) (quote ,attribute-definitions
)))
96 (mapcar #'find-class-attributes
(quote ,models
))))
98 (defun find-presentation-attributes (model)
99 (remove nil
(mapcar #'(lambda (att)
100 (when (keywordp (car att
))
102 (cdr (find-class-attributes model
)))))
105 ;;;; ** Default Attributes
108 ;;;; The default mewa class contains the types use as defaults.
109 ;;;; maps meta-model slot-types to slot-presentation
111 (defvar *default-attributes-class-name
* 'default
)
113 (define-attributes (default)
114 (boolean mewa-boolean
)
116 (number mewa-currency
)
117 (integer mewa-integer
)
118 (currency mewa-currency
)
119 (clsql:generalized-boolean mewa-boolean
)
120 (foreign-key foreign-key
)
121 (:viewer mewa-viewer
)
122 (:editor mewa-editor
)
123 (:creator mewa-creator
)
124 (:one-line mewa-one-line-presentation
)
125 (:listing mewa-list-presentation
:global-properties
(:editablep nil
) :editablep t
)
126 (:search-model mewa-object-presentation
))
129 (defun find-default-presentation-attributes ()
130 (if (eql *default-attributes-class-name
* 'default
)
131 (find-presentation-attributes 'default
)
132 (remove-duplicates (append
133 (find-presentation-attributes 'default
)
134 (find-presentation-attributes
135 *default-attributes-class-name
*)))))
138 (defmacro with-default-attributes
((model-name) &body body
)
139 `(let ((*default-attributes-class-name
* ',model-name
))
142 (defun gen-ptype (type)
143 (let ((type (if (consp type
) (car type
) type
)))
144 (or (second (find-attribute *default-attributes-class-name
* type
))
145 (second (find-attribute 'default type
))
148 (defun gen-presentation-slots (instance)
149 (mapcar #'(lambda (x) (gen-pslot (cadr x
)
152 (meta-model:list-slot-types instance
)))
155 (defun gen-pslot (type label slot-name
)
156 (copy-list `(,(gen-ptype type
)
158 :slot-name
,slot-name
)))
160 (defun gen-presentation-args (instance args
)
161 (declare (ignore instance
))
165 (defmethod find-default-attributes ((model t
))
166 "return the default attributes for a given model using the meta-model's meta-data"
167 (append (mapcar #'(lambda (s)
170 (if (meta-model:foreign-key-p model
(car s
))
173 (string (car s
)) (car s
))))
174 (meta-model:list-slot-types model
))
175 (mapcar #'(lambda (s)
176 (cons s
(append (gen-pslot 'has-many
(string s
) s
)
181 (meta-model:list-has-many model
))
182 (find-default-presentation-attributes)))
184 (defmethod set-default-attributes ((model t
))
185 "Set the default attributes for MODEL"
186 (clear-class-attributes model
)
187 (mapcar #'(lambda (x)
188 (setf (find-attribute model
(car x
)) (cdr x
)))
189 (find-default-attributes model
)))
192 (defgeneric attributes-getter
(model))
196 (defcomponent mewa
()
197 ((instance :accessor instance
:initarg
:instance
)
203 :accessor attributes-getter
204 :initform
#'get-attributes
205 :initarg
:attributes-getter
)
207 :accessor attribute-slot-map
210 :initarg
:global-properties
211 :accessor global-properties
217 (use-instance-class-p
218 :initarg
:use-instance-class-p
219 :accessor use-instance-class-p
221 (initializedp :initform nil
)
222 (modifiedp :accessor modifiedp
:initform nil
:initarg
:modifiedp
)
223 (modifications :accessor modifications
:initform nil
)))
226 (defmethod attributes :around
((self mewa
))
227 (let ((a (call-next-method)))
228 (or a
(funcall (attributes-getter self
) self
))))
230 (defgeneric get-attributes
(mewa))
232 (defmethod get-attributes ((self mewa
))
234 (append (meta-model:list-slots
(instance self
))
235 (meta-model:list-has-many
(instance self
)))
238 (defmethod find-instance-classes ((self mewa
))
240 (it.bese.arnesi.mopp
:compute-class-precedence-list
(class-of (instance self
)))))
242 (defmethod find-all-attributes ((self mewa
))
244 (mapcar #'(lambda (x)
245 (cdr (find-class-attributes x
)))
248 (defun make-attribute (&rest props
&key type
&allow-other-keys
)
250 (cons (gensym) (cons type props
)))
253 (defmethod find-applicable-attributes ((self mewa
))
254 (let ((all-attributes (find-all-attributes self
)))
255 (flet ((gen-att (x) (let ((att (assoc x all-attributes
)))
257 (setf (cddr att
) (plist-union (global-properties self
) (cddr att
)))
259 (if (attributes self
)
261 (mapcar #'(lambda (x)
266 ;;if the car is a keyword then this is an inline def
267 ((and (listp x
) (keywordp (car x
)))
268 (let ((att (apply #'make-attribute x
)))
270 (plist-union (cddr att
) (global-properties self
)))
272 ;; if the plist has a :type
273 ((and (listp x
) (getf (cdr x
) :type
))
274 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
275 (def (gen-att (car x
))))
276 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
277 (cons (car def
) new
)))
278 ;;finally if we are just overiding the props
279 ((and (listp x
) (symbolp (car x
)))
280 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
281 (def (gen-att (car x
))))
282 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
283 (cons (car def
) (cons (second def
) (cdr new
)))))
291 (defmethod find-slot-presentation-for-attribute ((self mewa
) attribute
)
293 (or (gethash (if (consp (second attribute
))
294 (car (second attribute
))
296 *presentation-slot-type-mapping
*)
297 (error "Can't find slot type for ~A in ~A" attribute self
))))
299 (cons (first attribute
) (apply #'make-instance
301 (append (cddr attribute
) (list :parent self
:size
30))))))
303 (defmethod find-slot-presentations ((self mewa
))
304 (mapcar #'(lambda (a) (find-slot-presentation-for-attribute self a
))
305 (find-applicable-attributes self
)))
307 (defmethod find-attribute-slot ((self mewa
) (attribute symbol
))
308 (cdr (assoc attribute
(attribute-slot-map self
))))
310 (defmethod initialize-slots ((self mewa
))
311 (when (instance self
)
312 (when (use-instance-class-p self
)
314 (append (find-instance-classes self
)
316 (setf (attribute-slot-map self
) (find-slot-presentations self
))
317 (setf (slots self
) (mapcar #'(lambda (x)(cdr x
)) (attribute-slot-map self
)))))
319 (defmethod make-presentation ((object t
) &key
(type :viewer
) (initargs nil
))
320 (let* ((p (make-instance 'mewa-object-presentation
))
321 (a (progn (setf (slot-value p
'instance
) object
)
323 (assoc type
(find-all-attributes p
))))
324 (i (apply #'make-instance
(or (second a
)
325 ;; if we didnt find the type,
326 ;; use the symbol as a class.
327 (if (eql (symbol-package type
)
328 (find-package 'keyword
))
331 (plist-union initargs
(cddr a
)))))
332 (setf (slot-value i
'instance
) object
)
334 (setf (slot-value i
'initializedp
) t
)
338 (defmethod initialize-slots-place ((place ucw
::place
) (mewa mewa
))
339 (setf (slots mewa
) (mapcar #'(lambda (x)
341 (setf (component.place x
) place
)))
344 (arnesi:defmethod
/cc call-component
:before
((from standard-component
) (to mewa
))
345 (unless (slot-value to
'initializedp
)
346 (initialize-slots to
))
347 (setf (slot-value to
'initializedp
) t
)
348 (initialize-slots-place (component.place from
) to
)
353 (defmacro call-presentation
(object &rest args
)
354 `(present-object ,object
:presentation
(make-presentation ,object
,@args
)))
357 (defcomponent about-dialog
(option-dialog)
358 ((body :initarg
:body
)))
360 (defmethod render-on ((res response
) (self about-dialog
))
362 (render-on res
(slot-value self
'body
)))
364 (defmethod instance-is-stored-p ((instance clsql
:standard-db-object
))
365 (slot-value instance
'clsql-sys
::view-database
))
367 (defmethod instance-is-stored-p ((mewa mewa
))
368 (instance-is-stored-p (instance mewa
)))
370 (defaction cancel-save-instance
((self mewa
))
372 ((instance-is-stored-p (instance self
))
373 (meta-model::update-instance-from-records
(instance self
))
377 (defaction save-instance
((self mewa
))
378 (meta-model:sync-instance
(instance self
))
379 (setf (modifiedp self
) nil
)
382 (defmethod confirm-sync-instance ((self mewa
))
385 (defaction ensure-instance-sync
((self mewa
))
386 (when (modifiedp self
)
388 (let ((message (format nil
"Record has been modified, Do you wish to save the changes?")))
389 (case (call 'about-dialog
390 :body
(make-presentation (instance self
)
393 :options
'((:save .
"Save changes to Database")
394 (:cancel .
"Cancel all changes")))
396 (cancel-save-instance self
))
398 (save-instance self
))))
399 (save-instance self
))))
401 (defaction sync-and-answer
((self mewa
))
402 (ensure-instance-sync self
)
403 (answer (instance self
)))
405 (defaction ok
((self mewa
) &optional arg
)
406 "Returns the component if it has not been modified. if it has been, prompt user to save or cancel"
407 ;(declare (ignore arg))
408 (sync-and-answer self
))
410 (defmethod (setf presentation-slot-value
) :around
(value (slot slot-presentation
) instance
)
412 (presentation-slot-value slot instance
)
414 (new (presentation-slot-value slot instance
)))
416 (unless (equal new old
)
417 (let ((self (ucw::parent slot
)))
418 (setf (modifiedp self
) instance
419 (modifications self
) (append (list new old value slot instance
) (modifications self
)))))))
427 ;; This software is Copyright (c) Drew Crampsie, 2004-2005.
428 ;; You are granted the rights to distribute
429 ;; and use this software as governed by the terms
430 ;; of the Lisp Lesser GNU Public License
431 ;; (http://opensource.franz.com/preamble.html),
432 ;; known as the LLGPL.