-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdated_config_models.java
More file actions
251 lines (198 loc) · 10 KB
/
updated_config_models.java
File metadata and controls
251 lines (198 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Updated Configuration Models to handle comma-separated values
@Data
@ConfigurationProperties(prefix = "solr.update")
public class SolrUpdateConfiguration {
private Map<String, CollectionConfig> collections = new HashMap<>();
}
@Data
public class CollectionConfig {
private String collectionName;
private String entityClass;
private IdentifierConfig identifier;
private String partialUpdateFields; // Changed to String for comma-separated
private Map<String, SolrFieldMapping> solrFieldMappings = new HashMap<>();
private Map<String, Object> defaultValues = new HashMap<>();
private int batchSize = 1000;
// Helper method to get partial update fields as list
public List<String> getPartialUpdateFieldsList() {
if (partialUpdateFields == null || partialUpdateFields.trim().isEmpty()) {
return new ArrayList<>();
}
return Arrays.stream(partialUpdateFields.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}
}
@Data
public class IdentifierConfig {
private String sourceFields; // Changed to String for comma-separated
private String separator = "_";
private String solrField = "id";
// Helper method to get source fields as list
public List<String> getSourceFieldsList() {
if (sourceFields == null || sourceFields.trim().isEmpty()) {
return new ArrayList<>();
}
return Arrays.stream(sourceFields.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}
}
@Data
public class SolrFieldMapping {
private String sourceField;
private String solrField;
private String tableName;
}
// Updated Service to use the new helper methods
@Service
@Slf4j
public class UpdatedTableBasedPartialUpdateService {
private final SolrUpdateConfiguration config;
private final SolrClient solrClient;
private final CsvProcessingService csvService;
private final BusinessDataPopulationService businessService;
public UpdatedTableBasedPartialUpdateService(
SolrUpdateConfiguration config,
SolrClient solrClient,
CsvProcessingService csvService,
BusinessDataPopulationService businessService) {
this.config = config;
this.solrClient = solrClient;
this.csvService = csvService;
this.businessService = businessService;
}
public void processPartialUpdate(String fileName, String csvFilePath) {
try {
String collectionName = extractCollectionName(fileName);
log.info("Processing partial update for collection: {}", collectionName);
CollectionConfig collectionConfig = getCollectionConfig(collectionName);
// Log configuration for debugging
logConfigurationDetails(collectionConfig);
List<Map<String, String>> csvRecords = csvService.readCsv(csvFilePath);
log.info("Read {} records from CSV", csvRecords.size());
processBatches(collectionConfig, csvRecords);
solrClient.commit(collectionConfig.getCollectionName());
log.info("Successfully processed {} records for collection {}",
csvRecords.size(), collectionName);
} catch (Exception e) {
log.error("Error processing partial update for file: {}", fileName, e);
throw new RuntimeException("Partial update failed for file: " + fileName, e);
}
}
private void logConfigurationDetails(CollectionConfig config) {
log.debug("Collection Configuration:");
log.debug(" Collection Name: {}", config.getCollectionName());
log.debug(" Entity Class: {}", config.getEntityClass());
log.debug(" Identifier Fields: {}", config.getIdentifier().getSourceFieldsList());
log.debug(" Identifier Separator: '{}'", config.getIdentifier().getSeparator());
log.debug(" Partial Update Fields: {}", config.getPartialUpdateFieldsList());
for (String field : config.getPartialUpdateFieldsList()) {
SolrFieldMapping mapping = config.getSolrFieldMappings().get(field);
if (mapping != null) {
log.debug(" Field Mapping - {}: {} → {} (Table: {})",
field, mapping.getSourceField(), mapping.getSolrField(), mapping.getTableName());
}
}
}
private void executeBusinessMethods(CollectionConfig config, Map<String, String> csvRecord, Object entity)
throws Exception {
// Get unique tables for the partial update fields
List<String> partialUpdateFields = config.getPartialUpdateFieldsList();
Set<String> uniqueTables = getUniqueTablesForFields(partialUpdateFields, config.getSolrFieldMappings());
log.debug("Unique tables to execute: {} for fields: {}", uniqueTables, partialUpdateFields);
Set<String> executedTables = new HashSet<>();
for (String field : partialUpdateFields) {
SolrFieldMapping mapping = config.getSolrFieldMappings().get(field);
if (mapping == null) {
log.warn("No mapping found for field: {}", field);
continue;
}
String tableName = mapping.getTableName();
if (!executedTables.contains(tableName)) {
log.debug("Executing business method for table: {} (needed by field: {})", tableName, field);
businessService.populateFromTable(tableName, csvRecord, entity);
executedTables.add(tableName);
log.debug("Successfully executed table: {}", tableName);
} else {
log.debug("Skipping table: {} for field: {} (already executed)", tableName, field);
}
}
log.info("Executed {} unique tables out of {} fields for collection: {}",
executedTables.size(), partialUpdateFields.size(), config.getCollectionName());
}
private Set<String> getUniqueTablesForFields(List<String> partialUpdateFields,
Map<String, SolrFieldMapping> solrFieldMappings) {
return partialUpdateFields.stream()
.map(field -> solrFieldMappings.get(field))
.filter(Objects::nonNull)
.map(SolrFieldMapping::getTableName)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
private String createCompositeIdentifier(Object entity, IdentifierConfig identifier) throws Exception {
List<String> sourceFields = identifier.getSourceFieldsList();
List<String> idParts = new ArrayList<>();
for (String sourceField : sourceFields) {
Object value = getEntityFieldValue(entity, sourceField);
if (value == null) {
throw new IllegalArgumentException("Required identifier field is null: " + sourceField);
}
idParts.add(value.toString());
}
String compositeId = String.join(identifier.getSeparator(), idParts);
log.debug("Created composite ID: {} from fields: {}", compositeId, sourceFields);
return compositeId;
}
private SolrInputDocument createSolrDocument(CollectionConfig config, Object entity) throws Exception {
SolrInputDocument doc = new SolrInputDocument();
// Create composite identifier
String compositeId = createCompositeIdentifier(entity, config.getIdentifier());
doc.addField(config.getIdentifier().getSolrField(), compositeId);
// Create atomic updates for partial update fields only
List<String> partialUpdateFields = config.getPartialUpdateFieldsList();
for (String field : partialUpdateFields) {
SolrFieldMapping mapping = config.getSolrFieldMappings().get(field);
if (mapping == null) {
log.warn("No Solr field mapping found for field: {}", field);
continue;
}
Object value = getEntityFieldValue(entity, mapping.getSourceField());
if (value != null) {
Map<String, Object> atomicUpdate = new HashMap<>();
atomicUpdate.put("set", value);
doc.addField(mapping.getSolrField(), atomicUpdate);
log.debug("Added field to Solr update: {} = {}", mapping.getSolrField(), value);
}
}
addDefaultValues(doc, config.getDefaultValues());
return doc;
}
// ... rest of the methods remain the same ...
}
// Configuration Examples with comma-separated values
/*
Example Configuration:
# Simple single field identifier
solr.update.collections.product.identifier.source-fields=productId
# Two field composite identifier
solr.update.collections.subscriber.identifier.source-fields=customerId,subscriberId
solr.update.collections.subscriber.identifier.separator=_
# Three field composite identifier
solr.update.collections.order.identifier.source-fields=customerId,orderId,regionCode
solr.update.collections.order.identifier.separator=_
# Multiple partial update fields
solr.update.collections.subscriber.partial-update-fields=fieldA,fieldB,fieldC,fieldD
solr.update.collections.customer.partial-update-fields=name,email,phone,address,subscription
solr.update.collections.product.partial-update-fields=name,price,category,inventory,description
Examples:
- productId → "P001"
- customerId,subscriberId with separator "_" → "CUST001_SUB123"
- customerId,orderId,regionCode with separator "_" → "CUST001_ORD456_US"
- customerId,regionCode with separator "-" → "CUST001-US"
Partial update fields:
- fieldA,fieldB,fieldC,fieldD → ["fieldA", "fieldB", "fieldC", "fieldD"]
- name,email,phone → ["name", "email", "phone"]
*/