// Path: org.juzidian.dataload/src/main/java/org/juzidian/dataload/DictionaryResource.java
// public interface DictionaryResource {
// 
// 	/**
// 	 * @return the URL for downloading the remote dictionary resource.
// 	 */
// 	String getUrl();
// 
// 	/**
// 	 * @return the size of the dictionary resource in bytes.
// 	 */
// 	int getSize();
// 
// 	/**
// 	 * @return the SHA-1 hash of the dictionary resource.
// 	 */
// 	String getSha1();
// 
// }
// 
// Path: org.juzidian.dataload/src/main/java/org/juzidian/dataload/DictionaryResourceDownloader.java
// public class DictionaryResourceDownloader {
// 
// 	/**
// 	 * Download a compressed dictionary resource, verify its SHA1 hash code and
// 	 * uncompress it into the given output stream.
// 	 * 
// 	 * @param resource a {@link DictionaryResource} to download.
// 	 * @param out the output stream to write the downloaded data to.
// 	 * @param handler the {@link DownloadProgressHandler} to receive events for
// 	 *        download progress.
// 	 * @throws DictionaryResourceDownloaderException if download fails or a hash
// 	 *         code does not match.
// 	 */
// 	public void download(final DictionaryResource resource, final OutputStream out, final DownloadProgressHandler handler) throws DictionaryResourceDownloaderException {
// 		final URL url = createUrl(resource);
// 		try {
// 			final int contentLength = url.openConnection().getContentLength();
// 			final InputStream rawInputStream = url.openStream();
// 			final InputStream progressMonitoringInputStream = new ProgressMonitoringInputStream(rawInputStream, contentLength, handler);
// 			final MessageDigest messageDigest = getSha1Digest();
// 			final InputStream digestInputStream = new DigestInputStream(progressMonitoringInputStream, messageDigest);
// 			final InputStream gzipInputStream = new GZIPInputStream(digestInputStream);
// 			IoUtil.copy(gzipInputStream, out);
// 			this.verifyChecksum(resource, messageDigest.digest());
// 		} catch (final IOException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// 	private void verifyChecksum(final DictionaryResource resource, final byte[] digest) throws DictionaryResourceDownloaderException {
// 		final String digestHex = HexUtil.bytesToHex(digest);
// 		if (!digestHex.equals(resource.getSha1())) {
// 			throw new DictionaryResourceDownloaderException("Checksum mismatch. Expected " + resource.getSha1() + " but was " + digestHex);
// 		}
// 	}
// 
// 	private static MessageDigest getSha1Digest() throws DictionaryResourceDownloaderException {
// 		try {
// 			return MessageDigest.getInstance("SHA1");
// 		} catch (final NoSuchAlgorithmException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// 	private static URL createUrl(final DictionaryResource resource) throws DictionaryResourceDownloaderException {
// 		try {
// 			return new URL(resource.getUrl());
// 		} catch (final MalformedURLException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// }


