Changeset 277


Ignore:
Timestamp:
04/14/10 14:51:13 (15 years ago)
Author:
lgiessmann
Message:

rest-interface: finalized and tested the mark-as-deleted-handler of the RESTful interface; json: added some helpers for the rest-interface-mark-as-deleted-handler; added the corresponding docu into json.ebnf and xtm_json.txt

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/json/json_tmcl.lisp

    r276 r277  
    1414;; --- mark-as-deleted handler -------------------------------------------------
    1515;; =============================================================================
    16 ; a test string ...
    17 (defvar cl-user::*js-1*
    18   "{\"type\":\"Association\",
    19     \"topics\":[\"http://textgrid.org/isidorus/tmcl/service\",
    20                 \"http://textgrid.org/isidorus/tmcl/parameter\"],
    21     \"associations\":[{\"itemIdentities\":null,
    22                       \"type\":[\"http://psi.topicmaps.org/tmcl/applies-to\"],
    23                       \"scopes\":null,
    24                       \"roles\":[{\"itemIdentities\":null,
    25                                   \"type\":[\"http://psi.topicmaps.org/tmcl/constraint-role\"],
    26                                   \"topicRef\":[\"http://textgrid.org/isidorus/tmcl/exc\"]},
    27                                  {\"itemIdentities\":null,
    28                                   \"type\":[\"http://psi.topicmaps.org/tmcl/topic-type-role\"],
    29                                   \"topicRef\":[\"http://textgrid.org/isidorus/tmcl/service\"]}]}],
    30     \"parent-topic\":[\"http://textgrid.org/isidorus/my-service/my-service\"],
    31     \"parent-name\":{\"itemIdentities\":null,
    32                      \"type\":[\"http://textgrid.org/isidorus/tmcl/service-name\"],
    33                      \"scopes\":null,
    34                      \"value\":\"my-service\",
    35                      \"variants\":null},
    36     \"names\":[{\"itemIdentities\":null,
    37                 \"type\":[\"http://textgrid.org/isidorus/tmcl/service-name\"],
    38                 \"scopes\":null,
    39                 \"value\":\"my-service\",
    40                 \"variants\":null}],
    41     \"variants\":[{\"itemIdentities\":null,
    42                    \"scopes\":[[\"http://textgrid.org/isidorus/tmcl/display\"]],
    43                    \"resourceData\":{\"datatype\":\"http://www.w3.org/2001/XMLSchema#string\",
    44                                      \"value\":\"http://textgrid.org/isidorus/tmcl/service\"},
    45                    \"resourceRef\":null},
    46                   {\"itemIdentities\":null,
    47                    \"scopes\":[[\"http://textgrid.org/isidorus/tmcl/is-ref\"]],
    48                    \"resourceData\":null,
    49                    \"resourceRef\":\"http://any-ref.org\"}],
    50     \"occurrences\":[{\"itemIdentities\":null,
    51                       \"type\":[\"http://textgrid.org/isidorus/tmcl/service-key\"],
    52                       \"scopes\":null,
    53                       \"resourceData\":{\"datatype\":\"http://www.w3.org/2001/XMLSchema#string\",
    54                                         \"value\":\"service-key\"}}],
    55     \"parent-association\":{\"itemIdentities\":null,
    56                             \"type\":[\"http://psi.topicmaps.org/tmcl/applies-to\"],
    57                             \"scopes\":null,
    58                             \"roles\":[{\"itemIdentities\":null,
    59                                         \"type\":[\"http://psi.topicmaps.org/tmcl/constraint-role\"],
    60                                         \"topicRef\":[\"http://textgrid.org/isidorus/tmcl/exc\"]},
    61                                        {\"itemIdentities\":null,
    62                                         \"type\":[\"http://psi.topicmaps.org/tmcl/topictype-role\"],
    63                                         \"topicRef\":[\"http://textgrid.org/isidorus/tmcl/service\"]}]},
    64     \"roles\":[{\"itemIdentities\":null,
    65                 \"type\":[\"http://psi.topicmaps.org/tmcl/constraint-role\"],
    66                 \"topicRef\":[\"http://textgrid.org/isidorus/tmcl/exc\"]}]}")
    67 
    68 
    6916(defun mark-as-deleted-from-json (json-data)
     17  "Marks an object that is specified by the given JSON data as deleted."
    7018  (declare (string json-data))
    71  
    72 ;{\"type\":<\"Topic\" | \"Occurrence\" | \"Name\"
    73 ;           \"Association\" | \"Role\" | \"Variant\" >,
    74 ; \"topics\": <one psi per topic of the topic that have to be deleted>,
    75 ; \"associations\": <a list of associations that should be deleted in the
    76 ;                   specified json format>,
    77 ; \"parent-topic\": <one psi of the parent topic the deleted
    78 ;                    objects are contained in or null if the
    79 ;                    deleted object is the topic itself>,
    80 ; \"parent-name\": <the owner parent of the deleted variants>,
    81 ; \"names\": <a list of names that have to be deleted>,
    82 ; \"variants\": <a list of variants that have to be deleted>,
    83 ; \"occurrences\": <a list of occurrences that have to be deleted>,
    84 ; \"parent-association\": <one association in the specified json
    85 ;                          format, that is the parent of the passed
    86 ;                          role>
    87 ; \"roles\": <a list of roles in the specified json format>}
    8819  (let ((values (json:decode-json-from-string json-data)))
    8920    (let ((type nil)
     
    11748             (delete-associations-from-json associations rev))
    11849            ((string= type "Occurrence")
    119              nil)
     50             (delete-occurrences-from-json occurrences parent-topic rev))
    12051            ((string= type "Name")
    121              nil)
     52             (delete-names-from-json names parent-topic rev))
    12253            ((string= type "Variant")
    123              nil)
     54             (delete-variants-from-json variants parent-topic parent-name rev))
    12455            ((string= type "Role")
    125              nil)
     56             (delete-roles-from-json roles parent-association rev))
    12657            (t
    12758             (error "From mark-as-deleted-from-json(): the type ~a is not defined"
    12859                    type))))))
    12960
     61
     62(defun find-role-from-json (parent-association json-plist)
     63  (declare (AssociationC parent-association) (list json-plist))
     64  (let ((found-role
     65         (find-if
     66          #'(lambda(role)
     67              (let ((type (when (getf json-plist :type)
     68                            (d:get-item-by-psi (first (getf json-plist :type)))))
     69                    (player (when (getf json-plist :topicRef)
     70                              (d:get-item-by-psi
     71                               (first (getf json-plist :topicRef))))))
     72                (and (eql type (d:instance-of role))
     73                     (eql player (d:player role)))))
     74          (d:roles parent-association))))
     75    found-role))
     76
     77
     78(defun delete-roles-from-json (roles parent-association revision)
     79  (declare (list roles parent-association) (integer revision))
     80  (let ((err "From delete-roles-from-association(): ")
     81        (parent-assoc
     82         (find-association-from-json
     83          (json-importer::get-association-values-from-json-list
     84           parent-association))))
     85    (unless parent-assoc
     86      (error "~a~a not found" err parent-association))
     87    (dolist (j-role roles)
     88      (let ((plist (json-importer::get-role-values-from-json-list j-role)))
     89        (let ((role (find-role-from-json parent-assoc plist)))
     90          (unless role
     91            (error "~a~a not found" err plist))
     92          (format t "~a~%" role)
     93          (mark-as-deleted role :revision revision))))))
     94
     95
     96(defun find-variant-from-json (parent-name json-plist)
     97  (declare (NameC parent-name) (list json-plist))
     98  (let ((err "From find-variant-from-json(): "))
     99    (let ((found-var
     100           (find-if
     101            #'(lambda(var)
     102                (let ((datatype (cond ((getf json-plist :datatype)
     103                                       (getf json-plist :datatype))
     104                                      ((getf json-plist :resourceRef)
     105                                       constants:*xml-uri*)
     106                                      ((getf json-plist :resourceData)
     107                                       (let ((val
     108                                              (getf
     109                                               (getf json-plist :resourceData)
     110                                               :datatype)))
     111                                         (if val val constants:*xml-string*)))
     112                                      (t
     113                                       constants:*xml-string*)))
     114                      (charvalue (cond ((getf json-plist :resourceRef)
     115                                        (getf json-plist :resourceRef))
     116                                       ((getf json-plist :resourceData)
     117                                        (getf (getf json-plist :resourceData)
     118                                              :value))
     119                                       (t
     120                                        "")))
     121                      (scopes nil))
     122                  (loop for scope-entry in (getf json-plist :scopes)
     123                     do (let ((top (d:get-item-by-psi (first scope-entry))))
     124                          (unless top
     125                            (error "~a ~a not found" err (first scope-entry)))
     126                          (pushnew top scopes)))
     127                  (and (not (set-exclusive-or scopes (d:themes var)))
     128                       (string= datatype (d:datatype var))
     129                       (string= charvalue (d:charvalue var)))))
     130            (d:variants parent-name :revision 0))))
     131      found-var)))
     132
     133
     134(defun delete-variants-from-json (variants parent-psi parent-name revision)
     135  (declare (string parent-psi) (list variants parent-name))
     136  (let ((err "From delete-variants-from-json(): ")
     137        (parent-topic (d:get-item-by-psi parent-psi)))
     138    (unless parent-topic
     139      (error "~a~a not found" err parent-psi))
     140    (let ((v-name
     141           (find-name-from-json
     142            parent-topic
     143            (json-importer::get-name-values-from-json-list parent-name))))
     144      (unless v-name
     145        (error "~a~a not found" err parent-name))
     146      (dolist (j-variant variants)
     147        (let ((plist
     148               (json-importer::get-variant-values-from-json-list j-variant)))
     149          (let ((variant (find-variant-from-json v-name plist)))
     150            (unless variant
     151              (error "~a~a not found" err plist))
     152            (mark-as-deleted variant :revision revision)))))))
     153
     154
     155(defun find-name-from-json(parent-topic json-plist)
     156  (declare (TopicC parent-topic) (list json-plist))
     157  (let ((err "From find-name-from-json(): "))
     158    (let ((found-name
     159           (find-if
     160            #'(lambda(name)
     161                (let ((type (when (getf json-plist :type)
     162                              (d:get-item-by-psi (first (getf json-plist :type)))))
     163                      (charvalue (if (getf json-plist :value)
     164                                     (getf json-plist :value)
     165                                     ""))
     166                      (scopes nil))
     167                  (loop for scope-entry in (getf json-plist :scopes)
     168                     do (let ((top (d:get-item-by-psi (first scope-entry))))
     169                          (unless top
     170                            (error "~a ~a not found" err (first scope-entry)))
     171                          (pushnew top scopes)))
     172                  (and (eql type (d:instance-of name))
     173                       (not (set-exclusive-or scopes (d:themes name)))
     174                       (string= charvalue (d:charvalue name)))))
     175            (names parent-topic :revision 0))))
     176      found-name)))
     177
     178
     179(defun delete-names-from-json (names parent-psi revision)
     180  (declare (list names) (string parent-psi) (integer revision))
     181  (let ((parent-topic (d:get-item-by-psi parent-psi))
     182        (err "From delete-name-from-json(): "))
     183    (unless parent-topic
     184      (error "~a~a not found"
     185             err parent-psi))
     186    (dolist (j-name names)
     187      (let ((plist (json-importer::get-name-values-from-json-list j-name)))
     188        (let ((name (find-name-from-json parent-topic plist)))
     189          (unless name
     190            (error "~a~a not found" err plist))
     191          (mark-as-deleted name :revision revision))))))
     192
     193
     194(defun find-occurrence-from-json(parent-topic json-plist)
     195  (declare (TopicC parent-topic) (list json-plist))
     196  (let ((err "From find-occurrence-from-json(): "))
     197    (let ((found-occ
     198           (find-if
     199            #'(lambda(occ)
     200                (let ((type (when (getf json-plist :type)
     201                              (d:get-item-by-psi (first (getf json-plist :type)))))
     202                      (datatype (cond ((getf json-plist :datatype)
     203                                       (getf json-plist :datatype))
     204                                      ((getf json-plist :resourceRef)
     205                                       constants:*xml-uri*)
     206                                      ((getf json-plist :resourceData)
     207                                       (let ((val
     208                                              (getf
     209                                               (getf json-plist :resourceData)
     210                                               :datatype)))
     211                                         (if val val constants:*xml-string*)))
     212                                      (t
     213                                       constants:*xml-string*)))
     214                      (charvalue (cond ((getf json-plist :resourceRef)
     215                                        (getf json-plist :resourceRef))
     216                                       ((getf json-plist :resourceData)
     217                                        (getf (getf json-plist :resourceData)
     218                                              :value))
     219                                       (t
     220                                        "")))
     221                      (scopes nil))
     222                  (loop for scope-entry in (getf json-plist :scopes)
     223                     do (let ((top (d:get-item-by-psi (first scope-entry))))
     224                          (unless top
     225                            (error "~a ~a not found" err (first scope-entry)))
     226                          (pushnew top scopes)))
     227                  (and (eql type (d:instance-of occ))
     228                       (not (set-exclusive-or scopes (d:themes occ)))
     229                       (string= datatype (d:datatype occ))
     230                       (string= charvalue (d:charvalue occ)))))
     231            (occurrences parent-topic :revision 0))))
     232      found-occ)))
     233
     234
     235(defun delete-occurrences-from-json(occurrences parent-psi revision)
     236  (declare (list occurrences) (string parent-psi) (integer revision))
     237  (let ((parent-topic (d:get-item-by-psi parent-psi))
     238        (err "From delete-occurrence-from-json(): "))
     239    (unless parent-topic
     240      (error "~a~a not found" err parent-psi))
     241    (dolist (j-occ occurrences)
     242      (let ((plist (json-importer::get-occurrence-values-from-json-list j-occ)))
     243        (let ((occ (find-occurrence-from-json parent-topic plist)))
     244          (unless occ
     245            (error "~a~a not found" err plist))
     246          (mark-as-deleted occ :revision revision))))))
    130247
    131248
     
    141258       do (let ((top (d:get-item-by-psi (first scope-entry))))
    142259            (unless top
    143               (error "~a ~a not found"
    144                      err (first scope-entry)))
     260              (error "~a ~a not found" err (first scope-entry)))
    145261            (pushnew top scopes)))
    146262    (let ((scope-assocs
Note: See TracChangeset for help on using the changeset viewer.