
/* $Header: $ */
/* $Source: $ */
/* $Author: $ */

#ifndef lint
static char rcs_id[] = "$Header: $";
#endif lint

#include <stdio.h>
#include "PDsrvr.h"

#define ADDCHPAIR(LHS,RHS) \
    if ((RHS != NULL) && (strlen(RHS) > 0)) \
         if (add_char_pair(pairs, LHS, savestr(RHS), listsize) == FALSE) \
              return(NOMEMORY)

#define ADDINTPAIR(LHS,RHS)\
  if (add_int_pair(pairs, LHS, RHS, listsize) == FALSE) \
              return(NOMEMORY)

int
build_job_entry (pairs, listsize, jobId, time, jobInfo, resultHandling,
		 eventHandling, schedInstr, printerId, prodInstr,
		 docDescr, specProc, security)
     value_list *pairs;
     int *listsize;
     LongCardinal time;
     String jobId;
     PDif_JobInformation *jobInfo;
     PDif_JobResultsHandling *resultHandling;
     PDif_JobEventHandling *eventHandling;
     PDif_JobSchedulingInstructions *schedInstr;
     PDif_PrinterIdentification *printerId;
     PDif_ProductionInstructions *prodInstr;
     PDif_DocumentDescription *docDescr;
     PDif_SpecialProcessingInstructions *specProc;
     PDif_SecurityAccessAccounting *security;
     
{
  int error;

  new_int_pair(pairs, CURRENTJOBSTATE, Pending, listsize);
  add_int_pair(pairs, PREVIOUSJOBSTATE, Transit, listsize);
  ADDINTPAIR(SUBMISSIONTIME, time);
  ADDCHPAIR(JOBID, jobId);
  ADDCHPAIR(JOBTITLE, jobInfo->jobTitle);
  ADDCHPAIR(JOBORIGINATOR, jobInfo->jobOriginator);
  ADDCHPAIR(JOBCOMMENT, jobInfo->jobComment);
  ADDCHPAIR(JOBSTARTMSG, jobInfo->jobStartMessage);
  ADDCHPAIR(JOBENDMSG, jobInfo->jobEndMessage);
  ADDCHPAIR(JOBSTARTSHEET, jobInfo->jobStartSheet);
  ADDCHPAIR(JOBSEPSHEET, jobInfo->jobSeparatorSheet);
  ADDCHPAIR(JOBENDSHEET, jobInfo->jobEndSheet);

  ADDCHPAIR(DELIVERYMETHOD, resultHandling->deliveryMethod);
  ADDCHPAIR(DELIVERYNAME, resultHandling->recipientName);
  ADDCHPAIR(DELIVERYADDR, resultHandling->recipientAddress);
  add_event_profile(&(eventHandling->notificationProfile.events),
		    NOTIFYTRIGS, pairs, listsize);
  ADDCHPAIR(NOTIFYMETHOD,
	    eventHandling->notificationProfile.deliveryMethod);
  ADDCHPAIR(NOTIFYNAME,
	    eventHandling->notificationProfile.deliveryName);
  ADDCHPAIR(NOTIFYADDRESS,
	    eventHandling->notificationProfile.deliveryAddress);

  /* 
   * Event handling needs to be done
   *   EVENTS
   *   NOTIFYTRIGS
   *   NOTIFYMETHOD
   *   NOTIFYNAME
   *   NOTIFYADDRESS
   *   NOTIFYEXTENSIONS
   */

  ADDINTPAIR(JOBSTARTWAIT, schedInstr->jobStartWait);
  ADDINTPAIR(JOBENDWAIT, schedInstr->jobEndWait);
  ADDINTPAIR(JOBHOLD, schedInstr->jobHold);
  ADDCHPAIR(JOBPASSWD, schedInstr->jobPassword);
  ADDINTPAIR(JOBPRINTAFTER, schedInstr->jobPrintAfter);
  ADDINTPAIR(JOBPRIORITY, schedInstr->jobPriority);
  ADDINTPAIR(JOBDEADLINE, schedInstr->jobDeadlineTime);
  ADDINTPAIR(JOBRETENTION, schedInstr->jobRetentionTime);

  ADDCHPAIR(PRINTERNAME, printerId->printerName);
  ADDCHPAIR(PRINTERLOCATION, printerId->printerLocation);
  ADDCHPAIR(PRINTERMODEL, printerId->printerModel);
  ADDCHPAIR(PRINTERTYPE, printerId->printerType);
  ADDCHPAIR(PRINTERSECURITY, printerId->printerSecurity);
  ADDINTPAIR(PRINTERSPEED, printerId->printerSpeed);
  ADDINTPAIR(PRINTERRES, printerId->printerResolution);

  /* Substitutions are pain-in-the-???? sequences - we'll
     do these later */
  /*
   * FONTSUBS
   * MEDIASUBS
   * INKSUBS
   */
  /* Egads - so are all of these. Sigh.
     FINISHSPECS
     OUTPUTSPECS
     LAYUPSPECS */
  /*
   * Pradeep
   * Added code to add LAYUP, FINISHING and OUTPUT as a string rather
   * than a sequence. We just take the first element of the sequence.
   */
  if ((prodInstr->finishing).length != 0)
    ADDCHPAIR(FINISHSPECS,(prodInstr->finishing).sequence[0]);
  if ((prodInstr->output).length != 0)
    ADDCHPAIR(OUTPUTSPECS,(prodInstr->output).sequence[0]);
  if ((prodInstr->layup).length != 0)
    ADDCHPAIR(LAYUP,(prodInstr->layup).sequence[0]);
  ADDCHPAIR(PRINTQUALITY, prodInstr->printQuality);
  ADDCHPAIR(SIDES , prodInstr->sides);
  if ((error = add_page_Id(pairs, listsize, STARTINGPAGE,
			   &(prodInstr->pageSelect))) != NOERROR)
    return(error);
  if ((error = add_page_Id(pairs, listsize, ENDINGPAGE,
			   &(prodInstr->pageSelect))) != NOERROR)
    return(error);
  ADDINTPAIR(COPYCOUNT, prodInstr->copyCount);

/* Document Description */

  /* Sequences from hell!!
     PRINTFORMATS
     CHARSETSUSED
     CHARMAPSUSED
     FONTSUSED
     CHARREPSUSED
     MEDIAUSED
     INKSUSED */

  ADDINTPAIR(ASSUREDREPRODAREAX,
	     docDescr->documentProfile.assuredReproduction.x);
  ADDINTPAIR(ASSUREDREPRODAREAY,
	     docDescr->documentProfile.assuredReproduction.y);
  ADDCHPAIR(CONTENTORIENTATION,
	    docDescr->documentProfile.contentOrientation);
  ADDCHPAIR(PAGEORDER,
	    docDescr->documentProfile.page_order);
  ADDINTPAIR(PAGEINDEP,
	     docDescr->documentProfile.page_independent);

  ADDCHPAIR(DOCTITLE,
	    docDescr->documentUsersId.documentTitle);
  ADDCHPAIR(DOCAUTHOR,
	    docDescr->documentUsersId.documentAuthor);
  ADDCHPAIR(DOCCOMMENT,
	    docDescr->documentUsersId.documentComment);
  ADDINTPAIR(DOCREVDATE,
	     docDescr->documentUsersId.documentRevisionDate);
  ADDCHPAIR(XFERMETHOD, docDescr->transferMethod);
  ADDCHPAIR(DOCFORMAT, docDescr->documentFormat);

  /* Special Processing Instructions */

  ADDCHPAIR(LOGICALSERVICE, specProc->logicalService);

  /* Security-Access-Accounting */

  ADDCHPAIR(USERNAME, security->userName);
  /* The next two are block of characters, which were used at the time
     user was authorized */
  
  ADDINTPAIR(PAGESUSED, 0);
  ADDINTPAIR(SECONDSUSED, 0);
  ADDINTPAIR(PAGESCOMPLETED, 0);
  ADDINTPAIR(COPIESCOMPLETED, 0);
  ADDINTPAIR(INTERVENINGOCTETS, 0);
  ADDINTPAIR(INTERVENINGJOBS, 0);
  return(NOERROR);
}

