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

org.onebusaway.csv_entities.schema.AbstractFieldMapping Maven / Gradle / Ivy

/**
 * 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.csv_entities.schema;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;

import org.onebusaway.csv_entities.exceptions.MethodInvocationException;
import org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException;

public abstract class AbstractFieldMapping implements FieldMapping {

  protected final Class _entityType;

  protected final String _csvFieldName;

  protected final String _objFieldName;

  protected final boolean _required;

  protected int _order = 0;

  protected Method _isSetMethod = null;

  public AbstractFieldMapping(Class entityType, String csvFieldName,
      String objFieldName, boolean required) {
    _entityType = entityType;
    _csvFieldName = csvFieldName;
    _objFieldName = objFieldName;
    _required = required;
  }

  public void setOrder(int order) {
    _order = order;
  }

  public void setIsSetMethod(Method isSetMethod) {
    _isSetMethod = isSetMethod;
  }

  public void getCSVFieldNames(Collection names) {
    names.add(_csvFieldName);
  }

  public int getOrder() {
    return _order;
  }

  protected boolean isMissing(Map csvValues) {
    return !(csvValues.containsKey(_csvFieldName) && csvValues.get(
        _csvFieldName).toString().length() > 0);
  }

  protected boolean isMissing(BeanWrapper object) {
    if (_isSetMethod != null) {
      Object instance = object.getWrappedInstance(Object.class);
      try {
        Object r = _isSetMethod.invoke(instance);
        if (r != null && r instanceof Boolean) {
          Boolean b = (Boolean) r;
          return !b.booleanValue();
        }
      } catch (Exception ex) {
        throw new MethodInvocationException(_entityType, _isSetMethod, ex);
      }
    } else {
      Object obj = object.getPropertyValue(_objFieldName);
      return obj == null;
    }
    return false;
  }

  protected boolean isMissingAndOptional(Map csvValues) {

    boolean missing = isMissing(csvValues);

    if (_required && missing)
      throw new MissingRequiredFieldException(_entityType, _csvFieldName);

    return missing;
  }

  protected boolean isMissingAndOptional(BeanWrapper object) {
    boolean missing = isMissing(object);

    if (_required && missing)
      throw new MissingRequiredFieldException(_entityType, _objFieldName);

    return missing;
  }

  protected boolean isOptional() {
    return !_required;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy