Skip to content

Commit 1e19970

Browse files
author
AlexanderLitus
committed
Merge pull request #123 from SpineEventEngine/schedule-commands
Schedule command delivering
2 parents f07b08b + e0e8215 commit 1e19970

File tree

25 files changed

+927
-267
lines changed

25 files changed

+927
-267
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ allprojects {
1515
apply plugin: 'jacoco'
1616

1717
group = 'org.spine3'
18-
version = '0.2'
18+
version = '0.3'
1919
}
2020

2121
project.ext {

client/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
55
}
66
dependencies {
7-
classpath group: 'org.spine3.tools', name: 'protobuf-plugin', version: '1.2', changing: true
7+
classpath group: 'org.spine3.tools', name: 'protobuf-plugin', version: '1.3.1', changing: true
88
}
99
}
1010

client/src/main/java/org/spine3/base/Commands.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.google.common.base.Predicate;
2424
import com.google.protobuf.Descriptors.FileDescriptor;
25+
import com.google.protobuf.Duration;
2526
import com.google.protobuf.Message;
2627
import com.google.protobuf.Timestamp;
2728
import org.spine3.protobuf.EntityPackagesMap;
@@ -39,6 +40,7 @@
3940
import static com.google.common.base.Preconditions.checkArgument;
4041
import static com.google.common.base.Preconditions.checkNotNull;
4142
import static com.google.protobuf.util.TimeUtil.getCurrentTime;
43+
import static org.spine3.validate.Validate.isNotDefault;
4244

4345
/**
4446
* Client-side utilities for working with commands.
@@ -48,11 +50,12 @@
4850
public class Commands {
4951

5052
/**
51-
* A substring which the {@code .proto} file containing commands must have in its name.
53+
* A suffix which the {@code .proto} file containing commands must have in its name.
5254
*/
53-
public static final String COMMANDS_FILE_SUBSTRING = "commands";
55+
public static final String FILE_NAME_SUFFIX = "commands";
5456

55-
private static final char PROTO_FILE_SEPARATOR = '/';
57+
private static final char FILE_PATH_SEPARATOR = '/';
58+
private static final char FILE_EXTENSION_SEPARATOR = '.';
5659

5760
private Commands() {}
5861

@@ -193,13 +196,14 @@ public static String formatMessageTypeAndId(String format, Message commandMessag
193196
* Checks if the file is for commands.
194197
*
195198
* @param file a descriptor of a {@code .proto} file to check
196-
* @return {@code true} if the file name contains {@link #COMMANDS_FILE_SUBSTRING} substring, {@code false} otherwise
199+
* @return {@code true} if the file name ends with the {@link #FILE_NAME_SUFFIX}, {@code false} otherwise
197200
*/
198201
public static boolean isCommandsFile(FileDescriptor file) {
199202
final String fqn = file.getName();
200-
final int startIndexOfFileName = fqn.lastIndexOf(PROTO_FILE_SEPARATOR) + 1;
201-
final String fileName = fqn.substring(startIndexOfFileName);
202-
final boolean isCommandsFile = fileName.contains(COMMANDS_FILE_SUBSTRING);
203+
final int startIndexOfFileName = fqn.lastIndexOf(FILE_PATH_SEPARATOR) + 1;
204+
final int endIndexOfFileName = fqn.lastIndexOf(FILE_EXTENSION_SEPARATOR);
205+
final String fileName = fqn.substring(startIndexOfFileName, endIndexOfFileName);
206+
final boolean isCommandsFile = fileName.endsWith(FILE_NAME_SUFFIX);
203207
return isCommandsFile;
204208
}
205209

@@ -216,4 +220,20 @@ public static boolean isEntityFile(FileDescriptor file) {
216220
final boolean isCommandForEntity = EntityPackagesMap.contains(protoPackage);
217221
return isCommandForEntity;
218222
}
223+
224+
/**
225+
* Checks if the command is scheduled to be delivered later.
226+
*
227+
* @param command a command to check
228+
* @return {@code true} if the command context has a scheduling option set, {@code false} otherwise
229+
*/
230+
public static boolean isScheduled(Command command) {
231+
final Schedule schedule = command.getContext().getSchedule();
232+
final Duration delay = schedule.getAfter();
233+
if (isNotDefault(delay)) {
234+
checkArgument(delay.getSeconds() > 0, "Command delay seconds must be a positive value.");
235+
return true;
236+
}
237+
return false;
238+
}
219239
}

client/src/main/java/org/spine3/base/Responses.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import com.google.protobuf.Empty;
2424

25+
import static org.spine3.protobuf.Messages.fromAny;
26+
2527
/**
2628
* Utilities for working with {@link org.spine3.base.Response} objects.
2729
*
@@ -47,6 +49,8 @@ public static Response ok() {
4749
}
4850

4951
/**
52+
* Checks if the response is OK.
53+
*
5054
* @return {@code true} if the passed response represents `ok` status,
5155
* {@code false} otherwise
5256
*/
@@ -56,13 +60,31 @@ public static boolean isOk(Response response) {
5660
}
5761

5862
/**
63+
* Checks if the response is `unsupported command`.
64+
*
5965
* @return {@code true} if the passed response represents `unsupported command` error,
6066
* {@code false} otherwise
6167
*/
6268
public static boolean isUnsupportedCommand(Response response) {
6369
if (response.getStatusCase() == Response.StatusCase.ERROR) {
6470
final Error error = response.getError();
65-
return error.getCode() == CommandValidationError.UNSUPPORTED_COMMAND.getNumber();
71+
final boolean isUnsupported = error.getCode() == CommandValidationError.UNSUPPORTED_COMMAND.getNumber();
72+
return isUnsupported;
73+
}
74+
return false;
75+
}
76+
77+
/**
78+
* Checks if the response is `invalid command`.
79+
*
80+
* @return {@code true} if the passed response represents `invalid command` error,
81+
* {@code false} otherwise
82+
*/
83+
public static boolean isInvalidCommand(Response response) {
84+
if (response.getStatusCase() == Response.StatusCase.FAILURE) {
85+
final ValidationFailure failure = fromAny(response.getFailure().getInstance());
86+
final boolean isInvalid = !failure.getConstraintViolationList().isEmpty();
87+
return isInvalid;
6688
}
6789
return false;
6890
}

0 commit comments

Comments
 (0)