Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / MultiDocJob.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/MultiDocJob.java b/jspi/src/main/java/de/lohndirekt/print/MultiDocJob.java
new file mode 100644 (file)
index 0000000..a16a655
--- /dev/null
@@ -0,0 +1,177 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.logging.Level;\r
+import java.util.logging.Logger;\r
+\r
+import javax.print.DocFlavor;\r
+import javax.print.MultiDoc;\r
+import javax.print.MultiDocPrintJob;\r
+import javax.print.PrintException;\r
+import javax.print.attribute.AttributeSet;\r
+import javax.print.attribute.DocAttributeSet;\r
+import javax.print.attribute.HashAttributeSet;\r
+import javax.print.attribute.PrintRequestAttributeSet;\r
+import javax.print.event.PrintJobEvent;\r
+\r
+import de.lohndirekt.print.attribute.AttributeHelper;\r
+import de.lohndirekt.print.attribute.IppAttributeName;\r
+import de.lohndirekt.print.attribute.IppStatus;\r
+import de.lohndirekt.print.attribute.ipp.jobdesc.JobUri;\r
+import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;\r
+/**\r
+ * @author bpusch\r
+ *\r
+ */\r
+class MultiDocJob extends Job implements MultiDocPrintJob {\r
+    \r
+       Logger log = Logger.getLogger(this.getClass().getName());\r
+    \r
+       /**\r
+        * \r
+        */\r
+       protected MultiDocJob(IppMultiDocPrintService service) {\r
+               super(service);\r
+       }\r
+    \r
+       void processMultiDocEvent(MultiDocEvent event) {\r
+               SimpleMultiDoc doc = (SimpleMultiDoc) event.getSource();\r
+               sendDocument(doc);\r
+       }\r
+    \r
+       /**\r
+        *\r
+        */\r
+       public void print(MultiDoc multiDoc, PrintRequestAttributeSet attributes) throws PrintException {\r
+               SimpleMultiDoc multi = (SimpleMultiDoc) multiDoc;\r
+               multi.addMultiDocListener(new MDListener());\r
+               createJob(attributes);\r
+               sendDocument(multi);\r
+       }\r
+    \r
+       private void createJob(PrintRequestAttributeSet attributes) {\r
+               IppRequest request = null;\r
+               request = this.printService.request(OperationsSupported.CREATE_JOB);\r
+               //add the operationn attributes\r
+               request.addOperationAttributes(AttributeHelper.jobOperationAttributes(attributes));\r
+               request.setJobAttributes(AttributeHelper.jobAttributes(attributes));\r
+               IppResponse response = null;\r
+               try {\r
+                       response = request.send();\r
+               } catch (IOException e) {\r
+                       log.log(Level.SEVERE, e.getMessage(), e);\r
+               }\r
+               if (response != null) {\r
+                       Map responseAttributes = response.getAttributes();\r
+                       if (responseAttributes.containsKey(IppAttributeName.JOB_URI.getCategory())) {\r
+                               Set jobUriSet = (Set) responseAttributes.get(IppAttributeName.JOB_URI.getCategory());\r
+                               this.jobUri = (JobUri) jobUriSet.iterator().next();\r
+                       }\r
+                       if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r
+                       } else {\r
+                               notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+                       }\r
+               } else {\r
+                       notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+               }\r
+       }\r
+    \r
+       protected void sendDocument(SimpleMultiDoc multiDoc) {\r
+               IppRequest request = null;\r
+               AttributeSet operationAttributes = new HashAttributeSet();\r
+               request = this.request(OperationsSupported.SEND_DOCUMENT);\r
+               IppResponse response = null;\r
+               try {\r
+                       operationAttributes.addAll(AttributeHelper.docOperationAttributes(multiDoc));\r
+                       request.addOperationAttributes(operationAttributes);\r
+                       if (multiDoc.getDoc().getDocFlavor() instanceof DocFlavor.INPUT_STREAM) {\r
+                               DocAttributeSet set = multiDoc.getDoc().getAttributes();\r
+                               request.setData((InputStream) multiDoc.getDoc().getPrintData());\r
+                       }\r
+                       response = request.send();\r
+               } catch (IOException e) {\r
+                       log.log(Level.SEVERE, e.getMessage(), e);\r
+               }\r
+               if (response != null) {\r
+                       Map responseAttributes = response.getAttributes();\r
+                       if (responseAttributes.containsKey(IppAttributeName.JOB_URI.getCategory())) {\r
+                               Set jobUriSet = (Set) responseAttributes.get(IppAttributeName.JOB_URI.getCategory());\r
+                               this.jobUri = (JobUri) jobUriSet.iterator().next();\r
+                       }\r
+                       if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r
+                       } else {\r
+                               notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+                       }\r
+               } else {\r
+                       notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+               }\r
+               if (multiDoc.isLast()) {\r
+                       releaseJob();\r
+               }\r
+       }\r
+    \r
+       /**\r
+        * \r
+        */\r
+       private void releaseJob() {\r
+               IppRequest request = this.request(OperationsSupported.RELEASE_JOB);\r
+               IppResponse response = null;\r
+               try {\r
+                       response = request.send();\r
+                       \r
+               } catch (IOException e) {\r
+                       log.log(Level.SEVERE, e.getMessage(), e);\r
+               }\r
+               if (response != null) {\r
+                       if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r
+                               || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r
+                               notifyJobListeners(PrintJobEvent.JOB_COMPLETE);\r
+                       } else {\r
+                               notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+                       }\r
+               } else {\r
+                       notifyJobListeners(PrintJobEvent.JOB_FAILED);\r
+               }\r
+       }\r
+    \r
+       private IppRequest request(OperationsSupported operation) {\r
+               IppRequest request = IppRequestFactory.createIppRequest(this.jobUri.getURI(), operation, this.printService.getRequestingUserName(), this.printService.getRequestingUserPassword());\r
+               AttributeSet operationAttributes = new HashAttributeSet();\r
+               operationAttributes.add(new JobUri(this.jobUri.getURI()));\r
+               request.addOperationAttributes(operationAttributes);\r
+               return request;\r
+       }\r
+    \r
+       private class MDListener implements MultiDocListener {\r
+               public void processEvent(MultiDocEvent event) throws IOException {\r
+                       log.log(Level.FINEST, "MultiDocevent");\r
+                       processMultiDocEvent(event);\r
+               }\r
+       }\r
+}\r