|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package catalog |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/sha256" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/mendixlabs/mxcli/sdk/mpr" |
| 11 | +) |
| 12 | + |
| 13 | +// buildContractEntities parses cached $metadata from consumed OData services |
| 14 | +// and populates the contract_entities and contract_actions catalog tables. |
| 15 | +func (b *Builder) buildContractEntities() error { |
| 16 | + services, err := b.reader.ListConsumedODataServices() |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + |
| 21 | + entityStmt, err := b.tx.Prepare(` |
| 22 | + INSERT INTO contract_entities (Id, ServiceId, ServiceQualifiedName, |
| 23 | + EntityName, EntitySetName, KeyProperties, PropertyCount, NavigationCount, |
| 24 | + Summary, Description, ModuleName, |
| 25 | + ProjectId, SnapshotId, SnapshotDate, SnapshotSource) |
| 26 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 27 | + `) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + defer entityStmt.Close() |
| 32 | + |
| 33 | + actionStmt, err := b.tx.Prepare(` |
| 34 | + INSERT INTO contract_actions (Id, ServiceId, ServiceQualifiedName, |
| 35 | + ActionName, IsBound, ParameterCount, ReturnType, ModuleName, |
| 36 | + ProjectId, SnapshotId, SnapshotDate, SnapshotSource) |
| 37 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 38 | + `) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + defer actionStmt.Close() |
| 43 | + |
| 44 | + projectID, _, snapshotID, snapshotDate, snapshotSource, _, _, _ := b.snapshotMeta() |
| 45 | + |
| 46 | + entityCount := 0 |
| 47 | + actionCount := 0 |
| 48 | + |
| 49 | + for _, svc := range services { |
| 50 | + if svc.Metadata == "" { |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + moduleID := b.hierarchy.findModuleID(svc.ContainerID) |
| 55 | + moduleName := b.hierarchy.getModuleName(moduleID) |
| 56 | + svcQN := moduleName + "." + svc.Name |
| 57 | + |
| 58 | + doc, err := mpr.ParseEdmx(svc.Metadata) |
| 59 | + if err != nil { |
| 60 | + continue // skip services with unparseable metadata |
| 61 | + } |
| 62 | + |
| 63 | + // Build entity set lookup |
| 64 | + esMap := make(map[string]string) |
| 65 | + for _, es := range doc.EntitySets { |
| 66 | + esMap[es.EntityType] = es.Name |
| 67 | + } |
| 68 | + |
| 69 | + for _, s := range doc.Schemas { |
| 70 | + for _, et := range s.EntityTypes { |
| 71 | + syntheticID := fmt.Sprintf("%x", sha256.Sum256([]byte(svcQN+"|entity|"+et.Name)))[:32] |
| 72 | + entitySetName := esMap[s.Namespace+"."+et.Name] |
| 73 | + keyProps := strings.Join(et.KeyProperties, ", ") |
| 74 | + |
| 75 | + _, err := entityStmt.Exec( |
| 76 | + syntheticID, |
| 77 | + string(svc.ID), |
| 78 | + svcQN, |
| 79 | + et.Name, |
| 80 | + entitySetName, |
| 81 | + keyProps, |
| 82 | + len(et.Properties), |
| 83 | + len(et.NavigationProperties), |
| 84 | + et.Summary, |
| 85 | + et.Description, |
| 86 | + moduleName, |
| 87 | + projectID, snapshotID, snapshotDate, snapshotSource, |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + entityCount++ |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for _, a := range doc.Actions { |
| 97 | + syntheticID := fmt.Sprintf("%x", sha256.Sum256([]byte(svcQN+"|action|"+a.Name)))[:32] |
| 98 | + isBound := 0 |
| 99 | + if a.IsBound { |
| 100 | + isBound = 1 |
| 101 | + } |
| 102 | + retType := a.ReturnType |
| 103 | + if retType == "" { |
| 104 | + retType = "(void)" |
| 105 | + } |
| 106 | + |
| 107 | + _, err := actionStmt.Exec( |
| 108 | + syntheticID, |
| 109 | + string(svc.ID), |
| 110 | + svcQN, |
| 111 | + a.Name, |
| 112 | + isBound, |
| 113 | + len(a.Parameters), |
| 114 | + retType, |
| 115 | + moduleName, |
| 116 | + projectID, snapshotID, snapshotDate, snapshotSource, |
| 117 | + ) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + actionCount++ |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + b.report("Contract Entities", entityCount) |
| 126 | + if actionCount > 0 { |
| 127 | + b.report("Contract Actions", actionCount) |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// buildContractMessages parses cached AsyncAPI documents from business event client |
| 133 | +// services and populates the contract_messages catalog table. |
| 134 | +func (b *Builder) buildContractMessages() error { |
| 135 | + services, err := b.cachedBusinessEventServices() |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + stmt, err := b.tx.Prepare(` |
| 141 | + INSERT INTO contract_messages (Id, ServiceId, ServiceQualifiedName, |
| 142 | + ChannelName, OperationType, MessageName, Title, ContentType, PropertyCount, |
| 143 | + ModuleName, ProjectId, SnapshotId, SnapshotDate, SnapshotSource) |
| 144 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 145 | + `) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + defer stmt.Close() |
| 150 | + |
| 151 | + projectID, _, snapshotID, snapshotDate, snapshotSource, _, _, _ := b.snapshotMeta() |
| 152 | + |
| 153 | + count := 0 |
| 154 | + |
| 155 | + for _, svc := range services { |
| 156 | + if svc.Document == "" { |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + moduleID := b.hierarchy.findModuleID(svc.ContainerID) |
| 161 | + moduleName := b.hierarchy.getModuleName(moduleID) |
| 162 | + svcQN := moduleName + "." + svc.Name |
| 163 | + |
| 164 | + doc, err := mpr.ParseAsyncAPI(svc.Document) |
| 165 | + if err != nil { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + // Build message lookup for property counts |
| 170 | + msgProps := make(map[string]int) |
| 171 | + for _, msg := range doc.Messages { |
| 172 | + msgProps[msg.Name] = len(msg.Properties) |
| 173 | + } |
| 174 | + |
| 175 | + for _, ch := range doc.Channels { |
| 176 | + syntheticID := fmt.Sprintf("%x", sha256.Sum256([]byte(svcQN+"|channel|"+ch.Name+"|"+ch.MessageRef)))[:32] |
| 177 | + |
| 178 | + propCount := msgProps[ch.MessageRef] |
| 179 | + |
| 180 | + // Find message details |
| 181 | + title := "" |
| 182 | + contentType := "" |
| 183 | + if msg := doc.FindMessage(ch.MessageRef); msg != nil { |
| 184 | + title = msg.Title |
| 185 | + contentType = msg.ContentType |
| 186 | + } |
| 187 | + |
| 188 | + _, err := stmt.Exec( |
| 189 | + syntheticID, |
| 190 | + string(svc.ID), |
| 191 | + svcQN, |
| 192 | + ch.Name, |
| 193 | + ch.OperationType, |
| 194 | + ch.MessageRef, |
| 195 | + title, |
| 196 | + contentType, |
| 197 | + propCount, |
| 198 | + moduleName, |
| 199 | + projectID, snapshotID, snapshotDate, snapshotSource, |
| 200 | + ) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + count++ |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + b.report("Contract Messages", count) |
| 209 | + return nil |
| 210 | +} |
0 commit comments