/* Ċ Ġ* ĠCopyright Ġ( c ) Ġ 2 0 2 0 . ĠT ol gee Ċ Ġ*/ Ċ Ċ package Ġio . tol gee . api . v 2 . controllers . translation Ċ Ċ import Ġio . swagger . v 3 . o as . annotations . Operation Ċ import Ġio . swagger . v 3 . o as . annotations . media . Content Ċ import Ġio . swagger . v 3 . o as . annotations . media . Schema Ċ import Ġio . swagger . v 3 . o as . annotations . responses . ApiResponse Ċ import Ġio . swagger . v 3 . o as . annotations . tags . Tag Ċ import Ġio . swagger . v 3 . o as . annotations . tags . Tags Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Keys With Translations Page Model Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Keys With Translations Paged Resources Assembler Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Set Translations Response Model Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Translation History Model Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Translation History Model Assembler Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Translation Model Ċ import Ġio . tol gee . api . v 2 . h ate o as . translations . Translation Model Assembler Ċ import Ġio . tol gee . controllers . I Controller Ċ import Ġio . tol gee . d tos . Path DTO Ċ import Ġio . tol gee . d tos . query _ results . Translation History View Ċ import Ġio . tol gee . d tos . request . translation . Get Translations Params Ċ import Ġio . tol gee . d tos . request . translation . Select All Response Ċ import Ġio . tol gee . d tos . request . translation . Set Translations With Key Dto Ċ import Ġio . tol gee . d tos . request . translation . Translation Filters Ċ import Ġio . tol gee . exceptions . BadRequest Exception Ċ import Ġio . tol gee . exceptions . NotFoundException Ċ import Ġio . tol gee . model . Language Ċ import Ġio . tol gee . model . Permission Ċ import Ġio . tol gee . model . Screenshot Ċ import Ġio . tol gee . model . enums . Api Scope Ċ import Ġio . tol gee . model . enums . Translation State Ċ import Ġio . tol gee . model . key . Key Ċ import Ġio . tol gee . model . translation . Translation Ċ import Ġio . tol gee . security . Authentication Facade Ċ import Ġio . tol gee . security . api _ key _ auth . Access With ApiKey Ċ import Ġio . tol gee . security . project _ auth . Access With Any Project Permission Ċ import Ġio . tol gee . security . project _ auth . Access With Project Permission Ċ import Ġio . tol gee . security . project _ auth . Project Holder Ċ import Ġio . tol gee . service . Key Service Ċ import Ġio . tol gee . service . Language Service Ċ import Ġio . tol gee . service . Screenshot Service Ċ import Ġio . tol gee . service . Security Service Ċ import Ġio . tol gee . service . Translation Service Ċ import Ġio . tol gee . service . query _ builders . Cursor Util Ċ import Ġorg . spring doc . api . annotations . Parameter Object Ċ import Ġorg . springframework . data . domain . Page able Ċ import Ġorg . springframework . data . web . Paged Resources Assembler Ċ import Ġorg . springframework . h ate o as . Paged Model Ċ import Ġorg . springframework . web . bind . annotation . Cross Origin Ċ import Ġorg . springframework . web . bind . annotation . GetMapping Ċ import Ġorg . springframework . web . bind . annotation . PathVariable Ċ import Ġorg . springframework . web . bind . annotation . PostMapping Ċ import Ġorg . springframework . web . bind . annotation . Put Mapping Ċ import Ġorg . springframework . web . bind . annotation . RequestBody Ċ import Ġorg . springframework . web . bind . annotation . RequestMapping Ċ import Ġorg . springframework . web . bind . annotation . RestController Ċ import Ġjavax . validation . Valid Ċ Ċ @ Suppress (" MVC PathVariable Inspection ", Ġ" Spring Java Injection Points Autow iring Inspection ") Ċ @ RestController Ċ @ Cross Origin ( orig ins Ġ= Ġ[" * "]) Ċ @ RequestMapping ( ĊĠ Ġvalue Ġ= Ġ[ ĊĠĠĠ Ġ"/ v 2 / projects /{ projectId :[ 0 - 9 ]+ }/ translations ", ĊĠĠĠ Ġ"/ v 2 / projects / translations " ĊĠ Ġ] Ċ ) Ċ @ Tags ( ĊĠ Ġvalue Ġ= Ġ[ ĊĠĠĠ ĠTag ( name Ġ= Ġ" Translations ", Ġdescription Ġ= Ġ" Operations Ġrelated Ġto Ġtranslations Ġin Ġproject "), ĊĠ Ġ] Ċ ) Ċ class ĠV 2 Translations Controller ( ĊĠ Ġprivate Ġval Ġproject Holder : ĠProject Holder , ĊĠ Ġprivate Ġval Ġtranslation Service : ĠTranslation Service , ĊĠ Ġprivate Ġval Ġkey Service : ĠKey Service , ĊĠ Ġprivate Ġval Ġpage d Assembler : ĠKey sWith Translations Paged Resources Assembler , ĊĠ Ġprivate Ġval Ġhistory Paged Assembler : ĠP aged Resources Assembler < Translation History View >, ĊĠ Ġprivate Ġval Ġtranslation History Model Assembler : ĠTranslation History Model Assembler , ĊĠ Ġprivate Ġval Ġtranslation Model Assembler : ĠTranslation Model Assembler , ĊĠ Ġprivate Ġval Ġlanguage Service : ĠLanguage Service , ĊĠ Ġprivate Ġval Ġsecurity Service : ĠSecurity Service , ĊĠ Ġprivate Ġval Ġauthentication Facade : ĠAuthentication Facade , ĊĠ Ġprivate Ġval Ġscreenshot Service : ĠScreen shot Service Ċ ) Ġ: ĠI Controller Ġ{ ĊĠ Ġ@ GetMapping ( value Ġ= Ġ[" /{ languages }" ]) ĊĠ Ġ@ Access With Any Project Permission ĊĠ Ġ@ Access With ApiKey ( scopes Ġ= Ġ[ Api Scope . TRANSL ATIONS _ VIEW ]) ĊĠ Ġ@ Operation ( ĊĠĠĠ Ġsummary Ġ= Ġ" Returns Ġall Ġtranslations Ġfor Ġspecified Ġlanguages ", ĊĠĠĠ Ġresponses Ġ= Ġ[ ĊĠĠĠĠĠ ĠApiResponse ( ĊĠĠĠĠĠĠĠ Ġresponse Code Ġ= Ġ" 2 0 0 ", ĊĠĠĠĠĠĠĠ Ġcontent Ġ= Ġ[ ĊĠĠĠĠĠĠĠĠĠ ĠContent ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġschema Ġ= ĠSchema ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġexample Ġ= Ġ""" {" en ": Ġ{" what Ġa Ġkey ": Ġ" Translated Ġvalue ", Ġ" another Ġkey ": Ġ" Another Ġkey Ġtranslated "}, """ Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ""" " cs ": Ġ{" what Ġa Ġkey ": Ġ" P ÅĻ ek lad ", Ġ" another Ġkey ": Ġ" Dal Å¡ÃŃ ĠpÅĻ ek lad "}} """ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ] ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ] ĊĠ Ġ) ĊĠ Ġfun ĠgetAll Translations (@ PathVariable (" languages ") Ġlanguages : ĠSet < String >): ĠMap < String , ĠAny > Ġ{ ĊĠĠĠ Ġreturn Ġtranslation Service . get Translations ( languages , Ġproject Holder . project . id ) ĊĠ Ġ} ĊĊĠ Ġ@ Put Mapping ("") ĊĠ Ġ@ Access With ApiKey ( scopes Ġ= Ġ[ Api Scope . TRANSL ATIONS _ EDIT ]) ĊĠ Ġ@ Access With Project Permission ( permission Ġ= ĠPermission . Project Permission Type . TRANSL ATE ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ" Sets Ġtranslations Ġfor Ġexisting Ġkey ") ĊĠ Ġfun Ġset Translations (@ RequestBody Ġ@ Valid Ġdto : ĠSet Translations With Key Dto ): ĠSet Translations Response Model Ġ{ ĊĠĠĠ Ġval Ġkey Ġ= Ġkey Service . find Optional ( ĊĠĠĠĠĠ Ġproject Holder . project . id , ĊĠĠĠĠĠ ĠPath DTO . from FullPath ( dto . key ) ĊĠĠĠ Ġ). orElse Throw Ġ{ ĠNotFound Exception () Ġ} ĊĊĠĠĠ Ġval Ġmodified Translations Ġ= Ġtranslation Service . set ForKey ( key , Ġdto . translations ) ĊĊĠĠĠ Ġval Ġtranslations Ġ= Ġdto . languages To Return ĊĠĠĠĠĠ Ġ? . let Ġ{ Ġlanguages To Return Ġ-> ĊĠĠĠĠĠĠĠ Ġkey . translations ĊĠĠĠĠĠĠĠĠĠ Ġ. filter Ġ{ Ġlanguages To Return . contains ( it . language . tag ) Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. associate By Ġ{ Ġit . language . tag Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ?: Ġmodified Translations ĊĊĠĠĠ Ġreturn Ġget Set Translations Response ( key , Ġtranslations ) ĊĠ Ġ} ĊĊĠ Ġ@ PostMapping ("") ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ EDIT ]) ĊĠ Ġ@ Access With Project Permission ( permission Ġ= ĠPermission . Project Permission Type . EDIT ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ" Sets Ġtranslations Ġfor Ġexisting Ġor Ġnot Ġexisting Ġkey ") ĊĠ Ġfun Ġcreate OrUpdate Translations (@ RequestBody Ġ@ Valid Ġdto : ĠSet Translations With Key Dto ): ĠSet Translations Response Model Ġ{ ĊĠĠĠ Ġcheck Scopes If Key Exists ( dto ) ĊĠĠĠ Ġval Ġkey Ġ= Ġkey Service . get OrCreate Key ( project Holder . project Entity , ĠPath DTO . from FullPath ( dto . key )) ĊĠĠĠ Ġval Ġtranslations Ġ= Ġtranslation Service . set ForKey ( key , Ġdto . translations ) ĊĠĠĠ Ġreturn Ġget Set Translations Response ( key , Ġtranslations ) ĊĠ Ġ} ĊĊĠ Ġ@ Put Mapping ("/ { translation Id }/ set - state /{ state }") ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ EDIT ]) ĊĠ Ġ@ Access With Project Permission ( permission Ġ= ĠPermission . Project Permission Type . TRANSL ATE ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ" Sets Ġtranslation Ġstate ") ĊĠ Ġfun Ġset Translation State (@ PathVariable Ġtranslation Id : ĠLong , Ġ@ PathVariable Ġstate : ĠTranslation State ): ĠTranslation Model Ġ{ ĊĠĠĠ Ġval Ġtranslation Ġ= Ġtranslation Service . find ( translation Id ) Ġ?: Ġthrow ĠNotFound Exception () ĊĠĠĠ Ġtranslation . check From Project () ĊĠĠĠ Ġreturn Ġtranslation Model Assembler . to Model ( translation Service . setState ( translation , Ġstate )) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ( value Ġ= Ġ[ "" ]) ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ VIEW ]) ĊĠ Ġ@ Access With Project Permission ( Permission . Project Permission Type . VIEW ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ" Returns Ġtranslations Ġin Ġproject ") ĊĠ Ġfun Ġget Translations ( ĊĠĠĠ Ġ@ Parameter Object Ġparams : ĠGet Translations Params , ĊĠĠĠ Ġ@ Parameter Object Ġpage able : ĠPage able ĊĠ Ġ): ĠKey sWith Translations Page Model Ġ{ ĊĠĠĠ Ġval Ġlanguages : ĠSet < Language > Ġ= Ġlanguage Service ĊĠĠĠĠĠ Ġ. get Languages For Translations View ( params . languages , Ġproject Holder . project . id ) ĊĊĠĠĠ Ġval Ġdata Ġ= Ġtranslation Service . getView Data ( project Holder . project . id , Ġpage able , Ġparams , Ġlanguages ) ĊĊĠĠĠ Ġval Ġkeys With Screenshots Ġ= Ġget Keys With Screenshots ( data . map Ġ{ Ġit . key Id Ġ}. toList ()) ĊĊĠĠĠ Ġif Ġ( keys With Screenshots Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠ Ġdata . content . forEach Ġ{ Ġit . screenshots Ġ= Ġkeys With Screenshots [ it . key Id ] Ġ?: ĠlistOf () Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġcursor Ġ= Ġif Ġ( data . content . isNotEmpty ()) ĠCursor Util . get Cursor ( data . content . last (), Ġdata . sort ) Ġelse Ġnull ĊĠĠĠ Ġreturn Ġpage d Assembler . to Translation Model ( data , Ġlanguages , Ġcursor ) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ( value Ġ= Ġ[" select - all "]) ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ VIEW ]) ĊĠ Ġ@ Access With Project Permission ( Permission . Project Permission Type . VIEW ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ" Get Ġselect Ġall Ġkeys ") ĊĠ Ġfun Ġget Select All Key Ids ( ĊĠĠĠ Ġ@ Parameter Object Ġparams : ĠTranslation Filters , ĊĠ Ġ): ĠSelect All Response Ġ{ ĊĠĠĠ Ġval Ġlanguages : ĠSet < Language > Ġ= Ġlanguage Service ĊĠĠĠĠĠ Ġ. get Languages For Translations View ( params . languages , Ġproject Holder . project . id ) ĊĊĠĠĠ Ġreturn ĠSelect All Response ( ĊĠĠĠĠĠ Ġtranslation Service . get Select All Keys ( ĊĠĠĠĠĠĠĠ ĠprojectId Ġ= Ġproject Holder . project . id , ĊĠĠĠĠĠĠĠ Ġparams Ġ= Ġparams , ĊĠĠĠĠĠĠĠ Ġlanguages Ġ= Ġlanguages ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Put Mapping ( value Ġ= Ġ[" /{ translation Id :[ 0 - 9 ]+ }/ dismiss - auto - translated - state "]) ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ EDIT ]) ĊĠ Ġ@ Access With Project Permission ( Permission . Project Permission Type . TRANSL ATE ) ĊĠ Ġ@ Operation ( summary Ġ= Ġ""" Remov es Ġ" auto Ġtranslated " Ġindication """) ĊĠ Ġfun Ġdismiss Auto Translated State ( ĊĠĠĠ Ġ@ PathVariable Ġtranslation Id : ĠLong ĊĠ Ġ): ĠTranslation Model Ġ{ ĊĠĠĠ Ġval Ġtranslation Ġ= Ġtranslation Service . get ( translation Id ) ĊĠĠĠ Ġtranslation . check From Project () ĊĠĠĠ Ġtranslation Service . dismiss Auto Translated ( translation ) ĊĠĠĠ Ġreturn Ġtranslation Model Assembler . to Model ( translation ) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ( value Ġ= Ġ[" /{ translation Id :[ 0 - 9 ]+ }/ history "]) ĊĠ Ġ@ Access With ApiKey ([ Api Scope . TRANSL ATIONS _ VIEW ]) ĊĠ Ġ@ Access With Project Permission ( Permission . Project Permission Type . VIEW ) ĊĠ Ġ@ Operation ( ĊĠĠĠ Ġsummary Ġ= Ġ""" Returns Ġhistory Ġof Ġspecific Ġtranslation . ĠĊ Ċ Sorting Ġis Ġnot Ġsupported Ġfor Ġsupported . ĠIt Ġis Ġautomatically Ġsorted Ġfrom Ġnewest Ġto Ġoldest .""" ĊĠ Ġ) ĊĠ Ġfun Ġget Translation History ( ĊĠĠĠ Ġ@ PathVariable Ġtranslation Id : ĠLong , ĊĠĠĠ Ġ@ Parameter Object Ġpage able : ĠPage able ĊĠ Ġ): ĠP aged Model < Translation History Model > Ġ{ ĊĠĠĠ Ġval Ġtranslation Ġ= Ġtranslation Service . get ( translation Id ) ĊĠĠĠ Ġtranslation . check From Project () ĊĠĠĠ Ġval Ġtranslations Ġ= Ġtranslation Service . get History ( translation Id , Ġpage able ) ĊĠĠĠ Ġreturn Ġhistory Paged Assembler . to Model ( translations , Ġtranslation History Model Assembler ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget Keys With Screenshots ( key Ids : ĠCollection < Long >): ĠMap < Long , ĠMutable Set < Screenshot >> ? Ġ{ ĊĠĠĠ Ġif Ġ( ĊĠĠĠĠĠ Ġ! authentication Facade . is ApiKey Authentication Ġ|| ĊĠĠĠĠĠ Ġauthentication Facade . apiKey . scopes Enum . contains ( Api Scope . SCREEN SHOT S _ VIEW ) ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ Ġreturn Ġscreenshot Service . get Keys With Screenshots ( key Ids ). associate Ġ{ Ġit . id Ġto Ġit . screenshots Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġnull ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget Set Translations Response ( key : ĠKey , Ġtranslations : ĠMap < String , ĠTranslation >): ĊĠĠĠ ĠSet Translations Response Model Ġ{ ĊĠĠĠ Ġreturn ĠSet Translations Response Model ( ĊĠĠĠĠĠ Ġkey Id Ġ= Ġkey . id , ĊĠĠĠĠĠ Ġkey Name Ġ= Ġkey . name , ĊĠĠĠĠĠ Ġtranslations Ġ= Ġtranslations . entries . associate Ġ{ Ġ( language Tag , Ġtranslation ) Ġ-> ĊĠĠĠĠĠĠĠ Ġlanguage Tag Ġto Ġtranslation Model Assembler . to Model ( translation ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcheck Scopes If Key Exists ( dto : ĠSet Translations With Key Dto ) Ġ{ ĊĠĠĠ Ġkey Service . find Optional ( project Holder . project Entity . id , Ġdto . key ). orElse ( null ) Ġ?: Ġlet Ġ{ ĊĠĠĠĠĠ Ġif Ġ( authentication Facade . is ApiKey Authentication ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsecurity Service . check ApiKey Scopes ( set Of ( Api Scope . KEYS _ EDIT ), Ġauthentication Facade . apiKey ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun ĠTranslation . check From Project () Ġ{ ĊĠĠĠ Ġif Ġ( this . key . project . id Ġ!= Ġproject Holder . project . id ) Ġ{ ĊĠĠĠĠĠ Ġthrow ĠBadRequest Exception ( io . tol gee . constants . Message . TRANSL ATION _ NOT _ FROM _ PROJECT ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