3 (defparameter *default-type
* :ucw
)
5 ;;; maps meta-model slot-types to slot-presentation
6 (defparameter *slot-type-map
* '(number ucw
:currency
))
8 ;;; an alist of model-class-name . attributes
9 ;;; should really be a hash-table.
10 (defvar *attribute-map
* (list))
12 ;;; some utilities for merging plists
14 (defun plist-nunion (new-props plist
)
15 (loop for cons on new-props by
#'cddr
16 do
(setf (getf plist
(first cons
)) (second cons
))
17 finally
(return plist
)))
19 (defun plist-union (new-props plist
)
20 "Non-destructive version of plist-nunion"
21 (plist-nunion new-props
(copy-list plist
)))
23 (defun gen-ptype (type)
24 (or (getf *slot-type-map
* type
) type
))
26 (defun gen-presentation-slots (instance)
27 (mapcar #'(lambda (x) (gen-pslot (cadr x
)
30 (list-slot-types instance
)))
33 (defun gen-pslot (type label slot-name
)
34 (copy-list `(,(gen-ptype type
)
36 :slot-name
,slot-name
)))
38 (defun gen-presentation-args (instance args
)
39 (declare (ignore instance
))
43 (defun find-or-create-attributes (class-name)
44 "return an exisiting class attribute map or create one.
46 A map is a cons of class-name . attributes.
47 attributes is an alist keyed on the attribute nreeame."
48 (or (assoc class-name
*attribute-map
*)
50 (setf *attribute-map
* (acons class-name
(list (list)) *attribute-map
*))
51 (assoc class-name
*attribute-map
*))))
53 (defgeneric find-class-attributes
(class))
55 (defmethod find-class-attributes ((model t
))
56 (find-or-create-attributes (class-name (class-of model
))))
58 (defmethod find-class-attributes ((model symbol
))
59 (find-or-create-attributes model
))
61 (defmethod add-attribute ((model t
) name def
)
62 (let ((map (find-class-attributes model
)))
63 (setf (cdr map
) (acons name def
(cdr map
)))))
65 (defmethod find-attribute ((model t
) name
)
66 (assoc name
(cdr (find-class-attributes model
))))
68 (defmethod (setf find-attribute
) ((def list
) (model t
) name
)
69 (let ((attr (find-attribute model name
)))
75 (add-attribute model name def
)
76 (find-attribute model name
)))))
78 (defmethod set-attribute ((model t
) name definition
&key
(inherit t
))
79 (setf (find-attribute model name
)
81 (cons (car definition
)
82 (plist-union (cdr definition
)
83 (cddr (find-attribute model name
))))
87 (defmethod default-attributes ((model t
))
88 (append (mapcar #'(lambda (s)
91 (if (meta-model:foreign-key-p model
95 (string (car s
)) (car s
))))
96 (meta-model:list-slot-types model
))
97 (mapcar #'(lambda (s) (cons s
(append (gen-pslot 'ucw
::has-many
(string s
) s
) `(:presentation
(make-presentation ,model
:type
:one-line
)))))
98 (meta-model:list-has-many model
))))
100 (defmethod set-default-attributes ((model t
))
101 (mapcar #'(lambda (x)
102 (setf (find-attribute model
(car x
)) (cdr x
)))
103 (default-attributes model
)))
106 (defgeneric attributes-getter
(model))
113 (defcomponent mewa
()
119 :accessor attributes-getter
120 :initform
#'get-attributes
121 :initarg
:attributes-getter
)
123 :initarg
:global-properties
124 :accessor global-properties
130 (use-instance-class-p
131 :initarg
:use-instance-class-p
132 :accessor use-instance-class-p
134 (initializedp :initform nil
)
135 (modifiedp :accessor modifiedp
:initform nil
)
136 (modifications :accessor modifications
:initform nil
)))
139 (defmethod attributes :around
((self mewa
))
140 (let ((a (call-next-method)))
141 (or a
(funcall (attributes-getter self
) self
))))
143 (defgeneric get-attributes
(mewa))
145 (defmethod get-attributes ((self mewa
))
147 (append (meta-model:list-slots
(instance self
))
148 (meta-model:list-has-many
(instance self
)))
152 (defmethod find-instance-classes ((self mewa
))
154 (it.bese.arnesi.mopp
:compute-class-precedence-list
(class-of (instance self
)))))
156 (defmethod find-all-attributes ((self mewa
))
158 (mapcar #'(lambda (x)
159 (cdr (find-class-attributes x
)))
162 (defun make-attribute (&rest props
&key type
&allow-other-keys
)
164 (cons (gensym) (cons type props
)))
167 (defmethod find-applicable-attributes ((self mewa
))
168 (let ((all-attributes (find-all-attributes self
)))
169 (flet ((gen-att (x) (let ((att (assoc x all-attributes
)))
171 (setf (cddr att
) (plist-union (global-properties self
) (cddr att
)))
173 (if (attributes self
)
175 (mapcar #'(lambda (x)
180 ;;if the car is a keyword then this is an inline def
181 ((and (listp x
) (keywordp (car x
)))
182 (let ((att (apply #'make-attribute x
)))
184 (plist-union (cddr att
) (global-properties self
)))
186 ;; if the plist has a :type
187 ((and (listp x
) (getf (cdr x
) :type
))
188 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
189 (def (gen-att (car x
))))
190 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
191 (cons (car def
) new
)))
192 ;;finally if we are just overiding the props
193 ((and (listp x
) (symbolp (car x
)))
194 (let ((new (cdr (apply #'make-attribute
(cdr x
))))
195 (def (gen-att (car x
))))
196 (setf (cdr new
) (plist-union (cdr new
) (cddr def
)))
197 (cons (car def
) (cons (second def
) (cdr new
)))))
205 (defmethod find-slot-presentations ((self mewa
))
206 (mapcar #'(lambda (s)
207 (let ((class-name (or (gethash (second s
) ucw
::*slot-type-mapping
*) 'mewa-object-presentation
)))
208 (apply #'make-instance
210 (append (cddr s
) (list :parent self
)))))
211 (find-applicable-attributes self
)))
215 (defmethod initialize-slots ((self mewa
))
216 (when (use-instance-class-p self
)
218 (append (find-instance-classes self
)
220 (setf (slots self
) (find-slot-presentations self
)))
223 (defmethod make-presentation ((object t
) &key
(type :viewer
) (initargs nil
))
224 (let* ((p (make-instance 'mewa-object-presentation
))
225 (a (progn (setf (slot-value p
'instance
) object
)
227 (assoc type
(find-all-attributes p
))))
229 (i (apply #'make-instance
(second a
) (plist-union initargs
(cddr a
)))))
230 (setf (slot-value i
'instance
) object
)
233 (defmethod make-presentation ((object t
) &key
(type :viewer
) (initargs nil
))
234 (let* ((p (make-instance 'mewa-object-presentation
))
235 (a (progn (setf (slot-value p
'instance
) object
)
237 (assoc type
(find-all-attributes p
))))
239 (i (apply #'make-instance
(or (second a
)
240 ;; if we didnt find the type,
241 ;; use the symbol as a class.
242 (if (eql (symbol-package type
)
243 (find-package 'keyword
))
246 (plist-union initargs
(cddr a
)))))
247 (setf (slot-value i
'instance
) object
)
249 (setf (slot-value i
'initializedp
) t
)
256 (defmethod call-component :before
((from standard-component
) (to mewa
))
257 (unless (slot-value to
'initializedp
)
258 (initialize-slots to
))
259 (setf (slot-value to
'initializedp
) t
)
260 (setf (slots to
) (mapcar #'(lambda (x) (prog2
261 (setf (component.place x
) (component.place from
))
265 (defmacro call-presentation
(object &rest args
)
266 `(present-object ,object
:presentation
(make-presentation ,object
,@args
)))
270 (defaction cancel-save-instance
((self mewa
))
273 (defaction save-instance
((self mewa
))
274 (meta-model:sync-instance
(instance self
))
275 (setf (modifiedp self
) nil
)
279 (defaction ok
((self mewa
) &optional arg
)
280 "Returns the component if it has not been modified. if it has been, prompt user to save or cancel"
281 (declare (ignore arg
))
282 (when (modifiedp self
)
283 (let ((message (format nil
"Record has been modified, Do you wish to save the changes?<br/> ~a" (print (modifications self
)))))
284 (case (call 'option-dialog
286 :options
'((:save .
"Save changes to Database")
287 (:cancel .
"Cancel all changes")))
289 (cancel-save-instance self
))
291 (save-instance self
)))))
296 (defmethod (setf presentation-slot-value
) :around
(value (slot slot-presentation
) instance
)
298 (presentation-slot-value slot instance
)
300 (new (presentation-slot-value slot instance
)))
302 (unless (equal new old
)
303 (let ((self (ucw::parent slot
)))
304 (setf (modifiedp self
) instance
305 (modifications self
) (append (list (type-of new
) (type-of old
) (type-of value
) slot instance
)))))))