Package com.sourcetap.sfa.ui

Source Code of com.sourcetap.sfa.ui.UIList

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityComparisonOperator;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.util.EntityListIterator;

import com.sourcetap.sfa.event.EventUtility;
import com.sourcetap.sfa.event.GenericEventProcessor;
import com.sourcetap.sfa.replication.GenericReplicator;
import com.sourcetap.sfa.security.SecurityLinkInfo;
import com.sourcetap.sfa.security.SecurityWrapper;
import com.sourcetap.sfa.util.QueryInfo;
import com.sourcetap.sfa.util.UserInfo;
public class UIList {
  public static final String module = UIList.class.getName();
   protected boolean primaryFieldsLoaded = false;
   protected boolean relatedFieldsLoaded = false;
   protected List primaryFields = null;
   protected List relatedFields = null;
   protected String listId = null;
   protected String listName = null;
    /**
     * @return Returns the detailSectionId.
     */
    public String getDetailSectionId() {
        return detailSectionId;
    }
    /**
     * @param detailSectionId The detailSectionId to set.
     */
    public void setDetailSectionId(String detailSectionId) {
        this.detailSectionId = detailSectionId;
    }
    /**
     * @return Returns the listSectionId.
     */
    public String getListSectionId() {
        return listSectionId;
    }
    /**
     * @param listSectionId The listSectionId to set.
     */
    public void setListSectionId(String listSectionId) {
        this.listSectionId = listSectionId;
    }
   protected String listSectionId = null;
   protected String detailSectionId = null;
   protected String partyId = null;
   protected String queryId = null;
   protected String listType = null
  
   protected List reportFields = null;
   protected List reportCriteria = null;
   protected List reportOrderBy = null;
  
   private List selectFieldLabels = null;
   private List selectFieldNames = null;
   protected GenericDelegator delegator = null;
   protected QueryInfo queryInfo = null;
   protected GenericEventProcessor eventProcessor = null;
  
  public UIList(GenericDelegator delegator_)
  {
    setDelegator(delegator_);
  }
  
   public UIList(String listId_, GenericDelegator delegator_)
   {
     setDelegator(delegator_);
     loadList(listId_);
   }
  
   public void loadList(String listId_)
   {
     try
    {
      GenericValue listGV = delegator.findByPrimaryKey("UiList", UtilMisc.toMap("listId", listId_));
      if ( listGV == null )
        throw new IllegalArgumentException("Unable to find report with ID " + listId_ );
       
      setListId(listId_);
      setPartyId(listGV.getString("partyId"));
      setListName(listGV.getString("listName"));

      // the query was save with the screen section for the LIST screen section.  Get the corresponding detail screen section
      // which will have a more complete list of fields to choose from
      String listSectionId = listGV.getString("sectionId");
      String detailSectionId = listSectionId;
      setListSectionId(listSectionId);
     
      GenericValue sectionGV = delegator.findByPrimaryKey("UiScreenSection", UtilMisc.toMap("sectionId", listSectionId));
      GenericValue screenGV = delegator.findByPrimaryKey("UiScreen", UtilMisc.toMap("screenId", sectionGV.getString("screenId")));
      detailSectionId = screenGV.getString("detailSectionId");
     
      if ( ( detailSectionId == null ) || (detailSectionId.length() < 1) )
          detailSectionId = listSectionId;
     
      setDetailSectionId(detailSectionId);
             
    } catch (GenericEntityException e)
    {
      Debug.logError("Error getting report: " + e.getMessage(), module);
      e.printStackTrace();
    }
   }
  
   public void saveReport(String partyId, String listName)
   {
     setPartyId(partyId);
    
     if ( !listName.equals(getListName()))
     {
      setListName(listName);
      setListId(null);      
     }
    
    saveList();
   }
  
