Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014 Blazebit.
*
* 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.blazebit.persistence.impl;
import java.util.*;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.metamodel.Attribute;
import com.blazebit.lang.ValueRetriever;
import com.blazebit.persistence.impl.expression.ParameterExpression;
import com.blazebit.persistence.impl.expression.VisitorAdapter;
import com.blazebit.persistence.impl.function.entity.ValuesEntity;
import com.blazebit.reflection.PropertyPathExpression;
/**
*
* @author Christian Beikov
* @author Moritz Becker
* @since 1.0
*/
public class ParameterManager {
private static final String prefix = "param_";
private int counter;
private final Map> parameters = new HashMap>();
private final Map valuesParameters = new HashMap();
private final VisitorAdapter parameterRegistrationVisitor;
public ParameterManager() {
this.parameterRegistrationVisitor = new ParameterRegistrationVisitor(this);
}
public VisitorAdapter getParameterRegistrationVisitor() {
return parameterRegistrationVisitor;
}
Set getParameterListNames(Query q) {
return getParameterListNames(q, Collections.EMPTY_SET);
}
Set getParameterListNames(Query q, Set skippedParameters) {
Set parameterListNames = new HashSet();
collectParameterListNames(q, parameterListNames, skippedParameters);
return parameterListNames;
}
void collectParameterListNames(Query q, Set parameterListNames) {
collectParameterListNames(q, parameterListNames, Collections.EMPTY_SET);
}
void collectParameterListNames(Query q, Set parameterListNames, Set skippedParameters) {
for (Parameter> p: q.getParameters()) {
String name = p.getName();
if (skippedParameters.contains(name)) {
continue;
}
if (getParameter(name).isCollectionValued()) {
parameterListNames.add(name);
}
}
}
void parameterizeQuery(Query q) {
parameterizeQuery(q, Collections.EMPTY_SET);
}
void parameterizeQuery(Query q, Set skippedParameters) {
Set requestedValueParameters = new HashSet();
for (Parameter> p : q.getParameters()) {
String parameterName = p.getName();
if (skippedParameters.contains(parameterName)) {
continue;
}
ParameterImpl> parameter = parameters.get(parameterName);
if (parameter == null) {
String valuesParameter = valuesParameters.get(parameterName);
if (valuesParameter == null) {
throw new IllegalArgumentException(String.format("Parameter name \"%s\" does not exist", parameterName));
}
// Skip binding the sub-parameter, we will do that in one go at the end
requestedValueParameters.add(valuesParameter);
continue;
}
// If a query requests the values parameter directly, it is aware of handling it
if (parameter.getParamerterValue() instanceof ValuesParameterWrapper) {
q.setParameter(parameterName, parameter.getValue());
} else {
parameter.bind(q);
}
}
for (String parameterName : requestedValueParameters) {
ParameterImpl> parameter = parameters.get(parameterName);
parameter.bind(q);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public ParameterImpl> getParameter(String parameterName) {
if (parameterName == null) {
throw new NullPointerException("parameterName");
}
ParameterImpl> parameter = parameters.get(parameterName);
return parameter;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public Set extends Parameter>> getParameters() {
return new HashSet>(parameters.values());
}
public Map getValuesParameters() {
return Collections.unmodifiableMap(valuesParameters);
}
public Map getValuesBinders() {
Map binders = new HashMap();
for (Map.Entry> entry : parameters.entrySet()) {
ParamerterValue value = entry.getValue().getParamerterValue();
if (value instanceof ValuesParameterWrapper) {
binders.put(entry.getKey(), ((ValuesParameterWrapper) value).getBinder());
}
}
return binders;
}
public boolean containsParameter(String parameterName) {
if (parameterName == null) {
throw new NullPointerException("parameterName");
}
return parameters.containsKey(parameterName);
}
public boolean isParameterSet(String parameterName) {
if (parameterName == null) {
throw new NullPointerException("parameterName");
}
ParameterImpl> parameter = parameters.get(parameterName);
return parameter != null && parameter.getValue() != null;
}
public Object getParameterValue(String parameterName) {
if (parameterName == null) {
throw new NullPointerException("parameterName");
}
ParameterImpl> parameter = parameters.get(parameterName);
if (parameter == null) {
throw new IllegalArgumentException(String.format("Parameter name \"%s\" does not exist", parameterName));
}
return parameter.getValue();
}
public ParameterExpression addParameterExpression(Object o) {
String name = addParameter(o, o instanceof Collection);
return new ParameterExpression(name, o, o instanceof Collection);
}
public String addParameter(Object o, boolean collectionValued) {
if (o == null) {
throw new NullPointerException();
}
String name = prefix + counter++;
parameters.put(name, new ParameterImpl