Skip to content

Commit

Permalink
Merge pull request #4 from Mam-sDeveloper-ITMO/lab6
Browse files Browse the repository at this point in the history
Lab 6 improvements
  • Loading branch information
butvinm authored Jun 6, 2023
2 parents 0423fac + 7122478 commit 08d9c10
Show file tree
Hide file tree
Showing 58 changed files with 820 additions and 385 deletions.
13 changes: 0 additions & 13 deletions CollectionManager/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
### Roadmap

- [ ] Add ability to create empty collection if file not specified and ask destination on saving

- [ ] Better human string representation

- [ ] Search command

- [ ] PrintFields command

- [ ] Handle Ctrl+C to skip command

- [ ] Check all corner cases and bugs
5 changes: 3 additions & 2 deletions CollectionManager/cliapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</parent>
<artifactId>cliapp</artifactId>
<name>Command line client</name>
<version>2.4</version>

<dependencies>
<dependency>
Expand Down Expand Up @@ -39,12 +40,12 @@
<dependency>
<groupId>mamsdeveloper.itmo</groupId>
<artifactId>collections-service</artifactId>
<version>1.0</version>
<version>1.3</version>
</dependency>
<dependency>
<groupId>mamsdeveloper.itmo</groupId>
<artifactId>textlocale</artifactId>
<version>1.0</version>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
14 changes: 6 additions & 8 deletions CollectionManager/cliapp/src/main/java/cliapp/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package cliapp;

import static textlocale.TextLocale._;

import java.io.IOException;

import adapter.Adapter;
Expand All @@ -27,7 +25,7 @@
import cliapp.commands.collection.TailCommand;
import cliapp.commands.collection.UpdateElementCommand;
import humandeque.manager.CollectionManager;
import textlocale.TextLocale;
import textlocale.TextSupplier;

