added perform-set-attribute and a few other fixes to this builds again.
[clinton/lisp-on-lines.git] / src / mewa / mewa.lisp
CommitLineData
233380f7 1
579597e3 2(in-package :mewa)
3
4(defparameter *default-type* :ucw)
5
6;;; maps meta-model slot-types to slot-presentation
9d6c69fb
DC
7(defparameter *slot-type-map*
8 '(boolean ucw::mewa-boolean
9 string ucw::mewa-string
10 number ucw::mewa-currency
11 integer ucw::mewa-integer
12 currency ucw::mewa-currency
13 ))
579597e3 14
15;;; an alist of model-class-name . attributes
16;;; should really be a hash-table.
17(defvar *attribute-map* (list))
18
19;;; some utilities for merging plists
20
21(defun plist-nunion (new-props plist)
d0c40011 22 (loop for cons on new-props by #'cddr
579597e3 23 do (setf (getf plist (first cons)) (second cons))
24 finally (return plist)))
25
26(defun plist-union (new-props plist)
27 "Non-destructive version of plist-nunion"
28 (plist-nunion new-props (copy-list plist)))
29
30(defun gen-ptype (type)
31 (or (getf *slot-type-map* type) type))
32
33(defun gen-presentation-slots (instance)
34 (mapcar #'(lambda (x) (gen-pslot (cadr x)
35 (string (car x))
36 (car x)))
9d6c69fb 37 (meta-model:list-slot-types instance)))
579597e3 38
39
40(defun gen-pslot (type label slot-name)
41 (copy-list `(,(gen-ptype type)
42 :label ,label
43 :slot-name ,slot-name)))
44
45(defun gen-presentation-args (instance args)
46 (declare (ignore instance))
47 (if args args nil))
48
49
50(defun find-or-create-attributes (class-name)
51 "return an exisiting class attribute map or create one.
52
53A map is a cons of class-name . attributes.
54attributes is an alist keyed on the attribute nreeame."
55 (or (assoc class-name *attribute-map*)
56 (progn
57 (setf *attribute-map* (acons class-name (list (list)) *attribute-map*))
58 (assoc class-name *attribute-map*))))
59
60(defgeneric find-class-attributes (class))
61
62(defmethod find-class-attributes ((model t))
63 (find-or-create-attributes (class-name (class-of model))))
64
65(defmethod find-class-attributes ((model symbol))
66 (find-or-create-attributes model))
67
68(defmethod add-attribute ((model t) name def)
69 (let ((map (find-class-attributes model)))
70 (setf (cdr map) (acons name def (cdr map)))))
71
72(defmethod find-attribute ((model t) name)
73 (assoc name (cdr (find-class-attributes model))))
74
75(defmethod (setf find-attribute) ((def list) (model t) name)
76 (let ((attr (find-attribute model name)))
77 (if attr
78 (prog2
79 (setf (cdr attr) def)
80 attr)
81 (prog2
82 (add-attribute model name def)
83 (find-attribute model name)))))
84
85(defmethod set-attribute ((model t) name definition &key (inherit t))
86 (setf (find-attribute model name)
87 (if inherit
88 (cons (car definition)
89 (plist-union (cdr definition)
90 (cddr (find-attribute model name))))
91 definition)))
92
e8e743d7 93(defmethod perform-set-attributes ((model t) definitions)
94 (dolist (def definitions)
95 (funcall #'set-attribute model (first def) (rest def))))
579597e3 96
19531fbd 97(defmethod default-attributes ((model t))
a6644385 98 "return the default attributes for a given model using the meta-model's meta-data"
7129498f 99 (append (mapcar #'(lambda (s)
100 (cons (car s)
101 (gen-pslot
1679abef 102 (if (meta-model:foreign-key-p model (car s))
103 'ucw::foreign-key
104 (cadr s))
105 (string (car s)) (car s))))
106 (meta-model:list-slot-types model))
107 (mapcar #'(lambda (s)
108 (cons s (append (gen-pslot 'ucw::has-many (string s) s)
109 `(:presentation
110 (make-presentation
111 ,model
112 :type :one-line)))))
19531fbd 113 (meta-model:list-has-many model))))
114
115(defmethod set-default-attributes ((model t))
116 (mapcar #'(lambda (x)
117 (setf (find-attribute model (car x)) (cdr x)))
118 (default-attributes model)))
119
120
579597e3 121(defgeneric attributes-getter (model))
122
19531fbd 123;;;presentations
124
125
126
127
579597e3 128(defcomponent mewa ()
129 ((attributes
130 :initarg :attributes
131 :accessor attributes
132 :initform nil)
133 (attributes-getter
134 :accessor attributes-getter
135 :initform #'get-attributes
136 :initarg :attributes-getter)
137 (global-properties
138 :initarg :global-properties
139 :accessor global-properties
140 :initform nil)
141 (classes
142 :initarg :classes
143 :accessor classes
144 :initform nil)
145 (use-instance-class-p
146 :initarg :use-instance-class-p
147 :accessor use-instance-class-p
148 :initform t)
149 (initializedp :initform nil)
d1bb68e0 150 (modifiedp :accessor modifiedp :initform nil)
151 (modifications :accessor modifications :initform nil)))
579597e3 152
579597e3 153
154(defmethod attributes :around ((self mewa))
155 (let ((a (call-next-method)))
156 (or a (funcall (attributes-getter self) self))))
157
19531fbd 158(defgeneric get-attributes (mewa))
159
579597e3 160(defmethod get-attributes ((self mewa))
161 (if (instance self)
162 (append (meta-model:list-slots (instance self))
163 (meta-model:list-has-many (instance self)))
164 nil))
165
579597e3 166
167(defmethod find-instance-classes ((self mewa))
168 (mapcar #'class-name
169 (it.bese.arnesi.mopp:compute-class-precedence-list (class-of (instance self)))))
170
171(defmethod find-all-attributes ((self mewa))
172 (reduce #'append
173 (mapcar #'(lambda (x)
174 (cdr (find-class-attributes x)))
175 (classes self))))
176
177(defun make-attribute (&rest props &key type &allow-other-keys)
178 (remf props :type)
179 (cons (gensym) (cons type props)))
180
181
182(defmethod find-applicable-attributes ((self mewa))
183 (let ((all-attributes (find-all-attributes self)))
184 (flet ((gen-att (x) (let ((att (assoc x all-attributes)))
185 (when att
186 (setf (cddr att) (plist-union (global-properties self) (cddr att)))
187 att))))
188 (if (attributes self)
189 (remove 'nil
190 (mapcar #'(lambda (x)
191 (cond
192 ;;simple casee
193 ((symbolp x)
194 (gen-att x))
195 ;;if the car is a keyword then this is an inline def
196 ((and (listp x) (keywordp (car x)))
197 (let ((att (apply #'make-attribute x)))
198 (setf (cddr att)
199 (plist-union (cddr att) (global-properties self)))
200 att))
201 ;; if the plist has a :type
202 ((and (listp x) (getf (cdr x) :type))
203 (let ((new (cdr (apply #'make-attribute (cdr x))))
204 (def (gen-att (car x))))
205 (setf (cdr new) (plist-union (cdr new) (cddr def)))
206 (cons (car def) new)))
207 ;;finally if we are just overiding the props
208 ((and (listp x) (symbolp (car x)))
209 (let ((new (cdr (apply #'make-attribute (cdr x))))
210 (def (gen-att (car x))))
211 (setf (cdr new) (plist-union (cdr new) (cddr def)))
212 (cons (car def) (cons (second def) (cdr new)))))
213
214 )
215 )
216
217 (attributes self)))
218 all-attributes))))
219
220(defmethod find-slot-presentations ((self mewa))
221 (mapcar #'(lambda (s)
222 (let ((class-name (or (gethash (second s) ucw::*slot-type-mapping*) 'mewa-object-presentation)))
223 (apply #'make-instance
224 class-name
225 (append (cddr s) (list :parent self)))))
226 (find-applicable-attributes self)))
227
579597e3 228
229
230(defmethod initialize-slots ((self mewa))
231 (when (use-instance-class-p self)
232 (setf (classes self)
233 (append (find-instance-classes self)
234 (classes self))))
235 (setf (slots self) (find-slot-presentations self)))
236
237
579597e3 238(defmethod make-presentation ((object t) &key (type :viewer) (initargs nil))
239 (let* ((p (make-instance 'mewa-object-presentation))
240 (a (progn (setf (slot-value p 'instance) object)
241 (initialize-slots p)
242 (assoc type (find-all-attributes p))))
243
244 (i (apply #'make-instance (second a) (plist-union initargs (cddr a)))))
245 (setf (slot-value i 'instance) object)
246 i))
247
19531fbd 248(defmethod make-presentation ((object t) &key (type :viewer) (initargs nil))
249 (let* ((p (make-instance 'mewa-object-presentation))
250 (a (progn (setf (slot-value p 'instance) object)
251 (initialize-slots p)
252 (assoc type (find-all-attributes p))))
253
254 (i (apply #'make-instance (or (second a)
255 ;; if we didnt find the type,
256 ;; use the symbol as a class.
257 (if (eql (symbol-package type)
258 (find-package 'keyword))
259 (symbol-name type)
260 type))
261 (plist-union initargs (cddr a)))))
262 (setf (slot-value i 'instance) object)
2acd3ba2 263 (initialize-slots i)
264 (setf (slot-value i 'initializedp) t)
19531fbd 265 i))
266
267
268
269
270
579597e3 271(defmethod call-component :before ((from standard-component) (to mewa))
272 (unless (slot-value to 'initializedp)
273 (initialize-slots to))
274 (setf (slot-value to 'initializedp) t)
275 (setf (slots to) (mapcar #'(lambda (x) (prog2
276 (setf (component.place x) (component.place from))
277 x))
9d6c69fb 278 (slots to))))
579597e3 279
280(defmacro call-presentation (object &rest args)
ae09804a 281 `(present-object ,object :presentation (make-presentation ,object ,@args)))
282
ae09804a 283(defaction cancel-save-instance ((self mewa))
1679abef 284 (cond
285 ((slot-value (instance self) 'clsql-sys::view-database)
286 (meta-model::update-instance-from-records (instance self))
287 (answer self))
288 (t (answer nil))))
ae09804a 289
290(defaction save-instance ((self mewa))
7129498f 291 (meta-model:sync-instance (instance self))
ae09804a 292 (setf (modifiedp self) nil)
293 (answer self))
294
295
12dcf3d4 296(defaction ensure-instance-sync ((self mewa))
ae09804a 297 (when (modifiedp self)
298 (let ((message (format nil "Record has been modified, Do you wish to save the changes?<br/> ~a" (print (modifications self)))))
299 (case (call 'option-dialog
300 :message message
301 :options '((:save . "Save changes to Database")
302 (:cancel . "Cancel all changes")))
303 (:cancel
304 (cancel-save-instance self))
305 (:save
12dcf3d4 306 (save-instance self))))))
ae09804a 307
e8e743d7 308(defaction ok ((self mewa) &optional arg)
309 "Returns the component if it has not been modified. if it has been, prompt user to save or cancel"
310 (declare (ignore arg))
311 (ensure-instance-sync self)
312 (answer self))
313
314
315
ae09804a 316
317
318(defmethod (setf presentation-slot-value) :around (value (slot slot-presentation) instance)
319 (let* ((old (prog1
320 (presentation-slot-value slot instance)
321 (call-next-method)))
322 (new (presentation-slot-value slot instance)))
323
324 (unless (equal new old )
325 (let ((self (ucw::parent slot)))
326 (setf (modifiedp self) instance
233380f7 327 (modifications self) (append (list (type-of new) (type-of old) (type-of value) slot instance )))))))
328
329
330;; This software is Copyright (c) Drew Crampsie, 2004-2005.
331;; You are granted the rights to distribute
332;; and use this software as governed by the terms
333;; of the Lisp Lesser GNU Public License
334;; (http://opensource.franz.com/preamble.html),
335;; known as the LLGPL.