   public void saveList()
   {
    try
    {
      String listId = getListId();
     
      if ( listId == null )
      {
        listId = GenericReplicator.getNextSeqId("UiList", delegator);
        setListId(listId);
      }
      else
      {
        // TODO need to delete old report info
      }
     
      List valuesToStore = new ArrayList();
         
      GenericValue listGV = new GenericValue(delegator.getModelEntity("UiList"));
      listGV.setDelegator(delegator);
      listGV.set("listId", listId);
      listGV.set("sectionId", getListSectionId());
      listGV.set("partyId", getPartyId());
      listGV.set("listName", getListName());
      listGV.set("queryId", getQueryId());
      listGV.set("listType", getListType());
     
      valuesToStore.add(listGV);
     
     
      delegator.storeAll(valuesToStore);
    } catch (GenericEntityException e)
    {
      e.printStackTrace();
      Debug.logError("error saving list" + e.getMessage(), module);
    }
   }
  
  public static boolean deleteList(String listId, GenericDelegator delegator)
  {
    try
    {
      List valuesToBeRemoved = new ArrayList();
     
      GenericValue gv = new GenericValue(delegator.getModelEntity("UiListItem"));
      gv.set("listId", listId);
      valuesToBeRemoved.add(gv);
     
      gv = new GenericValue(delegator.getModelEntity("UiList"));
      gv.set("listId", listId);
      valuesToBeRemoved.add(gv);
           
      delegator.removeAll(valuesToBeRemoved);
    } catch (GenericEntityException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
      Debug.logError("unable to delete list: " + listId + " error:" + e.getMessage(), module);
      return false;
    }
    return true;
  }
  public static List getLists(String companyId, String partyId, GenericDelegator delegator)
  {
    return getLists(null, companyId, partyId, delegator);
  }

  public static List getLists(String sectionId, String companyId, String partyId, GenericDelegator delegator)
  {
    try
    {
      Map condition = sectionId == null ? UtilMisc.toMap("partyId", "-1") : UtilMisc.toMap("sectionId", sectionId, "partyId", "-1");
      List orderBy = UtilMisc.toList("reportName");
       
      List reportList = delegator.findByAnd("UiReport", condition, orderBy);
      if ( !partyId.equals("-1")) {

        EntityCondition c1 = new EntityConditionList( UtilMisc.toList( new EntityExpr("partyId", EntityOperator.EQUALS, partyId),
                                         new EntityExpr("partyId", EntityOperator.EQUALS, companyId)), EntityOperator.OR);
       
          if ( sectionId != null )
          c1 = new EntityConditionList( UtilMisc.toList( c1, new EntityExpr("sectionId", EntityOperator.EQUALS, sectionId)), EntityOperator.AND);

        if ( reportList == null)
          reportList = delegator.findByCondition("UiList", c1, null, orderBy);
        else
          reportList.addAll(delegator.findByCondition("UiList", c1, null, orderBy));
      }
     
      return reportList;
    } catch (GenericEntityException e)
    {
      Debug.logError("Error getting reportList: " + e.getMessage(), module);
      e.printStackTrace();
    }
    return null;
  }
      
   public List loadPrimarySectionFields()
    {
    try
    {
      List primaryFields = getDelegator().findByAnd("UiScreenSectionFields",
          UtilMisc.toMap("sectionId", getDetailSectionId()),
          UtilMisc.toList("displayOrder"));
     
      setPrimaryFields(primaryFields);
      return primaryFields;
    } catch (GenericEntityException e)
    {
      Debug.logError("Unable to get Primary Screen Fields:" + e.getMessage(), module);
      return null;
    }
  }
   
   public List loadRelatedSectionQueryFields()
    {
    try
    {
      List relatedFields = getDelegator().findByAnd("UiRelatedSectionQueryFields",
          UtilMisc.toMap("sectionId", getDetailSectionId(), "useInAdvancedQueries", "Y", "isSearchable", "Y"),
          UtilMisc.toList("relatedSectionId", "displayOrder"));
      setRelatedFields(relatedFields);
      return relatedFields;
    } catch (GenericEntityException e)
    {
      Debug.logError("Unable to get Related Screen Fields:" + e.getMessage(), module);
      return null;
    }
  }

 
 
