Commit | Line | Data |
---|---|---|
91f2ab7b DC |
1 | (in-package :lisp-on-lines) |
2 | ||
3 | ;;;; * Relational Attributes | |
4 | ||
2b0fd9c8 DC |
5 | |
6 | ;;;; ** has-a | |
a4e6154d | 7 | ;;;; Used for foreign keys, currently only works with clsql. |
2b0fd9c8 DC |
8 | |
9 | (defattribute has-a () | |
10 | () | |
11 | (:default-properties | |
a4e6154d DC |
12 | :has-a nil |
13 | :test 'meta-model::generic-equal)) | |
2b0fd9c8 | 14 | |
a4e6154d | 15 | ;; |
2b0fd9c8 | 16 | (define-layered-method attribute-value (object (attribute has-a)) |
91b9f259 | 17 | (multiple-value-bind (obj key class) |
18 | (meta-model:explode-foreign-key object (slot-name attribute) :nilp t) | |
19 | (if (persistentp object) | |
20 | obj | |
21 | (first (select class | |
22 | :where [= [slot-value class key] (call-next-method)] | |
23 | :flatp t | |
24 | ))))) | |
a4e6154d DC |
25 | |
26 | (define-layered-method (setf attribute-value) ((value standard-object) object (attribute has-a)) | |
27 | (let ((val (slot-value value (find-if (curry #'primary-key-p value) (list-keys value))))) | |
28 | (setf (attribute-value object attribute) val))) | |
29 | ||
30 | ||
31 | ||
32 | (define-layered-function find-all-foreign-objects (o a)) | |
33 | ||
34 | (define-layered-method find-all-foreign-objects (object (attribute has-a)) | |
35 | (select (meta-model:find-join-class object (slot-name attribute)) :flatp t)) | |
2b0fd9c8 DC |
36 | |
37 | (defdisplay ((attribute has-a) object) | |
38 | (let ((args (plist-union (description.properties attribute) (has-a attribute))) | |
39 | (val (attribute-value object attribute))) | |
40 | (when val | |
41 | (setf (getf args :type) | |
42 | 'lol::one-line)) | |
43 | (apply #'display* val | |
44 | args))) | |
45 | ||
46 | ||
a4e6154d DC |
47 | (defdisplay |
48 | :in-layer editor ((attribute has-a) object) | |
49 | (<ucw:select | |
50 | :accessor (attribute-value object attribute) | |
51 | ||
52 | :test (test attribute) | |
53 | (dolist* (obj (find-all-foreign-objects object attribute)) | |
54 | (<ucw:option | |
55 | :value obj | |
0386c736 | 56 | (display* obj :layers '(+ as-string)))))) |
a4e6154d DC |
57 | |
58 | ||
91f2ab7b DC |
59 | ;;;; ** Has-Many attribute |
60 | ||
61 | (defattribute has-many () | |
62 | () | |
63 | (:default-properties | |
64 | :add-new-label "Add New" | |
2b0fd9c8 DC |
65 | :sort-arguments (list #'< :key #'(lambda (x) (funcall (car (list-keys x)) x)))) |
66 | (:default-initargs | |
67 | :type 'lol::one-line)) | |
68 | ||
2b0fd9c8 DC |
69 | (define-layered-method |
70 | attribute-value (object (has-many has-many)) | |
a4e6154d | 71 | (slot-value object (slot-name has-many))) |
91f2ab7b | 72 | |
2b0fd9c8 | 73 | (defdisplay ((attribute has-many) object) |
91f2ab7b | 74 | ; |
2b0fd9c8 DC |
75 | ;(<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label attribute)) |
76 | ||
77 | (<:div :style "clear:both;" | |
78 | (let* ((i (apply #'sort (slot-value object (slot-name attribute)) | |
79 | (sort-arguments attribute)))) | |
80 | (<:ul | |
81 | (dolist* (x i) | |
82 | (<:li (display* x | |
0386c736 | 83 | :type 'lol::as-string |
2b0fd9c8 | 84 | :layers '(+ wrap-link - label-attributes)))))))) |
91f2ab7b | 85 | |
dee565d0 DC |
86 | |
87 | (defun find-many-to-many-class (slot-name instance) | |
88 | (let* ((imd (getf (meta-model::find-slot-metadata instance slot-name) | |
89 | :db-info)) | |
90 | (jc (make-instance (getf imd :join-class))) | |
91 | (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot)) | |
92 | :db-info))) | |
93 | (getf jcmd :join-class))) | |
94 | ||
95 | ||
96 | (defattribute many-to-many () | |
97 | ()) | |
98 | ||
14a7e1bc | 99 | |
dee565d0 | 100 | |
2b0fd9c8 | 101 | (defdisplay ((attribute many-to-many) object) |
91f2ab7b DC |
102 | (<:as-html "ASDASD")) |
103 | ||
104 | #+nil(let ((instances (select-instances object t)) | |
dee565d0 DC |
105 | new-instance) |
106 | (<:ul | |
2b0fd9c8 | 107 | (<:li (<ucw:button :action (add-new-relation component object (.get slot-name)) |
d2882889 | 108 | (<:as-html "Add New"))) |
dee565d0 DC |
109 | (<:li (<ucw:button :action (add-new-relation component object new-instance) |
110 | (<:as-html "Add:")) | |
111 | (<ucw:select :accessor new-instance | |
112 | (arnesi:dolist* (i instances) | |
113 | (<ucw:option | |
114 | :value i | |
115 | (display component i :type 'one-line))))) | |
116 | (dolist* (i (attribute-value object attribute)) | |
117 | (<:li | |
118 | (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot))) | |
119 | (<:as-html "(view) ")) | |
120 | (<ucw:a :action (delete-relationship slot (second i) instance) | |
121 | (<:as-html "(remove) ")) | |
122 | (display component object))))) | |
2b0fd9c8 | 123 | ;(display component (mapcar #'car (slot-value object (.get :slot-name)))) |
dee565d0 | 124 |