/*******************************************************************************
* Copyright (c) 2014 antoniomariasanchez at gmail.com.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* antoniomaria - initial API and implementation
******************************************************************************/
/*
* Copyright 2012 JAXIO http://www.jaxio.com 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 net.sf.gazpachoquest.qbe;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.metamodel.SingularAttribute;
import net.sf.gazpachoquest.domain.support.Persistable;
import org.apache.commons.lang3.Validate;
/**
* Used to construct OR predicate for a single foreign key value. In other words
* you can search all entities E having
* their x-to-one association value set to one of the selected values. To avoid
* a join we rely on the foreign key field,
* not the association itself.
*/
public class EntitySelector<E, T extends Persistable, TPK extends Serializable> {
/**
* Import statically this helper for smooth instanciation.
*/
static public <E2, T2 extends Persistable, TPK2 extends Serializable> EntitySelector<E2, T2, TPK2> entitySelector(
final SingularAttribute<E2, TPK2> field) {
return new EntitySelector<E2, T2, TPK2>(field);
}
static public <E2, T2 extends Persistable, TPK2 extends Serializable> EntitySelector<E2, T2, TPK2> entitySelector(
final SingularAttribute<E2, TPK2> field, final T2... values) {
return new EntitySelector<E2, T2, TPK2>(field, values);
}
/**
* Import statically this helper for smooth instanciation. It is used in the
* case where the PK is composite AND the
* pk member(s) are/is also a foreign key.
*/
static public <E2, T2 extends Persistable, TPK2 extends Serializable, CPK2> EntitySelector<E2, T2, TPK2> newEntitySelectorInCpk(
final SingularAttribute<E2, CPK2> cpkField, final SingularAttribute<CPK2, TPK2> cpkInnerField) {
return new EntitySelector<E2, T2, TPK2>(cpkField, cpkInnerField);
}
private final SingularAttribute<E, ?> cpkField;
private final SingularAttribute<?, TPK> cpkInnerField;
private final SingularAttribute<E, TPK> field;
private List<T> selected = new ArrayList<T>();
public EntitySelector(final SingularAttribute<E, ?> cpkField, final SingularAttribute<?, TPK> cpkInnerField) {
this.cpkField = cpkField;
this.cpkInnerField = cpkInnerField;
this.field = null; // not used
}
/**
* @param field
* the property holding an foreign key.
*/
public EntitySelector(final SingularAttribute<E, TPK> field) {
this.field = field;
this.cpkField = null;
this.cpkInnerField = null;
}
public EntitySelector(final SingularAttribute<E, TPK> field, final T... values) {
this(field);
for (T value : values) {
Validate.notNull(value);
selected.add(value);
}
}
public void clearSelected() {
if (selected != null) {
selected.clear();
}
}
public SingularAttribute<E, ?> getCpkField() {
return cpkField;
}
public SingularAttribute<?, TPK> getCpkInnerField() {
return cpkInnerField;
}
public SingularAttribute<E, TPK> getField() {
return field;
}
/**
* Get the possible candidates for the x-to-one association.
*/
public List<T> getSelected() {
return selected;
}
public boolean isNotEmpty() {
return selected != null && !selected.isEmpty();
}
public void selected(final T... selected) {
List<T> t = new ArrayList<T>();
for (T s : selected) {
t.add(s);
}
this.selected = t;
}
/**
* Set the possible candidates for the x-to-one association.
*/
public void setSelected(final List<T> selected) {
this.selected = selected;
}
}