|
| 1 | +/** |
| 2 | + * BDEF - Behavior Definition (RAP) |
| 3 | + * |
| 4 | + * ADK object for ABAP RAP Behavior Definitions (BDEF). |
| 5 | + * These are source-based objects (`.abdl`) stored at: |
| 6 | + * GET /sap/bc/adt/bo/behaviordefinitions/<name> |
| 7 | + * GET/PUT /sap/bc/adt/bo/behaviordefinitions/<name>/source/main |
| 8 | + * |
| 9 | + * Lock/activate/create follow the same patterns as other source objects |
| 10 | + * (DDL, DCL). The metadata document uses the shared `blue:blueSource` |
| 11 | + * wrapper — same envelope as TABL/STRUCT. |
| 12 | + * |
| 13 | + * This class intentionally mirrors `AdkDdlSource` (lightweight ADK object, |
| 14 | + * not a full AdkMainObject subclass) because BDEF metadata is source-driven |
| 15 | + * and the typical lifecycle is: create skeleton → PUT source → activate. |
| 16 | + */ |
| 17 | + |
| 18 | +import { getGlobalContext } from '../../../base/global-context'; |
| 19 | +import type { AdkContext } from '../../../base/context'; |
| 20 | +import { toText } from '../../../base/fetch-utils'; |
| 21 | + |
| 22 | +export class AdkBehaviorDefinition { |
| 23 | + /** Static ADK kind marker — used by abapGit handler registry if needed. */ |
| 24 | + static readonly kind = 'BehaviorDefinition' as const; |
| 25 | + readonly kind = AdkBehaviorDefinition.kind; |
| 26 | + |
| 27 | + readonly name: string; |
| 28 | + protected readonly ctx: AdkContext; |
| 29 | + |
| 30 | + constructor(ctx: AdkContext, name: string) { |
| 31 | + this.ctx = ctx; |
| 32 | + this.name = name.toUpperCase(); |
| 33 | + } |
| 34 | + |
| 35 | + get objectUri(): string { |
| 36 | + return `/sap/bc/adt/bo/behaviordefinitions/${encodeURIComponent(this.name.toLowerCase())}`; |
| 37 | + } |
| 38 | + |
| 39 | + /** Placeholder description — full metadata requires additional SAP fetch */ |
| 40 | + get description(): string { |
| 41 | + return this.name; |
| 42 | + } |
| 43 | + |
| 44 | + private get contract(): any { |
| 45 | + return this.ctx.client.adt.bo.behaviordefinitions; |
| 46 | + } |
| 47 | + |
| 48 | + // ─── Source ──────────────────────────────────────────────────────────────── |
| 49 | + |
| 50 | + async getSource(): Promise<string> { |
| 51 | + const result = await this.contract.source.main.get(this.name); |
| 52 | + return toText(result); |
| 53 | + } |
| 54 | + |
| 55 | + async saveMainSource( |
| 56 | + source: string, |
| 57 | + options?: { lockHandle?: string; transport?: string }, |
| 58 | + ): Promise<void> { |
| 59 | + await this.contract.source.main.put( |
| 60 | + this.name, |
| 61 | + { |
| 62 | + ...(options?.lockHandle ? { lockHandle: options.lockHandle } : {}), |
| 63 | + ...(options?.transport ? { corrNr: options.transport } : {}), |
| 64 | + }, |
| 65 | + source, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // ─── Lock / Unlock ───────────────────────────────────────────────────────── |
| 70 | + |
| 71 | + async lock(transport?: string): Promise<{ handle: string }> { |
| 72 | + const lockService = this.ctx.lockService; |
| 73 | + if (!lockService) { |
| 74 | + throw new Error( |
| 75 | + 'Lock not available: no lockService in context. Did you call initializeAdk()?', |
| 76 | + ); |
| 77 | + } |
| 78 | + return lockService.lock(this.objectUri, { |
| 79 | + transport, |
| 80 | + objectName: this.name, |
| 81 | + objectType: 'BDEF', |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + async unlock(lockHandle: string): Promise<void> { |
| 86 | + const lockService = this.ctx.lockService; |
| 87 | + if (!lockService) { |
| 88 | + throw new Error( |
| 89 | + 'Unlock not available: no lockService in context. Did you call initializeAdk()?', |
| 90 | + ); |
| 91 | + } |
| 92 | + await lockService.unlock(this.objectUri, { lockHandle }); |
| 93 | + } |
| 94 | + |
| 95 | + // ─── Activate ────────────────────────────────────────────────────────────── |
| 96 | + |
| 97 | + async activate(): Promise<this> { |
| 98 | + await this.ctx.client.adt.activation.activate.post({}, { |
| 99 | + objectReferences: { |
| 100 | + objectReference: [{ uri: this.objectUri, name: this.name }], |
| 101 | + }, |
| 102 | + } as any); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + // ─── Static Factory Methods ───────────────────────────────────────────────── |
| 107 | + |
| 108 | + /** |
| 109 | + * Get a BDEF (validates it exists by fetching source). |
| 110 | + */ |
| 111 | + static async get( |
| 112 | + name: string, |
| 113 | + ctx?: AdkContext, |
| 114 | + ): Promise<AdkBehaviorDefinition> { |
| 115 | + const context = ctx ?? getGlobalContext(); |
| 116 | + const obj = new AdkBehaviorDefinition(context, name); |
| 117 | + // Validate it exists by fetching source |
| 118 | + await obj.getSource(); |
| 119 | + return obj; |
| 120 | + } |
| 121 | + |
| 122 | + static async exists(name: string, ctx?: AdkContext): Promise<boolean> { |
| 123 | + try { |
| 124 | + await AdkBehaviorDefinition.get(name, ctx); |
| 125 | + return true; |
| 126 | + } catch { |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Create a new BDEF on SAP. |
| 133 | + * |
| 134 | + * POST /sap/bc/adt/bo/behaviordefinitions?corrNr=... |
| 135 | + * Body matches the `blue:blueSource` envelope (extends |
| 136 | + * abapsource:AbapSourceMainObject). |
| 137 | + */ |
| 138 | + static async create( |
| 139 | + name: string, |
| 140 | + description: string, |
| 141 | + packageName: string, |
| 142 | + options?: { transport?: string }, |
| 143 | + ctx?: AdkContext, |
| 144 | + ): Promise<AdkBehaviorDefinition> { |
| 145 | + const context = ctx ?? getGlobalContext(); |
| 146 | + const nameU = name.toUpperCase(); |
| 147 | + const pkgU = packageName.toUpperCase(); |
| 148 | + |
| 149 | + await context.client.adt.bo.behaviordefinitions.post( |
| 150 | + options?.transport ? { corrNr: options.transport } : {}, |
| 151 | + { |
| 152 | + blueSource: { |
| 153 | + name: nameU, |
| 154 | + type: 'BDEF/BDO', |
| 155 | + description, |
| 156 | + language: 'EN', |
| 157 | + masterLanguage: 'EN', |
| 158 | + responsible: pkgU, |
| 159 | + packageRef: { |
| 160 | + name: pkgU, |
| 161 | + type: 'DEVC/K', |
| 162 | + uri: `/sap/bc/adt/packages/${pkgU.toLowerCase()}`, |
| 163 | + }, |
| 164 | + }, |
| 165 | + } as any, |
| 166 | + ); |
| 167 | + |
| 168 | + return new AdkBehaviorDefinition(context, nameU); |
| 169 | + } |
| 170 | + |
| 171 | + static async delete( |
| 172 | + name: string, |
| 173 | + options?: { transport?: string; lockHandle?: string }, |
| 174 | + ctx?: AdkContext, |
| 175 | + ): Promise<void> { |
| 176 | + const context = ctx ?? getGlobalContext(); |
| 177 | + await context.client.adt.bo.behaviordefinitions.delete(name.toUpperCase(), { |
| 178 | + ...(options?.transport ? { corrNr: options.transport } : {}), |
| 179 | + ...(options?.lockHandle ? { lockHandle: options.lockHandle } : {}), |
| 180 | + }); |
| 181 | + } |
| 182 | +} |
0 commit comments