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.util.server.control;
018
019import com.echothree.model.control.core.common.EventTypes;
020import com.echothree.model.control.core.server.control.EntityInstanceControl;
021import com.echothree.model.control.core.server.control.EventControl;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.workflow.server.control.WorkflowControl;
024import com.echothree.model.data.core.server.entity.EntityInstance;
025import com.echothree.model.data.core.server.entity.Event;
026import com.echothree.util.common.persistence.BasePK;
027import com.echothree.util.server.persistence.BaseEntity;
028import com.echothree.util.server.persistence.Session;
029import com.echothree.util.server.persistence.ThreadSession;
030import java.sql.Connection;
031import javax.inject.Inject;
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034
035public abstract class BaseModelControl {
036    
037    protected Session session;
038    protected Connection connection;
039    private Log log;
040
041    @Inject
042    protected EntityInstanceControl entityInstanceControl;
043
044    @Inject
045    protected EventControl eventControl;
046
047    @Inject
048    protected PartyControl partyControl;
049
050    @Inject
051    protected WorkflowControl workflowControl;
052    
053    /** Creates a new instance of BaseModelControl */
054    protected BaseModelControl() {
055        this.session = ThreadSession.currentSession();
056        this.connection = session.getConnection();
057    }
058    
059    public Session getSession() {
060        return session;
061    }
062    
063    public Connection getConnection() {
064        return connection;
065    }
066    
067    protected Log getLog() {
068        if(log == null) {
069            log = LogFactory.getLog(this.getClass());
070        }
071        
072        return log;
073    }
074    
075    // --------------------------------------------------------------------------------
076    //   Utilities
077    // --------------------------------------------------------------------------------
078
079    protected EntityInstance getEntityInstanceByBasePK(final BasePK pk) {
080        return entityInstanceControl.getEntityInstanceByBasePK(pk);
081    }
082    
083    protected EntityInstance getEntityInstanceByBaseEntity(final BaseEntity baseEntity) {
084        return getEntityInstanceByBasePK(baseEntity.getPrimaryKey());
085    }
086
087    protected Event sendEvent(final BasePK basePK, final EventTypes eventType, final BasePK relatedBasePK,
088            final EventTypes relatedEventType, final BasePK createdByBasePK) {
089        return eventControl.sendEvent(basePK, eventType, relatedBasePK, relatedEventType, createdByBasePK);
090    }
091    
092    protected Event sendEvent(final EntityInstance entityInstance, final EventTypes eventType, final BasePK relatedBasePK,
093            final EventTypes relatedEventType, final BasePK createdByBasePK) {
094        return eventControl.sendEvent(entityInstance, eventType, relatedBasePK, relatedEventType, createdByBasePK);
095    }
096    
097    public Event sendEvent(final EntityInstance entityInstance, final EventTypes eventType, final EntityInstance relatedEntityInstance,
098            final EventTypes relatedEventType, final BasePK createdByBasePK) {
099        return eventControl.sendEvent(entityInstance, eventType, relatedEntityInstance, relatedEventType, createdByBasePK);
100    }
101    
102}