summaryrefslogtreecommitdiff
path: root/src/logikorr.clj
blob: a8f08e7f753573b989be3ccc66c5ba0ca2fb3642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(ns logikorr
  (:gen-class :extends javax.servlet.http.HttpServlet)
  (:use compojure.http compojure.html)
  (:require [appengine-clj.datastore :as ds]
            [org.danlarkin.json :as json])
  (:import [com.google.appengine.api.datastore DatastoreServiceFactory Entity Query Query$FilterOperator Query$SortDirection KeyFactory EntityNotFoundException Key]
           [com.google.appengine.api.users UserServiceFactory]))

(defmacro with-ds-transaction [& body]
  `(call-with-ds-transaction (fn [] ~@body)))

(defn call-with-ds-transaction [thunk]
  (let [ds (DatastoreServiceFactory/getDatastoreService)
        success (atom false)
        transaction (.beginTransaction ds)
        return-value (atom nil)]
    (try (do
           (swap! return-value (fn [_] (thunk)))
           (.commit transaction)
           (swap! success (fn [_] true)))
         (finally
          (when-not @success
            (.rollback transaction))))
    @return-value))

(defn ds-update
  "Update the corresponding entity from the supplied map in the data store."
  [map]
  (let [datastore (DatastoreServiceFactory/getDatastoreService),
        entity (.get datastore #^Key (:key map))]
    (doseq [[key value] (dissoc map :kind :key)]
      (.setProperty entity (name key) value))
    (.put datastore entity)))

(def *static-directory* "/Users/mulk/Dropbox/Projekte/LogiCLJ/war")

(defn current-revision []
  (or (first (ds/find-all (doto (Query. "revision")
                            (.addSort "number" Query$SortDirection/DESCENDING))))
      (ds/create {:kind "revision" :number 0})))

(defn find-students []
  (ds/find-all (doto (Query. "student" (:key (current-revision))))))

(defn position [coll thing]
  (loop [i 0,
         coll2 coll]
    (if (= (first coll2) thing)
      i
      (if-let [coll3 (next coll2)]
        (recur (+ i 1) coll3)
        nil))))

(defn split-name [name]
  (let [n (position name \,)]
    (if n
      [(apply str (drop (+ n 2) name)) (apply str (take n name))]
      [name ""])))

(defn find-student-by-name [name]
  (let [[first-name last-name] (split-name name)]
    (first (ds/find-all (doto (Query. "student" (:key (current-revision)))
                          (.addFilter "first-name" Query$FilterOperator/EQUAL first-name)
                          (.addFilter "last-name" Query$FilterOperator/EQUAL last-name))))))

(defn find-student-by-id [id]
  (ds/get (KeyFactory/stringToKey id)))

(defn index [request]
  (let [students (find-students)]
    (html
     [:html
      [:head
       [:title "Logik I: Korrekturergebnisse"]
       [:script {:type "text/javascript" :src "js/prototype.js"}]
       [:script {:type "text/javascript" :src "js/scriptaculous.js"}]
       [:link {:type "text/css" :rel "stylesheet" :href "style.css"}]
       [:script {:type "text/javascript" :src "http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"}]
       [:script {:type "text/javascript" :src "logikorr.js"}]
       [:script {:type "text/javascript" :src "logikorr-completion-data.js"}]]
      [:body
       [:h1 "Logik I: Korrekturergebnisse"]
       [:h2 "Neue Ergebnisse"]
       [:form [:button#make-revision {:type "button"} "Aktuelle Version sichern"]
        [:div#new-version-label {:style "display: inline; color: #070"}]]
       [:table#ergebnisse]
       [:h2 "Bestehende Ergebnisse"]
       [:table
        [:tr
         [:th "ID"] [:th "Punkte"] [:th "Nachname"] [:th "Vorname"]]
        (map
         (fn [student]
           (let [{id :key score :score
                  last-name :last-name first-name :first-name}
                 student]
             [:tr
              [:td (str id)]
              [:td (str score)]
              [:td last-name]
              [:td first-name]]))
         students)]]])))

(defn unsplit-name [first last]
  (if last
    (str last ", " first)
    first))

(defn compute-completion-data-js []
  (str "autocompleteList = "
       (json/encode-to-str (map (fn [x]
                                  (unsplit-name (:first-name x) (:last-name x)))
                                (find-students)))
       ";"))

(defn find-student-json [name]
  (let [student (find-student-by-name name)]
    (json/encode-to-str {:id (KeyFactory/keyToString (:key student))
                         :score (seq (:score student))
                         :first-name (:first-name student)
                         :last-name (:last-name student)})))

(defn update-student-score [id score-number new-score-value]
  (let [student (find-student-by-id id)
        score (:score student)
        num (Integer. score-number)]
    (ds-update (assoc student
                 :score (concat (take num score)
                                [(Float. new-score-value)]
                                (drop (+ 1 num) score)))))
  "\"OK\"")

(defn create-new-revision [])

(defroutes logikorr
  (GET "/" index)
  (GET "/favicon.ico" (do nil))
  (GET "/logikorr-completion-data.js" (compute-completion-data-js))
  (GET "/find-student" (find-student-json (:name params)))
  (GET "/update-student-score" (update-student-score (:id params) (:score-number params) (:score params)))
  (GET "/create-new-revision" (create-new-revision))
  (GET "/*"
    (or (serve-file *static-directory* (params :*)) :next))
  (ANY "/*" (page-not-found)))

(defservice logikorr)