// CacheRegistryClient.java
//
// Created: November 2004
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.shared.*;
import org.biomoby.shared.CentralCached;

import org.biomoby.client.CmdLineHelper;
import org.biomoby.client.CentralDigestCachedImpl;

import org.biomoby.shared.event.LogListener;
import org.biomoby.shared.event.Notifier;

import org.tulsoft.tools.BaseCmdLine;
import org.apache.commons.lang.time.DurationFormatUtils;

/**
 * This is a command-line client creating (and removing) a cache of a
 * Moby registry. It allows to store locally (in a file system)
 * objects representing data types, service instances, namespaces, and
 * service types. <p>
 *
 * The client is quite simple and its basic functionality is included
 * also in other clients (such as {@link MobyGraphs}. The reason for
 * having this client separately is also to show how to use {@link
 * org.biomoby.client.CentralDigestCachedImpl CentralDigestCachedImpl}
 * - which is a real caching worker. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: CacheRegistryClient.java,v 1.10 2008/03/02 12:45:25 senger Exp $
 */

public class CacheRegistryClient
    extends CmdLineHelper {

    static boolean moreVerbose = false;

    /*************************************************************************
     *
     * Entry point...
     *
     *************************************************************************/
    public static void main (String [] args) {
	try {

	    BaseCmdLine cmd = getCmdLine (args, CacheRegistryClient.class);

	    // where is a Moby registry
	    CentralCached worker = getCachableRegistryWorker (cmd);

	    // there is not much to do without -cachedir
	    String cacheDir = cmd.getParam ("-cachedir");
	    if (cacheDir == null) {
		emsgln ("Parameter -cachedir must be specified.");
		exit (1);
	    }
	
	    // how much to show
	    moreVerbose = cmd.hasOption ("-v");
	    if (! cmd.hasOption ("-q"))
		worker.addNotificationListener (new LogListener());

	    long started, elapsed, total = 0;

	    //
	    // [re-]fill the cache
	    //
	    if (cmd.hasOption ("-fill") || cmd.hasOption ("-fill-n")) {
		worker.removeFromCache (CentralCached.CACHE_PART_NAMESPACES);
		decoration ("Retrieving namespaces...    ");
		started = System.currentTimeMillis();
		worker.getFullNamespaces();
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-fill") || cmd.hasOption ("-fill-d")) {
		worker.removeFromCache (CentralCached.CACHE_PART_DATATYPES);
		decoration ("Retrieving data types...    ");
		started = System.currentTimeMillis();
		worker.getDataTypes();
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-fill") || cmd.hasOption ("-fill-s")) {
		worker.removeFromCache (CentralCached.CACHE_PART_SERVICES);
		decoration ("Retrieving services...      ");
		started = System.currentTimeMillis();
		worker.getServices();
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-fill") || cmd.hasOption ("-fill-t")) {
		worker.removeFromCache (CentralCached.CACHE_PART_SERVICETYPES);
		decoration ("Retrieving service types... ");
		started = System.currentTimeMillis();
		worker.getFullServiceTypes();
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }

	    //
	    // update the cache
	    //
	    if (cmd.hasOption ("-update") || cmd.hasOption ("-update-n")) {
		decoration ("Updating namespaces...      ");
		started = System.currentTimeMillis();
		worker.updateCache (CentralCached.CACHE_PART_NAMESPACES);
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-update") || cmd.hasOption ("-update-d")) {
		decoration ("Updating data types...      ");
		started = System.currentTimeMillis();
		worker.updateCache (CentralCached.CACHE_PART_DATATYPES);
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-update") || cmd.hasOption ("-update-s")) {
		decoration ("Updating services...        ");
		started = System.currentTimeMillis();
		worker.updateCache (CentralCached.CACHE_PART_SERVICES);
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }
	    if (cmd.hasOption ("-update") || cmd.hasOption ("-update-t")) {
		decoration ("Updating service types...   ");
		started = System.currentTimeMillis();
		worker.updateCache (CentralCached.CACHE_PART_SERVICETYPES);
		elapsed = System.currentTimeMillis() - started;
		total += elapsed;
		decorationLn (DurationFormatUtils.formatDurationHMS (elapsed));
	    }

	    //
	    // remove the cache
	    //
	    if (cmd.hasOption ("-remove") || cmd.hasOption ("-remove-n")) {
		worker.removeFromCache (CentralCached.CACHE_PART_NAMESPACES);
		decorationLn ("Cache for namespaces removed.");
	    }
	    if (cmd.hasOption ("-remove") || cmd.hasOption ("-remove-d")) {
		worker.removeFromCache (CentralCached.CACHE_PART_DATATYPES);
		decorationLn ("Cache for data types removed.");
	    }
	    if (cmd.hasOption ("-remove") || cmd.hasOption ("-remove-s")) {
		worker.removeFromCache (CentralCached.CACHE_PART_SERVICES);
		decorationLn ("Cache for services removed.");
	    }
	    if (cmd.hasOption ("-remove") || cmd.hasOption ("-remove-t")) {
		worker.removeFromCache (CentralCached.CACHE_PART_SERVICETYPES);
		decorationLn ("Cache for service types removed.");
	    }

	    //
	    // cache info
	    //
	    if (cmd.hasOption ("-info") || cmd.hasOption ("-info-n")) {
		decorationLn ("Cache for namespaces...");
		msgln
		    (worker.getCacheInfoFormatted (CentralCached.CACHE_PART_NAMESPACES));
	    }
	    if (cmd.hasOption ("-info") || cmd.hasOption ("-info-d")) {
		decorationLn ("Cache for data types...");
		msgln
		    (worker.getCacheInfoFormatted (CentralCached.CACHE_PART_DATATYPES));
	    }
	    if (cmd.hasOption ("-info") || cmd.hasOption ("-info-s")) {
		decorationLn ("Cache for service authorities...");
		msgln
		    (worker.getCacheInfoFormatted (CentralCached.CACHE_PART_SERVICES));
	    }
	    if (cmd.hasOption ("-info") || cmd.hasOption ("-info-t")) {
		decorationLn ("Cache for service types...");
		msgln
		    (worker.getCacheInfoFormatted (CentralCached.CACHE_PART_SERVICETYPES));
	    }


	    decorationLn ("Total spent time:           " +
			  DurationFormatUtils.formatDurationHMS (total));


	} catch (Throwable e) {
	    processErrorAndExit (e);
	}
    }

    /*************************************************************************
     * Print 'msg' but only if in "more verbose" mode.
     *************************************************************************/
    static void decoration (String msg) {
 	if (moreVerbose) msg (msg);
    }
    static void decorationLn (String msg) {
 	if (moreVerbose) msgln (msg);
    }
}
