Package org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl

Source Code of org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.PrinterPortService

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/
package org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl;

import org.apache.ws.resource.ResourceContext;
import org.apache.ws.resource.ResourceException;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.impl.XmlBeansResourceProperty;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySet;
import org.apache.ws.resource.properties.v2004_11.impl.NamespaceVersionHolderImpl;
import org.apache.xmlbeans.XmlBoolean;
import org.apache.xmlbeans.XmlInteger;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreateJobRequestDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreationResponseDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.DocumentContentType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.JobPropertiesDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.JobStateType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.MimeMediaTypeType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.MimeMediaTypes;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrintJobRequestDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.math.BigInteger;
import java.util.Calendar;

/**
* This class should be generated ONCE (and not overwritten) to maintain user-added code.
* If there is a change to the WSDL, then the generated implemented interfaces
* (representing the "base" portTypes) will change, thus showing a compile error to the
* user.
* <p/>
* NOTE: This class is generated. However, it will not be overwritten by subsequent
* calls to the code generator.
*/
public class PrinterPortService
   extends AbstractPrinterPortService
   implements PrinterPortCustomOperationsPortType
{
   private ResourceContext m_resourceContext;

   /**
    * Creates a new {@link PrinterPortService } object.
    *
    * @param resourceContext DOCUMENT_ME
    */
   public PrinterPortService( ResourceContext resourceContext )
   {
      m_resourceContext = resourceContext;
      init(  );
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public ResourceContext getResourceContext(  )
   {
      return m_resourceContext;
   }

   /**
    * DOCUMENT_ME
    *
    * @param requestDoc DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreationResponseDocument Create_Job( org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreateJobRequestDocument requestDoc )
   {
      CreationResponseDocument                  responseDocument = createResponseDocument(  );
      CreationResponseDocument.CreationResponse creationResponse = responseDocument.getCreationResponse(  );
      JobPortResource                           jobResource      = null;

      if ( isAcceptingJobs(  ) )
      {
         try
         {
            Context                                   initialContext   = new InitialContext(  );
            JobPortHome                               home             =
               (JobPortHome) initialContext.lookup( JobPortHome.HOME_LOCATION );
            CreateJobRequestDocument.CreateJobRequest createJobRequest = requestDoc.getCreateJobRequest(  );
            String                                    jobName          = createJobRequest.getJobName(  );
            jobResource = (JobPortResource) home.createJob(
                                                            jobName,
                                                            (PrinterPortResource)getResourceContext().getResource() );
            Calendar currentTime = jobResource.getCurrentTime(  );
            Calendar termTime = Calendar.getInstance(  );
            termTime.set( currentTime.get( Calendar.YEAR ),
                          currentTime.get( Calendar.MONTH ),
                          currentTime.get( Calendar.DATE ),
                          currentTime.get( Calendar.HOUR ),
                          currentTime.get( Calendar.MINUTE ) + 4 );
            jobResource.setTerminationTime( termTime );
            addJobPropsToPrinterProps( jobResource );
            incrementQueueCount(  );
            addJobInfoToResponse( jobResource, creationResponse );
         }
         catch ( Exception e )
         {
            creationResponse.setSuccessStatusCode( "server-error-internal-error" );
         }
      }
      else
      {
         throw new ClientErrorNotPossibleFaultException(new NamespaceVersionHolderImpl(), "Printer Is Not Accepting Jobs.");
      }

      return responseDocument;
   }

   /**
    * DOCUMENT_ME
    *
    * @param requestDoc DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreationResponseDocument Print_Job( org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrintJobRequestDocument requestDoc )
   {
      CreationResponseDocument                  responseDocument = createResponseDocument(  );
      CreationResponseDocument.CreationResponse creationResponse = responseDocument.getCreationResponse(  );
      JobPortResource                           jobResource      = null;
      if ( isAcceptingJobs(  ) )
      {
         try
         {
            Context                                 initialContext  = new InitialContext(  );
            JobPortHome                             home            =
               (JobPortHome) initialContext.lookup( JobPortHome.HOME_LOCATION );
            PrintJobRequestDocument.PrintJobRequest printJobRequest = requestDoc.getPrintJobRequest(  );
            String                                  jobName         = printJobRequest.getJobName(  );

            //check if valid format type
            MimeMediaTypeType.Enum documentFormat = printJobRequest.getDocumentFormat(  );

            if ( isDocTypeSupported( documentFormat ) )
            {
               DocumentContentType documentContent = printJobRequest.getDocumentContent(  );
               byte[]              base64Data = documentContent.getBase64Data(  );
               jobResource =
                  (JobPortResource) home.createJob(
                                                    jobName,
                                                    "",
                                                    documentFormat,
                                                    base64Data,
                                                    (PrinterPortResource)getResourceContext().getResource() );
               addJobPropsToPrinterProps( jobResource );
               incrementQueueCount(  );
               addJobInfoToResponse( jobResource, creationResponse );
               jobResource.enqueue();//add to queue              
            }
            else
            {
               throw new ClientErrorNotPossibleFaultException(new NamespaceVersionHolderImpl(), "Document type is not supported.");
            }
         }
         catch ( Exception e )
         {
            creationResponse.setSuccessStatusCode( "server-error-internal-error" );
         }
      }
      else
      {
        throw new ClientErrorNotPossibleFaultException(new NamespaceVersionHolderImpl(), "Printer is Not Accepting Jobs.");
      }


      return responseDocument;
   }

   private boolean isAcceptingJobs(  )
   {
      PrinterPortResource resource = null;
      try
      {
         resource = (PrinterPortResource) getResourceContext(  ).getResource(  );
      }
      catch ( Exception e )
      {
         throw new ClientErrorNotPossibleFaultException(new NamespaceVersionHolderImpl(), "Problem occurred when retrieving resource: " + e.getLocalizedMessage());
      }

      XmlBeansResourceProperty resourceProperty =
         (XmlBeansResourceProperty) resource.getResourcePropertySet(  ).get( PrinterPortPropertyQNames.PRINTER_IS_ACCEPTING_JOBS );
      XmlBoolean               doc = (XmlBoolean) resourceProperty.get( 0 );
      return doc.getBooleanValue(  );
   }

   private boolean isDocTypeSupported( MimeMediaTypeType.Enum documentFormat )
   throws
          ResourceException
   {
      boolean                  docTypeSupported        = false;
      PrinterPortResource      resource                =
         (PrinterPortResource) getResourceContext(  ).getResource(  );
      XmlBeansResourceProperty resourceProperty        =
         (XmlBeansResourceProperty) resource.getResourcePropertySet(  ).get( PrinterPortPropertyQNames.DOCUMENT_FORMAT_SUPPORTED );
      MimeMediaTypes           documentFormatSupported = (MimeMediaTypes) resourceProperty.get( 0 );
      MimeMediaTypeType.Enum[] mimeMediaTypeArray      = documentFormatSupported.getMimeMediaTypeArray(  );

      for ( int i = 0; i < mimeMediaTypeArray.length; i++ )
      {
         MimeMediaTypeType.Enum enum = mimeMediaTypeArray[i];
         if ( enum.equals( documentFormat ) )
         {
            docTypeSupported = true;
         }
      }

      return docTypeSupported;
   }

   private JobPropertiesDocument getJobPropDoc( JobPortResource instance )
   {
      XmlBeansResourcePropertySet propSet = (XmlBeansResourcePropertySet) instance.getResourcePropertySet(  );
      return (JobPropertiesDocument) propSet.toXmlObject(  );
   }

   private void addJobInfoToResponse( JobPortResource                           instance,
                                      CreationResponseDocument.CreationResponse creationResponse )
   {
      org.apache.ws.resource.properties.ResourcePropertySet resourcePropertySet =
         instance.getResourcePropertySet(  );
      creationResponse.setJobReference( (EndpointReferenceType) ( (XmlBeansResourceProperty) resourcePropertySet
                                                                  .get( JobPortPropertyQNames.JOB_REFERENCE ) )
                                                                .toXmlObjects(  )[0] );
      JobStateType jobState =
         (JobStateType) ( (XmlBeansResourceProperty) resourcePropertySet.get( JobPortPropertyQNames.JOB_STATE ) )
                        .toXmlObjects(  )[0];
      creationResponse.xsetJobState( jobState ); //only way I could get to compile
      creationResponse.setSuccessStatusCode( "successful-ok" );
   }

   private void addJobPropsToPrinterProps( JobPortResource instance )
   throws
          ResourceException
   {
      JobPropertiesDocument jobPropDoc           = getJobPropDoc( instance );
      PrinterPortResource   printerResource      = (PrinterPortResource) getResourceContext(  ).getResource(  );
      ResourceProperty      printerJobProperties =
         printerResource.getResourcePropertySet(  ).get( PrinterPortPropertyQNames.JOB_PROPERTIES );
      printerJobProperties.add( jobPropDoc );
   }

   private CreationResponseDocument createResponseDocument(  )
   {
      CreationResponseDocument creationResponseDocument = CreationResponseDocument.Factory.newInstance(  );
      creationResponseDocument.addNewCreationResponse(  );
      return creationResponseDocument;
   }

   private void incrementQueueCount(  )
   throws ResourceException
   {
      PrinterPortResource printerResource      = (PrinterPortResource) getResourceContext(  ).getResource(  );
      ResourceProperty    printerJobProperties =
         printerResource.getResourcePropertySet(  ).get( PrinterPortPropertyQNames.QUEUED_JOB_COUNT );
      XmlInteger          queued               = (XmlInteger) printerJobProperties.get( 0 );
      BigInteger          bigIntegerValue      = queued.getBigIntegerValue(  );
      int                 count                = bigIntegerValue.intValue(  );
      count++;
      queued.setBigIntegerValue( BigInteger.valueOf( count ) );
   }
}
TOP

Related Classes of org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.PrinterPortService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.