Skip to content

Commit 7a6b05c

Browse files
authored
Merge pull request #27 from jenkinsci/feature/as400messagewrapper
Extend AS400Message to convert message data
2 parents bfc2814 + 60eb81a commit 7a6b05c

File tree

5 files changed

+93
-19
lines changed

5 files changed

+93
-19
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ IFS transfers.
2222
- [OnMSGW](#onmsgw)
2323
- [Returned objects](#returned-objects)
2424
- [CallResult](#callresult)
25+
- [IBMiMessage](#ibmimessage)
2526
- [IBMiJob](#ibmijob)
2627
- [SQLResult](#sqlresult)
2728
- [SQLColumn](#sqlcolumn)
@@ -347,15 +348,20 @@ if(result.successful) {
347348
## Returned objects
348349

349350
### CallResult
350-
351-
| Methods | Return type | Description |
352-
|:-------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
353-
| getMessages() | [`List`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html)<[AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html)> | Returns the list of [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) generated during the command execution. |
354-
| getMessage(`String` messageId) | [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) | Look for the first [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) whose ID is `messageId` and returns it. Returns `null` if not found. |
355-
| getLastMessage() | [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) | Returns the last [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) generated by the command or `null` if there is no message. |
356-
| getPrettyMessages() | `String` | Returns a `String` resulting from the concatenation of every [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) returned by the command, one per line, formatted using this pattern: `[{id}][{severity}] {text}`. |
357-
| isSuccessful() | `boolean` | Returns `true ` if the command execution was successful, `false` otherwise. |
358-
| getSubmittedJobs() | [`List`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html)<[IBMiJob](#ibmijob)> | Returns a list of [IBMiJob](#ibmijob) found by browsing the `CPC1221` messages from the call result. |
351+
| Methods | Return type | Description |
352+
|:-------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
353+
| getMessages() | [`List`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html)<[IBMiMessage](#ibmimessage)> | Returns the list of [IBMiMessage](#ibmimessage) generated during the command execution. |
354+
| getMessage(`String` messageId) | [IBMiMessage](#ibmimessage) | Look for the first [IBMiMessage](#ibmimessage) whose ID is `messageId` and returns it. Returns `null` if not found. |
355+
| getLastMessage() | [IBMiMessage](#ibmimessage) | Returns the last [IBMiMessage](#ibmimessage) generated by the command or `null` if there is no message. |
356+
| getPrettyMessages() | `String` | Returns a `String` resulting from the concatenation of every [IBMiMessage](#ibmimessage) returned by the command, one per line, formatted using this pattern: `[{id}][{severity}] {text}`. |
357+
| isSuccessful() | `boolean` | Returns `true ` if the command execution was successful, `false` otherwise. |
358+
| getSubmittedJobs() | [`List`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html)<[IBMiJob](#ibmijob)> | Returns a list of [IBMiJob](#ibmijob) found by browsing the `CPC1221` messages from the call result. |
359+
360+
### IBMiMessage
361+
Extends [AS400Message](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400Message.html) and adds the method(s) below.
362+
| Methods | Return type | Description |
363+
|:----------------------|:------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
364+
| getSubstitutionDataAsString() | `String` | The AS400 message substitution data converted using current connection's [CharConverter](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/CharConverter.html). |
359365

360366
### IBMiJob
361367

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.ibm.as400.access;
2+
3+
import org.jenkinsci.plugins.ibmisteps.model.IBMi;
4+
5+
import java.io.Serial;
6+
import java.io.Serializable;
7+
import java.util.Calendar;
8+
import java.util.Objects;
9+
import java.util.function.Supplier;
10+
11+
public class IBMiMessage extends AS400Message implements Serializable {
12+
@Serial
13+
private static final long serialVersionUID = 4119612591789660630L;
14+
15+
private final transient Supplier<String> substitutionDataRetriever;
16+
private final Calendar date;
17+
18+
public IBMiMessage(final IBMi ibmi, final AS400Message as400Message) {
19+
super(as400Message.getID(),
20+
as400Message.getText(),
21+
as400Message.getFileName(),
22+
as400Message.getLibraryName(),
23+
as400Message.getSeverity(),
24+
as400Message.getType(),
25+
as400Message.getSubstitutionData(),
26+
as400Message.getHelp());
27+
this.setDefaultReply(as400Message.getDefaultReply());
28+
this.date = as400Message.getDate();
29+
this.substitutionDataRetriever = () -> ibmi.getCharConverter().byteArrayToString(as400Message.getSubstitutionData());
30+
}
31+
32+
@Override
33+
public Calendar getDate() {
34+
return date;
35+
}
36+
37+
public String getSubstitutionDataAsString() {
38+
return substitutionDataRetriever != null ? substitutionDataRetriever.get() : "";
39+
}
40+
41+
@Override
42+
public boolean equals(final Object o) {
43+
return o instanceof AS400Message m &&
44+
checkEquals(getID(), m.getID()) &&
45+
checkEquals(getText(), m.getText()) &&
46+
checkEquals(getFileName(), m.getFileName()) &&
47+
checkEquals(getLibraryName(), m.getLibraryName()) &&
48+
checkEquals(getSeverity(), m.getSeverity()) &&
49+
checkEquals(getType(), m.getType()) &&
50+
checkEquals(getHelp(), m.getHelp()) &&
51+
checkEquals(getDate(), m.getDate()) &&
52+
checkEquals(getDefaultReply(), m.getDefaultReply());
53+
}
54+
55+
private boolean checkEquals(Object a, Object b) {
56+
if (a == null) {
57+
return b == null;
58+
} else {
59+
return a.equals(b);
60+
}
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(getID(), getText(), getFileName(), getLibraryName(), getSeverity(), getType(), getHelp(), getDate(), getDefaultReply());
66+
}
67+
}

src/main/java/org/jenkinsci/plugins/ibmisteps/model/CallResult.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
package org.jenkinsci.plugins.ibmisteps.model;
22

33
import com.ibm.as400.access.AS400Message;
4+
import com.ibm.as400.access.IBMiMessage;
45
import edu.umd.cs.findbugs.annotations.CheckForNull;
56

67
import java.io.Serial;
78
import java.io.Serializable;
89
import java.text.MessageFormat;
910
import java.util.ArrayList;
10-
import java.util.Collections;
1111
import java.util.List;
1212
import java.util.regex.Matcher;
1313
import java.util.regex.Pattern;
1414
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
1516

1617
public class CallResult implements Serializable {
1718
@Serial
1819
private static final long serialVersionUID = -7184769518123385346L;
1920

2021
private static final Pattern JOB_PATTERN = Pattern.compile(" (\\d{1,6})/([^/ ]{1,10})/([^/ ]{1,10}) ");
2122

22-
private final List<AS400Message> messages = new ArrayList<>();
23+
private final List<IBMiMessage> messages = new ArrayList<>();
2324
private final boolean successful;
2425

25-
public CallResult(final boolean successful, final AS400Message[] ibmiMessages) {
26+
public CallResult(final IBMi ibmi, final boolean successful, final AS400Message[] as400Messages) {
2627
this.successful = successful;
27-
Collections.addAll(messages, ibmiMessages);
28+
Stream.of(as400Messages).map(message -> new IBMiMessage(ibmi, message)).forEach(messages::add);
2829
}
2930

30-
public AS400Message getMessage(final String messageId) {
31+
public IBMiMessage getMessage(final String messageId) {
3132
return messages.stream() //
3233
.filter(m -> m.getID().equalsIgnoreCase(messageId))//
3334
.findFirst() //
3435
.orElse(null);
3536
}
3637

37-
public List<AS400Message> getMessages() {
38+
public List<IBMiMessage> getMessages() {
3839
return messages;
3940
}
4041

4142
@CheckForNull
42-
public AS400Message getLastMessage() {
43+
public IBMiMessage getLastMessage() {
4344
return messages.isEmpty() ? null : messages.get(messages.size() - 1);
4445
}
4546

src/main/java/org/jenkinsci/plugins/ibmisteps/model/IBMi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public synchronized CallResult executeCommand(@CheckForNull String command)
328328
final CommandCall commandCall = new CommandCall(ibmiConnection, command);
329329
commandCall.setMessageOption(AS400Message.MESSAGE_OPTION_ALL);
330330
final boolean executionOK = commandCall.run();
331-
return new CallResult(executionOK, commandCall.getMessageList());
331+
return new CallResult(this, executionOK, commandCall.getMessageList());
332332
}
333333

334334
public String getOSVersion() throws AS400SecurityException, IOException {

src/main/java/org/jenkinsci/plugins/ibmisteps/steps/IBMiCommandStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.jenkinsci.plugins.ibmisteps.steps;
22

3-
import com.ibm.as400.access.AS400Message;
43
import com.ibm.as400.access.AS400SecurityException;
54
import com.ibm.as400.access.ErrorCompletingRequestException;
5+
import com.ibm.as400.access.IBMiMessage;
66
import edu.umd.cs.findbugs.annotations.NonNull;
77
import hudson.AbortException;
88
import hudson.Extension;
@@ -49,7 +49,7 @@ protected CallResult runOnIBMi(final StepContext context, final LoggerWrapper lo
4949
logger.log(Messages.IBMICommandStep_running(command));
5050

5151
final CallResult result = ibmi.executeCommand(command);
52-
final AS400Message lastMessage = result.getLastMessage();
52+
final IBMiMessage lastMessage = result.getLastMessage();
5353
if (result.isSuccessful()) {
5454
logger.log(Messages.IBMICommandStep_succeeded(command));
5555
} else {

0 commit comments

Comments
 (0)