All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.appng.api.rest.DataSourceHelper Maven / Gradle / Ivy

/*
 * Copyright 2011-2021 the original author or authors.
 *
 * 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.appng.api.rest;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.appng.api.rest.model.Datasource;
import org.appng.api.rest.model.Element;
import org.appng.api.rest.model.FieldValue;
import org.appng.api.rest.model.Filter;
import org.appng.api.rest.model.Link;
import org.appng.api.rest.model.Page;
import org.springframework.http.ResponseEntity;

/**
 * Utility class that helps dealing with {@link Datasource}s.
 */
public class DataSourceHelper {

	private ResponseEntity dataSource;

	public DataSourceHelper(ResponseEntity dataSource) {
		this.dataSource = dataSource;
	}

	public static DataSourceHelper create(ResponseEntity dataSource) {
		return new DataSourceHelper(dataSource);
	}

	@Deprecated
	public Optional getResult(int i) {
		return getElement(i);
	}

	public Optional getElement(int i) {
		Page page = dataSource.getBody().getPage();
		if (null != page && null != page.getElements() && i > -1 && page.getElements().size() > i) {
			return Optional.of(page.getElements().get(i));
		}
		return Optional.empty();
	}

	public Optional getLink(Element item, String linkId) {
		return item.getLinks().stream().filter(l -> l.getId().equals(linkId)).findFirst();
	}

	public Optional getField(Element item, String name) {
		return getField(item.getFields(), name);
	}

	private Optional getField(List fields, String name) {
		if (!name.contains(".")) {
			return fields.stream().filter(f -> f.getName().equals(name)).findFirst();
		}
		String[] segments = name.split("\\.");
		Optional root = fields.stream().filter(f -> f.getName().equals(segments[0])).findFirst();
		if (root.isPresent()) {
			return getField(root.get().getValues(),
					StringUtils.join(Arrays.copyOfRange(segments, 1, segments.length), "."));
		}
		return Optional.empty();
	}

	/**
	 * Retrieves the filter with the given name, if any
	 * 
	 * @param name
	 *             the name of the filter
	 * 
	 * @return an {@code Optional}
	 */
	public Optional getFilter(String name) {
		List filters = dataSource.getBody().getFilters();
		if (null == filters) {
			return Optional.empty();
		}
		return filters.parallelStream().filter(f -> f.getName().equals(name)).findFirst();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy