001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.CoreControl;
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.echothree.util.server.persistence.Session;
026import com.google.common.eventbus.Subscribe;
027
028@SentEventSubscriber
029public class AppearanceModificationSubscriber
030        extends BaseEventSubscriber {
031
032    @Subscribe
033    public void receiveSentEvent(SentEvent se) {
034        decodeEventAndApply(se, touchEntityInstanceIfAppearance);
035    }
036
037    private static final Function5Arity<Event, EntityInstance, EventTypes, String, String>
038            touchEntityInstanceIfAppearance = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
039        if(AppearanceConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
040                && AppearanceConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
041                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
042            var coreControl = Session.getModelController(CoreControl.class);
043            var appearance = coreControl.getAppearanceByEntityInstance(entityInstance);
044            var entityAppearances = coreControl.getEntityAppearancesByAppearance(appearance);
045            var createdBy = PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy());
046
047            for(var entityAppearance : entityAppearances) {
048                coreControl.sendEvent(entityAppearance.getEntityInstance(), EventTypes.TOUCH,
049                        entityInstance, eventType,
050                        createdBy);
051            }
052        }
053    };
054
055}