1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.migrations.db
21 
22 import java.sql.Connection
23 import java.util.UUID
24 import scala.collection.mutable.HashSet
25 
26 class V126_1__Add_abusive_proposers_tpl_kinds extends Migration {
27 
28   private def nextId(): String = UUID.randomUUID().toString
29 
30   private val isProd = System.getenv("ENV_NAME") == "prod"
31 
32   private val templateAbusiveWarnIds: Map[String, (String, String)] =
33     Map("fr" -> ("3520961", "3520884"), "en" -> ("3536356", "3536356"), "de" -> ("3536371", "3536371"))
34 
35   private val templateAbusiveBlockIds: Map[String, (String, String)] =
36     Map("fr" -> ("3520981", "3520933"), "en" -> ("3536302", "3536302"), "de" -> ("3536641", "3536641"))
37 
38   private def makeAbusiveWarnTemplateRow(language: String): String = {
39     val (preprod, prod) = templateAbusiveWarnIds.getOrElse(language, templateAbusiveWarnIds("fr"))
40     s"('${nextId()}', 'MessageToAbusiveWarn', '$language', '${if (isProd) prod else preprod}')"
41   }
42 
43   private def makeAbusiveBlockTemplateRow(language: String): String = {
44     val (preprod, prod) = templateAbusiveBlockIds.getOrElse(language, templateAbusiveBlockIds("fr"))
45     s"('${nextId()}', 'MessageToAbusiveBlock', '$language', '${if (isProd) prod else preprod}')"
46   }
47 
48   @SuppressWarnings(Array("org.wartremover.warts.While", "org.wartremover.warts.MutableDataStructures"))
49   override def migrate(connection: Connection): Unit = {
50 
51     def insertTplRow(row: String): Unit =
52       connection
53         .prepareStatement(s"INSERT INTO crm_language_template (id, kind, language, template) VALUES $row")
54         .execute()
55 
56     val resultSet =
57       connection
58         .prepareStatement("SELECT language FROM crm_language_template")
59         .executeQuery()
60 
61     val languages = HashSet.empty[String]
62 
63     while (resultSet.next()) {
64       languages += resultSet.getString("language")
65     }
66 
67     languages.foreach(lang => {
68       insertTplRow(makeAbusiveWarnTemplateRow(lang))
69       insertTplRow(makeAbusiveBlockTemplateRow(lang))
70     })
71   }
72 }
Line Stmt Id Pos Tree Symbol Tests Code
28 20865 971 - 997 Apply java.util.UUID.toString java.util.UUID.randomUUID().toString()
30 19937 1022 - 1057 Apply java.lang.Object.== org.make.api.databasetest java.lang.System.getenv("ENV_NAME").==("prod")
33 21526 1137 - 1141 Literal <nosymbol> org.make.api.databasetest "fr"
33 20823 1201 - 1231 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3536371", "3536371")
33 21756 1201 - 1205 Literal <nosymbol> org.make.api.databasetest "de"
33 20009 1137 - 1167 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("fr").->[(String, String)](scala.Tuple2.apply[String, String]("3520961", "3520884"))
33 20221 1169 - 1199 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("en").->[(String, String)](scala.Tuple2.apply[String, String]("3536356", "3536356"))
33 19850 1201 - 1231 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("de").->[(String, String)](scala.Tuple2.apply[String, String]("3536371", "3536371"))
33 21378 1133 - 1232 Apply scala.collection.MapFactory.apply org.make.api.databasetest scala.Predef.Map.apply[String, (String, String)](scala.Predef.ArrowAssoc[String]("fr").->[(String, String)](scala.Tuple2.apply[String, String]("3520961", "3520884")), scala.Predef.ArrowAssoc[String]("en").->[(String, String)](scala.Tuple2.apply[String, String]("3536356", "3536356")), scala.Predef.ArrowAssoc[String]("de").->[(String, String)](scala.Tuple2.apply[String, String]("3536371", "3536371")))
33 20977 1137 - 1167 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3520961", "3520884")
33 21587 1169 - 1173 Literal <nosymbol> org.make.api.databasetest "en"
33 20737 1169 - 1199 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3536356", "3536356")
36 20189 1345 - 1375 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3536302", "3536302")
36 20960 1309 - 1408 Apply scala.collection.MapFactory.apply org.make.api.databasetest scala.Predef.Map.apply[String, (String, String)](scala.Predef.ArrowAssoc[String]("fr").->[(String, String)](scala.Tuple2.apply[String, String]("3520981", "3520933")), scala.Predef.ArrowAssoc[String]("en").->[(String, String)](scala.Tuple2.apply[String, String]("3536302", "3536302")), scala.Predef.ArrowAssoc[String]("de").->[(String, String)](scala.Tuple2.apply[String, String]("3536641", "3536641")))
36 21594 1313 - 1343 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("fr").->[(String, String)](scala.Tuple2.apply[String, String]("3520981", "3520933"))
36 20015 1313 - 1343 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3520981", "3520933")
36 20652 1345 - 1349 Literal <nosymbol> org.make.api.databasetest "en"
36 21441 1377 - 1407 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("de").->[(String, String)](scala.Tuple2.apply[String, String]("3536641", "3536641"))
36 21728 1345 - 1375 Apply scala.Predef.ArrowAssoc.-> org.make.api.databasetest scala.Predef.ArrowAssoc[String]("en").->[(String, String)](scala.Tuple2.apply[String, String]("3536302", "3536302"))
36 20959 1313 - 1317 Literal <nosymbol> org.make.api.databasetest "fr"
36 20830 1377 - 1381 Literal <nosymbol> org.make.api.databasetest "de"
36 19855 1377 - 1407 Apply scala.Tuple2.apply org.make.api.databasetest scala.Tuple2.apply[String, String]("3536641", "3536641")
39 19973 1490 - 1490 Select scala.Tuple2._1 x$1._1
39 21556 1499 - 1499 Select scala.Tuple2._2 x$1._2
44 19763 1771 - 1771 Select scala.Tuple2._2 x$2._2
44 20656 1762 - 1762 Select scala.Tuple2._1 x$2._1
54 21732 2167 - 2303 Apply java.sql.PreparedStatement.execute connection.prepareStatement(("INSERT INTO crm_language_template (id, kind, language, template) VALUES ".+(row): String)).execute()
54 20753 2301 - 2301 Literal <nosymbol> ()
59 19814 2331 - 2437 Apply java.sql.PreparedStatement.executeQuery org.make.api.databasetest connection.prepareStatement("SELECT language FROM crm_language_template").executeQuery()
61 21522 2459 - 2480 TypeApply scala.collection.mutable.HashSet.empty org.make.api.databasetest scala.collection.mutable.HashSet.empty[String]
63 21690 2486 - 2486 Literal <nosymbol> org.make.api.databasetest ()
63 21008 2493 - 2509 Apply java.sql.ResultSet.next org.make.api.databasetest resultSet.next()
63 19772 2519 - 2563 Block <nosymbol> { languages.+=(resultSet.getString("language")); while$1() }
63 20740 2486 - 2486 Block <nosymbol> org.make.api.databasetest ()
64 21561 2519 - 2563 Apply scala.collection.mutable.Growable.+= languages.+=(resultSet.getString("language"))
64 19978 2532 - 2563 Apply java.sql.ResultSet.getString resultSet.getString("language")
64 20623 2529 - 2529 Apply org.make.api.migrations.db.V126_1__Add_abusive_proposers_tpl_kinds.while$1 while$1()
67 21537 2575 - 2716 Apply scala.collection.mutable.HashSet.foreach org.make.api.databasetest languages.foreach[Unit](((lang: String) => { insertTplRow(V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveWarnTemplateRow(lang)); insertTplRow(V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveBlockTemplateRow(lang)) }))
68 21479 2609 - 2655 Apply org.make.api.migrations.db.V126_1__Add_abusive_proposers_tpl_kinds.insertTplRow insertTplRow(V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveWarnTemplateRow(lang))
68 19822 2622 - 2654 Apply org.make.api.migrations.db.V126_1__Add_abusive_proposers_tpl_kinds.makeAbusiveWarnTemplateRow V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveWarnTemplateRow(lang)
69 20012 2662 - 2709 Apply org.make.api.migrations.db.V126_1__Add_abusive_proposers_tpl_kinds.insertTplRow insertTplRow(V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveBlockTemplateRow(lang))
69 20572 2675 - 2708 Apply org.make.api.migrations.db.V126_1__Add_abusive_proposers_tpl_kinds.makeAbusiveBlockTemplateRow V126_1__Add_abusive_proposers_tpl_kinds.this.makeAbusiveBlockTemplateRow(lang)