-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
…nges to solr indexes fixup after API changes in #876 move testcases and remove now useless test, use solr service inject fix rebase issues
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.repository.events; | ||
|
||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import org.apache.solr.client.solrj.SolrServerException; | ||
import org.apache.solr.common.SolrException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.rest.core.annotation.HandleAfterDelete; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeSave; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.tailormap.api.geotools.featuresources.FeatureSourceFactoryHelper; | ||
import org.tailormap.api.persistence.SearchIndex; | ||
import org.tailormap.api.persistence.TMFeatureType; | ||
import org.tailormap.api.persistence.json.AppLayerSettings; | ||
import org.tailormap.api.repository.ApplicationRepository; | ||
import org.tailormap.api.repository.SearchIndexRepository; | ||
import org.tailormap.api.solr.SolrHelper; | ||
import org.tailormap.api.solr.SolrService; | ||
|
||
/** Event handler for Solr indexes when a {@code TMFeatureType} is updated or deleted. */ | ||
@RepositoryEventHandler | ||
public class SolrTMFeatureTypeEventHandler { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
Check warning on line 31 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L30-L31
|
||
|
||
private final SearchIndexRepository searchIndexRepository; | ||
private final SolrService solrService; | ||
private final FeatureSourceFactoryHelper featureSourceFactoryHelper; | ||
private final ApplicationRepository applicationRepository; | ||
|
||
public SolrTMFeatureTypeEventHandler( | ||
SearchIndexRepository searchIndexRepository, | ||
SolrService solrService, | ||
FeatureSourceFactoryHelper featureSourceFactoryHelper, | ||
ApplicationRepository applicationRepository) { | ||
this.searchIndexRepository = searchIndexRepository; | ||
this.solrService = solrService; | ||
this.featureSourceFactoryHelper = featureSourceFactoryHelper; | ||
this.applicationRepository = applicationRepository; | ||
} | ||
Check warning on line 47 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L42-L47
|
||
|
||
/** | ||
* Handle the update of a TMFeatureType. | ||
* | ||
* @param tmFeatureType the TMFeatureType to handle | ||
*/ | ||
@HandleBeforeSave | ||
public void handleTMFeatureTypeUpdate(TMFeatureType tmFeatureType) { | ||
logger.debug("Handling TMFeatureType save event for: {}", tmFeatureType); | ||
Check warning on line 56 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L56
|
||
// determine if it is a new FT or an update | ||
if (null == tmFeatureType.getId()) { | ||
// do nothing as there is no index defined for a new feature type | ||
logger.debug("New TMFeatureType: {}", tmFeatureType); | ||
Check warning on line 60 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L60
|
||
} else { | ||
logger.debug("Updated TMFeatureType: {}", tmFeatureType); | ||
searchIndexRepository.findByFeatureTypeId(tmFeatureType.getId()).stream() | ||
.findAny() | ||
.ifPresent( | ||
Check warning on line 65 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L62-L65
|
||
searchIndex -> { | ||
logger.debug( | ||
Check warning on line 67 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L67
|
||
"Updating search index {} for feature type: {}", | ||
searchIndex.getName(), | ||
Check warning on line 69 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L69
|
||
searchIndex); | ||
|
||
try (SolrHelper solrHelper = | ||
new SolrHelper(solrService.getSolrClientForIndexing())) { | ||
solrHelper.addFeatureTypeIndex( | ||
Check warning on line 74 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L72-L74
|
||
searchIndex, | ||
tmFeatureType, | ||
featureSourceFactoryHelper, | ||
searchIndexRepository); | ||
} catch (UnsupportedOperationException | ||
Check warning on line 79 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L79
|
||
| IOException | ||
| SolrServerException | ||
| SolrException e) { | ||
logger.error("Error re-indexing", e); | ||
searchIndex.setStatus(SearchIndex.Status.ERROR); | ||
searchIndexRepository.save(searchIndex); | ||
} | ||
}); | ||
Check warning on line 87 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L83-L87
|
||
} | ||
} | ||
Check warning on line 89 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L89
|
||
|
||
/** | ||
* Handle the deletion of a TMFeatureType. | ||
* | ||
* @param tmFeatureType the TMFeatureType to handle | ||
*/ | ||
@HandleAfterDelete | ||
public void handleTMFeatureTypeDeleteForSolr(TMFeatureType tmFeatureType) { | ||
logger.debug("Handling TMFeatureType delete event for: {}", tmFeatureType); | ||
searchIndexRepository.findByFeatureTypeId(tmFeatureType.getId()).stream() | ||
.findAny() | ||
.ifPresent( | ||
Check warning on line 101 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L98-L101
|
||
searchIndex -> { | ||
logger.info( | ||
Check warning on line 103 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L103
|
||
"Deleting search index {} for feature type: {}", | ||
searchIndex.getName(), | ||
Check warning on line 105 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L105
|
||
searchIndex); | ||
|
||
try (SolrHelper solrHelper = new SolrHelper(solrService.getSolrClientForIndexing())) { | ||
solrHelper.clearIndexForLayer(searchIndex.getId()); | ||
searchIndexRepository.delete(searchIndex); | ||
Check warning on line 110 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L108-L110
|
||
// find any application layers that use this index clear the index from them | ||
applicationRepository | ||
.findByIndexId(searchIndex.getId()) | ||
.forEach( | ||
Check warning on line 114 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L112-L114
|
||
application -> { | ||
application | ||
.getAllAppTreeLayerNode() | ||
.forEach( | ||
Check warning on line 118 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L116-L118
|
||
appTreeLayerNode -> { | ||
AppLayerSettings appLayerSettings = | ||
application.getAppLayerSettings(appTreeLayerNode); | ||
Check warning on line 121 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L120-L121
|
||
if (null != appLayerSettings.getSearchIndexId() | ||
&& appLayerSettings | ||
.getSearchIndexId() | ||
Check warning on line 124 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L124
|
||
.equals(searchIndex.getId())) { | ||
appLayerSettings.setSearchIndexId(null); | ||
Check warning on line 126 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L126
|
||
} | ||
}); | ||
applicationRepository.save(application); | ||
}); | ||
} catch (UnsupportedOperationException | ||
Check warning on line 131 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L128-L131
|
||
| IOException | ||
| SolrServerException | ||
| SolrException e) { | ||
logger.error("Error deleting index for {}", searchIndex, e); | ||
} | ||
}); | ||
} | ||
Check warning on line 138 in src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java Codecov / codecov/patchsrc/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java#L135-L138
|
||
} |