/**
 * Copyright (c) 2009-2012 EBM WebSourcing, 2012-2024 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.quartz.utils;

import org.ow2.petals.component.framework.jbidescriptor.generated.Consumes;

import com.ebmwebsourcing.easycommons.lang.StringHelper;
import com.ebmwebsourcing.easycommons.uuid.SimpleUUIDGenerator;

/**
 * @author Adrien LOUIS - EBM WebSourcing
 * @author Vincent Zurczak - EBM WebSourcing
 */
public class QuartzUtils {

    private static final SimpleUUIDGenerator UUID_GENERATOR = new SimpleUUIDGenerator();

    private QuartzUtils() {
        // No constructor because it's an utility class
    }

    /**
     * @param consume
     *            a consume section
     * @return a job name to invoke the target service
     */
    public static String generateJobName(final Consumes consume) {

        if (consume.getOperation() == null) {
            return String.format("JOB_%s/%s/%s", consume.getInterfaceName().toString(),
                    consume.getServiceName().toString(),
                    consume.getEndpointName() == null ? "autogenerated" : consume.getEndpointName());
        } else {
            return String.format("JOB_%s/%s/%s/%s", consume.getInterfaceName().toString(),
                    consume.getServiceName().toString(),
                    consume.getEndpointName() == null ? "autogenerated" : consume.getEndpointName(),
                    consume.getOperation().toString());
        }
    }

    /**
     * @param consume
     *            a consume section
     * @return a trigger name for the target service
     */
    public static String generateTriggerName(final Consumes consume) {

        if (consume.getOperation() == null) {
            return String.format("TRIGGER_%s/%s/%s", consume.getInterfaceName().toString(),
                    consume.getServiceName().toString(),
                    consume.getEndpointName() == null ? "autogenerated" : consume.getEndpointName());
        } else {
            return String.format("TRIGGER_%s/%s/%s/%s", consume.getInterfaceName().toString(),
                    consume.getServiceName().toString(),
                    consume.getEndpointName() == null ? "autogenerated" : consume.getEndpointName(),
                    consume.getOperation().toString());
        }
    }

    /**
     * Builds the identifier of the consumed service.
     * <p>
     * This method is mainly intended for a logging usage.
     * </p>
     * 
     * @param consume
     *            a consumes section
     * @return the service
     */
    public static String buildTargetServiceIdentifier(final Consumes consume) {

        String itfName = consume.getInterfaceName() != null ? consume.getInterfaceName().getLocalPart() : null;
        if (StringHelper.isNullOrEmpty(itfName))
            itfName = "unknown";

        String srvName = consume.getServiceName() != null ? consume.getServiceName().getLocalPart() : null;
        if (StringHelper.isNullOrEmpty(srvName))
            srvName = "unknown";

        String edptName = consume.getEndpointName();
        if (StringHelper.isNullOrEmpty(edptName))
            edptName = "unknown";

        String opName = consume.getOperation() != null ? consume.getOperation().getLocalPart() : null;
        if (StringHelper.isNullOrEmpty(opName))
            opName = "unknown";

        return itfName + "/" + srvName + "/" + edptName + "/" + opName;
    }
}
