Package com.sourcetap.sfa.util

Source Code of com.sourcetap.sfa.util.EntityHelper

/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
*  The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an  "AS IS"  basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/

package com.sourcetap.sfa.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.model.DynamicViewEntity;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.entity.model.ModelViewEntity;
import org.ofbiz.entity.util.EntityFindOptions;
import org.ofbiz.entity.util.EntityListIterator;

/**
* @author Steve Fowler
*
*
*/
public class EntityHelper
{
 
  public static final String module = EntityHelper.class.getName();

  /**
   * Create a dynamic view entity using the specified entity as the source.  This can be used
   * as a starting point when building a dynamic view with most fields from a single entity.
   * All fields from the specified entity will be added to the view entity.  Other entities
   * and fields can be added to the returned dynamic view
   * @param modelEntiy Primary Entity to be used to create the dynamic view entity.
   * @return a dynamic view entity that is based on the specified modelEntity
   */
  public static DynamicViewEntity createDynamicViewEntity(GenericDelegator delegator, String entityName)
  {
    try {
      ModelEntity modelEntity = delegator.getModelReader().getModelEntity(entityName);
      return createDynamicViewEntity( delegator, modelEntity);
    } catch (Exception e)
    {
      Debug.logError(e, module);
      throw new IllegalArgumentException("Error finding Entity with name " + entityName + " for createDynamicViewEntity")
    }
  }

  /**
   * Create a dynamic view entity using the specified entity as the source.  This can be used
   * as a starting point when building a dynamic view with most fields from a single entity.
   * All fields from the specified entity will be added to the view entity.  Other entities
   * and fields can be added to the returned dynamic view
   * @param modelEntiy Primary Entity to be used to create the dynamic view entity.
   * @return a dynamic view entity that is based on the specified modelEntity
   */
  public static DynamicViewEntity createDynamicViewEntity(GenericDelegator delegator, ModelEntity modelEntity)
  {
    String entityName = modelEntity.getEntityName();
    DynamicViewEntity dve = new DynamicViewEntity();
    dve.addMemberEntity(entityName, entityName);
   
    Iterator modelFieldIter = modelEntity.getFieldsIterator();
    while (modelFieldIter.hasNext()) {
      ModelField modelField = (ModelField) modelFieldIter.next();
      dve.addAlias(entityName, modelField.getName(), null, null, Boolean.valueOf(modelField.getIsPk()), null, null);
    }

    return dve;
  }

  public static List findByCondition( GenericDelegator delegator, DynamicViewEntity dve, EntityCondition condition, List orderBy )
  throws GenericEntityException
  {
    ModelViewEntity mve = dve.makeModelViewEntity(delegator);
    List fields = mve.getAllFieldNames();
    EntityListIterator eliOne = delegator.findListIteratorByCondition(dve, condition, null, fields, orderBy, null);
    List valueList = eliOne.getCompleteList();
    eliOne.close();
    return valueList;
 

  public static EntityListIterator findIteratorByCondition( GenericDelegator delegator, DynamicViewEntity dve, EntityCondition condition, List orderBy )
  throws GenericEntityException
  {
    return findIteratorByCondition( delegator, dve, condition, null, orderBy);
 

  public static EntityListIterator findIteratorByCondition( GenericDelegator delegator, DynamicViewEntity dve, EntityCondition condition, List selectFields, List orderBy )
  throws GenericEntityException
  {
    EntityListIterator eliOne = delegator.findListIteratorByCondition(dve, condition, null, selectFields, orderBy, 
                                       new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE , EntityFindOptions.CONCUR_READ_ONLY, true));
    return eliOne;
 
 
  public static String getEntityOperator( int operatorId)
  {
    switch ( operatorId )
    {
      case 1:    return "equals";
      case 2:    return "notEqual";
      case 3:    return "lessThan";
      case 4:    return "greaterThan";
      case 5:    return "lessThanEqualTo";
      case 6:    return "greaterThanEqualTo";
      case 7:    return "in";
      case 8:    return "between";
      case 9:    return "not";
      case 10return "and";
      case 11return "or";
      case 12return "like";
      case 13return "not-in";
      defaultthrow new IllegalArgumentException("Unknown operator with Id:" + operatorId);
    }
  }
 
  public static List findByClause(GenericDelegator delegator, String mainEntityName, List entityClauses, Map fields, List orderBy)
  {
    throw new RuntimeException("EntityHelper.findByClause is not implemented");
  }
 
  public static GenericValue getPrimaryGVFromDynamicGV( GenericDelegator delegator, GenericValue dynamicGV, String primaryEntityName)
  {
    if ( dynamicGV == null)
      return null;
     
    if ( primaryEntityName.equals(dynamicGV.getEntityName()))
      return dynamicGV;
   
    GenericValue retValue = delegator.makeValidValue( primaryEntityName, dynamicGV.getAllFields());
    return retValue;
  }
 
  public static List getPrimaryGVLFromDynamicGVL( GenericDelegator delegator, List dynamicGVL, String primaryEntityName)
  {
    if ( dynamicGVL == null)
      return null;
     
    Iterator dgvI = dynamicGVL.iterator();
    List list = new ArrayList();
   
    while ( dgvI.hasNext() )
    {
      GenericValue gv = (GenericValue) dgvI.next();
      list.add( getPrimaryGVFromDynamicGV( delegator, gv, primaryEntityName));
     
    }
    return list;
  }
 
}
TOP

Related Classes of com.sourcetap.sfa.util.EntityHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.