Search in sources :

Example 1 with AttributeMap

use of org.openremote.model.attribute.AttributeMap in project openremote by openremote.

the class AssetStorageService method publishModificationEvents.

protected void publishModificationEvents(PersistenceEvent<Asset<?>> persistenceEvent) {
    Asset<?> asset = persistenceEvent.getEntity();
    switch(persistenceEvent.getCause()) {
        case CREATE:
            // Fully load the asset
            Asset<?> loadedAsset = find(new AssetQuery().ids(asset.getId()));
            if (loadedAsset == null) {
                return;
            }
            if (LOG.isLoggable(Level.FINER)) {
                LOG.finer("Asset created: " + loadedAsset.toStringAll());
            } else {
                LOG.fine("Asset created: " + loadedAsset);
            }
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.CREATE, loadedAsset, null));
            // ));
            break;
        case UPDATE:
            String[] updatedProperties = persistenceEvent.getPropertyNames();
            boolean attributesChanged = Arrays.asList(updatedProperties).contains("attributes");
            // String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {
            // Object oldValue = persistenceEvent.getPreviousState(propertyName);
            // Object newValue = persistenceEvent.getCurrentState(propertyName);
            // return !Objects.deepEquals(oldValue, newValue);
            // }).toArray(String[]::new);
            // Fully load the asset
            loadedAsset = find(new AssetQuery().ids(asset.getId()));
            if (loadedAsset == null) {
                return;
            }
            LOG.finer("Asset updated: " + persistenceEvent);
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, updatedProperties));
            // Did any attributes change if so raise attribute events on the event bus
            if (attributesChanged) {
                AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");
                AttributeMap newAttributes = persistenceEvent.getCurrentState("attributes");
                // Get removed attributes and raise an attribute event with deleted flag in attribute state
                oldAttributes.stream().filter(oldAttribute -> newAttributes.stream().noneMatch(newAttribute -> oldAttribute.getName().equals(newAttribute.getName()))).forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
                // Get new or modified attributes
                getAddedOrModifiedAttributes(oldAttributes.values(), newAttributes.values()).forEach(newOrModifiedAttribute -> publishAttributeEvent(asset, newOrModifiedAttribute));
            }
            break;
        case DELETE:
            if (LOG.isLoggable(Level.FINER)) {
                LOG.finer("Asset deleted: " + asset.toStringAll());
            } else {
                LOG.fine("Asset deleted: " + asset);
            }
            clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.DELETE, asset, null));
            // Raise attribute event with deleted flag for each attribute
            AttributeMap deletedAttributes = asset.getAttributes();
            deletedAttributes.forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
            break;
    }
}
Also used : ValuePredicate.asPredicateOrTrue(org.openremote.model.query.filter.ValuePredicate.asPredicateOrTrue) ClientRole(org.openremote.model.security.ClientRole) AuthContext(org.openremote.container.security.AuthContext) NoResultException(javax.persistence.NoResultException) ValueUtil(org.openremote.model.util.ValueUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeEvent(org.openremote.model.attribute.AttributeEvent) GroupAsset(org.openremote.model.asset.impl.GroupAsset) TextUtil(org.openremote.model.util.TextUtil) ConstraintViolation(javax.validation.ConstraintViolation) LogicGroup(org.openremote.model.query.LogicGroup) Attribute.getAddedOrModifiedAttributes(org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes) Predicate(java.util.function.Predicate) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ACCESS_RESTRICTED_READ(org.openremote.model.value.MetaItemType.ACCESS_RESTRICTED_READ) Query(javax.persistence.Query) StringArrayType(com.vladmihalcea.hibernate.type.array.StringArrayType) org.openremote.model(org.openremote.model) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) AssetInfo(org.openremote.model.event.shared.AssetInfo) EventSubscription(org.openremote.model.event.shared.EventSubscription) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) SharedEvent(org.openremote.model.event.shared.SharedEvent) GatewayService(org.openremote.manager.gateway.GatewayService) java.sql(java.sql) IntStream(java.util.stream.IntStream) AttributeMap(org.openremote.model.attribute.AttributeMap) EventSubscriptionAuthorizer(org.openremote.manager.event.EventSubscriptionAuthorizer) java.util(java.util) ACCESS_PUBLIC_READ(org.openremote.model.value.MetaItemType.ACCESS_PUBLIC_READ) CLIENT_EVENT_TOPIC(org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Session(org.hibernate.Session) TypedQuery(javax.persistence.TypedQuery) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) PGobject(org.postgresql.util.PGobject) Attribute(org.openremote.model.attribute.Attribute) PersistenceService(org.openremote.container.persistence.PersistenceService) org.openremote.model.query.filter(org.openremote.model.query.filter) BiConsumer(java.util.function.BiConsumer) ManagerWebService(org.openremote.manager.web.ManagerWebService) org.openremote.model.asset(org.openremote.model.asset) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) Access(org.openremote.model.query.AssetQuery.Access) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) EventRequestResponseWrapper(org.openremote.model.event.shared.EventRequestResponseWrapper) ClientEventService(org.openremote.manager.event.ClientEventService) ConstraintViolationException(javax.validation.ConstraintViolationException) TimerService(org.openremote.container.timer.TimerService) ConsoleResourceImpl(org.openremote.manager.asset.console.ConsoleResourceImpl) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) AttributeMap(org.openremote.model.attribute.AttributeMap) AssetQuery(org.openremote.model.query.AssetQuery)

Aggregations

StringArrayType (com.vladmihalcea.hibernate.type.array.StringArrayType)1 java.sql (java.sql)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 IntStream (java.util.stream.IntStream)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 PredicateBuilder.or (org.apache.camel.builder.PredicateBuilder.or)1