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

it.tidalwave.bluebill.taxonomy.birds.TaxonUniqueIdManager Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: TaxonUniqueIdManager.java,v bd7cfbf9c999 2010/07/22 22:54:20 fabrizio $
 *
 **********************************************************************************************************************/
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.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: TaxonUniqueIdManager.java,v bd7cfbf9c999 2010/07/22 22:54:20 fabrizio $
 *
 **********************************************************************************************************************/
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 (@Nonnull final 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 String findId (@Nonnull final 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();
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy