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.content.server.eventbus;
018
019import com.echothree.model.control.content.server.control.ContentControl;
020import com.echothree.model.control.core.common.EventTypes;
021import com.echothree.model.control.core.server.control.EventControl;
022import com.echothree.model.control.core.server.eventbus.BaseEventSubscriber;
023import com.echothree.model.control.core.server.eventbus.Function5Arity;
024import com.echothree.model.control.core.server.eventbus.SentEvent;
025import com.echothree.model.control.core.server.eventbus.SentEventSubscriber;
026import com.echothree.model.data.content.common.ContentCatalogConstants;
027import com.echothree.model.data.core.server.entity.EntityInstance;
028import com.echothree.model.data.core.server.entity.Event;
029import com.echothree.util.server.persistence.PersistenceUtils;
030import com.google.common.eventbus.Subscribe;
031import javax.inject.Inject;
032
033@SentEventSubscriber
034public class ContentCategoryModificationSubscriber
035        extends BaseEventSubscriber {
036
037    @Inject
038    ContentControl contentControl;
039
040    @Inject
041    EventControl eventControl;
042
043    @Subscribe
044    public void receiveSentEvent(SentEvent se) {
045        decodeEventAndApply(se, touchContentCategoriesIfContentCatalog);
046    }
047
048    private final Function5Arity<Event, EntityInstance, EventTypes, String, String>
049            touchContentCategoriesIfContentCatalog = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
050        if(ContentCatalogConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
051                && ContentCatalogConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
052                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
053            var contentCatalog = contentControl.getContentCatalogByEntityInstance(entityInstance);
054            var contentCategories = contentControl.getContentCategories(contentCatalog);
055
056            for(var contentCategory : contentCategories) {
057                eventControl.sendEvent(contentCategory.getPrimaryKey(), EventTypes.TOUCH,
058                        contentCatalog.getPrimaryKey(), eventType,
059                        PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy()));
060            }
061        }
062    };
063
064}