Skip to content

Commit

Permalink
new functional-utils and this and that...
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrimatti committed Feb 25, 2015
1 parent cf76da8 commit 290052e
Show file tree
Hide file tree
Showing 32 changed files with 224 additions and 179 deletions.
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.solita.utils</groupId>
<artifactId>query-utils</artifactId>
<version>0.8.9</version>
<version>0.9</version>
<build>
<resources>
<resource>
Expand Down Expand Up @@ -76,9 +76,8 @@
<compilerArguments>-source 1.6 -target 1.6</compilerArguments>
<encoding>UTF-8</encoding>
<optionMap>
<CommonMetadataProcessor.excludesRegex>fi.solita.utils.query.(Department|Employee|PartTimeEmployee|Municipality|Report|entities.Table|.*Dto)</CommonMetadataProcessor.excludesRegex>
<JpaMetamodelProcessor.includesRegex>fi.solita.utils.query.(Department|Employee|PartTimeEmployee|Municipality|Report|entities.Table)</JpaMetamodelProcessor.includesRegex>
<JpaMetamodelProcessor.extendClassNamePattern>fi.solita.utils.query.(Department|Employee|PartTimeEmployee|Municipality|Report|entities.Table)</JpaMetamodelProcessor.extendClassNamePattern>
<CommonMetadataProcessor.excludesRegex>fi.solita.utils.query.(Department|Employee|PartTimeEmployee|Municipality|Report|entities.Table|backend.hibernate.TableValueType|.*Dto)</CommonMetadataProcessor.excludesRegex>
<JpaMetamodelProcessor.extendClassNamePattern>fi.solita.utils.query.(Department|Employee|PartTimeEmployee|Municipality|Report|entities.Table|backend.hibernate.TableValueType)</JpaMetamodelProcessor.extendClassNamePattern>
<JpaMetamodelProcessor.onlyPublicMembers>true</JpaMetamodelProcessor.onlyPublicMembers>
<JpaConstructorProcessor.includesRegex>fi.solita.utils.query..*Dto</JpaConstructorProcessor.includesRegex>
</optionMap>
Expand All @@ -103,13 +102,13 @@
<dependency>
<groupId>fi.solita.utils</groupId>
<artifactId>functional-utils</artifactId>
<version>0.8.1</version>
<version>0.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fi.solita.utils</groupId>
<artifactId>meta-utils</artifactId>
<version>0.8.2</version>
<version>0.9</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package fi.solita.utils.query;

