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.EventTypes;
020import com.echothree.model.control.core.server.control.AppearanceControl;
021import com.echothree.model.data.core.common.AppearanceConstants;
022import com.echothree.model.data.core.server.entity.EntityInstance;
023import com.echothree.model.data.core.server.entity.Event;
024import com.echothree.util.server.persistence.PersistenceUtils;
025import com.google.common.eventbus.Subscribe;
026import javax.inject.Inject;
027
028@SentEventSubscriber
029public class AppearanceModificationSubscriber
030        extends BaseEventSubscriber {
031
032    @Inject
033    AppearanceControl appearanceControl;
034
035    @Subscribe
036    public void receiveSentEvent(SentEvent se) {
037        decodeEventAndApply(se, touchEntityInstancesIfAppearance);
038    }
039
040    private final Function5Arity<Event, EntityInstance, EventTypes, String, String>
041            touchEntityInstancesIfAppearance = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
042        if(AppearanceConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
043                && AppearanceConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
044                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
045            var appearance = appearanceControl.getAppearanceByEntityInstance(entityInstance);
046            var entityAppearances = appearanceControl.getEntityAppearancesByAppearance(appearance);
047            var createdBy = PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy());
048
049            for(var entityAppearance : entityAppearances) {
050                appearanceControl.sendEvent(entityAppearance.getEntityInstance(), EventTypes.TOUCH,
051                        entityInstance, eventType,
052                        createdBy);
053            }
054        }
055    };
056
057}