  public String getSelectListAvailableOptions()
  {
    if (detailSectionId == null)
      throw new IllegalArgumentException("section ID must be set before calling getConditionHtml");
     
    StringBuffer displayHtml = new StringBuffer(5000);
    List fieldList = getPrimaryFields();
    int fieldListSize = fieldList.size();
           
    StringBuffer fieldNameOptions = new StringBuffer(200);
//    fieldNameOptions.append("<option value=''></option>");
    for (int fieldNbr = 0; fieldNbr < fieldListSize; fieldNbr++) {
      GenericValue fieldInfo = (GenericValue) fieldList.get(fieldNbr);
      String isVisible = UtilFormatOut.checkNull(fieldInfo.getString("isVisible"));
      if ( isVisible.equals("Y")) {
        String htmlName = UIWebUtility.getHtmlName(fieldInfo.getString("sectionName"), fieldInfo.getString("entityName"), fieldInfo.getString("attributeName"),0);
        String displayObjectId = fieldInfo.getString("displayObjectId");
        String displayTypeId = fieldInfo.getString("displayTypeId");
        String attributeId = fieldInfo.getString("attributeId");
        String displayLabel = fieldInfo.getString("displayLabel");
        String optValue = htmlName + ";" + attributeId + ";" + displayTypeId + ";" + displayObjectId + ";" + displayLabel;
        fieldNameOptions.append("<option value='" + optValue + "'>" + displayLabel + "</option>")
      }
 
    }
 
    List extendedFields = getRelatedFields();
    if ( extendedFields != null )
    {
      int numFields = extendedFields.size();
      for ( int i=0; i < numFields; i++)
      {
        GenericValue fieldInfo = (GenericValue) extendedFields.get(i);
        String entityName = fieldInfo.getString("entityName");
        String attributeId = fieldInfo.getString("attributeId");
        String attributeName = fieldInfo.getString("attributeName");
        String displayObjectId = fieldInfo.getString("displayObjectId");
        String displayTypeId = fieldInfo.getString("displayTypeId");
        String sectionName = fieldInfo.getString("sectionName");
        String sectionDescription = fieldInfo.getString("sectionDescription");
        String displayLabel = fieldInfo.getString("displayLabel");
        String htmlName = UIWebUtility.getHtmlName( sectionName, entityName, attributeName, 0 );
        String optValue = htmlName + ";" + attributeId + ";" + displayTypeId + ";" + displayObjectId + ";" + displayLabel;
        displayLabel = sectionDescription + "." + displayLabel;
        fieldNameOptions.append("<option value='" + optValue + "'>" + displayLabel + "</option>")
      }
    }

    return fieldNameOptions.toString();
  }
 