public interface EntityRepresentation {
public interface EntityRepresentation<T> {

}
24 changes: 12 additions & 12 deletions src/main/java/fi/solita/utils/query/QueryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import static fi.solita.utils.functional.Functional.cons;
import static fi.solita.utils.functional.Functional.head;
import static fi.solita.utils.functional.Functional.isEmpty;
import static fi.solita.utils.functional.FunctionalImpl.flatMap;
import static fi.solita.utils.functional.FunctionalImpl.forall;
import static fi.solita.utils.functional.FunctionalImpl.grouped;
import static fi.solita.utils.functional.FunctionalImpl.map;
import static fi.solita.utils.functional.Functional.flatMap;
import static fi.solita.utils.functional.Functional.forall;
import static fi.solita.utils.functional.Functional.grouped;
import static fi.solita.utils.functional.Functional.map;
import static fi.solita.utils.query.attributes.AttributeProxy.unwrap;

import java.lang.reflect.AnnotatedElement;
Expand Down Expand Up @@ -157,7 +157,7 @@ public static <T> void checkOrdering(CriteriaQuery<T> query, Page page) throws N

public static final <E extends IEntity> CriteriaQuery<E> applyOrder(CriteriaQuery<E> query, final Path<E> selection, Iterable<? extends fi.solita.utils.query.Order<? super E, ?>> orderings, final CriteriaBuilder cb) {
if (!isEmpty(orderings)) {
query.orderBy(newList(map(orderings, QueryUtils_.<E>order2jpaOrder().ap(cb, selection))));
query.orderBy(newList(map(QueryUtils_.<E>order2jpaOrder().ap(cb, selection), orderings)));
} else {
applyOrder(query, selection, cb);
}
Expand Down Expand Up @@ -189,7 +189,7 @@ else if (vals.size() > 5 && Table.isSupported(vals)) {
return path.in(tableselect);
} else {
// oracle fails if more than 1000 parameters
List<? extends List<?>> groups = newList(grouped(vals, 1000));
List<? extends List<?>> groups = newList(grouped(1000, vals));
List<Predicate> preds = newListOfSize(groups.size());
for (List<?> g: groups) {
preds.add(path.in(g));
Expand Down Expand Up @@ -225,12 +225,12 @@ public static <T> Subquery<T> table(Subquery<T> tableselect, Expression<? extend
}

public static Iterable<Join<?,?>> getAllJoins(From<?, ?> parent) {
return flatMap(parent.getJoins(), new Transformer<Join<?,?>,Iterable<Join<?,?>>>() {
return flatMap(new Transformer<Join<?,?>,Iterable<Join<?,?>>>() {
@Override
public Iterable<Join<?, ?>> transform(Join<?, ?> source) {
return cons(source, getAllJoins(source));
}
});
}, parent.getJoins());
}

public static void checkOptionalAttributes(Attribute<?,?> param) {
Expand All @@ -253,7 +253,7 @@ public static boolean isRequiredByMetamodel(Attribute<?,?> param) {
if (param instanceof OptionalAttribute) {
return false;
} else if (param instanceof JoiningAttribute && param instanceof SingularAttribute) {
return forall(((JoiningAttribute) param).getAttributes(), QueryUtils_.isRequiredByMetamodel);
return forall(QueryUtils_.isRequiredByMetamodel, ((JoiningAttribute) param).getAttributes());
} else if (param instanceof JoiningAttribute) {
return true;
} else if (param instanceof SingularAttribute && ((SingularAttribute<?,?>)param).getPersistentAttributeType() == PersistentAttributeType.EMBEDDED) {
Expand Down Expand Up @@ -286,7 +286,7 @@ public static boolean isRequiredByMetamodel(Attribute<?,?> param) {
MetaJpaConstructor<?,?,?> c = ((AdditionalQueryPerformingAttribute)param).getConstructor();
if (c instanceof TransparentProjection) {
// optionality of a TransparentProjection propagates
ret &= forall(c.getParameters(), QueryUtils_.isRequiredByMetamodel);
ret &= forall(QueryUtils_.isRequiredByMetamodel, c.getParameters());
}
}

Expand All @@ -304,13 +304,13 @@ public static boolean isRequiredByQueryAttribute(Attribute<?,?> param) {
if (param == null || unwrap(PseudoAttribute.class, param).isDefined()) {
return true;
} else if (param instanceof JoiningAttribute) {
return forall(((JoiningAttribute) param).getAttributes(), QueryUtils_.isRequiredByQueryAttribute);
return forall(QueryUtils_.isRequiredByQueryAttribute, ((JoiningAttribute) param).getAttributes());
}

boolean ret = !unwrap(OptionalAttribute.class, param).isDefined();

if (param instanceof AdditionalQueryPerformingAttribute && param instanceof SingularAttribute) {
ret &= forall(((AdditionalQueryPerformingAttribute)param).getConstructor().getParameters(), QueryUtils_.isRequiredByQueryAttribute);
ret &= forall(QueryUtils_.isRequiredByQueryAttribute, ((AdditionalQueryPerformingAttribute)param).getConstructor().getParameters());
}

return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static fi.solita.utils.functional.Collections.newList;
import static fi.solita.utils.functional.Functional.mkString;
import static fi.solita.utils.functional.FunctionalImpl.map;
import static fi.solita.utils.functional.Functional.map;

import java.util.List;

Expand Down Expand Up @@ -109,12 +109,12 @@ ListAttribute<E,Y5> list(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) {


static <A extends Attribute<?,?> & JoiningAttribute> String joiningAttributeToString(A a) {
return a.getClass().getSimpleName() + "(" + mkString("->", map(a.getAttributes(), new Transformer<Attribute<?,?>,String>() {
return a.getClass().getSimpleName() + "(" + mkString("->", map(new Transformer<Attribute<?,?>,String>() {
@Override
public String transform(Attribute<?,?> source) {
return source.getDeclaringType() == null ? "?" : source.getDeclaringType().getJavaType().getSimpleName() + "." + source.getName();
}
})) + ")";
}, a.getAttributes())) + ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static fi.solita.utils.functional.Collections.newArray;
import static fi.solita.utils.functional.Collections.newList;
import static fi.solita.utils.functional.Functional.head;
import static fi.solita.utils.functional.FunctionalImpl.map;
import static fi.solita.utils.functional.Functional.map;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -63,7 +63,7 @@ public <T> List<T> getMany(CriteriaQuery<T> query, Page page) {
q.setMaxResults(page.getMaxResults());
}
try {
return newList(map(q.getResultList(), HibernateQueryExecutor_.<T>replaceProxy()));
return newList(map(HibernateQueryExecutor_.<T>replaceProxy(), q.getResultList()));
} finally {
if (page != Page.NoPaging) {
q.setFirstResult(originalFirstResult);
Expand Down Expand Up @@ -94,7 +94,7 @@ public <T> List<T> getMany(NativeQuery<T> query, Page page) {
q = bindParams(q, query.params);
q = bindReturnValues(q, query.retvals);
q = bindTransformer(q, query);
return newList(map(applyPaging(q, page).list(), HibernateQueryExecutor_.replaceProxy()));
return newList(map(HibernateQueryExecutor_.replaceProxy(), applyPaging(q, page).list()));
}

@SuppressWarnings("unchecked")
Expand All @@ -110,7 +110,7 @@ public <T> Option<T> find(QLQuery<T> query) {
public <T> List<T> getMany(QLQuery<T> query, Page page) {
Query q = em.apply().unwrap(Session.class).createQuery(query.query);
q = bindParams(q, query.params);
return newList(map(applyPaging(q, page).list(), HibernateQueryExecutor_.replaceProxy()));
return newList(map(HibernateQueryExecutor_.replaceProxy(), applyPaging(q, page).list()));
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ private final SQLQuery bindReturnValues(SQLQuery q, List<Pair<String, Option<Typ
}

private static final SQLQuery bindTransformer(SQLQuery q, NativeQuery<?> query) {
String[] retvals = newArray(String.class, map(query.retvals, Transformers.<String>left()));
String[] retvals = newArray(String.class, map(Transformers.<String>left(), query.retvals));
final OptionResultTransformer resultTransformer = new OptionResultTransformer(query.retvals);
if (query instanceof NativeQuery.NativeQuerySingleEntity ||
query instanceof NativeQuery.NativeQueryT1 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import static fi.solita.utils.functional.Collections.newList;
import static fi.solita.utils.functional.Collections.newMap;
import static fi.solita.utils.functional.FunctionalImpl.filter;
import static fi.solita.utils.functional.FunctionalImpl.find;
import static fi.solita.utils.functional.FunctionalImpl.flatMap;
import static fi.solita.utils.functional.FunctionalImpl.groupBy;
import static fi.solita.utils.functional.FunctionalImpl.map;
import static fi.solita.utils.functional.Functional.filter;
import static fi.solita.utils.functional.Functional.find;
import static fi.solita.utils.functional.Functional.flatMap;
import static fi.solita.utils.functional.Functional.groupBy;
import static fi.solita.utils.functional.Functional.map;
import static fi.solita.utils.functional.Option.None;
import static fi.solita.utils.functional.Option.Some;

Expand Down Expand Up @@ -65,11 +65,11 @@ public Long transform(Iterable<?> source) {
private Map<Class<?>,String> typesByUniqueReturnedClass() {
if (typesByUniqueReturnedClassCache == null) {
Iterable<ClassMetadata> allClassMetadata = em.apply().unwrap(Session.class).getSessionFactory().getAllClassMetadata().values();
Map<String, List<org.hibernate.type.Type>> allPropertyTypesByName = groupBy(flatMap(allClassMetadata, HibernateTypeProvider_.classMetadata2propertyTypes), HibernateTypeProvider_.type2Name);
Iterable<org.hibernate.type.Type> allDifferentPropertyTypes = map(allPropertyTypesByName.values(), head);
Iterable<List<org.hibernate.type.Type>> typesByReturnedClass = groupBy(allDifferentPropertyTypes, HibernateTypeProvider_.type2ReturnedClass).values();
Iterable<List<org.hibernate.type.Type>> typesUniqueByReturnedClass = filter(typesByReturnedClass, size.andThen(Predicates.equalTo(1l)));
typesByUniqueReturnedClassCache = newMap(map(typesUniqueByReturnedClass, head.andThen(HibernateTypeProvider_.type2ReturnedClassAndNamePair)));
Map<String, List<org.hibernate.type.Type>> allPropertyTypesByName = groupBy(HibernateTypeProvider_.type2Name, flatMap(HibernateTypeProvider_.classMetadata2propertyTypes, allClassMetadata));
Iterable<org.hibernate.type.Type> allDifferentPropertyTypes = map(head, allPropertyTypesByName.values());
Iterable<List<org.hibernate.type.Type>> typesByReturnedClass = groupBy(HibernateTypeProvider_.type2ReturnedClass, allDifferentPropertyTypes).values();
Iterable<List<org.hibernate.type.Type>> typesUniqueByReturnedClass = filter(size.andThen(Predicates.equalTo(1l)), typesByReturnedClass);
typesByUniqueReturnedClassCache = newMap(map(head.andThen(HibernateTypeProvider_.type2ReturnedClassAndNamePair), typesUniqueByReturnedClass));
}
return typesByUniqueReturnedClassCache;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public <T> Type<T> type(final Class<T> clazz) {
}

// no luck with standard stuff, try searching a unique match from all entity metadata
for (String typeName: find(typesByUniqueReturnedClass(), clazz)) {
for (String typeName: find(clazz, typesByUniqueReturnedClass())) {
org.hibernate.type.Type heuristicType = typeHelper.heuristicType(typeName);
if (heuristicType != null) {
return new HibernateType<T>(heuristicType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessi
throw new UnsupportedOperationException("Shouldn't be here");
}

static final String toString(CharSequence cs) {
public static final String toStr(CharSequence cs) {
return cs.toString();
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public static Option<Pair<String,Apply<Connection,Iterable<Object>>>> getSqlType
if (h.get() instanceof CharSequence) {
t = "SYS.ODCIVARCHAR2LIST";
@SuppressWarnings("unchecked")
Iterable<Object> m = (Iterable<Object>)(Object)map(TableValueType_.toString, (Iterable<CharSequence>)values);
Iterable<Object> m = (Iterable<Object>)(Object)map(TableValueType_.toStr, (Iterable<CharSequence>)values);
v = Function.constant(m);
} else if (h.get() instanceof Number) {
t = "SYS.ODCINUMBERLIST";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Object transformTuple(Object[] tuple, String[] aliases) {
if (aliases.length == 2) {
return Pair.of(m.get(a[0]), m.get(a[1]));
} else {
return Tuple.of(newArray(Object.class, map(a, getter(m))));
return Tuple.of(newArray(Object.class, map(getter(m), a)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public <E extends IEntity & Identifiable<? extends Id<E>> & Removable> void remo
q.multiselect(projectionSupport.prepareProjectingQuery(Project.id(), selection));
List<Object> results = queryExecutor.getMany(q, Page.NoPaging);

Collection<Id<E>> idList = projectionSupport.finalizeProjectingQuery(Project.<E>id(), map(results, ProjectionUtil_.objectToObjectList));
Collection<Id<E>> idList = projectionSupport.finalizeProjectingQuery(Project.<E>id(), map(ProjectionUtil_.objectToObjectList, results));
if (!idList.isEmpty()) {
em.apply().createQuery("delete from " + resolveSelection(query).getJavaType().getName() + " e where e.id in(:idList)").setParameter("idList", idList).executeUpdate();
}
Expand Down Expand Up @@ -151,6 +151,6 @@ private <E extends IEntity & Identifiable<? extends Id<? super E>>, T extends IE
query.where(em.apply().getCriteriaBuilder().equal(root.get(QueryUtils.id(root.getJavaType(), em.apply())), entity.getId()));
query.select(((Join<?,T>)QueryUtils.join((Root<?>)root, (Attribute<?,?>)relation, JoinType.INNER)).get(id));

return map(queryExecutor.getMany(query, Page.NoPaging), JpaBasicQueries_.<T>toProxy().ap(this));
return map(JpaBasicQueries_.<T>toProxy().ap(this), queryExecutor.getMany(query, Page.NoPaging));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static fi.solita.utils.functional.Collections.newList;
import static fi.solita.utils.functional.Functional.head;
import static fi.solita.utils.functional.Functional.headOption;
import static fi.solita.utils.functional.FunctionalImpl.map;
import static fi.solita.utils.functional.Functional.map;
import static fi.solita.utils.functional.Option.None;
import static fi.solita.utils.functional.Option.Some;
import static fi.solita.utils.query.QueryUtils.applyOrder;
Expand Down Expand Up @@ -98,6 +98,6 @@ public <E extends IEntity,R> List<R> getMany(CriteriaQuery<E> query, MetaJpaCons
q.multiselect(projectionSupport.prepareProjectingQuery(constructor, selection));

List<Object> results = queryExecutor.getMany(ordered, page);
return projectionSupport.finalizeProjectingQuery(constructor, map(results, ProjectionUtil_.objectToObjectList));
return projectionSupport.finalizeProjectingQuery(constructor, map(ProjectionUtil_.objectToObjectList, results));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public <T> T get(NativeQuery<T> query) {
}

public <T, P> P get(NativeQuery<T> query, Apply<T, P> constructor) {
return head(map(find(query), constructor));
return head(map(constructor, find(query)));
}

public <T> Option<T> find(NativeQuery<T> query) {
return queryExecutor.find(query);
}

public <T, P> Option<P> find(NativeQuery<T> query, Apply<T, P> constructor) {
return headOption(map(queryExecutor.find(query), constructor));
return headOption(map(constructor, queryExecutor.find(query)));
}

public <T> Option<T> findFirst(NativeQuery<T> query) {
Expand Down Expand Up @@ -82,6 +82,6 @@ public <T> List<T> getMany(NativeQuery<T> query, Page page) {
}

public <T, P> List<P> getMany(NativeQuery<T> query, Page page, Apply<T, P> constructor) {
return newList(map(getMany(query, page), constructor));
return newList(map(constructor, getMany(query, page)));
}
}
6 changes: 3 additions & 3 deletions src/main/java/fi/solita/utils/query/execution/QLQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public <T> T get(QLQuery<T> query) {
}

public <T, P> P get(QLQuery<T> query, Apply<T, P> constructor) {
return head(map(find(query), constructor));
return head(map(constructor, find(query)));
}

public <T> Option<T> find(QLQuery<T> query) {
return queryExecutor.find(query);
}

public <T, P> Option<P> find(QLQuery<T> query, Apply<T, P> constructor) {
return headOption(map(queryExecutor.find(query), constructor));
return headOption(map(constructor, queryExecutor.find(query)));
}

public <T> Option<T> findFirst(QLQuery<T> query) {
Expand Down Expand Up @@ -75,6 +75,6 @@ public <T> List<T> getMany(QLQuery<T> query, Page page) {
}

public <T, P> List<P> getMany(QLQuery<T> query, Page page, Apply<T, P> constructor) {
return newList(map(getMany(query, page), constructor));
return newList(map(constructor, getMany(query, page)));
}
}
Loading

0 comments on commit 290052e

Please sign in to comment.