Initial very quick hack up of what the module should do
1 files changed, 142 insertions(+), 0 deletions(-)

A => src/org/overchat/overham/modules/ISO3166.java
A => src/org/overchat/overham/modules/ISO3166.java +142 -0
@@ 0,0 1,142 @@ 
+/*
+ * OverHam; An object-orientated ham radio math and tool library
+ * Copyright (c) 2015-2017, Duncan Ross Palmer (2E0EOL),
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *     * Redistributions of source code must retain the above copyright notice,
+ *       this list of conditions and the following disclaimer.
+ *
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *
+ *     * Neither the name of the Daybo Logic nor the names of its contributors
+ *       may be used to endorse or promote products derived from this software
+ *       without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.overchat.overham.modules;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.overchat.overham.Server;
+import org.overchat.overham.module.Base;
+import org.overchat.overham.module.CallStyle;
+import org.overchat.overham.module.ExitCodes;
+import org.overchat.overham.module.interfaces.Module;
+import org.overchat.overham.modules.qcode.Authority;
+import org.overchat.overham.modules.qcode.RangeLimits;
+
+import net.sf.json.JSONObject;
+
+/**
+ * This module implements ISO-3166
+ *
+ * @author duncan.palmer
+ */
+public class ISO3166 extends Base implements Module {
+
+	/**
+	 * This is the main entrypoint which is called with the name of the method the
+	 * user requested to call.  A JSON structure is returned.
+	 *
+	 * @param methodName
+	 */
+	@Override
+	public JSONObject call(String methodName, Map<String, String> methodParams) {
+		JSONObject jsonObject = new JSONObject();
+		if (methodName.equals("dump")) {
+			return userDump(jsonObject);
+		} else if (methodName.equals("lookup")) {
+			return userLookup(jsonObject, methodParams);
+		}
+		return null;
+	}
+
+	public JSONObject userLookup(JSONObject output, Map<String, String> methodParams) {
+		List<Object> boundParams;
+		String code = methodParams.get("code");
+
+		if (code == null) {
+			output = this.buildErrorForUser(output, ExitCodes.MISSING);
+			return output;
+		} else {
+			boundParams = new ArrayList<Object>(1);
+			boundParams.add(code);
+		}
+
+		try (ResultSet rs = this.owner.getHamDatabase().query("SELECT code, name FROM countries WHERE code = ?", boundParams)) {
+			if (rs.next()) {
+				Map<String, String> data = new HashMap<String, String>(3);
+				data.put("code", rs.getString("code"));
+				data.put("name", rs.getString("name"));
+				output.accumulate("data", data);
+				output = this.buildErrorForUser(output, ExitCodes.SUCCESS);
+			} else {
+				output = this.buildErrorForUser(output, ExitCodes.NOT_FOUND);
+			}
+		} catch (SQLException e) {
+			// TODO: Log error
+			java.lang.System.out.println(e);
+			return null;
+		} catch (InterruptedException e) {
+			// TODO: Log error
+			java.lang.System.out.println(e);
+			return null;
+		}
+
+		return output;
+	}
+
+	public JSONObject userDump(JSONObject output) {
+		List<String> dump = new ArrayList<String>();
+		try (ResultSet rs = this.owner.getHamDatabase().query("SELECT code FROM countries")) {
+			while (rs.next()) {
+				dump.add(rs.getString("code"));
+			}
+		} catch (SQLException e) {
+			// TODO: Log error
+			java.lang.System.out.println(e);
+			return null;
+		} catch (InterruptedException e) {
+			// TODO: Log error
+			java.lang.System.out.println(e);
+			return null;
+		}
+
+		output.accumulate("data", dump);
+		output = this.buildErrorForUser(output, ExitCodes.SUCCESS);
+		return output;
+	}
+
+	@Override
+	public void init(Server server) {
+		super.init(server);
+		//this.registerUserMethod("dump", null, CallStyle.RETURN_DATA);
+		//this.registerUserMethod("lookup", null, CallStyle.RETURN_DATA);
+		this.registerUserMethod("dump", null, CallStyle.LEGACY);
+		this.registerUserMethod("lookup", null, CallStyle.LEGACY);
+	}
+}