it.tidalwave.bluebill.taxonomy.birds.TaxonUniqueIdManager Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Resources - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://java.net/hg/bluebill~resources-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;
import com.eaio.uuid.UUID;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.zip.GZIPOutputStream;
/***********************************************************************************************************************
*
* This class implements a persistent map between paths and identifiers. It is used to assign the same ids to known
* concepts at every run. If a new, inknown concept appears, {@link #findId(java.lang.String)} fails with an exception.
* In this case, you have to re-run with readOnly = false, so a new id will be generated for new concepts.
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class TaxonUniqueIdManager
{
public static final String ID_BASE = "urn:bluebill.tidalwave.it:taxon:";
@Nonnull
private final File backingStore;
private final Map idMapByPath = new TreeMap();
private boolean dirty = false;
private boolean readOnly = true;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public TaxonUniqueIdManager (final @Nonnull File backingStore)
throws IOException
{
this.backingStore = backingStore;
if (backingStore.exists())
{
final InputStream is = new GZIPInputStream(new FileInputStream(backingStore));
final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
for (;;)
{
final String s = br.readLine();
if (s == null)
{
break;
}
final int i = s.indexOf(':');
final String path = s.substring(0, i).trim();
final String id = s.substring(i + 1).trim();
idMapByPath.put(path, id);
}
br.close();
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public synchronized Collection getPaths()
{
return Collections.unmodifiableCollection(idMapByPath.keySet());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public synchronized String findId (final @Nonnull String path)
{
// final String translatedPath = itisTranslator.translated(path);
final String reducedPath = path.replaceAll("/Animalia/Chordata/Aves/", "").replaceAll("^.*formes/", "");
String id = idMapByPath.get(reducedPath);
if (id == null)
{
// if (path.endsWith("formes"))
// {
// return ID_BASE + path.toLowerCase();
// }
if (readOnly)
{
// FIXME: throw a NotFoundException
throw new RuntimeException("Path not found and can't create a new id (read only mode): " + path);
}
else
{
id = ID_BASE + (new UUID().toString());
idMapByPath.put(reducedPath, id);
dirty = true;
}
}
return id;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void close()
throws IOException
{
if (dirty)
{
// System.err.println("DIRTY!");
final OutputStream os = new GZIPOutputStream(new FileOutputStream(backingStore));
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
for (final Map.Entry entry : idMapByPath.entrySet())
{
pw.printf("%-110s: %s\n", entry.getKey(), entry.getValue());
}
pw.close();
}
}
}