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