com.jaxio.jpa.querybyexample.PropertySelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-querybyexample Show documentation
Show all versions of jpa-querybyexample Show documentation
This API allows you to construct Query By Example using the JPA 2 Criteria API.
/*
* Copyright 2015 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 com.jaxio.jpa.querybyexample;
import org.apache.commons.lang.builder.ToStringBuilder;
import javax.persistence.metamodel.Attribute;
import java.io.Serializable;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
/**
* Used to construct OR predicate for a property value. In other words you can search
* all entities E having a given property set to one of the selected values.
*/
public class PropertySelector implements Serializable {
/*
* PropertySelector builder
*/
public static PropertySelector newPropertySelector(Attribute, ?>... fields) {
return new PropertySelector(checkNotNull(fields));
}
/*
* PropertySelector builder
*/
public static PropertySelector newPropertySelector(String path, Class from) {
return new PropertySelector(path, from);
}
/*
* PropertySelector builder
*/
public static PropertySelector newPropertySelector(boolean orMode, Attribute, ?>... fields) {
PropertySelector ps = new PropertySelector(checkNotNull(fields));
return ps.orMode(orMode);
}
private static final long serialVersionUID = 1L;
private final PathHolder pathHolder;
private List selected = newArrayList();
private SearchMode searchMode; // for string property only.
private Boolean notIncludingNull;
private boolean orMode = true;
public PropertySelector(Attribute, ?>... attributes) {
this.pathHolder = new PathHolder(checkNotNull(attributes));
}
public PropertySelector(String path, Class from) {
this.pathHolder = new PathHolder(path, from);
}
public List> getAttributes() {
return pathHolder.getAttributes();
}
public boolean isNotIncludingNullSet() {
return notIncludingNull != null;
}
public Boolean isNotIncludingNull() {
return notIncludingNull;
}
public PropertySelector withoutNull() {
this.notIncludingNull = true;
return this;
}
/*
* Get the possible candidates for property.
*/
public List getSelected() {
return selected;
}
public PropertySelector add(F object) {
this.selected.add(object);
return this;
}
/*
* Set the possible candidates for property.
*/
public void setSelected(List selected) {
this.selected = selected;
}
public PropertySelector selected(F... selected) {
setSelected(newArrayList(selected));
return this;
}
public boolean isNotEmpty() {
return selected != null && !selected.isEmpty();
}
public void clearSelected() {
if (selected != null) {
selected.clear();
}
}
@SuppressWarnings("unchecked")
public void setValue(F value) {
this.selected = newArrayList(value);
}
public F getValue() {
return isNotEmpty() ? selected.get(0) : null;
}
public boolean isBoolean() {
return isType(Boolean.class);
}
public boolean isLabelizedEnum() {
return isType(LabelizedEnum.class);
}
public boolean isString() {
return isType(String.class);
}
public boolean isNumber() {
return isType(Number.class);
}
public boolean isType(Class> type) {
return type.isAssignableFrom(getAttributes().get(getAttributes().size() - 1).getJavaType());
}
public SearchMode getSearchMode() {
return searchMode;
}
/*
* In case, the field's type is a String, you can set a searchMode to use. It is null by default.
*/
public void setSearchMode(SearchMode searchMode) {
this.searchMode = searchMode;
}
public PropertySelector searchMode(SearchMode searchMode) {
setSearchMode(searchMode);
return this;
}
public boolean isOrMode() {
return orMode;
}
public void setOrMode(boolean orMode) {
this.orMode = orMode;
}
public PropertySelector orMode(boolean orMode) {
setOrMode(orMode);
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy