ShortFilterService.java

/*
 * Copyright 2018 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;

import org.genesys.blocks.model.filters.SuperModelFilter;
import org.genesys.server.model.ShortFilter;

import java.io.IOException;

/**
 * The Interface ShortFilterService.
 *
 * @author Maxym Borodenko
 */
public interface ShortFilterService {

	/**
	 * Normalize the filter.
	 *
	 * @param filter object to be normalized
	 * @return string with normalized filter
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	<T extends SuperModelFilter<T, ?>> String normalizeFilter(T filter) throws IOException;

	/**
	 * Normalize the filter.
	 *
	 * @param filter object to be normalized
	 * @param clazz the clazz
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	<T extends SuperModelFilter<T, ?>> T normalizeFilter(T filter, Class<T> clazz) throws IOException;

	/**
	 * Load short filter or create a new code.
	 *
	 * @param filter original filter object
	 * @return found or created name of the shortened filter object
	 * @throws IOException 
	 */
	<T extends SuperModelFilter<T, ?>> String getCode(T filter) throws IOException;

	/**
	 * Load ShortFilter by short name.
	 *
	 * @param code short name of ShortFilter
	 * @return found ShortFilter
	 */
	ShortFilter loadByCode(String code);

	/**
	 * Load ShortFilter by json filters.
	 *
	 * @param json json filters of ShortFilter
	 * @return found ShortFilter
	 */
	ShortFilter loadByJSON(String json);

	/**
	 * Returns the Java filter object by short name.
	 *
	 * @param <T> the generic type
	 * @param code the filter code
	 * @param clazz the clazz
	 * @return the Instance of type T with data from JSON
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	<T extends SuperModelFilter<T,?>> T filterByCode(String code, Class<T> clazz) throws IOException;

	public static class FilterInfo<T> {
		public T filter;
		public String filterCode;
	}

	/**
	 * Upgrade a {@link ShortFilter} to the current version of the short code.
	 * 
	 * Maintains the original JSON + registers a new entry!
	 * 
	 * @param shortFilter the filter
	 * @return filterCode by current standards
	 */
	ShortFilter upgradeFilterCode(ShortFilter shortFilter);

	/**
	 * Parse JSON to filter type
	 *
	 * @param json
	 * @param clazz
	 * @return
	 * @throws IOException 
	 */
	<T extends SuperModelFilter<T,?>> T readFilter(String json, Class<T> clazz) throws IOException;

}