From 2039c6a86f353755be8033478b63f5697bd618d5 Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Tue, 14 Mar 2023 17:26:31 -0400 Subject: [PATCH 1/8] reindent code --- .../command/CommandInReachSchemaDefs.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 6c6d8d200..b54e5aabc 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -64,7 +64,7 @@ public void runCommand( GlobalSettings gs ,Options options ) throws Exception { - + if( options.getArguments().size() > 1 ){ throw new Exception("Unexpected argument: "+options.getArguments().get(1)); } @@ -89,7 +89,7 @@ public void runCommand( InReachSettings inReachSettings = InReachConfiguration.getInReachSettings(); for(InReachForm form : inReachSettings.getForms()){ JSONObject jsonDef = schemaDefinitionFromForm(form); - + // Pretty print gs.getOutStream().println(jsonDef.toString(3)); gs.getOutStream().println(); @@ -98,14 +98,14 @@ public void runCommand( private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { JSONObject jsonDef = new JSONObject(); - + jsonDef.put("group", "inReach"); jsonDef.put("id", form.getTitle()); jsonDef.put("label", "InReach "+form.getTitle()); - + JSONArray attributes = new JSONArray(); jsonDef.put("attributes", attributes); - + // Title { JSONObject attribute = new JSONObject(); @@ -115,24 +115,24 @@ private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { attribute.put("type", "title"); attribute.put("includedInBrief", true); } - + for(InReachFormField field : form.getFields()){ JSONObject attribute = new JSONObject(); attributes.put(attribute); - + String label = field.getName(); String id = escapeJsonAttribute( field.getName() ); - + attribute.put("label", label); attribute.put("id", id); - + InReachFormField.Type fieldType = field.getType(); if( InReachFormField.Type.PICKLIST == fieldType ){ attribute.put("type", "selection"); JSONArray options = new JSONArray(); attribute.put("options", options); - + for(String v : field.getValues()){ JSONObject option = new JSONObject(); options.put(option); @@ -140,25 +140,25 @@ private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { option.put("label", v); option.put("value", v); } - + } else if( InReachFormField.Type.TEXT == fieldType ) { attribute.put("type", "string"); attribute.put("textarea", true); - } else if( InReachFormField.Type.NUMBER == fieldType) { + } else if( InReachFormField.Type.NUMBER == fieldType) { attribute.put("type", "string"); } else { throw new Exception("Unexpected field type: "+fieldType); } } - + return jsonDef; } private String escapeJsonAttribute(String fieldName) { StringBuilder sb = new StringBuilder(); - + for(char c : fieldName.toCharArray()){ if( c >= '0' && c <= '9' ){ sb.append(c); @@ -172,7 +172,7 @@ private String escapeJsonAttribute(String fieldName) { // skip } } - + return sb.toString(); } } From 8d421acb04bb7d201038c8e7f8c6c89441d8265a Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Tue, 14 Mar 2023 17:39:04 -0400 Subject: [PATCH 2/8] create schema folder from inreach form definition while executing schemas-inreach command --- .../command/CommandInReachSchemaDefs.java | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index b54e5aabc..d6f60912a 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -6,6 +6,14 @@ import org.json.JSONArray; import org.json.JSONObject; +import java.util.Scanner; +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import ca.carleton.gcrc.couch.command.schema.SchemaDefinition; + import ca.carleton.gcrc.couch.onUpload.inReach.InReachConfiguration; import ca.carleton.gcrc.couch.onUpload.inReach.InReachForm; import ca.carleton.gcrc.couch.onUpload.inReach.InReachFormField; @@ -72,7 +80,7 @@ public void runCommand( File atlasDir = gs.getAtlasDir(); // Load properties for atlas -// AtlasProperties atlasProperties = AtlasProperties.fromAtlasDir(atlasDir); + AtlasProperties atlasProperties = AtlasProperties.fromAtlasDir(atlasDir); // InReach configuration File configDir = new File(atlasDir, "config"); @@ -87,13 +95,48 @@ public void runCommand( // Iterate over each inReach form InReachSettings inReachSettings = InReachConfiguration.getInReachSettings(); + File docsDir = new File(atlasDir, "docs"); + String groupName = atlasProperties.getAtlasName(); + Scanner scanner = new Scanner(System.in); for(InReachForm form : inReachSettings.getForms()){ JSONObject jsonDef = schemaDefinitionFromForm(form); - - // Pretty print - gs.getOutStream().println(jsonDef.toString(3)); - gs.getOutStream().println(); + String id = form.getPrefix().replace("-", "_") + form.getTitle(); + + System.out.print("Do you want to create a schema for the "+ id +" definition? (yes/no): "); + String response = scanner.next().toLowerCase(); + if (response.equals("yes")) { + SchemaDefinition schemaDef = new SchemaDefinition(groupName, id); + + // Save schema definition to disk + schemaDef.saveToDocsDir(docsDir); + File schemaDir = new File(docsDir, schemaDef.getDocumentIdentifier()); + + // Write JSON to file + File file = new File(schemaDir, "definition.json"); + try (FileOutputStream fos = new FileOutputStream(file); + OutputStreamWriter osw = new OutputStreamWriter(fos)) { + osw.write(jsonDef.toString(4).replace(" ", "\t")); + osw.flush(); + gs.getOutStream().println("Schema written to "+schemaDir.getAbsolutePath()); + + Options newOptions = new Options(); + List args = new ArrayList(); + args.add("update-schema"); + args.add("--name"); + args.add(groupName + "_" + id); + newOptions.parseOptions(args); + CommandUpdateSchema cmdUpdateSchems = new CommandUpdateSchema(); + cmdUpdateSchems.runCommand(gs, newOptions); + } catch (IOException e) { + gs.getOutStream().println("Could not write schema definition to "+file.getAbsolutePath()); + } + } else { + // Pretty print + gs.getOutStream().println(jsonDef.toString(3)); + gs.getOutStream().println(); + } } + scanner.close(); } private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { From f00aa00e347b2711a5ea913ffbc5941f11c07e6b Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Wed, 15 Mar 2023 17:57:35 -0400 Subject: [PATCH 3/8] implement --add-schema flag with schemas-for-inreach comd to create a schema folder --- .../command/CommandInReachSchemaDefs.java | 92 ++++++++++--------- .../carleton/gcrc/couch/command/Options.java | 8 ++ 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index d6f60912a..46eebf498 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -80,7 +80,7 @@ public void runCommand( File atlasDir = gs.getAtlasDir(); // Load properties for atlas - AtlasProperties atlasProperties = AtlasProperties.fromAtlasDir(atlasDir); + // AtlasProperties atlasProperties = AtlasProperties.fromAtlasDir(atlasDir); // InReach configuration File configDir = new File(atlasDir, "config"); @@ -95,55 +95,65 @@ public void runCommand( // Iterate over each inReach form InReachSettings inReachSettings = InReachConfiguration.getInReachSettings(); - File docsDir = new File(atlasDir, "docs"); - String groupName = atlasProperties.getAtlasName(); - Scanner scanner = new Scanner(System.in); - for(InReachForm form : inReachSettings.getForms()){ + Boolean shouldAddSchema = options.getAddSchema(); + String userResponse = ""; + for (InReachForm form : inReachSettings.getForms()) { JSONObject jsonDef = schemaDefinitionFromForm(form); - String id = form.getPrefix().replace("-", "_") + form.getTitle(); - - System.out.print("Do you want to create a schema for the "+ id +" definition? (yes/no): "); - String response = scanner.next().toLowerCase(); - if (response.equals("yes")) { - SchemaDefinition schemaDef = new SchemaDefinition(groupName, id); - - // Save schema definition to disk - schemaDef.saveToDocsDir(docsDir); - File schemaDir = new File(docsDir, schemaDef.getDocumentIdentifier()); - - // Write JSON to file - File file = new File(schemaDir, "definition.json"); - try (FileOutputStream fos = new FileOutputStream(file); - OutputStreamWriter osw = new OutputStreamWriter(fos)) { - osw.write(jsonDef.toString(4).replace(" ", "\t")); - osw.flush(); - gs.getOutStream().println("Schema written to "+schemaDir.getAbsolutePath()); - - Options newOptions = new Options(); - List args = new ArrayList(); - args.add("update-schema"); - args.add("--name"); - args.add(groupName + "_" + id); - newOptions.parseOptions(args); - CommandUpdateSchema cmdUpdateSchems = new CommandUpdateSchema(); - cmdUpdateSchems.runCommand(gs, newOptions); - } catch (IOException e) { - gs.getOutStream().println("Could not write schema definition to "+file.getAbsolutePath()); - } - } else { - // Pretty print - gs.getOutStream().println(jsonDef.toString(3)); - gs.getOutStream().println(); + String schemaId = form.getPrefix().replace("-", "_") + form.getTitle(); + if(null == shouldAddSchema) { + //if --add-schema flag not provided, ask user if they want to create schema or not + Scanner scanner = new Scanner(System.in); + System.out.print("Do you want to create a schema for: " + schemaId + "? (yes/no): "); + userResponse = scanner.next().toLowerCase(); + scanner.close(); + } + + if(null != shouldAddSchema || userResponse.equals("yes")) { + createSchema(gs, atlasDir, schemaId, jsonDef); } + + // Pretty print + gs.getOutStream().println(jsonDef.toString(3)); + gs.getOutStream().println(); + } + } + + private void createSchema(GlobalSettings gs, File atlasDir, String schemaId, JSONObject formDefinition) throws Exception { + File docsDir = new File(atlasDir, "docs"); + String groupName = "inReach"; + SchemaDefinition schemaDef = new SchemaDefinition(groupName, schemaId); + + // Save schema definition to disk + schemaDef.saveToDocsDir(docsDir); + File schemaDir = new File(docsDir, schemaDef.getDocumentIdentifier()); + + // Write JSON to file + File file = new File(schemaDir, "definition.json"); + try { + FileOutputStream fos = new FileOutputStream(file); + OutputStreamWriter osw = new OutputStreamWriter(fos); + osw.write(formDefinition.toString(4).replace(" ", "\t")); + osw.flush(); + gs.getOutStream().println("Schema written to " + schemaDir.getAbsolutePath()); + + Options newOptions = new Options(); + List args = new ArrayList(); + args.add("update-schema"); + args.add("--name"); + args.add(groupName + "_" + schemaId); + newOptions.parseOptions(args); + CommandUpdateSchema cmdUpdateSchems = new CommandUpdateSchema(); + cmdUpdateSchems.runCommand(gs, newOptions); + } catch (IOException e) { + gs.getOutStream().println("Could not write schema definition to " + file.getAbsolutePath()); } - scanner.close(); } private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { JSONObject jsonDef = new JSONObject(); jsonDef.put("group", "inReach"); - jsonDef.put("id", form.getTitle()); + jsonDef.put("id", form.getPrefix().replace("-", "_") + form.getTitle()); jsonDef.put("label", "InReach "+form.getTitle()); JSONArray attributes = new JSONArray(); diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java index 90600acf0..d07f6ee88 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java @@ -40,6 +40,7 @@ public void setLevel(Level level) { static final public String OPTION_SCHEMA = "--schema"; static final public String OPTION_LAYER = "--layer"; static final public String OPTION_NAME = "--name"; + static final public String OPTION_ADD_SCHEMA = "--add-schema"; static final public String OPTION_SET_LOGGER = "--set-logger"; static final public String OPTION_DEBUG = "--debug"; @@ -61,6 +62,7 @@ public void setLevel(Level level) { private Boolean overwriteDocs; private Boolean all; private Boolean test; + private Boolean addSchema; private String atlasDir; private String def; private String group; @@ -239,6 +241,8 @@ public void parseOptions(List args) throws Exception { name = argumentStack.pop(); + } else if (OPTION_ADD_SCHEMA.equals(arg)) { + addSchema = Boolean.TRUE; } else { throw new Exception("Unrecognized option: "+arg); } @@ -344,6 +348,10 @@ public Boolean getAll() { public Boolean getTest() { return test; } + + public Boolean getAddSchema() { + return addSchema; + } public String getAtlasDir() { return atlasDir; From 124043024e9372c709e84f9a0d8ba44d5c331077 Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Thu, 16 Mar 2023 10:51:08 -0400 Subject: [PATCH 4/8] remove undescore from definition label, add help message information --- .../couch/command/CommandInReachSchemaDefs.java | 13 ++++++++----- .../gcrc/couch/onUpload/inReach/InReachFormXml.java | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 46eebf498..525f8425a 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -63,6 +63,9 @@ public void reportHelp(PrintStream ps) { ps.println(" nunaliit schemas-for-inreach "); ps.println(); ps.println("options:"); + ps.println(" "+Options.OPTION_ADD_SCHEMA); + ps.println(" When specified, generates schemas for inReach forms."); + ps.println(" Form prefix and title will used as the id of the schema."); ps.println(); CommandHelp.reportGlobalOptions(ps, getExpectedOptions()); } @@ -96,16 +99,15 @@ public void runCommand( // Iterate over each inReach form InReachSettings inReachSettings = InReachConfiguration.getInReachSettings(); Boolean shouldAddSchema = options.getAddSchema(); - String userResponse = ""; + Scanner scanner = new Scanner(System.in); for (InReachForm form : inReachSettings.getForms()) { + String userResponse = ""; JSONObject jsonDef = schemaDefinitionFromForm(form); - String schemaId = form.getPrefix().replace("-", "_") + form.getTitle(); + String schemaId = form.getPrefix().replace("-", "_") + form.getTitle().replace(" ", "_"); if(null == shouldAddSchema) { //if --add-schema flag not provided, ask user if they want to create schema or not - Scanner scanner = new Scanner(System.in); System.out.print("Do you want to create a schema for: " + schemaId + "? (yes/no): "); userResponse = scanner.next().toLowerCase(); - scanner.close(); } if(null != shouldAddSchema || userResponse.equals("yes")) { @@ -116,6 +118,7 @@ public void runCommand( gs.getOutStream().println(jsonDef.toString(3)); gs.getOutStream().println(); } + scanner.close(); } private void createSchema(GlobalSettings gs, File atlasDir, String schemaId, JSONObject formDefinition) throws Exception { @@ -153,7 +156,7 @@ private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception { JSONObject jsonDef = new JSONObject(); jsonDef.put("group", "inReach"); - jsonDef.put("id", form.getPrefix().replace("-", "_") + form.getTitle()); + jsonDef.put("id", form.getPrefix().replace("-", "_") + form.getTitle().replace(" ", "_")); jsonDef.put("label", "InReach "+form.getTitle()); JSONArray attributes = new JSONArray(); diff --git a/nunaliit2-couch-onUpload/src/main/java/ca/carleton/gcrc/couch/onUpload/inReach/InReachFormXml.java b/nunaliit2-couch-onUpload/src/main/java/ca/carleton/gcrc/couch/onUpload/inReach/InReachFormXml.java index 6e490be35..18ef42ba7 100644 --- a/nunaliit2-couch-onUpload/src/main/java/ca/carleton/gcrc/couch/onUpload/inReach/InReachFormXml.java +++ b/nunaliit2-couch-onUpload/src/main/java/ca/carleton/gcrc/couch/onUpload/inReach/InReachFormXml.java @@ -18,7 +18,7 @@ public InReachFormXml(Element formElem){ title = getTextElement(formElem, "title"); if (title != null) { - title = title.trim().replace(" ", "_"); + title = title.trim(); } destination = getTextElement(formElem, "destination"); From b10d5b144945c026c143e929ca783a8fc3e7f135 Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Thu, 16 Mar 2023 14:03:37 -0400 Subject: [PATCH 5/8] replace --add-schema flag with --generate-schema, fix typo, set default user input for schema create as 'N' --- .../command/CommandInReachSchemaDefs.java | 19 ++++++++++--------- .../carleton/gcrc/couch/command/Options.java | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 525f8425a..9f596c805 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -63,9 +63,8 @@ public void reportHelp(PrintStream ps) { ps.println(" nunaliit schemas-for-inreach "); ps.println(); ps.println("options:"); - ps.println(" "+Options.OPTION_ADD_SCHEMA); - ps.println(" When specified, generates schemas for inReach forms."); - ps.println(" Form prefix and title will used as the id of the schema."); + ps.println(" "+Options.OPTION_GENERATE_SCHEMA); + ps.println(" When specified, automatically creates or updates all schemas derived from the current inReach form."); ps.println(); CommandHelp.reportGlobalOptions(ps, getExpectedOptions()); } @@ -101,16 +100,16 @@ public void runCommand( Boolean shouldAddSchema = options.getAddSchema(); Scanner scanner = new Scanner(System.in); for (InReachForm form : inReachSettings.getForms()) { - String userResponse = ""; + String userResponse = "n"; JSONObject jsonDef = schemaDefinitionFromForm(form); String schemaId = form.getPrefix().replace("-", "_") + form.getTitle().replace(" ", "_"); if(null == shouldAddSchema) { //if --add-schema flag not provided, ask user if they want to create schema or not - System.out.print("Do you want to create a schema for: " + schemaId + "? (yes/no): "); - userResponse = scanner.next().toLowerCase(); + System.out.print("Do you want to generate schema for: " + schemaId + "?[N]: "); + userResponse = scanner.nextLine().toLowerCase(); } - if(null != shouldAddSchema || userResponse.equals("yes")) { + if(null != shouldAddSchema || userResponse.equals("y")) { createSchema(gs, atlasDir, schemaId, jsonDef); } @@ -137,6 +136,8 @@ private void createSchema(GlobalSettings gs, File atlasDir, String schemaId, JSO OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write(formDefinition.toString(4).replace(" ", "\t")); osw.flush(); + fos.flush(); + fos.close(); gs.getOutStream().println("Schema written to " + schemaDir.getAbsolutePath()); Options newOptions = new Options(); @@ -145,8 +146,8 @@ private void createSchema(GlobalSettings gs, File atlasDir, String schemaId, JSO args.add("--name"); args.add(groupName + "_" + schemaId); newOptions.parseOptions(args); - CommandUpdateSchema cmdUpdateSchems = new CommandUpdateSchema(); - cmdUpdateSchems.runCommand(gs, newOptions); + CommandUpdateSchema cmdUpdateSchema = new CommandUpdateSchema(); + cmdUpdateSchema.runCommand(gs, newOptions); } catch (IOException e) { gs.getOutStream().println("Could not write schema definition to " + file.getAbsolutePath()); } diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java index d07f6ee88..4885fe456 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Options.java @@ -40,7 +40,7 @@ public void setLevel(Level level) { static final public String OPTION_SCHEMA = "--schema"; static final public String OPTION_LAYER = "--layer"; static final public String OPTION_NAME = "--name"; - static final public String OPTION_ADD_SCHEMA = "--add-schema"; + static final public String OPTION_GENERATE_SCHEMA = "--generate-schema"; static final public String OPTION_SET_LOGGER = "--set-logger"; static final public String OPTION_DEBUG = "--debug"; @@ -241,7 +241,7 @@ public void parseOptions(List args) throws Exception { name = argumentStack.pop(); - } else if (OPTION_ADD_SCHEMA.equals(arg)) { + } else if (OPTION_GENERATE_SCHEMA.equals(arg)) { addSchema = Boolean.TRUE; } else { throw new Exception("Unrecognized option: "+arg); From bb23ef0788bfc8fa6c14fec030e6d4ded80201dc Mon Sep 17 00:00:00 2001 From: alexgao1 Date: Thu, 16 Mar 2023 14:21:43 -0400 Subject: [PATCH 6/8] formatting --- .../gcrc/couch/command/CommandInReachSchemaDefs.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 9f596c805..3943a4f23 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -64,7 +64,8 @@ public void reportHelp(PrintStream ps) { ps.println(); ps.println("options:"); ps.println(" "+Options.OPTION_GENERATE_SCHEMA); - ps.println(" When specified, automatically creates or updates all schemas derived from the current inReach form."); + ps.println(" When specified, automatically creates or updates"); + ps.println(" all schemas derived from the current inReach form."); ps.println(); CommandHelp.reportGlobalOptions(ps, getExpectedOptions()); } @@ -104,8 +105,8 @@ public void runCommand( JSONObject jsonDef = schemaDefinitionFromForm(form); String schemaId = form.getPrefix().replace("-", "_") + form.getTitle().replace(" ", "_"); if(null == shouldAddSchema) { - //if --add-schema flag not provided, ask user if they want to create schema or not - System.out.print("Do you want to generate schema for: " + schemaId + "?[N]: "); + // if --generate-schema flag not provided, ask user if they want to create schema or not + System.out.print("Do you want to generate the schema for: " + schemaId + "? (Y/N) [Default: N]: "); userResponse = scanner.nextLine().toLowerCase(); } From 79a032c62c790639cf9208cbaf34e3f629e014d9 Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Thu, 16 Mar 2023 15:12:19 -0400 Subject: [PATCH 7/8] prompt user to enter valid input for genreating schema from inreach form --- .../gcrc/couch/command/CommandInReachSchemaDefs.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 3943a4f23..7ce0f7bdd 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -101,13 +101,18 @@ public void runCommand( Boolean shouldAddSchema = options.getAddSchema(); Scanner scanner = new Scanner(System.in); for (InReachForm form : inReachSettings.getForms()) { - String userResponse = "n"; + String userResponse = null; JSONObject jsonDef = schemaDefinitionFromForm(form); String schemaId = form.getPrefix().replace("-", "_") + form.getTitle().replace(" ", "_"); if(null == shouldAddSchema) { // if --generate-schema flag not provided, ask user if they want to create schema or not - System.out.print("Do you want to generate the schema for: " + schemaId + "? (Y/N) [Default: N]: "); - userResponse = scanner.nextLine().toLowerCase(); + do { + System.out.print("Do you want to generate the schema for: " + schemaId + "? (Y/N) [Default: N]: "); + userResponse = scanner.nextLine().trim().toLowerCase(); + if (!userResponse.matches("[yn]|")) { + System.out.println("A valid response must be provided: Y, N or blank to accept default value."); + } + } while (!userResponse.matches("[yn]|")); } if(null != shouldAddSchema || userResponse.equals("y")) { From cc2a504830f616b32230fcad914f44284f84876d Mon Sep 17 00:00:00 2001 From: Jagdish Kunwar Date: Fri, 17 Mar 2023 09:45:02 -0400 Subject: [PATCH 8/8] code refactor --- .../command/CommandInReachSchemaDefs.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java index 7ce0f7bdd..be6b81fa5 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandInReachSchemaDefs.java @@ -137,26 +137,24 @@ private void createSchema(GlobalSettings gs, File atlasDir, String schemaId, JSO // Write JSON to file File file = new File(schemaDir, "definition.json"); - try { - FileOutputStream fos = new FileOutputStream(file); - OutputStreamWriter osw = new OutputStreamWriter(fos); + try(FileOutputStream fos = new FileOutputStream(file); + OutputStreamWriter osw = new OutputStreamWriter(fos)) + { osw.write(formDefinition.toString(4).replace(" ", "\t")); - osw.flush(); - fos.flush(); - fos.close(); gs.getOutStream().println("Schema written to " + schemaDir.getAbsolutePath()); - - Options newOptions = new Options(); - List args = new ArrayList(); - args.add("update-schema"); - args.add("--name"); - args.add(groupName + "_" + schemaId); - newOptions.parseOptions(args); - CommandUpdateSchema cmdUpdateSchema = new CommandUpdateSchema(); - cmdUpdateSchema.runCommand(gs, newOptions); } catch (IOException e) { gs.getOutStream().println("Could not write schema definition to " + file.getAbsolutePath()); + throw new Exception("Could not write schema definition to " + file.getAbsolutePath()); } + + Options newOptions = new Options(); + List args = new ArrayList(); + args.add("update-schema"); + args.add("--name"); + args.add(groupName + "_" + schemaId); + newOptions.parseOptions(args); + CommandUpdateSchema cmdUpdateSchema = new CommandUpdateSchema(); + cmdUpdateSchema.runCommand(gs, newOptions); } private JSONObject schemaDefinitionFromForm(InReachForm form) throws Exception {