title: JSF : ManagedBean, Good place to deal with Business Logic?
body: <p>I have <code>managedBean</code> for <code>fileUpload</code>, once file is uploaded then i need to call different parsers based on what value is selected from the parser dropdown and then in parser am creating object of <code>DetailsClass</code> where am calling <code>getDetails</code> method for that particular class, thing to note here is that neither <code>parserClass</code> nor <code>DetailsClass</code> is registered in faces-config.xml, my question here is </p>
  
  <ul>
  <li>If i want to maintain session information from <code>FileUpload</code> class to <code>Parser</code> class to <code>DetailsClass</code> then I should be defining it in <code>faces-config.xml</code> but how should <code>parser</code> class and <code>DetailsClass</code> be defined, should it be defined as <code>managedBean</code> or like something else?</li>
  </ul>
  
  <p>Here is the code:</p>
  
  <p>In my managedBean class i have two function, <code>fileUpload</code> and <code>callParser</code> as shown:</p>
  
  <pre><code> public void uploadFile(FileEntryEvent event)
  {
      FacesContext ctx = FacesContext.getCurrentInstance();
      //Setting getSession to false, container will not create new session if session is not present and return null
      HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
      setSession(session);
  
      resultBean = new ResultBean();
      FileEntry fileEntry = (FileEntry) event.getSource();
      FileEntryResults results = fileEntry.getResults();
      FileEntry fe = (FileEntry) event.getComponent();
  
      FacesMessage msg = new FacesMessage();
      for (FileEntryResults.FileInfo fileInfo : results.getFiles())
      {
          if (fileInfo.isSaved())
          {
              File file = fileInfo.getFile();
              String filePath = file.getAbsolutePath();
              callParser(selectedItem, filePath);
          }
          resultBeanList.add(resultBean);
      }
  }
  
  private void callParser(String parserType, String filePath)
  {
      if ("Delta".equals(parserType))
      {
          PositionParserDelta deltaParser = new PositionParserDelta();
          deltaParser.getQuotes(filePath);
      }
      else if ("Gamma".equals(parserType))
      {
          PositionParserGamma gammaParser = new PositionParserGamma();
          gammaParser.getQuotes(filePath);
      }
  }
  </code></pre>
  
  <p>Now, let say we consider <code>Delta Parser</code>, so in that class I have something like:</p>
  
  <pre><code>public class PositionParserDelta extends Base
  {
      List&lt;String[]&gt; dataList = new ArrayList&lt;String[]&gt;();
      ContractManager contractManager = new ContractManager();
  
      public PositionParserDelta()
      {
      }
  
      public void getQuotes(String fileName)
      {
          try
          {
              Map&lt;String, ContractSeries&gt; gsLookup = contractManager.getContractsMap(ContractManager.VendorQuotes.KRT);
              CSVReader reader = new CSVReader(new FileReader(fileName), '\t');
              String[] header = reader.readNext();
              dataList = reader.readAll();
              List&lt;Trade&gt; tradeBeanList = new ArrayList&lt;Trade&gt;();
              for (String[] data : dataList)
              {
                  //Some Business Logic
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>My <code>contractManager</code> Class looks like</p>
  
  <pre><code>    public class ContractManager extends Base
      {
          private List&lt;Series&gt; List = new ArrayList&lt;Series&gt;();
          private ContractSeries[] SeriesList = new Series[3];
          private ListingOps listingOps;
  
  //This line throws error as faces class not found and it makes sense because am not registering this with faces-config.xml
          HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
  
          public List&lt;ContractSeries&gt; getContracts(long orgId, HttpSession session)
          {
              log.info("Inside getContracts method call");
              if (false)
              {
                  //Some Logic
              }
              else
              {
                  try
                  {
                      //Set session and get initialContext to retrieve contractSeries data from ejb calls
                      log.info("Trying to get allContractSeries data from listingOpsBean");
                      Series[] allSeries = deltaOps.findAllSeries(true);
                      Collections.addAll(contractList, allContractSeries);
                  }
                  catch (Exception ex)
                  {
  
                  }
              }
              return contractList;
          }
  
          public Map&lt;String, ContractSeries&gt; getContractsMap(VendorQuotes vendorQuotes)
          { //Some Logic
      }
  </code></pre>
  
  <p>But in faces-config.xml file, </p>
  
  <pre><code> &lt;managed-bean&gt;
          &lt;managed-bean-name&gt;fileUpload&lt;/managed-bean-name&gt;
          &lt;managed-bean-class&gt;trade.UploadBlotterBean&lt;/managed-bean-class&gt;
          &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;
  &lt;/managed-bean&gt;
  </code></pre>
  
  <p>So basically my question is, </p>
  
  <blockquote>
    <p>if suppose am calling other classes from <code>managedBean</code> then how should
    they be defined in <code>faces-config.xml</code> and since am new to <code>JSF</code>, is
    calling other classes from <code>managedBean</code> and having some business
    logic in those classes considered good practice?</p>
  </blockquote>
  
  <p>Also i need to make sure that i maintain session that i obtain in <code>UploadFile</code> across <code>Parser</code> and <code>ContractMapping</code> class.</p>
  
  <p>Also, </p>
  
  <blockquote>
    <p>Is everything is 'registered' as managed-bean in faces-config ?</p>
  </blockquote>