package com.k_int.it4me;

//import java.io.FileOutputStream;
import java.sql.*;
import java.util.Date;
import java.util.Properties;
import com.k_int.ia.data_upload.dto.*;
import com.k_int.ia.data_upload.ConfigPair;
import com.k_int.ia.data_upload.ConfigTypes;
import com.k_int.ia.data_upload.DataPublishController;
import com.k_int.ia.data_upload.UploadStatus;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class RotherhamHelpingHandPublisher extends com.k_int.ia.data_upload.ThreadedDataPublisher {

  static final String GET_ORG_DETS_SQL = "Select ORGANISATION, DESCRIPTION, ACTIVITIES, Corresp_Address_Line_1,Corresp_Address_Line_2,Corresp_Address_Line_3,Corresp_Address_Line_4, Corresp_Post_Code, Corresp_Tel_No, Subject from dbo_RothFile";

  /**static final String[] config_fields = { "name", 
                                          "publishURL",
                                          "ODBCDSNName",
                                          "targetCollection", 
                                          "publisherIdentity", 
                                          "publisherCredentials"};
  **/
static ConfigPair[] config_fields; //= new ArrayList();
  
  static
  {
      config_fields = new ConfigPair[6];
      ConfigPair pair = new ConfigPair();
      pair.setFieldName("name");
      pair.setFieldType(ConfigTypes.TEXTFIELD);    
      config_fields[0]=pair;
     
      pair = new ConfigPair();
      pair.setFieldName("publishURL");
      pair.setFieldType(ConfigTypes.TEXTAREA);
      config_fields[1]=pair;
      
      pair = new ConfigPair();
      pair.setFieldName("ODBCDSNName");
      pair.setFieldType(ConfigTypes.TEXTFIELD);
      config_fields[2]=pair;
      
      pair = new ConfigPair();
      pair.setFieldName("targetCollection");
      pair.setFieldType(ConfigTypes.TEXTFIELD);
      config_fields[3]=pair;
      
      pair = new ConfigPair();
      pair.setFieldName("publisherIdentity");
      pair.setFieldType(ConfigTypes.TEXTFIELD);
      config_fields[4]=pair;
      
      pair = new ConfigPair();
      pair.setFieldName("publisherCredentials");
      pair.setFieldType(ConfigTypes.PASSWORD);
      config_fields[5]=pair;
     
  }
  public static Log log = LogFactory.getLog(HelpYourselfPublisher.class);

  private String targetCollection = "RHH";
  private String odbc_dsn_name="jdbc:odbc:Rotherham";
  private String jdbc_driver_name = "sun.jdbc.odbc.JdbcOdbcDriver";
 
  private transient Connection conn = null;
  private transient PreparedStatement get_org_details_ps = null;

 
  
  public void run() {
    log.debug("Starting run");

    long start_date = 0;
    try {
      Class driver = Class.forName(jdbc_driver_name);
      conn = DriverManager.getConnection(odbc_dsn_name);
      get_org_details_ps = conn.prepareStatement(GET_ORG_DETS_SQL);

      if ( conn != null ) { 
        try {
          log.debug(Messages.getString("HelpYourselfPublisher.21")+new Date(start_date)); //$NON-NLS-1$
  
          get_org_details_ps.clearParameters();
          // get_org_details_ps.setDate(1,new java.sql.Date(start_date));
          ResultSet rs =  get_org_details_ps.executeQuery();
          while (rs.next()) {
            String organisation = rs.getString(1);
            String description = rs.getString(2);
            String activities = rs.getString(3);
            String addr_line_1 = rs.getString(4);
            String addr_line_2 = rs.getString(5);
            String addr_line_3 = rs.getString(6);
            String addr_line_4 = rs.getString(7);
            String postcode = rs.getString(8);
            String telno = rs.getString(9);
            String subject = rs.getString(10);
            
            String default_authority = "HelpingHandRotherham-UK";
            log.debug(Messages.getString("HelpYourselfPublisher.22")+organisation); //$NON-NLS-1$
            ResourceDTO result = new ResourceDTO();
            result.setIdentifier("HelpingHandRotherham-UK:"+organisation);
            result.setTitle(organisation);
            result.setDescription(description+"\n"+activities);
            result.getSubjects().add(new SubjectDTO(default_authority,subject));
            AvailabilityDTO availability = new AvailabilityDTO();
            availability.getAddresses().add(new AddressDTO("Corresp", addr_line_1, addr_line_2, addr_line_4, addr_line_4,null,null,postcode,null));
            availability.getContacts().add(new ContactDTO(null, telno, null, null, null, null));
            result.getAvailability().add(availability);
   
            log.debug("Start Upload");
            controller.upload(result,
                              "HelpingHandRotherham-UK:"+organisation,
                              "Rotherham Helping Hand",  // Record Source
                              "RIS",                      // Publisher
                              "RIS-Credentials",          // Publisher Credentials
                              getPublishURL(),
                              getTargetCollection(),
                              default_authority);
            log.debug("Done Upload");
            update_count++;
          }
          setStatus(UploadStatus.COMPLETED_OK);
          rs.close();
          get_org_details_ps.close();
          conn.close();
          log.debug(Messages.getString("HelpYourselfPublisher.23")); //$NON-NLS-1$
        } catch ( com.k_int.ia.data_upload.DataPublishException dpe ) {
          dpe.printStackTrace();
        } catch (SQLException sqle) {
          log.debug(Messages.getString("HelpYourselfPublisher.24")); //$NON-NLS-1$
          sqle.printStackTrace();
        }
      }
      else {
        log.debug(Messages.getString("HelpYourselfPublisher.25")); //$NON-NLS-1$
      }
    } catch (ClassNotFoundException cnfe) {
      log.debug(Messages.getString("HelpYourselfPublisher.19")); //$NON-NLS-1$
      cnfe.printStackTrace();
    } catch (SQLException sqle) {
      log.debug(Messages.getString("HelpYourselfPublisher.20")); //$NON-NLS-1$
      sqle.printStackTrace();
    }
    finally {
      try {
        get_org_details_ps.close();
        conn.close();
      }
      catch ( Exception e ) {
        e.printStackTrace();
      }
    }

   // setStatus(Messages.getString("HelpYourselfPublisher.26")); //$NON-NLS-1$
    if(this.getStatus()==UploadStatus.BUSY)
        this.setStatus(UploadStatus.FAILED);
    running = false;
    controller.notifyComplete(this);
    log.debug("Completed run");
  }
 
  public String getODBCDSNName() {
    return odbc_dsn_name;
  }
 
  public void setODBCDSNName(String odbc_dsn_name) {
    this.odbc_dsn_name = odbc_dsn_name;    
  }
 
  public com.k_int.ia.codbif.xdevicegui.defs.XDLayoutHDO getLayout() {
    return new com.k_int.ia.codbif.xdevicegui.defs.XDLayoutHDO(
        "IT For Me Publisher",
        "com.k_int.it4me.messages",
        new com.k_int.ia.codbif.xdevicegui.defs.XDNameValuePairPanel(config_fields));
  }
  public String getTargetCollection() {
    return targetCollection;
  }
  public void setTargetCollection(String targetCollection) {
    this.targetCollection = targetCollection;
  }

}
