org.usergrid.services.ServiceParameter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of usergrid-services Show documentation
Show all versions of usergrid-services Show documentation
Service layer for Usergrid system.
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* 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 org.usergrid.services;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.usergrid.utils.ListUtils.dequeue;
import static org.usergrid.utils.ListUtils.dequeueCopy;
import static org.usergrid.utils.ListUtils.isEmpty;
import static org.usergrid.utils.ListUtils.queue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.persistence.Query;
public abstract class ServiceParameter {
private static final Logger logger = LoggerFactory
.getLogger(ServiceParameter.class);
ServiceParameter() {
}
public UUID getId() {
return null;
}
public String getName() {
return null;
}
public Query getQuery() {
return null;
}
@Override
public String toString() {
return "";
}
public boolean isQuery() {
return false;
}
public boolean isId() {
return false;
}
public boolean isName() {
return false;
}
public static List addParameter(
List parameters, UUID entityId) {
if (parameters == null) {
parameters = new ArrayList();
}
if (entityId == null) {
return parameters;
}
ServiceParameter p = new IdParameter(entityId);
parameters.add(p);
return parameters;
}
public static List addParameter(
List parameters, String name) {
if (parameters == null) {
parameters = new ArrayList();
}
if (name == null) {
return parameters;
}
if ("all".equals(name)) {
Query query = new Query();
ServiceParameter p = new QueryParameter(query);
parameters.add(p);
return parameters;
}
ServiceParameter p = new NameParameter(name);
parameters.add(p);
return parameters;
}
public static List addParameter(
List parameters, Query query) {
if (parameters == null) {
parameters = new ArrayList();
}
if (query == null) {
return parameters;
}
if (lastParameterIsQuery(parameters)) {
logger.error("Adding two queries in a row");
}
ServiceParameter p = new QueryParameter(query);
parameters.add(p);
return parameters;
}
public static List addParameters(
List parameters, Object... params)
throws Exception {
if (parameters == null) {
parameters = new ArrayList();
}
if (params == null) {
return parameters;
}
for (Object param : params) {
if (param instanceof UUID) {
addParameter(parameters, (UUID) param);
} else if (param instanceof String) {
addParameter(parameters, (String) param);
} else if (param instanceof Query) {
addParameter(parameters, (Query) param);
}
}
return parameters;
}
public static List parameters(Object... params)
throws Exception {
return addParameters(null, params);
}
public static boolean firstParameterIsName(List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(0).isName();
}
return false;
}
public static boolean lastParameterIsName(List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(parameters.size() - 1).isName();
}
return false;
}
public static boolean firstParameterIsQuery(
List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(0).isQuery();
}
return false;
}
public static boolean lastParameterIsQuery(List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(parameters.size() - 1).isQuery();
}
return false;
}
public static boolean firstParameterIsId(List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(0).isId();
}
return false;
}
public static boolean lastParameterIsId(List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(parameters.size() - 1).isId();
}
return false;
}
public static ServiceParameter firstParameter(
List parameters) {
if (!isEmpty(parameters)) {
return parameters.get(0);
}
return null;
}
public static boolean moreParameters(List parameters) {
return moreParameters(parameters, true);
}
public static boolean moreParameters(List parameters,
boolean ignoreTrailingQueries) {
if (isEmpty(parameters)) {
return false;
}
if (ignoreTrailingQueries) {
for (ServiceParameter parameter : parameters) {
if (!(parameter instanceof QueryParameter)) {
return true;
}
}
return false;
}
return true;
}
public static int parameterCount(List parameters) {
return parameterCount(parameters, false);
}
public static int parameterCount(List parameters,
boolean ignoreTrailingQueries) {
if (isEmpty(parameters)) {
return 0;
}
int count = parameters.size();
if (ignoreTrailingQueries) {
count = 0;
for (ServiceParameter parameter : parameters) {
if (!(parameter instanceof QueryParameter)) {
count++;
} else {
return count;
}
}
}
return count;
}
public static ServiceParameter dequeueParameter(
List parameters) {
return dequeue(parameters);
}
public static void queueParameter(List parameters,
ServiceParameter parameter) {
parameters = queue(parameters, parameter);
}
public static List mergeQueries(Query query,
List parameters) {
while (firstParameterIsQuery(parameters)) {
parameters = dequeueCopy(parameters);
}
return parameters;
}
public static List filter(
List parameters,
Map, List> replaceParameters) {
if (replaceParameters == null) {
return parameters;
}
if ((parameters == null) || (parameters.size() == 0)) {
return parameters;
}
for (Entry, List> replaceSet : replaceParameters
.entrySet()) {
if (parameters.size() < replaceSet.getKey().size()) {
continue;
}
boolean found = true;
for (int i = 0; i < replaceSet.getKey().size(); i++) {
String matchStr = replaceSet.getKey().get(i);
ServiceParameter param = parameters.get(i);
if (matchStr.equals("$id")
&& ((param instanceof IdParameter) || (param instanceof NameParameter))) {
continue;
} else if (matchStr.equals("$query")
&& (param instanceof QueryParameter)) {
continue;
} else if (matchStr.equalsIgnoreCase(param.getName())) {
continue;
}
found = false;
break;
}
if (!found) {
continue;
}
ArrayList p = new ArrayList();
for (String name : replaceSet.getValue()) {
if (name.startsWith("\\")) {
int i = Integer.parseInt(name.substring(1));
p.add(parameters.get(i));
} else {
p.add(new NameParameter(name));
}
}
p.addAll(parameters.subList(replaceSet.getKey().size(),
parameters.size()));
return p;
}
return parameters;
}
public static class IdParameter extends ServiceParameter {
UUID entityId;
public IdParameter(UUID entityId) {
this.entityId = entityId;
}
@Override
public UUID getId() {
return entityId;
}
@Override
public boolean isId() {
return true;
}
@Override
public String toString() {
return entityId.toString();
}
}
public static class NameParameter extends ServiceParameter {
String name;
public NameParameter(String name) {
name = name.trim().toLowerCase();
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public boolean isName() {
return true;
}
@Override
public String toString() {
return name;
}
}
public static class QueryParameter extends ServiceParameter {
Query query;
public QueryParameter(Query query) {
this.query = query;
}
@Override
public Query getQuery() {
return query;
}
@Override
public boolean isQuery() {
return true;
}
@Override
public String toString() {
String queryStr = query.toString();
if (isNotBlank(queryStr)) {
return queryStr;
}
return "";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy