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

org.startupframework.data.service.ChildEntityServiceBase Maven / Gradle / Ivy

/*
 * Copyright 2019-2020 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
 *
 *      https://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.startupframework.data.service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;

import org.startupframework.data.entity.Entity;
import org.startupframework.data.entity.OldNewPred;
import org.startupframework.data.repository.EntityRepository;
import org.startupframework.entity.Identifiable;
import org.startupframework.exception.DataNotFoundException;
import org.startupframework.exception.DuplicateDataException;

import lombok.Getter;

/**
 * Service base class for EntityBase and inherited.
 *
 * @author Arq. Jesús Israel Anaya Salazar
 */
public abstract class ChildEntityServiceBase, E extends Entity>
		extends EntityServiceBase implements ChildEntityService {

	@Getter
	boolean isAggregated;

	protected ChildEntityServiceBase(final R repository, boolean isAggregated) {
		super(repository);
		this.isAggregated = isAggregated;
	}

	abstract protected String getParentId(E entity);

	abstract protected void setParentId(String parentId, E entity);

	abstract protected String getChildId(E entity);

	abstract protected void setChildId(String childId, E entity);

	abstract protected List onFindAll(String parentId);

	abstract protected Optional onFindById(String parentId, String childId);

	void validateIds(String parentId, String childId) {
		Identifiable.validate(parentId, "parentId");
		Identifiable.validate(childId, "childId");
	}

	final OldNewPred NEED_VALIDATE = (o, n) -> !Objects.equals(getChildId(o), getChildId(n));

	@Override
	protected void onValidateEntity(E entity) {

		Consumer validation = e -> {

			String parentId = getParentId(e);
			String childId = getChildId(e);

			Optional foundItem = onFindById(parentId, childId);
			foundItem.ifPresent(x -> {
				throw DuplicateDataException.from(childId);
			});
		};

		Optional oldEntity = getRepository().findById(entity.getId());
		validateIf(NEED_VALIDATE, validation, oldEntity, entity);

	}

	@Override
	final public E save(String parentId, String childId, E entity) {

		Identifiable.validate(parentId, "parentId");
		setParentId(parentId, entity);

		if (!isAggregated) {
			Identifiable.validate(childId, "childId");
			setChildId(childId, entity);
		}
		return this.save(entity);

	}

	@Override
	final public List findAll(String parentId) {
		Identifiable.validate(parentId, "parentId");
		return onFindAll(parentId);
	}

	@Override
	final public E findById(String parentId, String childId) {
		validateIds(parentId, childId);
		Optional foundItem = onFindById(parentId, childId);
		return foundItem.orElseThrow(() -> DataNotFoundException.fromIds(parentId, childId));
	}

	@Override
	final public void deleteById(String parentId, String childId) {
		validateIds(parentId, childId);
		E buffer = findById(parentId, childId);
		this.delete(buffer);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy