hatch printing
authorBill Erickson <berick@esilibrary.com>
Mon, 14 Apr 2014 17:51:34 +0000 (13:51 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 14 Apr 2014 17:51:34 +0000 (13:51 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
src/org/evergreen_ils/hatch/HatchSocket.java
src/org/evergreen_ils/hatch/PrintDriver.java

index e8a7ce3..7b5543e 100644 (file)
@@ -105,13 +105,14 @@ public class HatchSocket {
         this.session = null;
     }
 
-    private void reply(Object json) {
-        reply(json, true);
+    private void reply(Object json, String msgid) {
+        reply(json, msgid, true);
     }
 
-    private void reply(Object json, boolean success) {
+    private void reply(Object json, String msgid, boolean success) {
 
         HashMap<String, Object> response = new HashMap<String, Object>();
+        response.put("msgid", msgid);
         if (success) {
             response.put("success", json);
         } else {
@@ -138,18 +139,25 @@ public class HatchSocket {
         try {
             params = (HashMap<String,String>) JSON.parse(message);
         } catch (ClassCastException e) {
-            reply("Invalid WebSockets JSON message " + message, false);
+            reply("Invalid WebSockets JSON message " + message, "", false);
         }
 
         FileIO io;
+        String msgid = params.get("msgid");
         String action = params.get("action");
         String key = params.get("key");
         String value = params.get("value");
         String mime = params.get("mime");
 
+        // all requets require a message ID
+        if (msgid == null || msgid.equals("")) {
+            reply("No msgid specified in request", msgid, false);
+            return;
+        }
+
         // all requests require an action
         if (action == null || action.equals("")) {
-            reply("No action specified in request", false);
+            reply("No action specified in request", msgid, false);
             return;
         }
 
@@ -157,22 +165,22 @@ public class HatchSocket {
             io = new FileIO(profileDirectory);
             String[] keys = io.keys(key); // OK for key to be null
             if (keys != null) {
-                reply(keys);
+                reply(keys, msgid);
             } else {
-                reply("key lookup error", false);
+                reply("key lookup error", msgid, false);
             }
             return;
         }
 
         if (action.equals("printers")) {
             List printers = new PrintDriver().getPrinters();
-            reply(printers);
+            reply(printers, msgid);
             return;
         }
 
         // all remaining requests require a key
         if (key == null || key.equals("")) {
-            reply("No key specified in request", false);
+            reply("No key specified in request", msgid, false);
             return;
         }
 
@@ -186,13 +194,13 @@ public class HatchSocket {
                         // relay lines of text to the caller as we read them
                         // assume the text content is JSON and return it
                         // un-JSON-ified.
-                        reply(line);
+                        reply(line, msgid);
                     }
                 } catch (IOException e) {
                     logger.warn(e);
                 }
             } else {
-                reply("Error accessing property " + key, false);
+                reply("Error accessing property " + key, msgid, false);
             }
             return;
         }
@@ -200,16 +208,16 @@ public class HatchSocket {
         if (action.equals("delete")) {
             io = new FileIO(profileDirectory);
             if (io.delete(key)) {
-                reply("Delete of " + key + " successful");
+                reply("Delete of " + key + " successful", msgid);
             } else {
-                reply("Delete of " + key + " failed", false);
+                reply("Delete of " + key + " failed", msgid, false);
             }
             return;
         }
 
         // all remaining actions require value
         if (value == null) {
-            reply("No value specified in request", false);
+            reply("No value specified in request", msgid, false);
             return;
         }
 
@@ -218,32 +226,32 @@ public class HatchSocket {
             case "print" : 
                 boolean ok = new PrintDriver().printWithDialog(mime, value);
                 if (ok) {
-                    reply("print succeeded");
+                    reply("print succeeded", msgid);
                 } else {
-                    reply("print failed", false);
+                    reply("print failed", msgid, false);
                 }
                 break;
 
             case "set" :
                 io = new FileIO(profileDirectory);
                 if (io.set(key, value)) {
-                    reply("setting value for " + key + " succeeded");
+                    reply("setting value for " + key + " succeeded", msgid);
                 } else {
-                    reply("setting value for " + key + " succeeded", false);
+                    reply("setting value for " + key + " succeeded", msgid, false);
                 }
                 break;
 
             case "append" :
                 io = new FileIO(profileDirectory);
                 if (io.append(key, value)) {
-                    reply("appending value for " + key + " succeeded");
+                    reply("appending value for " + key + " succeeded", msgid);
                 } else {
-                    reply("appending value for " + key + " succeeded", false);
+                    reply("appending value for " + key + " succeeded", msgid, false);
                 }
                 break;
 
             default:
-                reply("No such action: " + action, false);
+                reply("No such action: " + action, msgid, false);
         }
     }
 }
index 89fc691..f825f9c 100644 (file)
@@ -96,11 +96,15 @@ public class PrintDriver implements Printable {
         // for now, assume we only have one page
         if (page > 0) return NO_SUCH_PAGE;
 
+
         // find the imageable area
-        Graphics2D g2d = (Graphics2D)g;
+        Graphics2D g2d = (Graphics2D) g;
         g2d.translate(pf.getImageableX(), pf.getImageableY());
-        int x = 5;
-        int y = 5;
+        Font normalFont = new Font(Font.MONOSPACED, Font.PLAIN, 10);
+        g2d.setFont(normalFont);
+
+        int x = 0;
+        int y = 0;
         for (String line : printText.split("\n"))
             g.drawString(line, x, y += g.getFontMetrics().getHeight());