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

de.tsl2.nano.cursus.Grex Maven / Gradle / Ivy

The newest version!
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Tom
 * created on: 31.03.2017
 * 
 * Copyright: (c) Thomas Schneider 2017, all rights reserved
 */
package de.tsl2.nano.cursus;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import de.tsl2.nano.bean.BeanContainer;
import de.tsl2.nano.bean.BeanFindParameters;
import de.tsl2.nano.bean.def.Bean;
import de.tsl2.nano.core.util.MapUtil;
import de.tsl2.nano.core.util.Util;

/**
 * Provides a generator for Res
 * @author Tom
 *
 * @param  instance type
 * @param  value type
 */
public class Grex implements Serializable {
	private static final String WILDCARD = "*";

	private static final long serialVersionUID = 1L;

	protected Res genRes;
	/** optional set of object ids */
	protected Set validObjectIDs;

	public Grex() {
		super();
	}

	public Grex(Class type, String path, Object... objectIDs) {
		this(new Res<>(type, WILDCARD, path), objectIDs);
	}

	public Grex(Res genericRes, Object... objectIDs) {
		this.genRes = genericRes;
		if (objectIDs.length > 0)
			this.validObjectIDs = MapUtil.asSet(objectIDs);
	}

	/**
	 * @return all res belonging to this group. Warning: will do calls to the database. if no find parameters were
	 * defined, all object-ids of the group type will be loaded!
	 */
	Set> findParts(BeanFindParameters parameters) {
		Set> rei = new HashSet<>();
		if (parameters == null)
			parameters = new BeanFindParameters(genRes.getType());
		Collection beans = BeanContainer.instance().getBeans(parameters);
		for (Object obj : beans) {
			rei.add(createResForId(Bean.getBean(obj).getId()));
		}
		return rei;
	}

	@SuppressWarnings("unchecked")
	public Set> createParts() {
		return !Util.isEmpty(validObjectIDs) && !isWildcardOjbect(validObjectIDs)
				? createNewParts(validObjectIDs.toArray())
				: genRes.getObjectid().toString().contains(WILDCARD) ? null
						: MapUtil.asSet(createResForId(genRes.getObjectid()));
	}

	private boolean isWildcardOjbect(Set validObjectIDs) {
		return validObjectIDs == null
				|| (validObjectIDs.size() == 1 && validObjectIDs.iterator().next().equals(WILDCARD));
	}

	public Set> createNewParts(Object... objectIds) {
		Set> parts = new HashSet<>(objectIds.length);
		for (int i = 0; i < objectIds.length; i++) {
			parts.add(createResForId(objectIds[i]));
		}
		return parts;
	}

	public Res createResForId(Object objectId) {
		return new Res<>(genRes.getType(), objectId, genRes.getPath());
	}

	public boolean isPart(Res res) {
		return res.type.equals(genRes.type) && res.getPath().equals(genRes.getPath())
				&& (validObjectIDs == null || validObjectIDs.contains(res.objectid));
	}

	@Override
	public int hashCode() {
		return Util.hashCode(genRes);
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Grex))
			return false;
		Grex m = (Grex) o;
		return Util.equals(this.genRes, m.genRes);
	}

	@Override
	public String toString() {
		return Util.toString(getClass(), genRes);
	}

}