<!--
 * BIWriteback.jsp
 *
 * Copyright © 1994-2004, Oracle. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 -->
<%@ taglib uri="http://xmlns.oracle.com/bibeans/jsp" prefix="orabi" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page errorPage="cabo/bi/jsp/error.jsp" %>
<%@ page contentType="text/html;charset=windows-1252"%>
<%-- Import statements --%>
<%@ page import="java.util.Vector"%>

<%@ page import="oracle.dss.thin.beans.dataView.Presentation"%>
<%@ page import="oracle.dss.thin.beans.crosstab.ThinCrosstab"%>
<%@ page import="oracle.dss.dataSource.common.Query"%>
<%@ page import="samples.jsp.WritebackHelper"%>

<%-- Constants --%>
<!-- The ID of the writeback validator OLAP DML program -->
<%! public static final String VALIDATOR_ID = "CS_OLAP.UTILS!WBVALIDATOR"; %>
    
<jsp:useBean id="writebackHelper" scope="session" class="samples.jsp.WritebackHelper"></jsp:useBean>
<!-- Start synchronization of the BI tags -->
<% synchronized(session){ %>
<orabi:BIThinSession id="BIThinSession1"  configuration="/jspOLAPConfig1.xml" >
    <!--  Business Intelligence definition tags here -->
    <orabi:Presentation imageHeight="300" imageWidth="300" location="WhatIfGraph" id="BIWriteback_pres1"/>
    <orabi:Presentation linkedQuery="BIWriteback_pres1" newViewType="Crosstab" id="BIWriteback_pres2"/>
</orabi:BIThinSession>
<%  
    // Retrieve the crossab from the page context
    Object presentation = pageContext.findAttribute("BIWriteback_pres2");
    if (presentation instanceof Presentation)
    {
      // Get the reference to the thin crosstab in this presentation
      ThinCrosstab crosstab = (ThinCrosstab)((Presentation)presentation).getView(); 
      // Get the DataSource of this crosstab
      Query query = (Query)crosstab.getDataSource();
    
      if (!writebackHelper.isInitialized())
      {
        // If this is the first time load, initialize.
        writebackHelper.setInitialized(true);
        crosstab.setHTMLColumnBlockSize(0);
        
        // Register an OLAP DML program as a value validator. This program will be 
        // called by the writeback engine and validate each overritten QDR.
        // The OLAP DML program name is: CS_OLAP.UTILS!WBVALIDATOR
        //query.setWritebackValidator(VALIDATOR_ID);
        
        // Register a WritebackHelper as a QueryListener so that 
        // we can handle errors upon unsuccessful writeback.
        query.addQueryListener(writebackHelper);
        
        // Create rules so that only Sales Revenue and Costs are editable
        writebackHelper.createCellEditingRules(crosstab);
      }

      // ----- Process the SUBMIT_WRITE_BACK event -----
      // This part handles the event generated when the user 
      // clicked on the "Submit Changes" button.
      String event = request.getParameter("event");
      if (event != null && event.equals("SUBMIT_WRITE_BACK"))
      {
          try
          {
              // The edited data cells will be passed to the writeback engine as a list of QDRoverride objects.
              crosstab.getModel().getDataAccess().submitChanges();
              
              // Create rules to display red font for the cells that did not succeed
              writebackHelper.updateCellFormattingRules(crosstab);
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
    }
%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>
      BI Beans Writeback Sample
    </title>
  </head>

  <orabi:BIBody>
    <form name="BIForm" method="POST"  action="BIWriteback.jsp">
      <orabi:InitBITags parentForm="BIForm"/>
      <!-- Insert your Business Intelligence tags here -->
      <orabi:Render targetId="BIWriteback_pres1" parentForm="BIForm"/>
      
      <!-- In case of error, retrieve the error messages from the WritebackHelper.
           Display the error messages above the crosstab. -->
          
      <p><font color="Red"><b><%= writebackHelper.getErrorMessages() %></b></font></p>
          
      <P>
        <orabi:Render targetId="BIWriteback_pres2" parentForm="BIForm"/>
      </P>
      <P>
        <orabi:Button onClick="submitForm(0, 0, {'event':'SUBMIT_WRITE_BACK'});" text="Submit Changes"/>
      </P>

      <!-- The InsertHiddenFields tag adds state fields to the parent form tag -->
      <orabi:InsertHiddenFields parentForm="BIForm"  biThinSessionId="BIThinSession1" />
    </form>

  </orabi:BIBody>
</html>
<% } %>
<!-- End synchronization of the BI tags -->
