package izyndoc;

import com.jacob.com.*;

/**
 * <p>Title: CreateReportFromTemplate</p>
 * <p>Description:  Open a template assign values to its custom properties,
 * save as a document in the client folder
 * and reopen the document with Word visible.</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Izyn Technologies</p>
 * @author Richard Norton
 * @version 1.0
 */

public class CreateReportFromTemplate
{
  private Error anError = null;
  private Word wordApp = null;
  private WordDocuments wordDocs = null;
  private WordDocument wordDoc = null;
  private WordDocCustomProps docCustomProps = null;
  private int docCustomPropsCount = 0;
  private String[] docCustomPropsArray = null;
  private WordDocCustomProperty docCustomProp = null;
  private boolean existsAlready = true;
  protected boolean gotWord = false;

  public CreateReportFromTemplate()
  {
    try
    {
      ComThread.InitSTA();
      //create an instance of Word
      wordApp = new Word();
      gotWord = true;
    }
    catch (ComFailException cfe)
    {
      String[] errorDetails = {"CreateReportFromTemplate"
        , "CreateReportFromTemplate()"
        , Constant.WORDOBJECTFAIL + "\n" + cfe.getMessage()};
      //"Can't get object clsid from progid" means Word not installed,
      //also get "can't co-create object".
      anError = new Error(errorDetails, Constant.WORDOBJECT);
      ComThread.Release();
    }
    catch (UnsatisfiedLinkError ule)
    {
      String[] errorDetails = {"CreateReportFromTemplate"
        , "CreateReportFromTemplate()"
        , Constant.JACOBDLLFAIL + "\n" + ule.getMessage()};
      //"Can't get object clsid from progid" means Word not installed.
      anError = new Error(errorDetails, Constant.WORDOBJECT);
      ComThread.Release();
    }
  }

  /**
   * Add a document created from a Word template (that we know exists)
   * to the documents collection, save it, then show it to user.
   *
   * @param aTemplateName String name of a template
   * @param aTemplatePath String path to template
   * @param aSavePath String path to save document to
   * @return boolean success/failure
   */
  protected boolean addDocument(String aTemplatePath
      , String aTemplateName
      , String aSavePath
      , int clientPos)
  {
    String aFullTemplateNameAndPath = aTemplatePath + aTemplateName;
    boolean docAdded = false;
    int fieldsUpdated = 0;

    try
    {
      //get the documents
      wordDocs = wordApp.getDocuments();

      //add a new document created from the template
      wordDoc = new WordDocument(wordDocs.addDoc(aFullTemplateNameAndPath));

      //add the values to the custom properties
      addCustomPropertiesValues(clientPos);

      //call update so the new values are shown
      fieldsUpdated = wordDoc.updateFields();

      //set 'saved' to false so Word thinks doc is 'dirty'
      wordDoc.dirtyDoc();

      //save document to client folder
      wordDoc.saveAs(aSavePath);

      docAdded = true;

      //did all the fields update properly
      if (fieldsUpdated > 0)
      {
        String[] errorDetails = {"CreateReportFromTemplate", "addDocument()"
          , Constant.WORDUPDATEFIELDSFAIL + fieldsUpdated
          + "\n[Path = " + aSavePath + "]"
          + "\nWord version: " + wordApp.getVersion()};
        anError = new Error(errorDetails, Constant.UPDATEFIELDSFAIL);

        //make Word visible
        wordApp.setVisible(true);

        //set focus to Word
        wordApp.activate();
      }
      else
      {
        //make Word visible
        wordApp.setVisible(true);
      }

      ComThread.Release();

      return docAdded;

      //consultant fills in rest of details
      //and saves file into same path
    }
    catch (ComFailException cfe)
    {
      String[] errorDetails = {"CreateReportFromTemplate", "addDocument()"
        , Constant.WORDDISPATCHFAIL
        + "\n[Path = " + aSavePath + "]"
        + "\nWord version: " + wordApp.getVersion()
        + "\n" + cfe.getMessage()};
      anError = new Error(errorDetails, Constant.DISPATCHFAIL);

      //close Word
      wordApp.quit(new Variant[] {});
      ComThread.Release();

      return docAdded;
    }
  }

