1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2020 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.crmTemplates
21 
22 import org.make.api.extensions.MakeDBExecutionContextComponent
23 import org.make.api.technical.ShortenedNames
24 import org.make.api.technical.DatabaseTransactions._
25 import org.make.api.technical.ScalikeSupport._
26 import org.make.core.crmTemplate.{CrmQuestionTemplate, CrmQuestionTemplateId}
27 import org.make.core.question.QuestionId
28 import org.make.core.reference.Language
29 import scalikejdbc._
30 
31 import scala.concurrent.Future
32 
33 trait DefaultPersistentCrmQuestionTemplateServiceComponent extends PersistentCrmQuestionTemplateServiceComponent {
34   this: MakeDBExecutionContextComponent =>
35 
36   override lazy val persistentCrmQuestionTemplateService: PersistentCrmQuestionTemplateService =
37     new DefaultPersistentCrmQuestionTemplateService
38 
39   class DefaultPersistentCrmQuestionTemplateService extends PersistentCrmQuestionTemplateService with ShortenedNames {
40 
41     private val templates = SQLSyntaxSupportFactory[CrmQuestionTemplate]()
42     private val t = templates.syntax
43 
44     override def list(questionId: QuestionId): Future[Seq[CrmQuestionTemplate]] = {
45       implicit val context: EC = readExecutionContext
46       Future(NamedDB("READ").retryableTx { implicit session =>
47         withSQL { select.from(templates.as(t)).where.eq(t.questionId, questionId) }
48           .map(templates.apply(t.resultName))
49           .list()
50       })
51     }
52 
53     override def listByLanguage(questionId: QuestionId, language: Language): Future[Seq[CrmQuestionTemplate]] = {
54       implicit val context: EC = readExecutionContext
55       Future(NamedDB("READ").retryableTx { implicit session =>
56         withSQL {
57           select
58             .from(templates.as(t))
59             .where
60             .eq(t.questionId, questionId)
61             .and
62             .eq(t.language, language)
63         }.map(templates.apply(t.resultName))
64           .list()
65       })
66     }
67 
68     override def get(id: CrmQuestionTemplateId): Future[Option[CrmQuestionTemplate]] = {
69       implicit val context: EC = readExecutionContext
70       Future(NamedDB("READ").retryableTx { implicit session =>
71         withSQL { select.from(templates.as(t)).where.eq(t.id, id) }
72           .map(templates.apply(t.resultName))
73           .single()
74       })
75     }
76 
77     override def persist(template: CrmQuestionTemplate): Future[CrmQuestionTemplate] = {
78       implicit val context: EC = writeExecutionContext
79       Future(NamedDB("WRITE").retryableTx { implicit session =>
80         withSQL { insert.into(templates).namedValues(autoNamedValues(template, templates.column)) }.update()
81       }).map(_ => template)
82     }
83 
84     override def modify(template: CrmQuestionTemplate): Future[CrmQuestionTemplate] = {
85       implicit val context: EC = writeExecutionContext
86       Future(NamedDB("WRITE").retryableTx { implicit session =>
87         withSQL {
88           update(templates).set(autoNamedValues(template, templates.column, "id")).where.eq(t.id, template.id)
89         }.update()
90       }).map(_ => template)
91     }
92 
93     override def remove(id: CrmQuestionTemplateId): Future[Unit] = {
94       implicit val context: EC = readExecutionContext
95       Future(NamedDB("WRITE").retryableTx { implicit session =>
96         withSQL { delete.from(templates).where.eq(t.id, id) }.execute()
97       })
98     }
99   }
100 
101 }
Line Stmt Id Pos Tree Symbol Tests Code
42 20898 1725 - 1741 Select scalikejdbc.SQLSyntaxSupportFeature.SQLSyntaxSupport.syntax org.scalatest.testsuite DefaultPersistentCrmQuestionTemplateService.this.templates.syntax
45 19942 1860 - 1880 Select org.make.api.extensions.MakeDBExecutionContextComponent.readExecutionContext DefaultPersistentCrmQuestionTemplateServiceComponent.this.readExecutionContext
46 21563 1887 - 2100 ApplyToImplicitArgs scala.concurrent.Future.apply scala.concurrent.Future.apply[List[org.make.core.crmTemplate.CrmQuestionTemplate]]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("READ") = "READ"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("READ", x$2); scalikejdbc.NamedDB.apply("READ", x$2)(x$3) }); <artifact> val x$7: scalikejdbc.DBSession => List[org.make.core.crmTemplate.CrmQuestionTemplate] @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => { <synthetic> <stable> <artifact> val stabilizer$1: scalikejdbc.SQLToList[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = scalikejdbc.`package`.withSQL.apply[Nothing](scalikejdbc.`package`.select.from[Nothing](DefaultPersistentCrmQuestionTemplateService.this.templates.as(DefaultPersistentCrmQuestionTemplateService.this.t)).where.eq[org.make.core.question.QuestionId]((DefaultPersistentCrmQuestionTemplateService.this.t.field("questionId"): scalikejdbc.interpolation.SQLSyntax), questionId)(org.make.api.technical.ScalikeSupport.questionIdBinders)).map[org.make.core.crmTemplate.CrmQuestionTemplate](((rs: scalikejdbc.WrappedResultSet) => DefaultPersistentCrmQuestionTemplateService.this.templates.apply(DefaultPersistentCrmQuestionTemplateService.this.t.resultName)(rs))).list; { <artifact> val x$4: scalikejdbc.DBSession = session; <artifact> val x$5: scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] =:= scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = GeneralizedTypeConstraintsForWithExtractor.this.=:=.tpEquals[scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor]]; <artifact> val x$6: scalikejdbc.ConnectionPoolContext = stabilizer$1.apply$default$2(); stabilizer$1.apply()(x$4, x$6, x$5) } }); <artifact> val x$8: scalikejdbc.TxBoundary[List[org.make.core.crmTemplate.CrmQuestionTemplate]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[List[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7); qual$1.retryableTx[List[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7)(x$8) })(context)
54 20660 2255 - 2275 Select org.make.api.extensions.MakeDBExecutionContextComponent.readExecutionContext DefaultPersistentCrmQuestionTemplateServiceComponent.this.readExecutionContext
55 20219 2282 - 2596 ApplyToImplicitArgs scala.concurrent.Future.apply scala.concurrent.Future.apply[List[org.make.core.crmTemplate.CrmQuestionTemplate]]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("READ") = "READ"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("READ", x$2); scalikejdbc.NamedDB.apply("READ", x$2)(x$3) }); <artifact> val x$7: scalikejdbc.DBSession => List[org.make.core.crmTemplate.CrmQuestionTemplate] @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => { <synthetic> <stable> <artifact> val stabilizer$1: scalikejdbc.SQLToList[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = scalikejdbc.`package`.withSQL.apply[Nothing](scalikejdbc.`package`.select.from[Nothing](DefaultPersistentCrmQuestionTemplateService.this.templates.as(DefaultPersistentCrmQuestionTemplateService.this.t)).where.eq[org.make.core.question.QuestionId]((DefaultPersistentCrmQuestionTemplateService.this.t.field("questionId"): scalikejdbc.interpolation.SQLSyntax), questionId)(org.make.api.technical.ScalikeSupport.questionIdBinders).and.eq[org.make.core.reference.Language]((DefaultPersistentCrmQuestionTemplateService.this.t.field("language"): scalikejdbc.interpolation.SQLSyntax), language)(org.make.api.technical.ScalikeSupport.languageBinders)).map[org.make.core.crmTemplate.CrmQuestionTemplate](((rs: scalikejdbc.WrappedResultSet) => DefaultPersistentCrmQuestionTemplateService.this.templates.apply(DefaultPersistentCrmQuestionTemplateService.this.t.resultName)(rs))).list; { <artifact> val x$4: scalikejdbc.DBSession = session; <artifact> val x$5: scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] =:= scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = GeneralizedTypeConstraintsForWithExtractor.this.=:=.tpEquals[scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor]]; <artifact> val x$6: scalikejdbc.ConnectionPoolContext = stabilizer$1.apply$default$2(); stabilizer$1.apply()(x$4, x$6, x$5) } }); <artifact> val x$8: scalikejdbc.TxBoundary[List[org.make.core.crmTemplate.CrmQuestionTemplate]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[List[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7); qual$1.retryableTx[List[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7)(x$8) })(context)
69 21692 2726 - 2746 Select org.make.api.extensions.MakeDBExecutionContextComponent.readExecutionContext org.scalatest.testsuite DefaultPersistentCrmQuestionTemplateServiceComponent.this.readExecutionContext
70 20741 2753 - 2952 ApplyToImplicitArgs scala.concurrent.Future.apply org.scalatest.testsuite scala.concurrent.Future.apply[Option[org.make.core.crmTemplate.CrmQuestionTemplate]]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("READ") = "READ"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("READ", x$2); scalikejdbc.NamedDB.apply("READ", x$2)(x$3) }); <artifact> val x$7: scalikejdbc.DBSession => Option[org.make.core.crmTemplate.CrmQuestionTemplate] @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => { <synthetic> <stable> <artifact> val stabilizer$1: scalikejdbc.SQLToOption[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = scalikejdbc.`package`.withSQL.apply[Nothing](scalikejdbc.`package`.select.from[Nothing](DefaultPersistentCrmQuestionTemplateService.this.templates.as(DefaultPersistentCrmQuestionTemplateService.this.t)).where.eq[org.make.core.crmTemplate.CrmQuestionTemplateId]((DefaultPersistentCrmQuestionTemplateService.this.t.field("id"): scalikejdbc.interpolation.SQLSyntax), id)(org.make.api.technical.ScalikeSupport.crmQuestionTemplateIdBinders)).map[org.make.core.crmTemplate.CrmQuestionTemplate](((rs: scalikejdbc.WrappedResultSet) => DefaultPersistentCrmQuestionTemplateService.this.templates.apply(DefaultPersistentCrmQuestionTemplateService.this.t.resultName)(rs))).single; { <artifact> val x$4: scalikejdbc.DBSession = session; <artifact> val x$5: scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] =:= scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = GeneralizedTypeConstraintsForWithExtractor.this.=:=.tpEquals[scalikejdbc.SQL[org.make.core.crmTemplate.CrmQuestionTemplate,scalikejdbc.HasExtractor]]; <artifact> val x$6: scalikejdbc.ConnectionPoolContext = stabilizer$1.apply$default$2(); stabilizer$1.apply()(x$4, x$6, x$5) } }); <artifact> val x$8: scalikejdbc.TxBoundary[Option[org.make.core.crmTemplate.CrmQuestionTemplate]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[Option[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7); qual$1.retryableTx[Option[org.make.core.crmTemplate.CrmQuestionTemplate]](x$7)(x$8) })(context)
78 19821 3082 - 3103 Select org.make.api.extensions.MakeDBExecutionContextComponent.writeExecutionContext org.scalatest.testsuite DefaultPersistentCrmQuestionTemplateServiceComponent.this.writeExecutionContext
81 21533 3110 - 3304 ApplyToImplicitArgs scala.concurrent.Future.map org.scalatest.testsuite scala.concurrent.Future.apply[Int]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("WRITE") = "WRITE"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("WRITE", x$2); scalikejdbc.NamedDB.apply("WRITE", x$2)(x$3) }); <artifact> val x$4: scalikejdbc.DBSession => Int @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => scalikejdbc.`package`.withSQL.apply[scalikejdbc.UpdateOperation](scalikejdbc.`package`.insert.into(DefaultPersistentCrmQuestionTemplateService.this.templates).namedValues((scala.collection.immutable.Map.apply[scalikejdbc.interpolation.SQLSyntax, scalikejdbc.ParameterBinder]((DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("id"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.crmTemplate.CrmQuestionTemplateId](template.id)(org.make.api.technical.ScalikeSupport.crmQuestionTemplateIdBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("kind"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.crmTemplate.CrmTemplateKind](template.kind)(org.make.api.technical.ScalikeSupport.crmTemplateKindBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("questionId"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.question.QuestionId](template.questionId)(org.make.api.technical.ScalikeSupport.questionIdBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("template"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.crmTemplate.TemplateId](template.template)(org.make.api.technical.ScalikeSupport.templateIdBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("language"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.reference.Language](template.language)(org.make.api.technical.ScalikeSupport.languageBinders)): Map[scalikejdbc.SQLSyntax,scalikejdbc.ParameterBinder]))).update.apply()(session)); <artifact> val x$5: scalikejdbc.TxBoundary[Int] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[Int](x$4); qual$1.retryableTx[Int](x$4)(x$5) })(context).map[org.make.core.crmTemplate.CrmQuestionTemplate](((x$1: Int) => template))(context)
85 21012 3433 - 3454 Select org.make.api.extensions.MakeDBExecutionContextComponent.writeExecutionContext DefaultPersistentCrmQuestionTemplateServiceComponent.this.writeExecutionContext
90 20014 3461 - 3694 ApplyToImplicitArgs scala.concurrent.Future.map scala.concurrent.Future.apply[Int]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("WRITE") = "WRITE"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("WRITE", x$2); scalikejdbc.NamedDB.apply("WRITE", x$2)(x$3) }); <artifact> val x$4: scalikejdbc.DBSession => Int @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => scalikejdbc.`package`.withSQL.apply[scalikejdbc.UpdateOperation](scalikejdbc.`package`.update.apply(DefaultPersistentCrmQuestionTemplateService.this.templates).set((scala.collection.immutable.Map.apply[scalikejdbc.interpolation.SQLSyntax, scalikejdbc.ParameterBinder]((DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("kind"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.crmTemplate.CrmTemplateKind](template.kind)(org.make.api.technical.ScalikeSupport.crmTemplateKindBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("questionId"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.question.QuestionId](template.questionId)(org.make.api.technical.ScalikeSupport.questionIdBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("template"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.crmTemplate.TemplateId](template.template)(org.make.api.technical.ScalikeSupport.templateIdBinders), (DefaultPersistentCrmQuestionTemplateService.this.templates.column.field("language"): scalikejdbc.interpolation.SQLSyntax).->[org.make.core.reference.Language](template.language)(org.make.api.technical.ScalikeSupport.languageBinders)): Map[scalikejdbc.SQLSyntax,scalikejdbc.ParameterBinder])).where.eq[org.make.core.crmTemplate.CrmQuestionTemplateId]((DefaultPersistentCrmQuestionTemplateService.this.t.field("id"): scalikejdbc.interpolation.SQLSyntax), template.id)(org.make.api.technical.ScalikeSupport.crmQuestionTemplateIdBinders)).update.apply()(session)); <artifact> val x$5: scalikejdbc.TxBoundary[Int] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[Int](x$4); qual$1.retryableTx[Int](x$4)(x$5) })(context).map[org.make.core.crmTemplate.CrmQuestionTemplate](((x$2: Int) => template))(context)
94 21538 3804 - 3824 Select org.make.api.extensions.MakeDBExecutionContextComponent.readExecutionContext DefaultPersistentCrmQuestionTemplateServiceComponent.this.readExecutionContext
95 20702 3831 - 3969 ApplyToImplicitArgs scala.concurrent.Future.apply scala.concurrent.Future.apply[Unit]({ { <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("WRITE") = "WRITE"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("WRITE", x$2); scalikejdbc.NamedDB.apply("WRITE", x$2)(x$3) }); <artifact> val x$4: scalikejdbc.DBSession => Boolean @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => scalikejdbc.`package`.withSQL.apply[scalikejdbc.UpdateOperation](scalikejdbc.`package`.delete.from(DefaultPersistentCrmQuestionTemplateService.this.templates).where.eq[org.make.core.crmTemplate.CrmQuestionTemplateId]((DefaultPersistentCrmQuestionTemplateService.this.t.field("id"): scalikejdbc.interpolation.SQLSyntax), id)(org.make.api.technical.ScalikeSupport.crmQuestionTemplateIdBinders)).execute.apply()(session)); <artifact> val x$5: scalikejdbc.TxBoundary[Boolean] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[Boolean](x$4); qual$1.retryableTx[Boolean](x$4)(x$5) }; () })(context)