|
| 1 | +/* |
| 2 | + * Copyright 2016, TeamDev Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and/or binary forms, with or without |
| 5 | + * modification, must retain the above copyright notice and the following |
| 6 | + * disclaimer. |
| 7 | + * |
| 8 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 9 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 10 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 11 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 12 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 13 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 14 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 15 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 16 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 17 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 18 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 19 | + */ |
| 20 | +package org.spine3.base; |
| 21 | + |
| 22 | +import com.google.protobuf.Any; |
| 23 | +import com.google.protobuf.FieldMask; |
| 24 | +import com.google.protobuf.Message; |
| 25 | +import org.spine3.client.EntityFilters; |
| 26 | +import org.spine3.client.EntityId; |
| 27 | +import org.spine3.client.EntityIdFilter; |
| 28 | +import org.spine3.client.Query; |
| 29 | +import org.spine3.client.Target; |
| 30 | +import org.spine3.protobuf.AnyPacker; |
| 31 | +import org.spine3.protobuf.KnownTypes; |
| 32 | +import org.spine3.protobuf.TypeUrl; |
| 33 | + |
| 34 | +import javax.annotation.Nullable; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Set; |
| 37 | + |
| 38 | +import static org.spine3.base.Queries.Targets.allOf; |
| 39 | +import static org.spine3.base.Queries.Targets.someOf; |
| 40 | + |
| 41 | +/** |
| 42 | + * Client-side utilities for working with queries. |
| 43 | + * |
| 44 | + * @author Alex Tymchenko |
| 45 | + * @author Dmytro Dashenkov |
| 46 | + */ |
| 47 | +public class Queries { |
| 48 | + |
| 49 | + private Queries() { |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a {@link Query} to read certain entity states by IDs with the {@link FieldMask} |
| 54 | + * applied to each of the results. |
| 55 | + * |
| 56 | + * <p>Allows to specify a set of identifiers to be used during the {@code Query} processing. The processing |
| 57 | + * results will contain only the entities, which IDs are present among the {@code ids}. |
| 58 | + * |
| 59 | + * <p>Allows to set property paths for a {@link FieldMask}, applied to each of the query results. |
| 60 | + * This processing is performed according to the |
| 61 | + * <a href="https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.FieldMask>FieldMask specs</a>. |
| 62 | + * |
| 63 | + * <p>In case the {@code paths} array contains entries inapplicable to the resulting entity |
| 64 | + * (e.g. a {@code path} references a missing field), such invalid paths are silently ignored. |
| 65 | + * |
| 66 | + * @param entityClass the class of a target entity |
| 67 | + * @param ids the entity IDs of interest |
| 68 | + * @param paths the property paths for the {@code FieldMask} applied to each of results |
| 69 | + * @return an instance of {@code Query} formed according to the passed parameters |
| 70 | + */ |
| 71 | + public static Query readByIds(Class<? extends Message> entityClass, Set<? extends Message> ids, String... paths) { |
| 72 | + final FieldMask fieldMask = FieldMask.newBuilder() |
| 73 | + .addAllPaths(Arrays.asList(paths)) |
| 74 | + .build(); |
| 75 | + final Query result = composeQuery(entityClass, ids, fieldMask); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a {@link Query} to read all entity states with the {@link FieldMask} |
| 81 | + * applied to each of the results. |
| 82 | + * |
| 83 | + * <p>Allows to set property paths for a {@link FieldMask}, applied to each of the query results. |
| 84 | + * This processing is performed according to the |
| 85 | + * <a href="https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.FieldMask>FieldMask specs</a>. |
| 86 | + * |
| 87 | + * <p>In case the {@code paths} array contains entries inapplicable to the resulting entity |
| 88 | + * (e.g. a {@code path} references a missing field), such invalid paths are silently ignored. |
| 89 | + * |
| 90 | + * @param entityClass the class of a target entity |
| 91 | + * @param paths the property paths for the {@code FieldMask} applied to each of results |
| 92 | + * @return an instance of {@code Query} formed according to the passed parameters |
| 93 | + */ |
| 94 | + public static Query readAll(Class<? extends Message> entityClass, String... paths) { |
| 95 | + final FieldMask fieldMask = FieldMask.newBuilder() |
| 96 | + .addAllPaths(Arrays.asList(paths)) |
| 97 | + .build(); |
| 98 | + final Query result = composeQuery(entityClass, null, fieldMask); |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Create a {@link Query} to read certain entity states by IDs. |
| 104 | + * |
| 105 | + * <p>Allows to specify a set of identifiers to be used during the {@code Query} processing. The processing |
| 106 | + * results will contain only the entities, which IDs are present among the {@code ids}. |
| 107 | + * |
| 108 | + * <p>Unlike {@link Queries#readByIds(Class, Set, String...)}, the {@code Query} processing will not change |
| 109 | + * the resulting entities. |
| 110 | + * |
| 111 | + * @param entityClass the class of a target entity |
| 112 | + * @param ids the entity IDs of interest |
| 113 | + * @return an instance of {@code Query} formed according to the passed parameters |
| 114 | + */ |
| 115 | + public static Query readByIds(Class<? extends Message> entityClass, Set<? extends Message> ids) { |
| 116 | + return composeQuery(entityClass, ids, null); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Create a {@link Query} to read all states of a certain entity. |
| 121 | + * |
| 122 | + * <p>Unlike {@link Queries#readAll(Class, String...)}, the {@code Query} processing will not change |
| 123 | + * the resulting entities. |
| 124 | + * |
| 125 | + * @param entityClass the class of a target entity |
| 126 | + * @return an instance of {@code Query} formed according to the passed parameters |
| 127 | + */ |
| 128 | + public static Query readAll(Class<? extends Message> entityClass) { |
| 129 | + return composeQuery(entityClass, null, null); |
| 130 | + } |
| 131 | + |
| 132 | + private static Query composeQuery(Class<? extends Message> entityClass, @Nullable Set<? extends Message> ids, @Nullable FieldMask fieldMask) { |
| 133 | + final Target target = ids == null ? allOf(entityClass) : someOf(entityClass, ids); |
| 134 | + final Query.Builder queryBuilder = Query.newBuilder() |
| 135 | + .setTarget(target); |
| 136 | + if (fieldMask != null) { |
| 137 | + queryBuilder.setFieldMask(fieldMask); |
| 138 | + } |
| 139 | + final Query result = queryBuilder |
| 140 | + .build(); |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Extract the type of {@link Target} for the given {@link Query}. |
| 146 | + * |
| 147 | + * <p>Returns null if the {@code Target} type is unknown to the application. |
| 148 | + * |
| 149 | + * @param query the query of interest. |
| 150 | + * @return the type of the {@code Query#getTarget()} or null, if the type is unknown. |
| 151 | + */ |
| 152 | + @Nullable |
| 153 | + public static TypeUrl typeOf(Query query) { |
| 154 | + final Target target = query.getTarget(); |
| 155 | + final String typeAsString = target.getType(); |
| 156 | + final TypeUrl type = KnownTypes.getTypeUrl(typeAsString); |
| 157 | + return type; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Client-side utilities for working with {@link Query} and {@link org.spine3.client.Subscription} targets. |
| 162 | + * |
| 163 | + * @author Alex Tymchenko |
| 164 | + * @author Dmytro Dashenkov |
| 165 | + */ |
| 166 | + public static class Targets { |
| 167 | + |
| 168 | + private Targets() { |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Create a {@link Target} for a subset of the entity states by specifying their IDs. |
| 173 | + * |
| 174 | + * @param entityClass the class of a target entity |
| 175 | + * @param ids the IDs of interest |
| 176 | + * @return the instance of {@code Target} assembled according to the parameters. |
| 177 | + */ |
| 178 | + public static Target someOf(Class<? extends Message> entityClass, Set<? extends Message> ids) { |
| 179 | + final Target result = composeTarget(entityClass, ids); |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Create a {@link Target} for all of the specified entity states. |
| 185 | + * |
| 186 | + * @param entityClass the class of a target entity |
| 187 | + * @return the instance of {@code Target} assembled according to the parameters. |
| 188 | + */ |
| 189 | + public static Target allOf(Class<? extends Message> entityClass) { |
| 190 | + final Target result = composeTarget(entityClass, null); |
| 191 | + return result; |
| 192 | + } |
| 193 | + |
| 194 | + /* package */ |
| 195 | + static Target composeTarget(Class<? extends Message> entityClass, @Nullable Set<? extends Message> ids) { |
| 196 | + final TypeUrl type = TypeUrl.of(entityClass); |
| 197 | + |
| 198 | + final boolean includeAll = ids == null; |
| 199 | + |
| 200 | + final EntityIdFilter.Builder idFilterBuilder = EntityIdFilter.newBuilder(); |
| 201 | + |
| 202 | + if (!includeAll) { |
| 203 | + for (Message rawId : ids) { |
| 204 | + final Any packedId = AnyPacker.pack(rawId); |
| 205 | + final EntityId entityId = EntityId.newBuilder() |
| 206 | + .setId(packedId) |
| 207 | + .build(); |
| 208 | + idFilterBuilder.addIds(entityId); |
| 209 | + } |
| 210 | + } |
| 211 | + final EntityIdFilter idFilter = idFilterBuilder.build(); |
| 212 | + final EntityFilters filters = EntityFilters.newBuilder() |
| 213 | + .setIdFilter(idFilter) |
| 214 | + .build(); |
| 215 | + final Target.Builder builder = Target.newBuilder() |
| 216 | + .setType(type.getTypeName()); |
| 217 | + if (includeAll) { |
| 218 | + builder.setIncludeAll(true); |
| 219 | + } else { |
| 220 | + builder.setFilters(filters); |
| 221 | + } |
| 222 | + |
| 223 | + return builder.build(); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments