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

org.onebusaway.gtfs.serialization.GtfsWriter Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/**
 * Copyright (C) 2011 Brian Ferris 
 *
 * 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.
 */
package org.onebusaway.gtfs.serialization;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.onebusaway.csv_entities.CsvEntityWriter;
import org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory;
import org.onebusaway.gtfs.services.GtfsDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GtfsWriter extends CsvEntityWriter {

  private final Logger _log = LoggerFactory.getLogger(GtfsWriter.class);

  public static final String KEY_CONTEXT = GtfsWriter.class.getName()
      + ".context";

  private List> _entityClasses = new ArrayList>();

  private Map, Comparator> _entityComparators = new HashMap, Comparator>();
  
  public GtfsWriter() {

    /**
     * Prep the Entity Schema Factories
     */
    _entityClasses.addAll(GtfsEntitySchemaFactory.getEntityClasses());
    _entityComparators.putAll(GtfsEntitySchemaFactory.getEntityComparators());
    DefaultEntitySchemaFactory schemaFactory = createEntitySchemaFactory();
    setEntitySchemaFactory(schemaFactory);
  }

  public List> getEntityClasses() {
    return _entityClasses;
  }
  
  public Map,Comparator> getEntityComparators() {
    return _entityComparators;
  }

  public void run(GtfsDao dao) throws IOException {

    List> classes = getEntityClasses();

    for (Class entityClass : classes) {
      _log.info("writing entities: " + entityClass.getName());
      Collection entities = dao.getAllEntitiesForType(entityClass);
      entities = sortEntities(entityClass,entities);
      for (Object entity : entities)
        handleEntity(entity);
      flush();
    }

    close();
  }

  @SuppressWarnings("unchecked")
  private Collection sortEntities(Class entityClass, Collection entities) {
    
    Comparator comparator = (Comparator) _entityComparators.get(entityClass);
    
    if( comparator == null)
      return entities;
    
    List sorted = new ArrayList();
    sorted.addAll(entities);
    Collections.sort(sorted, comparator);
    return sorted;
  }

  /****
   * Protected Methods
   ****/

  protected DefaultEntitySchemaFactory createEntitySchemaFactory() {
    return GtfsEntitySchemaFactory.createEntitySchemaFactory();
  }
}