Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thomas Allen my-chat resubmission #92

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
44 changes: 44 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
target/
bin/
*.iml
.vscode
*.json
34 changes: 34 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>my-chat</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1614007406848</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
9 changes: 9 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.mindlinksoft.recruitment.mychat;

import com.mindlinksoft.recruitment.mychat.Model.Conversation;

import picocli.CommandLine.ParseResult;
import picocli.CommandLine.Model.OptionSpec;

public class ConversationArgumentExecution implements IConversationArgumentExecution {

/**
* Called to process the handed conversation with respects to the ParseResult
* parameter.
* @param conversation The conversation for the command options to work on/with.
* @param pr The parseResult containing the options and their values.
* @throws Exception
*/
@Override
public Conversation executue(Conversation conversation, ParseResult pr) throws Exception {
try {
Conversation convo = conversation;

for(OptionSpec option : pr.matchedOptions()) {
convo = processOption(convo, option);
}

return convo;

} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error in processing matched options.");
}
}


/**
* This handles each matched option individually, peforming their respective tasks.
* @param conversation The convosation that the command option is called on.
* @param option The option that has been called.
* @return The {@link Conversation} that is freshly constructed from the filter/modifications to the original.
*/
protected Conversation processOption(Conversation convo, OptionSpec option) throws Exception {

ConversationTransformer convoT = new ConversationTransformer(convo);

switch (option.longestName()){
case "--filterByUser": return convoT.filterConvoByUser(option.getValue());
case "--filterByKeyword": return convoT.filterConvoByKeyword(option.getValue());
case "--blacklist": return convoT.censorConvo(option.getValue());
case "--inputFilePath": return convo;
case "--outputFilePath": return convo;
default:
throw new Exception("Error: " + option.longestName() + " has not been implemented in either processOption method or its overrides");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mindlinksoft.recruitment.mychat;

import com.google.gson.*;
import com.mindlinksoft.recruitment.mychat.Model.Conversation;
import com.mindlinksoft.recruitment.mychat.Model.Message;

import picocli.CommandLine;
import picocli.CommandLine.ParameterException;
Expand All @@ -11,6 +13,7 @@
import java.lang.reflect.Type;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -25,6 +28,12 @@ public class ConversationExporter {
*/
public static void main(String[] args) throws Exception {
// We use picocli to parse the command line - see https://picocli.info/

// for(String s : args){
// System.out.println(s);
// }


ConversationExporterConfiguration configuration = new ConversationExporterConfiguration();
CommandLine cmd = new CommandLine(configuration);

Expand All @@ -45,9 +54,10 @@ public static void main(String[] args) throws Exception {

ConversationExporter exporter = new ConversationExporter();

exporter.exportConversation(configuration.inputFilePath, configuration.outputFilePath);
exporter.exportConversation(parseResult);

System.exit(cmd.getCommandSpec().exitCodeOnSuccess());

} catch (ParameterException ex) {
cmd.getErr().println(ex.getMessage());
if (!UnmatchedArgumentException.printSuggestions(ex, cmd.getErr())) {
Expand All @@ -69,11 +79,40 @@ public static void main(String[] args) throws Exception {
*/
public void exportConversation(String inputFilePath, String outputFilePath) throws Exception {
Conversation conversation = this.readConversation(inputFilePath);

this.writeConversation(conversation, outputFilePath);

System.out.println("Conversation exported from '" + inputFilePath + "' to '" + outputFilePath + "'");
}

/**
* Exports the conversation at {@code inputFilePath} as JSON to {@code outputFilePath}.
* @param inputFilePath The input file path.
* @param outputFilePath The output file path.
* @param pr ParseResult containing matched command options.
* @throws Exception Thrown when something bad happens.
*/
public void exportConversation(ParseResult pr) throws Exception {

if( !pr.hasMatchedOption("--inputFilePath") ){
throw new Exception("Does not have i/p option");
}

if( !pr.hasMatchedOption("--outputFilePath") ){
throw new Exception("Does not have o/p option");
}

String inputFilePath = pr.matchedOption("--inputFilePath").getValue();
String outputFilePath = pr.matchedOption("outputFilePath").getValue();

Conversation conversation = this.readConversation(inputFilePath);

IConversationArgumentExecution cae = new ConversationArgumentExecution();
conversation = cae.executue(conversation, pr);

this.writeConversation(conversation, outputFilePath);

// TODO: Add more logging...
System.out.println("Conversation exported from '" + inputFilePath + "' to '" + outputFilePath);
System.out.println("Conversation successfully exported from '" + inputFilePath + "' to '" + outputFilePath + "'");
}

/**
Expand All @@ -83,23 +122,22 @@ public void exportConversation(String inputFilePath, String outputFilePath) thro
* @throws Exception Thrown when something bad happens.
*/
public void writeConversation(Conversation conversation, String outputFilePath) throws Exception {
// TODO: Do we need both to be resources, or will buffered writer close the stream?
try (OutputStream os = new FileOutputStream(outputFilePath, true);
try (OutputStream os = new FileOutputStream(outputFilePath, false);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os))) {

// TODO: Maybe reuse this? Make it more testable...
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Instant.class, new InstantSerializer());

Gson g = gsonBuilder.create();

bw.write(g.toJson(conversation));
} catch (FileNotFoundException e) {
// TODO: Maybe include more information?
throw new IllegalArgumentException("The file was not found.");
e.printStackTrace();
throw new IllegalArgumentException(outputFilePath + " file was not found.");
} catch (IOException e) {
// TODO: Should probably throw different exception to be more meaningful :/
throw new Exception("Something went wrong");
e.printStackTrace();
throw new IOException("IOException thrown in writing o/p JSON.");
}
}

Expand All @@ -121,14 +159,21 @@ public Conversation readConversation(String inputFilePath) throws Exception {
while ((line = r.readLine()) != null) {
String[] split = line.split(" ");

messages.add(new Message(Instant.ofEpochSecond(Long.parseUnsignedLong(split[0])), split[1], split[2]));
//This bit was apparently wrong, the content of the message was only ever the first word.
messages.add(new Message(
Instant.ofEpochSecond(Long.parseUnsignedLong(split[0])),
split[1],
String.join(" ", Arrays.copyOfRange(split, 2, split.length))
));
}

return new Conversation(conversationName, messages);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("The file was not found.");
e.printStackTrace();
throw new IllegalArgumentException(inputFilePath + " file was not found.");
} catch (IOException e) {
throw new Exception("Something went wrong");
e.printStackTrace();
throw new IOException("IOException thrown in reading i/p file.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mindlinksoft.recruitment.mychat;

import java.util.List;

import picocli.CommandLine.Option;
import picocli.CommandLine.Command;

Expand All @@ -20,4 +22,22 @@ public final class ConversationExporterConfiguration {
*/
@Option(names = { "-o", "--outputFilePath" }, description = "The path to the output JSON file.", required = true)
public String outputFilePath;

/**
* Filters msgs to only those sent by given user.
*/
@Option(names = { "-u", "--filterByUser"}, description = "Filters messeges o/p to JSON file to only those sent by provided user.")
public String userIdFilter;

/**
* Filters msgs to only those containing keyword.
*/
@Option(names = { "-k", "--filterByKeyword"}, description = "Filters messeges o/p to JSON file to only those containing the keyword.")
public String keyWordFilter;

/**
* Replaces any word that is blacklisted with *redacted*.
*/
@Option(names = { "-b", "--blacklist"}, description = "If word is in o/p JSON file, it is replaced with *redacted*")
public List<String> blacklist;
}
Loading