update_proposals.rkt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #lang racket/base
  2. ;; This program updates an entry in a proposals database.
  3. (require racket/cmdline
  4. racket/date
  5. db
  6. "config.rkt") ; load configuration file
  7. (define progname "update_proposals.rkt")
  8. ; give us the date in YYYY-MM-DD format
  9. (date-display-format 'iso-8601)
  10. ; set up command line arguments
  11. (define mode (command-line
  12. #:program "update_proposals"
  13. #:args ([updatetype "help"]) ; (add, update, list-open, list-closed, help)
  14. updatetype))
  15. ; print some help
  16. (define (printhelp)
  17. (displayln (string-append "Usage: "
  18. progname " MODE"))
  19. (newline)
  20. (displayln "Where MODE is one of:")
  21. (displayln " add\t\t - add new proposal to database.")
  22. (displayln " update\t\t - update a proposal with results.")
  23. (displayln " list-open\t - Show all submitted (but not resolved) proposals.")
  24. (displayln " list-closed\t - Show all resolved proposals.")
  25. (displayln " help\t\t - Show this help message.")
  26. (newline)
  27. (displayln "Copyright 2019-2020, 2022 George C. Privon"))
  28. ; set up a condensed prompt for getting information
  29. (define (getinput prompt)
  30. (write-string prompt)
  31. (write-string ": ")
  32. (read-line))
  33. ; take an input result from the SQL search and write it out nicely
  34. (define (printentry entry issub)
  35. (displayln (string-append
  36. (number->string (vector-ref entry 0))
  37. ": "
  38. (vector-ref entry 1)
  39. "("
  40. (vector-ref entry 2)
  41. "; PI: "
  42. (vector-ref entry 4)
  43. (if (not issub)
  44. (string-append "; "
  45. (vector-ref entry 5))
  46. "")
  47. ") \""
  48. (vector-ref entry 3)
  49. "\"")))
  50. ; add a new proposal to the database
  51. (define (addnew conn)
  52. (displayln "Adding new proposal to database.")
  53. ; user inputs proposal data
  54. (define proptype (getinput "Proposal type"))
  55. (define org (getinput "Submitting organization"))
  56. (define solic (getinput "Solitation/call"))
  57. (define tele (getinput "Telescope"))
  58. (define title (getinput "Proposal title"))
  59. (define pi (getinput "PI"))
  60. (define coi (getinput "CoIs"))
  61. ; assume all these proposals are submitted, don't ask the user
  62. (define status "submitted")
  63. (define submitdate (getinput "Submit date (YYYY-MM-DD)"))
  64. (define oID (getinput "Organization's proposal ID"))
  65. ; do the INSERT into the Sqlite database
  66. (query-exec conn "INSERT INTO proposals (type, organization, solicitation, telescope, PI, title, CoI, status, submitdate, orgpropID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  67. proptype org solic tele pi title coi status submitdate oID))
  68. ; update an entry with new status (accepted, rejected, etc.)
  69. (define (update conn ID)
  70. (displayln (string-append "Updating entry " (number->string ID)))
  71. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  72. (cond
  73. [(eq? #f entry) (error "Invalid ID. Row not found")])
  74. (displayln (string-append "Current status is: "
  75. (vector-ref entry 9)
  76. " ("
  77. (vector-ref entry 10)
  78. ")"))
  79. (write-string "Please enter new status: ")
  80. (define newstatus (read-line))
  81. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  82. ;(define resdate (read-line))
  83. (define resdate (date->string (seconds->date (current-seconds))))
  84. ; now update that entry
  85. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  86. newstatus
  87. resdate
  88. ID)
  89. (displayln "Entry updated."))
  90. ; retrieve and print proposals based on status
  91. (define (printprop conn #:submitted issub)
  92. (define selclause (if issub
  93. "status='submitted'"
  94. "status!='submitted'"))
  95. (define props (query-rows conn (string-append "SELECT ID,telescope,solicitation,title,PI,status FROM proposals WHERE "
  96. selclause)))
  97. (display (string-append (number->string (length props))))
  98. (if issub
  99. (displayln " pending proposals found.")
  100. (displayln " resolved proposals found."))
  101. (newline)
  102. ; print all the unresolved proposals to the screen
  103. (map (lambda (prop)
  104. (printentry prop issub))
  105. props))
  106. ; find proposals waiting for updates
  107. (define (findpending conn)
  108. (write-string "Updating proposals. ")
  109. (printprop conn #:submitted #t)
  110. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  111. (define upID (read-line))
  112. (cond
  113. [(eq? (string->number upID) 0) (exit)]
  114. [(string->number upID) (update conn (string->number upID))]
  115. [else (exit)]))
  116. ; make sure we can use the sqlite3 connection
  117. (define checkdblib
  118. (cond (not (sqlite3-available?))
  119. (error "Sqlite3 library not available.")))
  120. ; catch-all routine for when we need to access the database
  121. (define (querysys mode)
  122. ; first see if we need write access or if we can use read only
  123. (define dbmode (if (or (regexp-match "add" mode)
  124. (regexp-match "update" mode))
  125. 'read/write
  126. 'read-only))
  127. ; open the database with the specified mode
  128. (define conn (sqlite3-connect #:database dbloc
  129. #:mode dbmode))
  130. ; now handle the user's request
  131. (cond
  132. [(regexp-match "add" mode) (addnew conn)]
  133. [(regexp-match "update" mode) (findpending conn)]
  134. [(regexp-match "list-open" mode) (printprop conn #:submitted #t)]
  135. [(regexp-match "list-closed" mode) (printprop conn #:submitted #f)]
  136. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  137. ; close the databse
  138. (disconnect conn))
  139. ; First see if the user wants help or if we need to pass to one of the other
  140. ; procedures
  141. (cond
  142. [(regexp-match "help" mode) (printhelp)]
  143. [else (querysys mode)])