55import com .dbaagent .model .SchemaDocumentation ;
66import com .dbaagent .model .code .CodeKnowledgeSuggestion ;
77import com .dbaagent .repository .CodeKnowledgeSuggestionRepository ;
8+ import com .dbaagent .repository .CompanyKnowledgeEntryRepository ;
89import com .dbaagent .repository .SchemaDocumentationRepository ;
910import com .dbaagent .service .CompanyKnowledgeService ;
1011import com .dbaagent .service .SchemaScannerService ;
1516import org .springframework .transaction .annotation .Transactional ;
1617
1718import java .time .LocalDateTime ;
19+ import java .util .ArrayList ;
1820import java .util .List ;
1921import java .util .Map ;
2022import java .util .Optional ;
@@ -32,6 +34,7 @@ public class CodeSuggestionApplier {
3234
3335 private final CodeKnowledgeSuggestionRepository suggestionRepository ;
3436 private final SchemaDocumentationRepository schemaDocRepository ;
37+ private final CompanyKnowledgeEntryRepository companyKnowledgeEntryRepository ;
3538 private final CompanyKnowledgeService companyKnowledgeService ;
3639 private final TrainingService trainingService ;
3740 private final SchemaScannerService schemaScannerService ;
@@ -78,6 +81,36 @@ public CodeKnowledgeSuggestion reject(String suggestionId, String decidedBy, Str
7881 }
7982
8083 private void applySchemaDoc (CodeKnowledgeSuggestion suggestion , String decidedBy ) {
84+ Map <String , Object > payload = suggestion .getPayload ();
85+ Object existingDocId = payload == null ? null : payload .get ("existingDocId" );
86+ if (existingDocId != null && !existingDocId .toString ().isBlank ()) {
87+ SchemaDocumentation doc = schemaDocRepository .findById (existingDocId .toString ())
88+ .orElseThrow (() -> new IllegalArgumentException (
89+ "Existing schema doc not found: " + existingDocId
90+ ));
91+ doc .setDescription (mergeText (doc .getDescription (), suggestion .getContent ()));
92+ Object terms = payload .get ("businessTerms" );
93+ if (terms instanceof List <?> list && !list .isEmpty ()) {
94+ String mergedTerms = mergeCommaSeparated (doc .getBusinessTerms (), list );
95+ doc .setBusinessTerms (mergedTerms );
96+ }
97+ if (doc .getConfidence () == null || (suggestion .getConfidence () != null
98+ && suggestion .getConfidence () > doc .getConfidence ())) {
99+ doc .setConfidence (suggestion .getConfidence ());
100+ }
101+ if (suggestion .getSourceFiles () != null && !suggestion .getSourceFiles ().isEmpty ()) {
102+ doc .setSourceFiles (suggestion .getSourceFiles ());
103+ }
104+ SchemaDocumentation saved = schemaDocRepository .save (doc );
105+ suggestion .setAppliedDocId (saved .getId ());
106+ try {
107+ trainingService .upsertDocumentationEmbedding (saved );
108+ } catch (Exception e ) {
109+ log .warn ("Failed to embed schema doc {} after approval: {}" , saved .getId (), e .getMessage ());
110+ }
111+ return ;
112+ }
113+
81114 SchemaDocumentation .DocumentationType objectType = resolveObjectType (suggestion );
82115 String objectName ;
83116 String parentObject = null ;
@@ -166,6 +199,27 @@ private void applySchemaDoc(CodeKnowledgeSuggestion suggestion, String decidedBy
166199 }
167200
168201 private void applyKnowledgeEntry (CodeKnowledgeSuggestion suggestion , String decidedBy ) {
202+ Map <String , Object > payload = suggestion .getPayload ();
203+ Object existingId = payload == null ? null : payload .get ("existingEntryId" );
204+ if (existingId != null && !existingId .toString ().isBlank ()) {
205+ CompanyKnowledgeEntry existing = companyKnowledgeEntryRepository .findById (existingId .toString ())
206+ .orElseThrow (() -> new IllegalArgumentException (
207+ "Existing company knowledge entry not found: " + existingId
208+ ));
209+ CompanyKnowledgeEntry merged = CompanyKnowledgeEntry .builder ()
210+ .connectionId (existing .getConnectionId ())
211+ .title (pickTitle (existing .getTitle (), suggestion .getTitle ()))
212+ .content (mergeText (existing .getContent (), suggestion .getContent ()))
213+ .entryType (resolveEntryType (suggestion ))
214+ .linkedTables (mergeStringLists (existing .getLinkedTables (), suggestion .getLinkedTables ()))
215+ .linkedColumns (mergeStringLists (existing .getLinkedColumns (), suggestion .getLinkedColumns ()))
216+ .createdBy (decidedBy )
217+ .build ();
218+ CompanyKnowledgeEntry saved = companyKnowledgeService .updateEntry (existing .getId (), merged );
219+ suggestion .setAppliedEntryId (saved .getId ());
220+ return ;
221+ }
222+
169223 CompanyKnowledgeEntry entry = CompanyKnowledgeEntry .builder ()
170224 .connectionId (suggestion .getConnectionId ())
171225 .title (suggestion .getTitle ())
@@ -220,4 +274,53 @@ private static CompanyKnowledgeEntry.EntryType resolveEntryType(CodeKnowledgeSug
220274 }
221275 return CompanyKnowledgeEntry .EntryType .BUSINESS_RULE ;
222276 }
277+
278+ private static String pickTitle (String existing , String proposed ) {
279+ if (proposed == null || proposed .isBlank ()) return existing ;
280+ if (existing == null || existing .isBlank ()) return proposed ;
281+ return proposed .length () >= existing .length () ? proposed : existing ;
282+ }
283+
284+ private static String mergeText (String existing , String addition ) {
285+ if (addition == null || addition .isBlank ()) {
286+ return existing == null ? "" : existing ;
287+ }
288+ if (existing == null || existing .isBlank ()) {
289+ return addition .trim ();
290+ }
291+ String left = existing .trim ();
292+ String right = addition .trim ();
293+ if (left .equalsIgnoreCase (right )) return left ;
294+ if (left .toLowerCase ().contains (right .toLowerCase ())) return left ;
295+ if (right .toLowerCase ().contains (left .toLowerCase ())) return right ;
296+ return left + "\n \n " + right ;
297+ }
298+
299+ private static List <String > mergeStringLists (List <String > left , List <String > right ) {
300+ List <String > merged = new ArrayList <>();
301+ if (left != null ) merged .addAll (left );
302+ if (right != null ) {
303+ for (String value : right ) {
304+ if (value != null && !value .isBlank () && !merged .contains (value )) {
305+ merged .add (value );
306+ }
307+ }
308+ }
309+ return merged ;
310+ }
311+
312+ private static String mergeCommaSeparated (String existing , List <?> additions ) {
313+ List <String > merged = new ArrayList <>();
314+ if (existing != null && !existing .isBlank ()) {
315+ for (String part : existing .split ("," )) {
316+ String trimmed = part .trim ();
317+ if (!trimmed .isEmpty () && !merged .contains (trimmed )) merged .add (trimmed );
318+ }
319+ }
320+ for (Object item : additions ) {
321+ String value = String .valueOf (item ).trim ();
322+ if (!value .isEmpty () && !merged .contains (value )) merged .add (value );
323+ }
324+ return String .join (", " , merged );
325+ }
223326}
0 commit comments