private static List<Document> createPushes(final Feature feature) { final List<Document> result = feature.getProperties() .map(featureProperties -> createFlatFeaturesRepresentation(featureProperties, feature.getId())) .orElseGet(ArrayList::new); result.add(createDefaultFeatureDoc(feature.getId())); return result; }
/** * Gets the property value which is referred by the given JSON Pointer. * * @param pointer defines the hierarchical path to the property value to be returned. * @return the value of the property which is referred by {@code pointer}. * @throws NullPointerException if {@code pointer} is {@code null}. */ default Optional<JsonValue> getProperty(final CharSequence pointer) { return getProperty(JsonPointer.of(pointer)); }
@Override public JsonObject getEntity(final JsonSchemaVersion schemaVersion) { return feature.toJson(schemaVersion); }
/** * Returns a new {@code ImmutableFeatureFromCopyBuilder} which is initialised with the properties of the given * Feature. * * @param feature an existing Feature which provides the properties of the new Feature. * @return the new builder. * @throws NullPointerException if {@code feature} is {@code null}. */ public static ImmutableFeatureFromCopyBuilder of(final Feature feature) { checkNotNull(feature, "Feature"); final ImmutableFeatureFromCopyBuilder result = new ImmutableFeatureFromCopyBuilder(feature.getId()); result.properties(feature.getProperties().orElse(null)); result.definition(feature.getDefinition().orElse(null)); return result; }
/** * Returns the identifier of the {@code Feature} whose properties were modified. * * @return the identifier. */ public String getFeatureId() { return feature.getId(); }
/** * Enforce size restrictions on the feature and build a new instance that satisfies the thresholds. * * @param feature the feature. * @return The feature with content that satisfies the thresholds. */ public Feature enforceRestrictions(final Feature feature) { final Set<JsonField> exceededProps = calculateThresholdViolations( feature.getId(), feature.getProperties().orElse(FeatureProperties.newBuilder().build())); if (!exceededProps.isEmpty()) { log.warning("Feature <{}> exceeds size restriction for properties: <{}>", feature.getId(), exceededProps.stream() .map(JsonField::getKey) .map(CharSequence::toString) .collect(Collectors.toList())); final FeatureProperties newProps = fixViolations( feature.getId(), exceededProps, feature.getProperties().orElse(FeatureProperties.newBuilder().build())); return feature.setProperties(newProps); } return feature; }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(ThingModifyCommandResponse.JsonFields.JSON_THING_ID, thingId, predicate); jsonObjectBuilder.set(JSON_FEATURE_ID, featureCreated.getId()); jsonObjectBuilder.set(JSON_FEATURE, featureCreated.toJson(schemaVersion, thePredicate), predicate); }
private Result getFeatureDefinition(final Feature feature, final String thingId, final RetrieveFeatureDefinition command, @Nullable final Thing thing) { final String featureId = feature.getId(); final DittoHeaders dittoHeaders = command.getDittoHeaders(); return feature.getDefinition() .map(featureDefinition -> RetrieveFeatureDefinitionResponse.of(thingId, featureId, featureDefinition, dittoHeaders)) .map(response -> ResultFactory.newQueryResult(command, thing, response, this)) .orElseGet(() -> ResultFactory.newErrorResult( ExceptionFactory.featureDefinitionNotFound(thingId, featureId, dittoHeaders))); }
private static Object createComplexFeatureDocument(final Feature feature) { final Document featureDocument = new Document(); final Object propertiesDocument = feature.getProperties() .map(FeaturesUpdateFactory::createComplexPropertiesRepresentations) .orElseGet(Document::new); featureDocument.append(PersistenceConstants.FIELD_PROPERTIES, propertiesDocument); return featureDocument; }
@Override public FromScratch setFeatureProperties(final String featureId, final FeatureProperties featureProperties) { checkNotNull(featureId, "ID of the Feature to set the properties for"); checkNotNull(featureProperties, "FeatureProperties to be set"); invokeOnFeaturesBuilder(fb -> fb.set(fb.get(featureId) .map(feature -> feature.setProperties(featureProperties)) .orElseGet(() -> ThingsModelFactory.newFeature(featureId, featureProperties)))); return this; }
private Result getModifyOrCreateResult(final Feature feature, final Context context, final long nextRevision, final ModifyFeatureDefinition command) { return feature.getDefinition() .map(definition -> getModifyResult(context, nextRevision, command)) .orElseGet(() -> getCreateResult(context, nextRevision, command)); }
/** * Create a {@code Thing} with the counter {@code Feature}. Blocks until Thing has been created. * * @return a handle for the counter. */ public FeatureHandle createThingWithCounter() { final Thing thing = Thing.newBuilder() // .setId(thingId) // .setFeature(Feature.newBuilder() // .properties(JsonObject.newBuilder() // .set(COUNTER_VALUE, 0) // .build()) // .withId(COUNTER) // .build()) // .build(); FeatureHandle featureHandle = null; try { featureHandle = twin.create(thing) // .thenApply(created -> twin.forFeature(thingId, COUNTER)) // .get(TIMEOUT, TimeUnit.SECONDS); LOGGER.info("Thing with ID '{}' created.", thingId); } catch (InterruptedException | ExecutionException | TimeoutException e) { LOGGER.error(e.getMessage()); } return featureHandle; }
/** * Returns the identifier of the {@code Feature} whose properties were modified. * * @return the identifier. */ public String getFeatureId() { return feature.getId(); }
/** * Enforce size restrictions on the feature and build a new instance that satisfies the thresholds. * * @param feature the feature. * @return The feature with content that satisfies the thresholds. */ public Feature enforceRestrictions(final Feature feature) { final Set<JsonField> exceededProps = calculateThresholdViolations( feature.getId(), feature.getProperties().orElse(FeatureProperties.newBuilder().build())); if (!exceededProps.isEmpty()) { log.warning("Feature <{}> exceeds size restriction for properties: <{}>", feature.getId(), exceededProps.stream() .map(JsonField::getKey) .map(CharSequence::toString) .collect(Collectors.toList())); final FeatureProperties newProps = fixViolations( feature.getId(), exceededProps, feature.getProperties().orElse(FeatureProperties.newBuilder().build())); return feature.setProperties(newProps); } return feature; }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(ThingModifyCommandResponse.JsonFields.JSON_THING_ID, thingId, predicate); jsonObjectBuilder.set(JSON_FEATURE_ID, featureCreated.getId()); jsonObjectBuilder.set(JSON_FEATURE, featureCreated.toJson(schemaVersion, thePredicate), predicate); }
private Result getFeatureDefinition(final Feature feature, final String thingId, final RetrieveFeatureDefinition command, @Nullable final Thing thing) { final String featureId = feature.getId(); final DittoHeaders dittoHeaders = command.getDittoHeaders(); return feature.getDefinition() .map(featureDefinition -> RetrieveFeatureDefinitionResponse.of(thingId, featureId, featureDefinition, dittoHeaders)) .map(response -> ResultFactory.newQueryResult(command, thing, response, this)) .orElseGet(() -> ResultFactory.newErrorResult( ExceptionFactory.featureDefinitionNotFound(thingId, featureId, dittoHeaders))); }
/** * Returns a new {@code ImmutableFeatureFromCopyBuilder} which is initialised with the properties of the given * Feature. * * @param feature an existing Feature which provides the properties of the new Feature. * @return the new builder. * @throws NullPointerException if {@code feature} is {@code null}. */ public static ImmutableFeatureFromCopyBuilder of(final Feature feature) { checkNotNull(feature, "Feature"); final ImmutableFeatureFromCopyBuilder result = new ImmutableFeatureFromCopyBuilder(feature.getId()); result.properties(feature.getProperties().orElse(null)); result.definition(feature.getDefinition().orElse(null)); return result; }
private static Object createComplexFeatureDocument(final Feature feature) { final Document featureDocument = new Document(); final Object propertiesDocument = feature.getProperties() .map(FeaturesUpdateFactory::createComplexPropertiesRepresentations) .orElseGet(Document::new); featureDocument.append(PersistenceConstants.FIELD_PROPERTIES, propertiesDocument); return featureDocument; }
@Override public FromScratch setFeatureProperties(final String featureId, final FeatureProperties featureProperties) { checkNotNull(featureId, "ID of the Feature to set the properties for"); checkNotNull(featureProperties, "FeatureProperties to be set"); invokeOnFeaturesBuilder(fb -> fb.set(fb.get(featureId) .map(feature -> feature.setProperties(featureProperties)) .orElseGet(() -> ThingsModelFactory.newFeature(featureId, featureProperties)))); return this; }
private Result getModifyOrCreateResult(final Feature feature, final Context context, final long nextRevision, final ModifyFeatureDefinition command) { return feature.getDefinition() .map(definition -> getModifyResult(context, nextRevision, command)) .orElseGet(() -> getCreateResult(context, nextRevision, command)); }