All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flowcentraltech.flowcentral.messaging.os.business.AbstractOSMessagingProcessor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 FlowCentral Technologies Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.flowcentraltech.flowcentral.messaging.os.business;

import com.flowcentraltech.flowcentral.common.AbstractFlowCentralComponent;
import com.flowcentraltech.flowcentral.messaging.os.data.BaseOSMessagingReq;
import com.flowcentraltech.flowcentral.messaging.os.data.BaseOSMessagingResp;
import com.flowcentraltech.flowcentral.messaging.os.data.OSMessagingError;
import com.flowcentraltech.flowcentral.messaging.os.data.OSMessagingResponseConstants;
import com.tcdng.unify.core.UnifyException;
import com.tcdng.unify.core.constant.LocaleType;
import com.tcdng.unify.core.util.ReflectUtils;

/**
 * Convenient abstract base class for OS messaging processors.
 * 
 * @author FlowCentral Technologies Limited
 * @since 1.0
 */
public abstract class AbstractOSMessagingProcessor
        extends AbstractFlowCentralComponent implements OSMessagingProcessor {

    protected static final OSMessagingError NO_ERROR = new OSMessagingError();

    private final Class responseClass;

    private final Class requestClass;
    
    public AbstractOSMessagingProcessor(Class responseClass, Class requestClass) {
        this.responseClass = responseClass;
        this.requestClass = requestClass;
    }

    @Override
    public final Class getRequestClass() {
        return requestClass;
    }

    @Override
    public final T process(U request) throws UnifyException {
        OSMessagingError error = null;
        try {
            error = validateRequest(request);
            if (error == null || !error.isErrorPresent()) {
                return doProcess(request);
            }
        } catch (Exception e) {
            error = new OSMessagingError(OSMessagingResponseConstants.PROCESSING_EXCEPTION,
                    getExceptionMessage(LocaleType.APPLICATION, e));
        }

        T resp = ReflectUtils.newInstance(responseClass);
        resp.setResponseCode(error.getErrorCode());
        resp.setResponseMessage(error.getErrorMessage());
        return resp;
    }

    @Override
    protected void onInitialize() throws UnifyException {

    }

    @Override
    protected void onTerminate() throws UnifyException {

    }

    protected abstract OSMessagingError validateRequest(U request) throws UnifyException;

    protected abstract T doProcess(U request) throws UnifyException;
}