  /**
   * Add values to a documents custom properties.
   */
  private void addCustomPropertiesValues(int clientPos)
  {
    //check consultant and client are valid
    if (!(MainUI.consultantPos > Constant.NOSELECTION)
       && !(MainUI.clientPos > Constant.NOSELECTION))
    {
      return;
    }

    ConsultantObj consultant = IzynStart.consArray[MainUI.consultantPos];
    ClientObj client = consultant.clientArray[clientPos];

    //get the CustomDocumentProperties collection
    docCustomProps =
        new WordDocCustomProps(wordDoc.getCustomDocumentProperties());
    //get the CustomDocumentProperties collection count
    docCustomPropsCount = docCustomProps.getCustomPropsCount();

    //go through all custom properties, NOTE: 'item' starts at 1
    for (int docCustomPropsItem = 1;
         docCustomPropsItem <= docCustomPropsCount;
         docCustomPropsItem++)
    {
      WordDocCustomProperty customProperty = new WordDocCustomProperty(
            docCustomProps.getCustomProperty(docCustomPropsItem));

      //fill in client fields from client object
      if (customProperty.getCustomPropertyName()
          .equals("Claim Number"))
      {
        customProperty.setCustomPropertyValue(client.claimNum);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Surname"))
      {
        customProperty.setCustomPropertyValue(client.lastName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Given Name"))
      {
        customProperty.setCustomPropertyValue(client.firstName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Address City"))
      {
        customProperty.setCustomPropertyValue(client.city);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Address Line 1"))
      {
        customProperty.setCustomPropertyValue(client.address1);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Address Line 2"))
      {
        customProperty.setCustomPropertyValue(client.address2);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Telephone One"))
      {
        customProperty.setCustomPropertyValue(client.phone1);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Telephone Two"))
      {
        customProperty.setCustomPropertyValue(client.phone2);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Claimant Date of Birth"))
      {
        customProperty.setCustomPropertyValue(client.dob);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Referring Branch"))
      {
        customProperty.setCustomPropertyValue(client.refBranch);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Case Manager Surname"))
      {
        customProperty.setCustomPropertyValue(client.curCaseMngrLastName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Case Manager Given name"))
      {
        customProperty.setCustomPropertyValue(client.curCaseMngrFirstName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Case Manager Telephone"))
      {
        customProperty.setCustomPropertyValue(client.curCaseMngrPhone);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Case Manager Fax"))
      {
        customProperty.setCustomPropertyValue(client.curCaseMngrFax);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Case Manager Email"))
      {
        customProperty.setCustomPropertyValue(client.curCaseMngrEmail);
      }
      //fill in consultant fields from consultant object
      else if (customProperty.getCustomPropertyName()
               .equals("Consultant Name"))
      {
        customProperty.setCustomPropertyValue(consultant.name);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Consultant Telephone"))
      {
        customProperty.setCustomPropertyValue(consultant.phone);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Consultant Fax"))
      {
        customProperty.setCustomPropertyValue(consultant.fax);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Consultant Email"))
      {
        customProperty.setCustomPropertyValue(consultant.email);
      }
      //fill in branch fields from branch object
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Name"))
      {
        customProperty.setCustomPropertyValue(
            IzynStart.branchData.providerName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Name"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.branchName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Manager Name"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.branchManagerName);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Postal 1"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.postal1);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Postal 2"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.postal2);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Address 1"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.address1);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Address 2"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.address2);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch City"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.city);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Telephone"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.phone);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Fax"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.fax);
      }
      else if (customProperty.getCustomPropertyName()
               .equals("Provider Branch Email"))
      {
        customProperty.setCustomPropertyValue(IzynStart.branchData.email);
      }
    }
  }
}