Skip to content

Commit

Permalink
add two new classes for query options. use reflection to create query…
Browse files Browse the repository at this point in the history
… objects #528
  • Loading branch information
julie-sullivan committed Feb 20, 2020
1 parent 0cf4506 commit bc35aa0
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 166 deletions.
6 changes: 5 additions & 1 deletion cellbase-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
<groupId>org.opencb.biodata</groupId>
<artifactId>biodata-tools</artifactId>
</dependency>

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@

package org.opencb.cellbase.core.api.queries;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;

import java.util.ArrayList;
import javax.ws.rs.core.MultivaluedMap;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class AbstractQuery<T> {
public class AbstractQuery extends QueryOptions {

protected List<String> includes;
protected List<String> excludes;
protected Integer skip;
protected Integer limit;
protected String sort;
protected String facet;
protected String timeout;
protected Boolean count;
protected Logger logger;

final static int DEFAULT_LIMIT = 10;
Expand All @@ -40,133 +36,27 @@ public class AbstractQuery<T> {
public AbstractQuery() {
}

// public T of(Map<String, Object> map) throws JsonProcessingException {
// ObjectMapper objectMapper= new ObjectMapper();
// String value = objectMapper.writeValueAsString(map);
// return objectMapper.readValue(value, (Class) T);
// }

// public QueryOptions addQueryOption(String key, Object value) {
// if (queryOptions == null) {
// queryOptions = new QueryOptions();
// }
// queryOptions.put(key, value);
// return queryOptions;
// }
//
// public QueryOptions getQueryOptions() {
// return queryOptions;
// }
//
// public AbstractQuery setQueryOptions(QueryOptions queryOptions) {
// this.queryOptions = queryOptions;
// return this;
// }

public List<String> getIncludes() {
return includes;
}

public AbstractQuery<T> setIncludes(List<String> includes) {
this.includes = includes;
return this;
}

public List<String> getExcludes() {
return excludes;
}

public AbstractQuery<T> setExcludes(List<String> excludes) {
this.excludes = excludes;
return this;
}

public AbstractQuery<T> addExcludes(String excludes) {
if (this.excludes == null) {
this.excludes = new ArrayList<>();
public static Object of(MultivaluedMap<String, String> map, Class clazz)
throws NoSuchFieldException, IllegalAccessException, InstantiationException, InvocationTargetException {
Object query = clazz.newInstance();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String fieldName = entry.getKey();
List<String> values = entry.getValue();
Field field = clazz.getField(fieldName);
if (field.getType().equals("Boolean")) {
Boolean bool = Boolean.parseBoolean(values.get(0));
BeanUtils.setProperty(query, fieldName, bool);
} else if (field.getType().equals("Integer")) {
Integer intValue = Integer.parseInt(values.get(0));
BeanUtils.setProperty(query, fieldName, intValue);
} else if (field.getType().equals("List")) {
List<String> valuesArray = Arrays.asList(values.get(0).split(","));
BeanUtils.setProperty(query, fieldName, valuesArray);
} else {
BeanUtils.setProperty(query, fieldName, values.get(0));
}
}
this.excludes.addAll(Arrays.asList(excludes.split(",")));
return this;
}

public Integer getSkip() {
return skip;
}

public AbstractQuery<T> setSkip(Integer skip) {
this.skip = skip;
return this;
}

public AbstractQuery<T> addSkipIfAbsent() {
if (skip == null) {
skip = DEFAULT_SKIP;
}
return this;
}

public Integer getLimit() {
return limit;
}

public AbstractQuery<T> setLimit(Integer limit) {
this.limit = limit;
return this;
return query;
}

public AbstractQuery<T> addLimitIfAbsent() {
if (limit == null) {
limit = DEFAULT_LIMIT;
}
return this;
}

public String getSort() {
return sort;
}

public AbstractQuery<T> setSort(String sort) {
this.sort = sort;
return this;
}

public String getFacet() {
return facet;
}

public AbstractQuery<T> setFacet(String facet) {
this.facet = facet;
return this;
}

public String getTimeout() {
return timeout;
}

public AbstractQuery<T> setTimeout(String timeout) {
this.timeout = timeout;
return this;
}

public Boolean getCount() {
return count;
}

public AbstractQuery<T> setCount(Boolean count) {
this.count = count;
return this;
}

@Override
public String toString() {
return "AbstractQuery{" +
"includes=" + includes +
", excludes=" + excludes +
", skip=" + skip +
", limit=" + limit +
", sort='" + sort + '\'' +
", facet='" + facet + '\'' +
", timeout='" + timeout + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package org.opencb.cellbase.core.api.queries;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opencb.biodata.models.core.Region;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class GeneQuery extends FeatureQuery {

Expand All @@ -45,16 +42,8 @@ public class GeneQuery extends FeatureQuery {
private List<String> annotationDrugsGene;

public GeneQuery() {

}


public GeneQuery(Builder builder) {

}



// public GeneQuery(MultivaluedMap<String, String> multivaluedMap) throws CellbaseException {
// for (Map.Entry<String, List<String>> entry : multivaluedMap.entrySet()) {
//
Expand Down Expand Up @@ -125,11 +114,11 @@ public GeneQuery(Builder builder) {
// }
// }

public static GeneQuery of(Map<String, Object> map) throws JsonProcessingException {
ObjectMapper objectMapper= new ObjectMapper();
String value = objectMapper.writeValueAsString(map);
return objectMapper.readValue(value, GeneQuery.class);
}
// public static GeneQuery of(Map<String, Object> map) throws JsonProcessingException {
// ObjectMapper objectMapper= new ObjectMapper();
// String value = objectMapper.writeValueAsString(map);
// return objectMapper.readValue(value, GeneQuery.class);
// }

public List<String> getIds() {
return ids;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2015-2020 OpenCB
*
* 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.opencb.cellbase.core.api.queries;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProjectionQueryOptions {

List<String> excludes;
List<String> includes;

public ProjectionQueryOptions() {
}

public List<String> getExcludes() {
return excludes;
}

public ProjectionQueryOptions setExcludes(List<String> excludes) {
this.excludes = excludes;
return this;
}

public ProjectionQueryOptions addExcludes(String excludes) {
if (this.excludes == null) {
this.excludes = new ArrayList<>();
}
this.excludes.addAll(Arrays.asList(excludes.split(",")));
return this;
}

public List<String> getIncludes() {
return includes;
}

public ProjectionQueryOptions setIncludes(List<String> includes) {
this.includes = includes;
return this;
}

@Override
public String toString() {
return "ProjectionQueryOptions{" +
"excludes=" + excludes +
", includes=" + includes +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2015-2020 OpenCB
*
* 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.opencb.cellbase.core.api.queries;

public class QueryOptions extends ProjectionQueryOptions {

protected Integer skip;
protected Integer limit;
protected String sort;
protected Boolean count;
protected Order order;

enum Order {
ASCENDING,
DESCENDING
}

public QueryOptions() {
}

public Integer getSkip() {
return skip;
}

public QueryOptions setSkip(Integer skip) {
this.skip = skip;
return this;
}

public Integer getLimit() {
return limit;
}

public QueryOptions setLimit(Integer limit) {
this.limit = limit;
return this;
}

public String getSort() {
return sort;
}

public QueryOptions setSort(String sort) {
this.sort = sort;
return this;
}

public Boolean getCount() {
return count;
}

public QueryOptions setCount(Boolean count) {
this.count = count;
return this;
}

public Order getOrder() {
return order;
}

public QueryOptions setOrder(Order order) {
this.order = order;
return this;
}

@Override
public String toString() {
return "QueryOptions{" +
"skip=" + skip +
", limit=" + limit +
", sort='" + sort + '\'' +
", count=" + count +
", order=" + order +
'}';
}
}
Loading

0 comments on commit bc35aa0

Please sign in to comment.