RomaBeanDataSource.java :  » Web-Framework » roma-webwizard » org » romaframework » aspect » reporting » jr » ds » Java Open Source

Java Open Source » Web Framework » roma webwizard 
roma webwizard » org » romaframework » aspect » reporting » jr » ds » RomaBeanDataSource.java
/*
 * 
 * Copyright 2007 Giordano Maestro (giordano.maestro@assetdata.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.
 */
package org.romaframework.aspect.reporting.jr.ds;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRewindableDataSource;

import org.romaframework.aspect.reporting.jr.JRDesignHelper;
import org.romaframework.aspect.reporting.jr.ReflectionHelper;
import org.romaframework.aspect.reporting.jr.element.ExpandedFieldJr;
import org.romaframework.core.flow.ObjectContext;
import org.romaframework.core.schema.SchemaClass;
import org.romaframework.core.schema.SchemaClassDefinition;
import org.romaframework.core.schema.SchemaField;
import org.romaframework.core.schema.SchemaManager;

public class RomaBeanDataSource implements JRRewindableDataSource {

  List<Object>          list          = new LinkedList<Object>();
  SchemaClassDefinition  schemaObject  = null;
  Iterator<Object>      iterator;
  Object                current;

  public RomaBeanDataSource(Collection<Object> iList, SchemaClassDefinition iSchemaObject) {
    super();
    if (iList != null) {
      list.addAll(iList);
    }
    schemaObject = iSchemaObject;
    iterator = list.iterator();
  }

  public Object getFieldValue(JRField iField) throws JRException {
    final String fieldName = iField.getName();
    SchemaField schemaField = null;
    Object invokeOn;
    boolean subInvoke = false;
    if (fieldName.startsWith(ExpandedFieldJr.EXPANDED)) {
      final String fieldNames[] = fieldName.split("\\|");
      final SchemaField sourceSchemaField = schemaObject.getField(fieldNames[2]);
      invokeOn = ReflectionHelper.getObjectValue(sourceSchemaField, current);
      SchemaClass sourceSchemaClass = null;
      if (invokeOn != null) {
        sourceSchemaClass = ObjectContext.getInstance().getComponent(SchemaManager.class).getClassInfo(
            current.getClass().getSimpleName() + "." + fieldNames[2]);
        schemaField = sourceSchemaClass.getField(fieldNames[3]);
        subInvoke = true;
      }
    } else {
      invokeOn = current;
      schemaField = schemaObject.getField(fieldName);
    }

    if (invokeOn == null) {
      return null;
    }
    try {
      Method method;
      if (ReflectionHelper.isPrimitive(schemaField.getTypeClass())) {
        method = schemaField.getGetterMethod();
      } else {
        if (!subInvoke) {
          method = Object.class.getMethod("toString", new Class[0]);
        } else {
          method = schemaField.getGetterMethod();
          if (JRDesignHelper.getIsVisibleField(schemaField)) {
            invokeOn = method.invoke(invokeOn, new Object[0]);
          } else {
            return null;
          }
          method = Object.class.getMethod("toString", new Class[0]);
        }
      }

      if (invokeOn == null) {
        return null;
      }
      if (JRDesignHelper.getIsVisibleField(schemaField)) {
        return method.invoke(invokeOn, new Object[0]);
      } else {
        return null;
      }
    }
    catch (final Exception e) {
      throw new JRException(e);
    }
  }

  public boolean next() throws JRException {
    try {
      current = iterator.next();
      return true;
    }
    catch (final NoSuchElementException e) {
      return false;
    }
  }

  public void moveFirst() throws JRException {
    iterator = list.iterator();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.