SchemaInputObjects.scala 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2020 Prasoon Joshi
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. package graphql
  18. import sangria.macros.derive._
  19. import sangria.schema.{Argument, IDType}
  20. import sangria.marshalling.playJson._
  21. import play.api.libs.json._
  22. import models.NiosxUser
  23. import sangria.schema.EnumType
  24. import sangria.schema.EnumValue
  25. import repository.{NiosxAdminRole, NiosxPartnerRole, NiosxPatronRole}
  26. import models.{
  27. Subject,
  28. Person,
  29. Place,
  30. Partner,
  31. MediaType,
  32. DateRange,
  33. Language
  34. }
  35. import sangria.schema.InputObjectType
  36. import java.time.LocalDate
  37. import sangria.schema.InputField
  38. object SchemaInputObjects {
  39. val IdArg = Argument("id", IDType, description = "Id of the object.")
  40. case class DocumentInput(id: String)
  41. implicit val documentInputFormat = Json.format[DocumentInput]
  42. implicit val DocumentInputType = deriveInputObjectType[DocumentInput](
  43. InputObjectTypeName("DocumentInput")
  44. )
  45. val documentInputArg = Argument("docuIngest", DocumentInputType)
  46. case class MappingPair(field: String, columnName: String)
  47. implicit val mappingPairFormat = Json.format[MappingPair]
  48. implicit val MappingPairType = deriveInputObjectType[MappingPair](
  49. InputObjectTypeName("MappingPair")
  50. )
  51. object ValidField extends Enumeration {
  52. type AllowedField = Value
  53. val ME_recordId,
  54. ME_agencyCode,
  55. ME_unitId,
  56. ME_title,
  57. ME_creator,
  58. ME_dateOfCreation,
  59. ME_extent,
  60. ME_level,
  61. ME_image,
  62. ME_subject,
  63. ME_description,
  64. ME_location,
  65. ME_accessRestrict,
  66. ME_useRestrict,
  67. ME_language, //TODO: Change to Language type
  68. ME_unitType,
  69. ME_format = Value
  70. }
  71. implicit val afFormat = Json.formatEnum(ValidField)
  72. implicit val AllowedFieldType = deriveEnumType[ValidField.AllowedField]()
  73. type FlatRecord = Map[String, String]
  74. case class FlatRecordMapper(
  75. field: ValidField.AllowedField,//ValidField.Value,
  76. columnName: String,
  77. default: String)
  78. implicit val flatRecFormat = Json.format[FlatRecordMapper]
  79. implicit val FlatRecordMapperType = deriveInputObjectType[FlatRecordMapper](
  80. InputObjectTypeName("FlatRecordMapper"),
  81. InputObjectTypeDescription("""Map from a flat-record to any level of
  82. nested record structure. Ensure column names are unique.""")
  83. )
  84. type Crosswalk = List[FlatRecordMapper]
  85. case class IngestWithCrosswalk(url: String, crosswalk: Crosswalk)
  86. implicit val ingestWithCrosswalkFormat = Json.format[IngestWithCrosswalk]
  87. implicit val IngestWithCrosswalkType = deriveInputObjectType[IngestWithCrosswalk](
  88. InputObjectTypeName("IngestWithCrosswalk")
  89. )
  90. val ingestWithCrosswalkInputArg = Argument("ingestWithCrosswalk",
  91. IngestWithCrosswalkType)
  92. import models.SangriaMiter._
  93. implicit val typFormat = Json.formatEnum(models.EntityType)
  94. implicit val partnerInputFormat = Json.format[Partner]
  95. case class CsvIngestInput(
  96. filePath: String,
  97. crosswalk: Crosswalk,
  98. partner: Partner)
  99. implicit val PartnerInputType = deriveInputObjectType[Partner](
  100. InputObjectTypeName("PartnerInput"))
  101. // ExcludeInputFields("typ"))
  102. implicit val csvIngestInputFormat = Json.format[CsvIngestInput]
  103. implicit val CsvIngestInputType = deriveInputObjectType[CsvIngestInput](
  104. InputObjectTypeName("CsvIngestInput"))
  105. val CsvIngestInputArg = Argument("csvIngestInput", CsvIngestInputType)
  106. case class MilliEntityInput(agencyCode: String,
  107. recordId: String,
  108. unitId: String,
  109. title: String,
  110. creator: String,
  111. dateOfCreation: String,
  112. extent: String,
  113. level: String,
  114. partner: String,
  115. description: Option[String],
  116. location: Option[String],
  117. accessRestrict: Option[String],
  118. useRestrict: Option[String],
  119. language: Option[String],
  120. unitType: Option[String],
  121. format: Option[String])
  122. implicit val MilliEntityInputFormat = Json.format[MilliEntityInput]
  123. implicit val MilliEntityInputType = deriveInputObjectType[MilliEntityInput](
  124. InputObjectTypeName("NiosxEntityInput"))
  125. val MilliEntityInputArg = Argument("niosxEntityInput",
  126. MilliEntityInputType)
  127. case class DateRangeInput(from: String, to: String)
  128. implicit val DateRangeInputType = deriveInputObjectType[DateRangeInput](
  129. InputObjectTypeName("DateRangeInput"),
  130. InputObjectTypeDescription("ISO-8601 formatted string of format: 2017-06-25")
  131. )
  132. type GraphId = String
  133. case class EntityFilterInput(
  134. blob: String,
  135. dateRange: Option[DateRangeInput],
  136. lang: Option[Seq[GraphId]],
  137. subjects: Option[Seq[GraphId]],
  138. people: Option[Seq[GraphId]],
  139. places: Option[Seq[GraphId]],
  140. partners: Option[Seq[GraphId]])
  141. case class EntityFilterInputAlt(
  142. blob: String,
  143. dateRange: Option[DateRangeInput],
  144. lang: Option[Seq[Language]],
  145. subjects: Option[Seq[Subject]],
  146. people: Option[Seq[Person]],
  147. places: Option[Seq[Place]],
  148. partners: Option[Seq[Partner]])//,
  149. // mediaTypes: Option[Seq[MediaType.Value]])
  150. implicit val SubjectFormat = Json.format[Subject]
  151. implicit val PersonFormat = Json.format[Person]
  152. implicit val PlaceFormat = Json.format[Place]
  153. implicit val DateRangeFormat = Json.format[DateRangeInput]
  154. implicit val LanguageFormat = Json.format[Language]
  155. implicit val MediaTypeFormat = Json.formatEnum(MediaType)
  156. implicit val EntityFilterAltFormat = Json.format[EntityFilterInputAlt]
  157. implicit val LanguageInput = deriveInputObjectType[Language](
  158. InputObjectTypeName("LanguageInput"),
  159. ExcludeInputFields("typ"))
  160. implicit val SubjectInput = deriveInputObjectType[Subject](
  161. InputObjectTypeName("SubjectInput"),
  162. ExcludeInputFields("typ"))
  163. implicit val PersonInput = deriveInputObjectType[Person](
  164. InputObjectTypeName("PersonInput"),
  165. ExcludeInputFields("typ"))
  166. implicit val PlaceInput = deriveInputObjectType[Place](
  167. InputObjectTypeName("PlaceInput"),
  168. ExcludeInputFields("typ"))
  169. implicit val MediaTypeInput = deriveEnumType[MediaType.Value]()
  170. implicit val EntityFilterType = deriveInputObjectType[EntityFilterInput](
  171. InputObjectTypeName("EntityFilterInput"))
  172. implicit val EntityFilterFormat = Json.format[EntityFilterInput]
  173. val EntityFilterInputArg = Argument("entityFilterInputArg", EntityFilterType)
  174. //User
  175. //TODO: Change {UserInput} to something better. Use a Agent type
  176. //and derive different types of agents that would interact with the
  177. //system. There should not be any "users" of the application!
  178. //TODO: Enum serializer.
  179. // import repository.UserRole
  180. // implicit val UserRoleType = EnumType[UserRole]("UserRole",
  181. // Some("Patron's role"),
  182. // List(
  183. // EnumValue("Admin", value = NiosxAdmin),
  184. // EnumValue("Partner", value = NiosxPartner),
  185. // EnumValue("Patron", value = NiosxPatron)))
  186. // implicit val UserRoleFormat = Json.format[UserRole]
  187. case class UserInput(username: String,
  188. password: String,
  189. role: String)
  190. implicit val UserInputFormat = Json.format[UserInput]
  191. implicit val UserInputType = deriveInputObjectType[UserInput](
  192. InputObjectTypeName("NiosxUser"))
  193. val UserInputArg = Argument("niosxUserInput",
  194. UserInputType)
  195. // implicit val UserInputType = deriveInputObjectType[NiosxUser](
  196. // InputObjectTypeName("patron"),
  197. // InputObjectTypeDescription("niosx patrons"),
  198. // DocumentInputField("permissions",
  199. // "Each user role is associated with a list of permissions"),
  200. // ExcludeInputFields("passHash"))
  201. //Annotations
  202. case class BodyInput(typ: String,
  203. motivation: String,
  204. value: String,
  205. format: String,
  206. language: String,
  207. creator: String,
  208. concept: Option[String] = None)
  209. implicit val BodyInputFormat = Json.format[BodyInput]
  210. implicit val BodyInputType = deriveInputObjectType[BodyInput](
  211. InputObjectTypeName("BodyInput")
  212. )
  213. case class TargetInput(source: String, targetId: String)
  214. implicit val TargetInputFormat = Json.format[TargetInput]
  215. implicit val TargetInputType = deriveInputObjectType[TargetInput](
  216. InputObjectTypeName("Target")
  217. )
  218. //TODO: Move concept from AnnotationInput to BodyInput
  219. case class AnnotationInput(targetId: String,
  220. motivation: String,
  221. body: BodyInput,
  222. target: TargetInput,
  223. concept: Option[String])
  224. implicit val AnnotationInputFormat = Json.format[AnnotationInput]
  225. implicit val AnnotationInputType = deriveInputObjectType[AnnotationInput](
  226. InputObjectTypeName("AnnotationInput")
  227. )
  228. val AnnotationInputArg = Argument("annotation",
  229. AnnotationInputType)
  230. case class SubjectAnnoInput(targetId: String,
  231. subjectId: Option[String],
  232. prefLabel: Option[String])
  233. implicit val SubjectAnnoInFormat = Json.format[SubjectAnnoInput]
  234. implicit val SubAnnoInType = deriveInputObjectType[SubjectAnnoInput](
  235. InputObjectTypeName("SubjectAnnoInput")
  236. )
  237. val SubjectAnnoInArg = Argument("subjectAnno", SubAnnoInType)
  238. case class CommentInput(targetId: String,
  239. value: String)
  240. implicit val CommentInputFormat = Json.format[CommentInput]
  241. implicit val CommentInputType = deriveInputObjectType[CommentInput](
  242. InputObjectTypeName("Comment")
  243. )
  244. val CommentInputArg = Argument("comment", CommentInputType)
  245. case class SubjectIn(prefLabel: String, inScheme: String, id: String)
  246. implicit val SubjectInFormat = Json.format[SubjectIn]
  247. implicit val SubjectInType = deriveInputObjectType[SubjectIn](
  248. InputObjectTypeName("SubjectIn")
  249. )
  250. val SubjectInArg = Argument("SubjectIn", SubjectInType)
  251. }