// ĠCopyright Ġ 2 0 2 1 ĠThe ĠCross - Media ĠMeasurement ĠAuthors Ċ // Ċ // ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ // Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ // ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ // Ċ // ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ // Ċ // ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ // Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ // ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ // ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ // Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ċ package Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . writ ers Ċ Ċ import Ġcom . google . cloud . spanner . ErrorCode Ċ import Ġcom . google . cloud . spanner . Mutation Ċ import Ġcom . google . cloud . spanner . Sp anner Exception Ċ import Ġorg . wf an et . measurement . common . identity . External Id Ċ import Ġorg . wf an et . measurement . common . identity . Internal Id Ċ import Ġorg . wf an et . measurement . g cloud . common . to G cloud ByteArray Ċ import Ġorg . wf an et . measurement . g cloud . common . to G cloud Timestamp Ċ import Ġorg . wf an et . measurement . g cloud . spanner . Async Database Client Ċ import Ġorg . wf an et . measurement . g cloud . spanner . buffer To Ċ import Ġorg . wf an et . measurement . g cloud . spanner . insert Mutation Ċ import Ġorg . wf an et . measurement . g cloud . spanner . set Ċ import Ġorg . wf an et . measurement . g cloud . spanner . set Json Ċ import Ġorg . wf an et . measurement . internal . king dom . Certificate Ċ import Ġorg . wf an et . measurement . king dom . deploy . common . D uch y Ids Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . common . King dom Internal Exception Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . readers . DataProvider Reader Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . readers . Measurement Consumer Reader Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . readers . Model Provider Reader Ċ Ċ /** Ċ Ġ* ĠCreates Ġa Ġcertificate Ġin Ġthe Ġdatabase . Ċ Ġ* Ċ Ġ* ĠThrow ĠKing dom Internal Exception Ġwith Ġcode ĠCERT _ SUBJECT _ KEY _ ID _ AL READY _ EXISTS Ġwhen Ġexecuted Ġif Ċ Ġ* Ġsubject Key Identifier Ġof Ġ[ certificate ] Ġcol lide s Ġwith Ġa Ġcertificate Ġalready Ġin Ġthe Ġdatabase . Ċ Ġ*/ Ċ class ĠCreate Certificate ( private Ġval Ġcertificate : ĠCertificate ) Ġ: ĊĠ ĠSp anner Writer < Certificate , ĠCertificate >() Ġ{ ĊĊĠ Ġ@ Suppress (" WHEN _ ENUM _ CAN _ BE _ NULL _ IN _ JAVA ") Ġ// ĠProto Ġenum Ġfields Ġare Ġnever Ġnull . ĊĠ Ġprivate Ġval Ġowner TableName : ĠString Ġ= ĊĠĠĠ Ġwhen Ġ( certificate . parent Case ) Ġ{ ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ DATA _ PROVIDER _ ID Ġ-> Ġ" DataProvider " ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ MEAS UREMENT _ CON SUM ER _ ID Ġ-> Ġ" Measurement Consumer " ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ DU CH Y _ ID Ġ-> Ġ" D uch y " ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ MODEL _ PROVIDER _ ID Ġ-> Ġ" Model Provider " ĊĠĠĠĠĠ ĠCertificate . Parent Case . PARENT _ NOT _ SET Ġ-> ĊĠĠĠĠĠĠĠ Ġthrow ĠIllegalArgumentException (" Parent Ġfield Ġof ĠCertificate Ġis Ġnot Ġset ") ĊĠĠĠ Ġ} ĊĊĠ Ġoverride Ġsuspend Ġfun ĠTransaction Scope . run Transaction (): ĠCertificate Ġ{ ĊĠĠĠ Ġval Ġcertificate Id Ġ= Ġid Generator . generate Internal Id () ĊĠĠĠ Ġval Ġexternal Map Id Ġ= Ġid Generator . generate External Id () ĊĠĠĠ Ġcertificate . to Insert Mutation ( certificate Id ). buffer To ( transaction Context ) ĊĠĠĠ Ġcreate Certificate Map Table Mutation ( ĊĠĠĠĠĠĠĠ Ġget Owner Internal Id ( transaction Context ), ĊĠĠĠĠĠĠĠ Ġcertificate Id . value , ĊĠĠĠĠĠĠĠ Ġexternal Map Id . value ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ. buffer To ( transaction Context ) ĊĠĠĠ Ġreturn Ġcertificate . to Builder (). set External Certificate Id ( external Map Id . value ). build () ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠResult Scope < Certificate >. build Result (): ĠCertificate Ġ{ ĊĠĠĠ Ġreturn ĠcheckNotNull ( transaction Result ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġsuspend Ġfun Ġget Owner Internal Id ( ĊĠĠĠ Ġtransaction Context : ĠAsync Database Client . Transaction Context ĊĠ Ġ): ĠLong Ġ{ ĊĠĠĠ Ġ// ĠTODO : Ġchange Ġall Ġof Ġthe Ġreads Ġbelow Ġto Ġuse Ġindex Ġlook ups Ġor Ġsimple Ġqueries . ĊĠĠĠ Ġ@ Suppress (" WHEN _ ENUM _ CAN _ BE _ NULL _ IN _ JAVA ") Ġ// ĠProto Ġenum Ġfields Ġare Ġnever Ġnull . ĊĠĠĠ Ġreturn Ġwhen Ġ( certificate . parent Case ) Ġ{ ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ DATA _ PROVIDER _ ID Ġ-> ĊĠĠĠĠĠĠĠ ĠData Provider Reader () ĊĠĠĠĠĠĠĠĠĠ Ġ. read By External DataProvider Id ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtransaction Context , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠExternal Id ( certificate . external DataProvider Id ) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ? . dataProvider Id ĊĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠKing dom Internal Exception ( King dom Internal Exception . Code . DATA _ PROVIDER _ NOT _ FOUND ) ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ MEAS UREMENT _ CON SUM ER _ ID Ġ-> ĊĠĠĠĠĠĠĠ ĠMeasurement Consumer Reader () ĊĠĠĠĠĠĠĠĠĠ Ġ. read By External Measurement Consumer Id ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtransaction Context , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠExternal Id ( certificate . external Measurement Consumer Id ) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ? . measurement Consumer Id ĊĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠKing dom Internal Exception ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠKing dom Internal Exception . Code . MEAS UREMENT _ CON SUM ER _ NOT _ FOUND ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ DU CH Y _ ID Ġ-> ĊĠĠĠĠĠĠĠ ĠD uch y Ids . get Internal Id ( certificate . external D uch y Id ) ĊĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠKing dom Internal Exception ( King dom Internal Exception . Code . DU CH Y _ NOT _ FOUND ) ĊĠĠĠĠĠ ĠCertificate . Parent Case . EXTERNAL _ MODEL _ PROVIDER _ ID Ġ-> ĊĠĠĠĠĠĠĠ ĠModel Provider Reader () ĊĠĠĠĠĠĠĠĠĠ Ġ. read By External Model Provider Id ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtransaction Context , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠExternal Id ( certificate . external Model Provider Id ) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ? . model Provider Id ĊĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠKing dom Internal Exception ( King dom Internal Exception . Code . MODEL _ PROVIDER _ NOT _ FOUND ) ĊĠĠĠĠĠ ĠCertificate . Parent Case . PARENT _ NOT _ SET Ġ-> ĊĠĠĠĠĠĠĠ Ġthrow ĠIllegalArgumentException (" Parent Ġfield Ġof ĠCertificate Ġis Ġnot Ġset ") ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcreate Certificate Map Table Mutation ( ĊĠĠĠ Ġinternal OwnerId : ĠLong , ĊĠĠĠ Ġinternal Certificate Id : ĠLong , ĊĠĠĠ Ġexternal Map Id : ĠLong ĊĠ Ġ): ĠMutation Ġ{ ĊĠĠĠ Ġval ĠtableName Ġ= Ġ"${ owner TableName } Certificates " ĊĠĠĠ Ġval Ġinternal Id Field Ġ= Ġ"${ owner TableName } Id " ĊĠĠĠ Ġval Ġexternal Id Field Ġ= Ġ" External ${ owner TableName } Certificate Id " ĊĠĠĠ Ġreturn Ġinsert Mutation ( tableName ) Ġ{ ĊĠĠĠĠĠ Ġset ( internal Id Field Ġto Ġinternal OwnerId ) ĊĠĠĠĠĠ Ġset (" Certificate Id " Ġto Ġinternal Certificate Id ) ĊĠĠĠĠĠ Ġset ( external Id Field Ġto Ġexternal Map Id ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġsuspend Ġfun Ġhandle Sp anner Exception ( e : ĠSp anner Exception ): ĠCertificate ? Ġ{ ĊĠĠĠ Ġwhen Ġ( e . errorCode ) Ġ{ ĊĠĠĠĠĠ ĠErrorCode . AL READY _ EXISTS Ġ-> ĊĠĠĠĠĠĠĠ Ġthrow ĠKing dom Internal Exception ( ĊĠĠĠĠĠĠĠĠĠ ĠKing dom Internal Exception . Code . CERT _ SUBJECT _ KEY _ ID _ AL READY _ EXISTS ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġelse Ġ-> Ġthrow Ġe ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ Ċ fun ĠCertificate . to Insert Mutation ( internal Id : ĠInternal Id ): ĠMutation Ġ{ ĊĠ Ġreturn Ġinsert Mutation (" Certificates ") Ġ{ ĊĠĠĠ Ġset (" Certificate Id " Ġto Ġinternal Id ) ĊĠĠĠ Ġset (" Subject Key Identifier " Ġto Ġsubject Key Identifier . to G cloud ByteArray ()) ĊĠĠĠ Ġset (" Not Valid Before " Ġto Ġnot Valid Before . to G cloud Timestamp ()) ĊĠĠĠ Ġset (" Not Valid After " Ġto Ġnot Valid After . to G cloud Timestamp ()) ĊĠĠĠ Ġset (" Rev ocation State " Ġto Ġrev ocation State ) ĊĠĠĠ Ġset (" Certificate Details " Ġto Ġdetails ) ĊĠĠĠ Ġset Json (" Certificate Details Json " Ġto Ġdetails ) ĊĠ Ġ} Ċ } Ċ