001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.echothree.util.server.persistence.Session;
028import com.google.common.eventbus.Subscribe;
029
030@SentEventSubscriber
031public class EntityAttributeModificationSubscriber
032        extends BaseEventSubscriber {
033
034    @Subscribe
035    public void receiveSentEvent(SentEvent se) {
036        decodeEventAndApply(se, touchEntityListItemsIfEntityAttribute);
037    }
038
039    private static final Function5Arity<Event, EntityInstance, EventTypes, String, String>
040            touchEntityListItemsIfEntityAttribute = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
041        if(EntityAttributeConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
042                && EntityAttributeConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
043                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
044            var coreControl = Session.getModelController(CoreControl.class);
045            var eventControl = Session.getModelController(EventControl.class);
046            var entityAttribute = coreControl.getEntityAttributeByEntityInstance(entityInstance);
047            var entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
048            var entityAttributeType = EntityAttributeTypes.valueOf(entityAttributeTypeName);
049
050            switch(entityAttributeType) {
051                // Only these two types can have associated Entity List Items
052                case LISTITEM, MULTIPLELISTITEM -> {
053                    var entityListItems = coreControl.getEntityListItems(entityAttribute);
054                    var createdBy = PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy());
055
056                    for(var entityListItem : entityListItems) {
057                        eventControl.sendEvent(entityListItem.getPrimaryKey(), EventTypes.TOUCH,
058                                entityAttribute.getPrimaryKey(), eventType,
059                                createdBy);
060                    }
061                }
062                default -> {}
063            }
064        }
065    };
066
067}