ShortFilterProcessor.java
/*
* Copyright 2021 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.service.worker;
import java.io.IOException;
import org.genesys.blocks.model.filters.SuperModelFilter;
import org.genesys.server.model.ShortFilter;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.service.ShortFilterService.FilterInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonMappingException;
/**
* The Class ShortFilterProcessor.
*/
@Component
public class ShortFilterProcessor {
private static final Logger LOG = LoggerFactory.getLogger(ShortFilterProcessor.class);
@Autowired
protected ShortFilterService shortFilterService;
public <T extends SuperModelFilter<T, ?>> FilterInfo<T> processFilter(final String filterCode, final T filter, Class<T> filterType) throws IOException {
Throwable lastException = null;
for (int i = 5; i >=0; i--) {
try {
return doProcessFilter(filterCode, filter, filterType);
} catch (IOException e) {
throw e;
} catch (Throwable e) {
LOG.warn("Error processing filter: {}", e.getMessage());
lastException = e;
}
}
throw new IOException("Failed to process filter: " + lastException.getMessage(), lastException);
}
private <T extends SuperModelFilter<T, ?>> FilterInfo<T> doProcessFilter(final String filterCode, final T filter, Class<T> filterType) throws IOException {
FilterInfo<T> processedFilter = new FilterInfo<>();
if (filterCode != null) {
try {
processedFilter.filter = shortFilterService.filterByCode(filterCode, filterType);
processedFilter.filterCode = filterCode;
} catch (JsonMappingException e) {
LOG.warn("Stored filter does not match target {}", filterType);
ShortFilter shortFilter = shortFilterService.loadByCode(filterCode == null ? "" : filterCode);
// Reads recognized properties
T updatedFilter = shortFilterService.readFilter(shortFilter.getJson(), filterType);
processedFilter.filter = updatedFilter;
processedFilter.filterCode = shortFilterService.getCode(updatedFilter);
}
} else {
var normalizedFilter = shortFilterService.normalizeFilter(filter, filterType);
processedFilter.filter = normalizedFilter;
processedFilter.filterCode = shortFilterService.getCode(normalizedFilter);
}
return processedFilter;
}
public <T extends SuperModelFilter<T,?>> T filterByCode(String filterCode, Class<T> clazz) throws IOException {
return shortFilterService.filterByCode(filterCode, clazz);
}
public <T extends SuperModelFilter<T, ?>> T normalizeFilter(T filter, Class<T> filterType) throws IOException {
return shortFilterService.normalizeFilter(filter, filterType);
}
}