001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.core.server.eventbus;
018
019import com.echothree.model.control.core.common.EntityAttributeTypes;
020import com.echothree.model.control.core.common.EventTypes;
021import com.echothree.model.control.core.server.control.CoreControl;
022import com.echothree.model.control.core.server.control.EventControl;
023import com.echothree.model.data.core.common.EntityAttributeConstants;
024import com.echothree.model.data.core.server.entity.EntityInstance;
025import com.echothree.model.data.core.server.entity.Event;
026import com.echothree.util.server.persistence.PersistenceUtils;
027import com.google.common.eventbus.Subscribe;
028import javax.inject.Inject;
029
030@SentEventSubscriber
031public class EntityAttributeModificationSubscriber
032        extends BaseEventSubscriber {
033
034    @Inject
035    CoreControl coreControl;
036
037    @Inject
038    EventControl eventControl;
039
040    @Subscribe
041    public void receiveSentEvent(SentEvent se) {
042        decodeEventAndApply(se, touchEntityListItemsIfEntityAttribute);
043    }
044
045    private final Function5Arity<Event, EntityInstance, EventTypes, String, String>
046            touchEntityListItemsIfEntityAttribute = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
047        if(EntityAttributeConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
048                && EntityAttributeConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
049                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
050            var entityAttribute = coreControl.getEntityAttributeByEntityInstance(entityInstance);
051            var entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
052            var entityAttributeType = EntityAttributeTypes.valueOf(entityAttributeTypeName);
053
054            switch(entityAttributeType) {
055                // Only these two types can have associated Entity List Items
056                case LISTITEM, MULTIPLELISTITEM -> {
057                    var entityListItems = coreControl.getEntityListItems(entityAttribute);
058                    var createdBy = PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy());
059
060                    for(var entityListItem : entityListItems) {
061                        eventControl.sendEvent(entityListItem.getPrimaryKey(), EventTypes.TOUCH,
062                                entityAttribute.getPrimaryKey(), eventType,
063                                createdBy);
064                    }
065                }
066                default -> {}
067            }
068        }
069    };
070
071}