  public boolean loadQueryInfo(HttpServletRequest request, HttpServletResponse response, UICache uiCache)
  {
   
    try
    {
      List primaryFields = loadPrimarySectionFields();
     
      List uiScreenSectionEntityGVL = delegator.findByAnd("UiScreenSectionEntity",
            UtilMisc.toMap("sectionId", detailSectionId), UtilMisc.toList("retrieveOrder"));
                 
      UIScreenSectionEntity primaryEntity = new UIScreenSectionEntity((GenericValue) uiScreenSectionEntityGVL.get(0), delegator, uiCache);
      GenericValue screenSectionGV = delegator.findByPrimaryKey("UiScreenSection", UtilMisc.toMap("sectionId", detailSectionId));
      String eventProcessorClass = screenSectionGV.getString("eventProcessorClass");
      eventProcessor = UIUtility.getEventProcessor(eventProcessorClass);
     
      String primaryEntityName = primaryEntity.getUiEntity().getEntityName();
      String searchAttribValue = "";
      String searchEntityName = "";
      String searchAttribName = "";
      EntityComparisonOperator entityOperator = null;
      List relatedSearchClauses = new LinkedList();
      List selectFields = new ArrayList();
      List selectFieldLabels = new ArrayList();
      List sortFields = new ArrayList();
     
      List reportFields = new ArrayList();
      List reportCriteria = new ArrayList();
      List reportOrderBy = new ArrayList();
     
      ModelEntity primaryME = primaryEntity.getUiEntity().getModelEntity();
      List primaryPkFieldNames =primaryEntity.getUiEntity().getPrimaryKeyFieldNames();
     
      queryInfo = new QueryInfo(delegator, primaryEntityName);
     
      HashMap joinedEntities = new HashMap();
         
      joinedEntities.put(primaryEntityName, "Y");
         
      // Find the main entity(ies) by building a WHERE clause using LIKE and AND, with one or more tables in the FROM clause.
      // First join all secondary screen section entities in the WHERE and FROM clauses in case
      // we are going to use query values from them.
      Iterator uiScreenSectionEntityI = uiScreenSectionEntityGVL.iterator();
     
      uiScreenSectionEntityI.next(); // Pass up the primary entity.
     
      while (uiScreenSectionEntityI.hasNext()) {
        GenericValue entityGV = (GenericValue) uiScreenSectionEntityI.next();
        UIScreenSectionEntity uiScreenSectionEntity = new UIScreenSectionEntity(entityGV, delegator, uiCache) ;
        String relationTitle = uiScreenSectionEntity.getRelationTitle();
        String relatedEntityName = uiScreenSectionEntity.getUiEntity().getEntityName();
        String relatedAndFields = uiScreenSectionEntity.getRelationByAndFields();
        boolean isOuterJoin = uiScreenSectionEntity.getIsOuterJoined();
     
        eventProcessor.addOneRelationClause(delegator, relationTitle, relatedAndFields,
          relatedEntityName, primaryEntityName, primaryME, isOuterJoin,
          queryInfo);
             
        joinedEntities.put(relatedEntityName, "Y");
      }

      String pkFieldName = (String) primaryPkFieldNames.get(0);
      queryInfo.addJoin(primaryEntityName, "UiListItem", Boolean.FALSE, pkFieldName, "entityId");
      queryInfo.addCondition("UiListItem", "listId", EntityOperator.EQUALS, getListId());
     
      List fieldList = getPrimaryFields();
      int fieldListSize = fieldList.size();
             
      StringBuffer fieldNameOptions = new StringBuffer(200);
      for (int fieldNbr = 0; fieldNbr < fieldListSize; fieldNbr++) {
        GenericValue fieldInfo = (GenericValue) fieldList.get(fieldNbr);
        String entityName = fieldInfo.getString("entityName");
        String attributeName = fieldInfo.getString("attributeName");
        String aliasName = entityName + "_" + attributeName;
       
        if ( !entityName.equals(primaryEntityName))
            queryInfo.checkAttribute(entityName, attributeName, aliasName);
      }   
     
    } catch (NumberFormatException e)
    {
      Debug.logError("Error formatting Number: " + e.getMessage(), module);
      return false;
    } catch (GenericEntityException e)
    {
      Debug.logError("Error loading QueryInfo: " + e.getMessage(), module);
      return false;
    }
    return true;
  }
 
  public List getListItems(UserInfo userInfo, int maxRows)
  {
    EntityListIterator eli = null;
   
    if ( eventProcessor == null)
      eventProcessor = new GenericEventProcessor();
   
    if ( queryInfo == null )
      throw new RuntimeException("You must call loadQueryInfo prior to getListItems");
   
    SecurityLinkInfo securityInfo = eventProcessor.getSecurityLinkInfo(userInfo, delegator);
    if (securityInfo == null) {
      try {
        eli = queryInfo.getQueryIterator();
      } catch (GenericEntityException e) {
        Debug.logError("An error occurred in the " +
          "GenericDelegator.findByCondition method: " +
          e.getLocalizedMessage(), module);
      }
    } else {
      try {
       
        eli = SecurityWrapper.findListIteratorByCondition(queryInfo.getPrimaryEntity(),
            queryInfo, queryInfo.getOrderBy(), userInfo, securityInfo,  delegator);
      } catch (GenericEntityException e) {
        Debug.logError("An error occurred in the " +
          "SecurityWrapper.findByCondition method: " +
          e.getLocalizedMessage(), module);
      }
    }

    if ( eli != null )
    {
      try {
        List resultGVL = eli.getPartialList( 1, maxRows);
       
        boolean hasMoreRows = false;
       
        // check next value since we close the cursor anyway. 
        if ( (resultGVL.size() == maxRows) && (eli.next() != null ))
          hasMoreRows = true;
         
        eli.close();
       
        return resultGVL;

      } catch (GenericEntityException e) {
        Debug.logError("An error occurred in the  getPartialList method: " + e.getLocalizedMessage(), module);
      }
    }

    return new ArrayList();
  }
 
