@@ -115,6 +115,13 @@ public ApiResponse createCbPlan(ApiRequest request, String userOrgId, String aut
115115 ApiResponse resp = (ApiResponse ) cassandraOperation .insertRecord (Constants .KEYSPACE_SUNBIRD ,
116116 Constants .TABLE_CB_PLAN_V2 , requestMap );
117117 if (Constants .SUCCESS .equals (resp .get (Constants .RESPONSE ))) {
118+ String planId = String .valueOf (requestMap .get (Constants .PLAN_ID ));
119+ requestMap .put (Constants .ID , planId );
120+ List <String > contentIds =
121+ (List <String >) requestMap .get (Constants .CONTENT_LIST );
122+ if (CollectionUtils .isNotEmpty (contentIds )) {
123+ upsertCbPlanContentLookup (planId , contentIds );
124+ }
118125 requestMap .put (Constants .ID , String .valueOf (requestMap .get (Constants .PLAN_ID )));
119126 Map <String , Object > sanitizedMap = sanitizeForElastic (requestMap );
120127 esUtilService .addDocument (serverProperties .getCpPlanIndex (), Constants .INDEX_TYPE ,
@@ -210,6 +217,16 @@ public ApiResponse updateCbPlan(ApiRequest request, String userOrgId, String tok
210217 Map <String , Object > resp = cassandraOperation .updateRecord (Constants .KEYSPACE_SUNBIRD ,
211218 Constants .TABLE_CB_PLAN_V2 , updatedRequest , Map .of (Constants .PLAN_ID , cbPlanId ));
212219 if (resp .get (Constants .RESPONSE ).equals (Constants .SUCCESS )) {
220+ List <String > contentIds =
221+ (List <String >) updatedRequest .get (Constants .CONTENT_LIST );
222+ if (CollectionUtils .isNotEmpty (contentIds )) {
223+ // For the content Retirement validation Impl
224+ List <String > existingContentIds =
225+ (List <String >) existingCbPlan .get (Constants .CONTENT_LIST );
226+ upsertCbPlanContentLookup (cbPlanId , getAddedContent (existingContentIds , contentIds ));
227+ removeCbPlanInfoForUpdateOrDeleteCbPlan (cbPlanId , getDeletedContent (existingContentIds , contentIds ));
228+ }
229+
213230 Map <String , Object > sanitizedMap = sanitizeForElastic (updatedRequest );
214231 esUtilService .updateDocument (serverProperties .getCpPlanIndex (), Constants .INDEX_TYPE , cbPlanId ,
215232 sanitizedMap , serverProperties .getElasticCbPlanJsonPath ());
@@ -738,6 +755,11 @@ public ApiResponse retireCbPlan(ApiRequest request, String userOrgId, String tok
738755 if (resp .get (Constants .RESPONSE ).equals (Constants .SUCCESS )) {
739756 cbPlan .put (Constants .ID , cbPlanId );
740757 cbPlan .put (Constants .STATUS , Constants .CB_RETIRE );
758+ List <String > contentIds =
759+ (List <String >) cbPlan .get (Constants .CONTENT_LIST );
760+ if (CollectionUtils .isNotEmpty (contentIds )) {
761+ removeCbPlanInfoForUpdateOrDeleteCbPlan (cbPlanId , contentIds );
762+ }
741763 Map <String , Object > sanitizedMap = sanitizeForElastic (cbPlan );
742764 // TO DO : need to use upsert method instead of addDocument
743765 esUtilService .addDocument (serverProperties .getCpPlanIndex (), Constants .INDEX_TYPE ,
@@ -1101,4 +1123,136 @@ private void handleUpdateOfLiveCbPlan(ApiResponse response, Map<String, Object>
11011123 response .setResponseCode (HttpStatus .INTERNAL_SERVER_ERROR );
11021124 }
11031125 }
1104- }
1126+
1127+ private void upsertCbPlanContentLookup (String planId , List <String > contentIds ) {
1128+
1129+ for (String contentId : contentIds ) {
1130+
1131+ Map <String , Object > where = new HashMap <>();
1132+ where .put ("contentid" , contentId );
1133+
1134+ Set <String > planIds = new HashSet <>();
1135+
1136+ List <Map <String , Object >> rows =
1137+ cassandraOperation .getRecordsByProperties (
1138+ Constants .KEYSPACE_SUNBIRD ,
1139+ Constants .TABLE_CB_PLAN_V2_CONTENT_LOOKUP ,
1140+ where ,
1141+ Arrays .asList ("planid" ),
1142+ 1
1143+ );
1144+
1145+ if (CollectionUtils .isNotEmpty (rows )) {
1146+ Object existing = rows .get (0 ).get ("planId" );
1147+ if (existing instanceof Set ) {
1148+ planIds .addAll ((Set <String >) existing );
1149+ }
1150+ }
1151+
1152+ if (!planIds .contains (planId )) {
1153+ planIds .add (planId );
1154+
1155+ Map <String , Object > update = new HashMap <>();
1156+ update .put ("planid" , planIds );
1157+
1158+ cassandraOperation .updateRecord (
1159+ Constants .KEYSPACE_SUNBIRD ,
1160+ Constants .TABLE_CB_PLAN_V2_CONTENT_LOOKUP ,
1161+ update ,
1162+ where
1163+ );
1164+ }
1165+ }
1166+ }
1167+
1168+ private void removeCbPlanInfoForUpdateOrDeleteCbPlan (String cbPlanId , List <String > contentIds ) {
1169+
1170+ for (String contentId : contentIds ) {
1171+
1172+ Map <String , Object > where = new HashMap <>();
1173+ where .put ("contentid" , contentId );
1174+
1175+ List <Map <String , Object >> rows =
1176+ cassandraOperation .getRecordsByProperties (
1177+ Constants .KEYSPACE_SUNBIRD ,
1178+ Constants .TABLE_CB_PLAN_V2_CONTENT_LOOKUP ,
1179+ where ,
1180+ Arrays .asList ("planid" ),
1181+ 1
1182+ );
1183+
1184+ if (CollectionUtils .isEmpty (rows )) {
1185+ log .debug ("No row found for contentId {}" , contentId );
1186+ continue ; // Skip processing if no rows found
1187+ }
1188+
1189+ Object existing = rows .get (0 ).get ("planId" );
1190+ if (!(existing instanceof Set )) {
1191+ log .warn ("Invalid planid data for contentId {}" , contentId );
1192+ continue ; // Skip processing if data type is invalid
1193+ }
1194+
1195+ Set <String > planIds = new HashSet <>((Set <String >) existing );
1196+
1197+ // Case 1: only one planId and it matches → DELETE row
1198+ if (planIds .size () == 1 && planIds .contains (cbPlanId )) {
1199+
1200+ cassandraOperation .deleteRecord (
1201+ Constants .KEYSPACE_SUNBIRD ,
1202+ Constants .TABLE_CB_PLAN_V2_CONTENT_LOOKUP ,
1203+ where
1204+ );
1205+
1206+ log .info (
1207+ "Deleted row for contentId {} (only cbPlanId {} existed)" ,
1208+ contentId , cbPlanId
1209+ );
1210+ }
1211+
1212+ // Case 2: multiple planIds and contains cbPlanId → REMOVE & UPDATE
1213+ if (planIds .size () > 1 && planIds .contains (cbPlanId )) {
1214+
1215+ planIds .remove (cbPlanId );
1216+
1217+ Map <String , Object > update = new HashMap <>();
1218+ update .put ("planid" , planIds );
1219+
1220+ cassandraOperation .updateRecord (
1221+ Constants .KEYSPACE_SUNBIRD ,
1222+ Constants .TABLE_CB_PLAN_V2_CONTENT_LOOKUP ,
1223+ update ,
1224+ where
1225+ );
1226+
1227+ log .info (
1228+ "Removed cbPlanId {} from contentId {}. Remaining plans={}" ,
1229+ cbPlanId , contentId , planIds
1230+ );
1231+ }
1232+ }
1233+ }
1234+
1235+ private List <String > getAddedContent (List <String > existingContent , List <String > updatedContent ) {
1236+
1237+ Set <String > existingSet =
1238+ new HashSet <>(existingContent == null ? List .of () : existingContent );
1239+
1240+ Set <String > updatedSet =
1241+ new HashSet <>(updatedContent == null ? List .of () : updatedContent );
1242+
1243+ updatedSet .removeAll (existingSet );
1244+ return new ArrayList <>(updatedSet );
1245+ }
1246+
1247+ private List <String > getDeletedContent (List <String > existingContent , List <String > updatedContent ) {
1248+
1249+ Set <String > existingSet =
1250+ new HashSet <>(existingContent == null ? List .of () : existingContent );
1251+
1252+ Set <String > updatedSet =
1253+ new HashSet <>(updatedContent == null ? List .of () : updatedContent );
1254+
1255+ existingSet .removeAll (updatedSet );
1256+ return new ArrayList <>(existingSet );
1257+ }
1258+ }
0 commit comments