summaryrefslogtreecommitdiff
path: root/src/logikorr/servlet.clj
blob: d27bee174a9434d2d3ce85e88dd02c7ec91671fc (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(ns logikorr.servlet
  (: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]
  (with-ds-transaction
    (let [student (find-student-by-id id)
          score (:score student)
          num (Integer. score-number)]
      (ds-update (assoc student
                   :score (concat (take num score)
                                  [(binding [*read-eval* false]
                                     (read-string new-score-value))]
                                  (drop (+ 1 num) score)))))
    "\"OK\""))

(defn make-new-revision []
  (with-ds-transaction
    (let [current (current-revision)
          students (find-students)
          new (ds/create {:kind "revision"
                          :number (inc (:number current))})]
      (doseq [student students]
        (ds/create (assoc (dissoc student :key) :kind "student") (:key new)))
      (str (:number new)))))

(defn call-with-authentication [thunk]
  (let [users (UserServiceFactory/getUserService)
        user (.getCurrentUser users)]
    (if (and user
             (some #(= (.getEmail user) %)
                   #{"mulkiatsch@gmail.com"
                     "gpmfuchs@gmx.de"
                     "kilian@fachschaften.uni-muenchen.de"
                     "schwicht@mathematik.uni-muenchen.de"}))
      (thunk)
      (redirect-to (.createLoginURL users "/")))))

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

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

(defservice logikorr)