add_page_Id(pairs, listsize, lhs, pageId)
     value_list *pairs;
     int *listsize;
     char *lhs;
     PDif_PageIdentifier *pageId;
{
  switch (pageId->designator) {
  case nominalPageNumber:
    ADDINTPAIR(lhs,pageId->nominalPageNumber_case);
    break;
  case ordinalPageNumber:
    ADDINTPAIR(lhs, pageId->ordinalPageNumber_case);
    break;
  case alphanumericPageNumber:
    ADDINTPAIR(lhs, pageId->alphanumericPageNumber_case);
    break;
  case relativePageNumber:
    ADDINTPAIR(lhs, pageId->relativePageNumber_case);
    break;
  case searchString:
    ADDCHPAIR(lhs, pageId->searchString_case);
    break;
  case sectionNumber:
    ADDCHPAIR(lhs, pageId->sectionNumber_case);
    break;
  }
  return(NOERROR);
}

add_event_profile(ep, lhs, list, size)
     PDif_EventsProfile *ep;
     String lhs;
     value_list *list;
     int *size;
{
  int i;
  char rhs[256];
  String *ptr;

  if (ep->events.length == 0)
    return;
  ptr = ep->events.sequence;

  for (i=0; i < ep->events.length; i++) {
    if (i==0)
      strcpy(rhs,"(");
    else
      strcat(rhs,",");
    strcat(rhs,*ptr);
    *ptr++;
  }

  strcat(rhs,")");
  add_char_pair(list, lhs, savestr(rhs), size);
  return;
}
