list.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package list
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "text/template"
  10. "time"
  11. "github.com/remogatto/cloud"
  12. karmen_client "gogs.carducci-dante.gov.it/karmen/client"
  13. "gogs.carducci-dante.gov.it/karmen/core/config"
  14. "gogs.carducci-dante.gov.it/karmen/core/orm"
  15. "gogs.carducci-dante.gov.it/karmen/util/fileutil"
  16. "gogs.carducci-dante.gov.it/karmen/util/libreoffice"
  17. "gogs.carducci-dante.gov.it/karmen/util/pandoc"
  18. tpl_util "gogs.carducci-dante.gov.it/karmen/util/template"
  19. )
  20. type ListGenerator struct {
  21. Config *config.ConfigT
  22. }
  23. func NewListGenerator(config *config.ConfigT) *ListGenerator {
  24. return &ListGenerator{config}
  25. }
  26. var funcMap template.FuncMap = template.FuncMap{
  27. "comma": comma,
  28. "groupClasses": groupClasses,
  29. "abbrev": abbrev,
  30. "nbsp": nbsp,
  31. }
  32. func (generator *ListGenerator) prepareRemoteFolder() {
  33. files, err := filepath.Glob("!*.go")
  34. if err != nil {
  35. log.Println(err)
  36. }
  37. log.Println(files)
  38. }
  39. func (generator *ListGenerator) generate(outputPath string, teachers []*orm.Teacher, funcMap template.FuncMap) {
  40. filename := filepath.Join(outputPath, "elenco_docenti")
  41. // tplContent, err := ncClient.Download("")
  42. tpl, err := tpl_util.LoadTextTemplateFromString("./generator/generators/list/list.tpl.md", funcMap)
  43. if err != nil {
  44. panic(err)
  45. }
  46. f, err := os.Create(filename + ".md")
  47. if err != nil {
  48. panic(err)
  49. }
  50. defer f.Close()
  51. err = tpl.Execute(f, teachers)
  52. if err != nil {
  53. panic(err)
  54. }
  55. odtFilename := fileutil.ReplaceExt(filename, "odt")
  56. log.Println("Generate", odtFilename)
  57. if err := pandoc.Convert(filename+".md", odtFilename, "--data-dir", "./generator/generators/list/"); err != nil {
  58. panic(err)
  59. }
  60. if err := libreoffice.Convert(filename+".odt", "pdf", "--outdir", outputPath); err != nil {
  61. panic(err)
  62. }
  63. }
  64. func comma(classes []*orm.Class) string {
  65. var names []string
  66. for _, c := range classes {
  67. names = append(names, c.Name)
  68. }
  69. return strings.Join(names, ",")
  70. }
  71. func abbrev(name string) string {
  72. var result string
  73. splits := strings.Split(name, " ")
  74. for i := len(splits) - 1; i >= 0; i-- {
  75. if i == len(splits)-1 {
  76. result += splits[i] + " "
  77. continue
  78. }
  79. result += strings.ToUpper(string(splits[i][0]) + ".")
  80. }
  81. return result
  82. }
  83. func nbsp(num int, text string) string {
  84. return text + strings.Repeat(" ", num)
  85. }
  86. func groupClasses(classes []*orm.Class) string {
  87. var groups []string
  88. groupByAddresses := make(map[string][]string)
  89. addressesAbbrev := map[string]string{
  90. "Linguistico": "LIN",
  91. "Classico": "CLA",
  92. "Musicale": "M",
  93. "Economico sociale": "ES",
  94. "Scienze umane": "SU",
  95. }
  96. for _, c := range classes {
  97. groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
  98. }
  99. for address, classes := range groupByAddresses {
  100. var group string
  101. if len(classes) > 1 {
  102. group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
  103. } else {
  104. group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
  105. }
  106. groups = append(groups, group)
  107. }
  108. return strings.Join(groups, ",")
  109. }
  110. func (generator *ListGenerator) Run(jobId uint) {
  111. ncClient, err := cloud.Dial(
  112. config.Config.Cloud.Url,
  113. config.Config.Cloud.Username,
  114. config.Config.Cloud.Password,
  115. )
  116. if err != nil {
  117. panic(err)
  118. }
  119. log.Printf("Connecting to karmen...")
  120. client, err := karmen_client.Dial(
  121. config.Config.Url,
  122. config.Config.Admin.Username,
  123. config.Config.Admin.Password,
  124. )
  125. if err != nil {
  126. log.Println(err)
  127. }
  128. job, err := client.GetJob(jobId)
  129. if err != nil {
  130. panic(err)
  131. }
  132. job.Start = time.Now()
  133. err = client.UpdateJob(job)
  134. if err != nil {
  135. panic(err)
  136. }
  137. teachers, err := client.GetTeachers()
  138. if err != nil {
  139. panic(err)
  140. }
  141. outputPath := filepath.Join(config.Config.Documents.OutputPath, fmt.Sprintf("%d/%d", job.DocumentID, job.ID))
  142. if err := os.MkdirAll(outputPath, 0777); err != nil {
  143. panic(err)
  144. }
  145. generator.generate(outputPath, teachers, funcMap)
  146. job.End = time.Now()
  147. job.Files = append(job.Files, &orm.File{Path: "elenco_docenti.pdf"})
  148. time.Sleep(10 * time.Second)
  149. err = client.UpdateJob(job)
  150. if err != nil {
  151. panic(err)
  152. }
  153. log.Println("Uploading files to the cloud...")
  154. src, err := ioutil.ReadFile(filepath.Join(outputPath, "elenco_docenti.pdf"))
  155. if err != nil {
  156. panic(err)
  157. }
  158. err = ncClient.Upload(src, "Documents/elenco_docenti.pdf")
  159. if err != nil {
  160. panic(err)
  161. }
  162. log.Println("Files were uploaded to the cloud...")
  163. }