/**
* The main application class for running the space collection manager.
Expand All @@ -41,18 +39,18 @@ public class App {
*/
public static void main(String[] args) {
try {
TextLocale.loadPackage("cliapp");
TextLocale.setLocale("en");
} catch (IOException e) {
TextsManager.updateTexts();
} catch (Exception e) {
System.out.println("Failed to load locale");
System.exit(1);
}
TextSupplier ts = TextsManager.getTexts()::getText;

Adapter serviceAdapter = null;
try {
serviceAdapter = new Adapter("127.0.0.1", 8000);
} catch (Exception e) {
System.out.println(_("app.ConnectLater"));
System.out.println(ts.t("app.ConnectLater"));
System.exit(1);
}
Integer userId = 0;
Expand All @@ -69,7 +67,7 @@ public static void main(String[] args) {
try {
manager = new RemoteManager(serviceAdapter, userId);
} catch (Exception e) {
System.out.println(_("app.ConnectLater"));
System.out.println(ts.t("app.ConnectLater"));
System.exit(1);
}

Expand Down
39 changes: 39 additions & 0 deletions CollectionManager/cliapp/src/main/java/cliapp/TextsManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cliapp;

import lombok.Getter;
import lombok.Setter;
import textlocale.TextLocale;
import textlocale.TextPackage;

/**
* Manager for texts resources.
*/
public class TextsManager {
/**
* Current locale.
*/
@Getter
@Setter
private static String locale = "en";

/**
* Texts package.
*/
@Getter
private static TextPackage texts;

/**
* Static class.
*/
private TextsManager() {
}

/**
* Update texts.
*
* @throws Exception if an error occurs on package loading.
*/
public static void updateTexts() throws Exception {
TextsManager.texts = TextLocale.loadPackage("cliapp", () -> TextsManager.locale);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package cliapp.cliclient;

import static textlocale.TextLocale._;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -10,6 +8,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cliapp.TextsManager;
import cliapp.cliclient.exceptions.CommandNotFoundError;
import cliapp.cliclient.exceptions.InlineParamsError;
import cliapp.utils.TextColor;
Expand Down Expand Up @@ -229,7 +228,7 @@ public void executeCommand(Command command, List<String> inlineParams) {
* Waits for user input and tries to resolve and execute a command.
*/
public void runClient() {
System.out.println(_("cats.Cat3"));
System.out.println(TextsManager.getTexts().getPackage("cats").getText("Cat3"));
System.out.println();
while (true) {
// ASAP IMMEDIATELY REFACTOR
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package cliapp.cliclient;

import static textlocale.TextLocale._;

import java.util.Map;
import java.util.function.Supplier;

import cliapp.TextsManager;
import cliapp.utils.TextColor;
import commands.requirements.Requirement;
import commands.requirements.RequirementsPipeline;
import commands.requirements.exceptions.RequirementAskError;
import commands.requirements.exceptions.ValidationError;
import lombok.RequiredArgsConstructor;
import textlocale.TextSupplier;

/**
* This class provides requirements for commands by asking the user for input.
*/
@RequiredArgsConstructor
public class UserInputPipeline implements RequirementsPipeline {
static TextSupplier ts = TextsManager.getTexts().getPackage("cliclient.user_input_pipeline")::getText;

/**
* Map with names of static requirements and inline params that user input.
*/
Expand Down Expand Up @@ -45,7 +47,7 @@ private void showAttemptWarning(Requirement<?, ?> requirement, Integer attempts,
Exception exceptionWrapper = new RequirementAskError(requirement.getName(), error);
System.out.println(TextColor.getColoredString(exceptionWrapper.getMessage(), TextColor.RED));
// ask to new try with count of left attempts
String askText = _("cliclient.cliclient.AskRequirementWithAttempts").formatted(attempts);
String askText = ts.t("AskRequirementWithAttempts", attempts);
System.out.println();
System.out.println(TextColor.getColoredString(askText, TextColor.CYAN));
}
Expand All @@ -67,10 +69,7 @@ private <I, O> O askRequirementWithAttempts(Requirement<I, O> requirement)
int attempts = askRequirementAttempts;
do {
System.out.print(
_("cliclient.cliclient.AskRequirement").formatted(
requirement.getName(),
requirement.getDescription()));

ts.t("AskRequirement", requirement.getName(), requirement.getDescription()));
try {
// try get value
return requirement.getValue((I) userInputSupplier.get());
Expand All @@ -83,7 +82,7 @@ private <I, O> O askRequirementWithAttempts(Requirement<I, O> requirement)

// if attempt exceeded
throw new RequirementAskError(requirement.getName(),
_("cliclient.cliclient.AskRequirementAttemptsError"));
ts.t("AskRequirementAttemptsError"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package cliapp.cliclient.exceptions;

import static textlocale.TextLocale._;

import cliapp.TextsManager;
import textlocale.TextSupplier;
/**
* Exception thrown when the CLI client cannot find a command by its trigger.
*/
public class CommandNotFoundError extends Exception {
static TextSupplier ts = TextsManager.getTexts().getPackage("cliclient.exceptions")::getText;

/**
* Constructs a new CommandNotFoundError with the specified trigger.
*
* @param trigger the trigger that was not found
*/
public CommandNotFoundError(String trigger) {
super(_("cliclient.cliclient.CommandNotFoundError").formatted(trigger));
super(ts.t("CommandNotFoundError", trigger));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package cliapp.cliclient.exceptions;

import static textlocale.TextLocale._;

import java.util.List;

import cliapp.TextsManager;
import commands.requirements.Requirement;
import lombok.RequiredArgsConstructor;
import textlocale.TextSupplier;

/**
* Exception thrown when the number of inline parameters is not equal to the
* expected number of command static requirements.
*/
@RequiredArgsConstructor
public class InlineParamsError extends Exception {
static TextSupplier ts = TextsManager.getTexts().getPackage("cliclient.exceptions")::getText;

private final List<Requirement<?, ?>> requirements;

/**
Expand All @@ -31,6 +33,6 @@ public String getMessage() {
.reduce((name1, name2) -> name1 + " " + name2)
.orElse("");

return _("cliclient.cliclient.InlineParamsError").formatted(requirementsNames);
return ts.t("InlineParamsError", requirementsNames);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cliapp.collection;

import static textlocale.TextLocale._;

import java.io.Serializable;
import java.util.Map;

import adapter.Adapter;
import adapter.exceptions.ReceiveResponseFailed;
import adapter.exceptions.SendRequestFailed;
import adapter.exceptions.SocketInitFailed;
import cliapp.TextsManager;
import collections.service.api.StatusCodes;
import humandeque.HumanDeque;
import humandeque.TextResources.Manager.ExceptionsResources;
Expand All @@ -23,12 +22,15 @@
import lombok.SneakyThrows;
import models.Human;
import server.responses.Response;
import textlocale.TextSupplier;

/**
* That manager execute all manipulations on client and store collection in
* local csv file
*/
public class RemoteManager extends CollectionManager {
static TextSupplier ts = TextsManager.getTexts().getPackage("collection")::getText;

/**
* user id
*/
Expand Down Expand Up @@ -159,7 +161,7 @@ private Response sendRequestOrFail(String method, Map<String, Serializable> data
try {
return serviceAdapter.triggerServer(method, data);
} catch (SocketInitFailed | SendRequestFailed | ReceiveResponseFailed e) {
throw new ManipulationError(_("collection.remotemanager.FailedRequest"));
throw new ManipulationError(ts.t("remotemanager.FailedRequest"));
}
}
}
Loading

0 comments on commit 08d9c10

Please sign in to comment.