   public GenericDelegator getDelegator()
   {
     return delegator;
   }
  
   public void setDelegator(GenericDelegator delegator_)
   {
     delegator = delegator_;
   }

  /**
   * @return
   */
  public String getPartyId()
  {
    return partyId;
  }

  /**
   * @return
   */
  public List getPrimaryFields()
  {
    if ( !primaryFieldsLoaded )
      loadPrimarySectionFields();
    return primaryFields;
  }

  /**
   * @return
   */
  public List getRelatedFields()
  {
    if ( !relatedFieldsLoaded )
      loadRelatedSectionQueryFields();
    return relatedFields;
  }

  /**
   * @return
   */
  public String getListId()
  {
    return listId;
  }

  /**
   * @return
   */
  public String getListName()
  {
    return listName;
  }

  /**
   * @param string
   */
  public void setPartyId(String string)
  {
    partyId = string;
  }

  /**
   * @param list
   */
  public void setPrimaryFields(List list)
  {
    primaryFields = list;
    primaryFieldsLoaded = true;
  }

  /**
   * @param list
   */
  public void setRelatedFields(List list)
  {
    relatedFields = list;
    relatedFieldsLoaded = true;
  }

  /**
   * @param string
   */
  public void setListId(String string)
  {
    listId = string;
  }

  /**
   * @param string
   */
  public void setListName(String string)
  {
    listName = string;
  }

  /**
   * @return
   */
  public List getReportCriteria()
  {
    return reportCriteria;
  }

  /**
   * @return
   */
  public List getReportFields()
  {
    return reportFields;
  }

  /**
   * @return
   */
  public List getReportOrderBy()
  {
    return reportOrderBy;
  }

  /**
   * @return
   */
  public String getPrimaryEntityName()
  {
      if ( queryInfo == null )
          return null;
    return queryInfo.getPrimaryEntity();
  }

  /**
   * @param list
   */
  public void setReportCriteria(List list)
  {
    reportCriteria = list;
  }

  /**
   * @param list
   */
  public void setReportFields(List list)
  {
    reportFields = list;
  }

  /**
   * @param list
   */
  public void setReportOrderBy(List list)
  {
    reportOrderBy = list;
  }

  protected List getStringListfromGVL( List gvl, String fieldName)
  {
    List sList = new ArrayList();
   
    Iterator iter = gvl.iterator();
    while (iter.hasNext())
    {
      GenericValue gv = (GenericValue) iter.next();
      sList.add(gv.getString(fieldName))
    }
    return sList;
  }
 
  /**
   * @return
   */
  public List getSelectFieldLabels()
  {
    return selectFieldLabels;
  }

  /**
   * @return
   */
  public List getSelectFieldNames()
  {
    return selectFieldNames;
  }

  /**
   * @param list
   */
  public void setSelectFieldLabels(List list)
  {
    selectFieldLabels = list;
  }

  /**
   * @param list
   */
  public void setSelectFieldNames(List list)
  {
    selectFieldNames = list;
  }

    /**
     * @return Returns the listType.
     */
    public String getListType() {
        return listType;
    }
    /**
     * @param listType The listType to set.
     */
    public void setListType(String listType) {
        this.listType = listType;
    }
    /**
     * @return Returns the queryId.
     */
    public String getQueryId() {
        return queryId;
    }
    /**
     * @param queryId The queryId to set.
     */
    public void setQueryId(String queryId) {
        this.queryId = queryId;
    }
}
TOP

Related Classes of com.sourcetap.sfa.ui.UIList

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.