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.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.CoreControl;
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.ContentCategoryConstants;
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.echothree.util.server.persistence.Session;
031import com.google.common.eventbus.Subscribe;
032
033@SentEventSubscriber
034public class ContentCategoryModificationSubscriber
035        extends BaseEventSubscriber {
036
037    @Subscribe
038    public void receiveSentEvent(SentEvent se) {
039        decodeEventAndApply(se, touchContentCatalogItemsIfContentCategory);
040    }
041
042    private static final Function5Arity<Event, EntityInstance, EventTypes, String, String>
043            touchContentCatalogItemsIfContentCategory = (event, entityInstance, eventType, componentVendorName, entityTypeName) -> {
044        if(ContentCategoryConstants.COMPONENT_VENDOR_NAME.equals(componentVendorName)
045                && ContentCategoryConstants.ENTITY_TYPE_NAME.equals(entityTypeName)
046                && (eventType == EventTypes.MODIFY || eventType == EventTypes.TOUCH)) {
047            var coreControl = Session.getModelController(CoreControl.class);
048            var contentControl = Session.getModelController(ContentControl.class);
049            var contentCategory = contentControl.getContentCategoryByEntityInstance(entityInstance);
050            var contentCategoryItems = contentControl.getContentCategoryItemsByContentCategory(contentCategory);
051
052            for(var contentCategoryItem : contentCategoryItems) {
053                var contentCatalogItem = contentCategoryItem.getContentCatalogItem();
054
055                coreControl.sendEvent(contentCatalogItem.getPrimaryKey(), EventTypes.TOUCH,
056                        contentCategory.getPrimaryKey(), eventType,
057                        PersistenceUtils.getInstance().getBasePKFromEntityInstance(event.getCreatedBy()));
058            }
059        }
060    };
061
062}