import org.juzidian.util.IoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.inject.Inject;
import org.juzidian.core.DictionaryDataStore;
import org.juzidian.dataload.DictionaryResource;
import org.juzidian.dataload.DictionaryResourceDownloader;
import org.juzidian.dataload.DictionaryResourceRegistry;
import org.juzidian.dataload.DictionaryResourceRegistryService;
import org.juzidian.dataload.DictonaryResourceRegistryServiceException;
/*
 * Copyright Nathan Jones 2013
 *
 * This file is part of Juzidian.
 *
 * Juzidian is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Juzidian 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Juzidian.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.juzidian.cli;




class DictionaryDbInitializer {

	private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryDbInitializer.class);

	final DictionaryDataStore dictionaryDataStore;

	final DictionaryResourceRegistryService dictionaryRegistryService;

	final DictionaryResourceDownloader dictionaryDownloader;

	final File dictionaryDbFile;

	@Inject
	public DictionaryDbInitializer(final DictionaryDataStore dataStore, final DictionaryResourceRegistryService registryService,
			final DictionaryResourceDownloader dictionaryDownloader, @DictionaryDbPath final File dictionaryDbFile) {
		this.dictionaryDataStore = dataStore;
		this.dictionaryRegistryService = registryService;
		this.dictionaryDownloader = dictionaryDownloader;
		this.dictionaryDbFile = dictionaryDbFile;
	}

	public void initializeDb() throws Exception {
		if (!this.dictionaryDbFile.exists()) {
			LOGGER.info("Dictionary DB missing.");
			this.downloadDb();
		} else {
			if (this.dictionaryDataStore.getCurrentDataFormatVersion() != DictionaryDataStore.DATA_FORMAT_VERSION) {
				LOGGER.info("Dictionary DB incompatible.");
				this.downloadDb();
			} else {
				LOGGER.debug("Dictionary DB compatible.");
			}
		}
	}

	private void downloadDb() throws Exception {
		this.dictionaryDbFile.getParentFile().mkdirs();
// Path: org.juzidian.dataload/src/main/java/org/juzidian/dataload/DictionaryResource.java
// public interface DictionaryResource {
// 
// 	/**
// 	 * @return the URL for downloading the remote dictionary resource.
// 	 */
// 	String getUrl();
// 
// 	/**
// 	 * @return the size of the dictionary resource in bytes.
// 	 */
// 	int getSize();
// 
// 	/**
// 	 * @return the SHA-1 hash of the dictionary resource.
// 	 */
// 	String getSha1();
// 
// }
// 
// Path: org.juzidian.dataload/src/main/java/org/juzidian/dataload/DictionaryResourceDownloader.java
// public class DictionaryResourceDownloader {
// 
// 	/**
// 	 * Download a compressed dictionary resource, verify its SHA1 hash code and
// 	 * uncompress it into the given output stream.
// 	 * 
// 	 * @param resource a {@link DictionaryResource} to download.
// 	 * @param out the output stream to write the downloaded data to.
// 	 * @param handler the {@link DownloadProgressHandler} to receive events for
// 	 *        download progress.
// 	 * @throws DictionaryResourceDownloaderException if download fails or a hash
// 	 *         code does not match.
// 	 */
// 	public void download(final DictionaryResource resource, final OutputStream out, final DownloadProgressHandler handler) throws DictionaryResourceDownloaderException {
// 		final URL url = createUrl(resource);
// 		try {
// 			final int contentLength = url.openConnection().getContentLength();
// 			final InputStream rawInputStream = url.openStream();
// 			final InputStream progressMonitoringInputStream = new ProgressMonitoringInputStream(rawInputStream, contentLength, handler);
// 			final MessageDigest messageDigest = getSha1Digest();
// 			final InputStream digestInputStream = new DigestInputStream(progressMonitoringInputStream, messageDigest);
// 			final InputStream gzipInputStream = new GZIPInputStream(digestInputStream);
// 			IoUtil.copy(gzipInputStream, out);
// 			this.verifyChecksum(resource, messageDigest.digest());
// 		} catch (final IOException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// 	private void verifyChecksum(final DictionaryResource resource, final byte[] digest) throws DictionaryResourceDownloaderException {
// 		final String digestHex = HexUtil.bytesToHex(digest);
// 		if (!digestHex.equals(resource.getSha1())) {
// 			throw new DictionaryResourceDownloaderException("Checksum mismatch. Expected " + resource.getSha1() + " but was " + digestHex);
// 		}
// 	}
// 
// 	private static MessageDigest getSha1Digest() throws DictionaryResourceDownloaderException {
// 		try {
// 			return MessageDigest.getInstance("SHA1");
// 		} catch (final NoSuchAlgorithmException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// 	private static URL createUrl(final DictionaryResource resource) throws DictionaryResourceDownloaderException {
// 		try {
// 			return new URL(resource.getUrl());
// 		} catch (final MalformedURLException e) {
// 			throw new DictionaryResourceDownloaderException(e);
// 		}
// 	}
// 
// }


// Path: org.juzidian.cli/src/main/java/org/juzidian/cli/DictionaryDbInitializer.java
import org.juzidian.util.IoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.inject.Inject;
import org.juzidian.core.DictionaryDataStore;
import org.juzidian.dataload.DictionaryResource;
import org.juzidian.dataload.DictionaryResourceDownloader;
import org.juzidian.dataload.DictionaryResourceRegistry;
import org.juzidian.dataload.DictionaryResourceRegistryService;
import org.juzidian.dataload.DictonaryResourceRegistryServiceException;

/*
 * Copyright Nathan Jones 2013
 *
 * This file is part of Juzidian.
 *
 * Juzidian is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Juzidian 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Juzidian.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.juzidian.cli;




class DictionaryDbInitializer {

	private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryDbInitializer.class);

	final DictionaryDataStore dictionaryDataStore;

	final DictionaryResourceRegistryService dictionaryRegistryService;

	final DictionaryResourceDownloader dictionaryDownloader;

	final File dictionaryDbFile;

	@Inject
	public DictionaryDbInitializer(final DictionaryDataStore dataStore, final DictionaryResourceRegistryService registryService,
			final DictionaryResourceDownloader dictionaryDownloader, @DictionaryDbPath final File dictionaryDbFile) {
		this.dictionaryDataStore = dataStore;
		this.dictionaryRegistryService = registryService;
		this.dictionaryDownloader = dictionaryDownloader;
		this.dictionaryDbFile = dictionaryDbFile;
	}

	public void initializeDb() throws Exception {
		if (!this.dictionaryDbFile.exists()) {
			LOGGER.info("Dictionary DB missing.");
			this.downloadDb();
		} else {
			if (this.dictionaryDataStore.getCurrentDataFormatVersion() != DictionaryDataStore.DATA_FORMAT_VERSION) {
				LOGGER.info("Dictionary DB incompatible.");
				this.downloadDb();
			} else {
				LOGGER.debug("Dictionary DB compatible.");
			}
		}
	}

	private void downloadDb() throws Exception {
		this.dictionaryDbFile.getParentFile().mkdirs();
		final DictionaryResource resource = this.getDictionaryResource();