Skip to content

Commit d856d23

Browse files
Merge pull request #110 from TanyaEf/bugFix
Bug fixing of Report service
2 parents 9f7d2df + cbc45c3 commit d856d23

File tree

8 files changed

+393
-203
lines changed

8 files changed

+393
-203
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ OperationResult<InputStream> result = client
204204
.run();
205205
InputStream report = result.getEntity();
206206
```
207+
You can set format of report as String as well(name of format is case insensitive):
208+
```java
209+
OperationResult<InputStream> result = client
210+
.authenticate("jasperadmin", "jasperadmin")
211+
.reportingService()
212+
.report("/reports/samples/Cascading_multi_select_report")
213+
.prepareForRun("HTML", 1)
214+
.parameter("Cascading_name_single_select", "A & U Stalker Telecommunications, Inc")
215+
.run();
216+
```
207217
Also you can use this method to run report with several values for the same parameter. In this case new values of the parameter are added to the previous ones (new values do not replace previous values of the parameter):
208218
```java
209219
OperationResult<InputStream> result = client
@@ -217,6 +227,7 @@ OperationResult<InputStream> result = client
217227
.parameter("Country_multi_select", "USA")
218228
.run();
219229
```
230+
Please notice, if you pass zero as number of page, you will get all pages of report.
220231
In this mode you don't need to work in one session. In the above code we specified report URI, format in which we want to get a report and some report parameters. As we a result we got `InputStream` instance. In synchronous mode as a response you get a report itself while in asynchronous you get just a descriptor with report ID which you can use to download report afer it will be ready.
221232

222233
In order to run a report in asynchronous mode, you need firstly build `ReportExecutionRequest` instance and specify all the parameters needed to launch a report. The response from the server is the `ReportExecutionDescriptor` instance which contains the request ID needed to track the execution until completion and others report parameters. Here's the code to run a report:
@@ -226,7 +237,7 @@ ReportExecutionRequest request = new ReportExecutionRequest();
226237
request.setReportUnitUri("/reports/samples/StandardChartsReport");
227238
request
228239
.setAsync(true) //this means that report will be run on server asynchronously
229-
.setOutputFormat("html"); //report can be requested in different formats e.g. html, pdf, etc.
240+
.setOutputFormat(ReportOutputFormat.HTML); //report can be requested in different formats e.g. html, pdf, etc.
230241

231242
OperationResult<ReportExecutionDescriptor> operationResult =
232243
session //pay attention to this, all requests are in the same session!!!
@@ -236,6 +247,14 @@ OperationResult<ReportExecutionDescriptor> operationResult =
236247
reportExecutionDescriptor = operationResult.getEntity();
237248
```
238249
In the above code we've created `ReportExecutionRequest` instance and sent it to JR server through the `newReportExecutionRequest` method. As a response we've got `OperationResult` instance which contains HTTP response wrapper and instance of `ReportExecutionDescriptor` which we can get with `operationResult.getEntity()`.
250+
Also you can set output format as String:
251+
```java
252+
ReportExecutionRequest request = new ReportExecutionRequest();
253+
request.setReportUnitUri("/reports/samples/StandardChartsReport");
254+
request
255+
.setAsync(true)
256+
.setOutputFormat("html");
257+
```
239258
####Requesting report execution status:
240259
After you've got `ReportExecutionDescriptor` you can request for the report execution status:
241260
```java
@@ -294,7 +313,7 @@ for(AttachmentDescriptor attDescriptor : htmlExportDescriptor.getAttachments()){
294313
After running a report and downloading its content in a given format, you can request the same report in other formats. As with exporting report formats through the user interface, the report does not run again because the export process is independent of the report.
295314
```java
296315
ExportExecutionOptions exportExecutionOptions = new ExportExecutionOptions()
297-
.setOutputFormat("pdf")
316+
.setOutputFormat(ReportOutputFormat.PDF)
298317
.setPages("3");
299318

300319
OperationResult<ExportExecutionDescriptor> operationResult =

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/ReportingServiceIT.java

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.jaspersoft.jasperserver.jaxrs.client.core.JasperserverRestClient;
44
import com.jaspersoft.jasperserver.jaxrs.client.core.RestClientConfiguration;
5+
import com.jaspersoft.jasperserver.jaxrs.client.core.Session;
56
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult;
7+
import com.jaspersoft.jasperserver.jaxrs.client.dto.reports.ReportExecutionDescriptor;
8+
import com.jaspersoft.jasperserver.jaxrs.client.dto.reports.ReportExecutionRequest;
69
import java.io.InputStream;
710
import junit.framework.Assert;
811
import org.testng.annotations.AfterMethod;
@@ -17,19 +20,20 @@ public class ReportingServiceIT {
1720

1821
private RestClientConfiguration configuration;
1922
private JasperserverRestClient client;
23+
private Session session;
2024

2125
@BeforeMethod
2226
public void before() {
2327
configuration = new RestClientConfiguration("http://localhost:4444/jasperserver-pro");
2428
client = new JasperserverRestClient(configuration);
29+
session = client.authenticate("superuser", "superuser");
2530
}
2631

2732
@Test
28-
public void should_return_proper_entity_if_pass_jrprint_report_output_format() {
33+
public void should_return_proper_entity_if_pass_pdf_report_output_format() {
2934

3035
/** When **/
31-
OperationResult<InputStream> result = client
32-
.authenticate("superuser", "superuser")
36+
OperationResult<InputStream> result = session
3337
.reportingService()
3438
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
3539
.prepareForRun(ReportOutputFormat.PDF, 1)
@@ -45,6 +49,158 @@ public void should_return_proper_entity_if_pass_jrprint_report_output_format() {
4549

4650
}
4751

52+
@Test
53+
public void should_return_proper_entity_if_passed_number_of_pages_zero() {
54+
55+
/** When **/
56+
OperationResult<InputStream> result = session
57+
.reportingService()
58+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
59+
.prepareForRun(ReportOutputFormat.PDF, 0)
60+
.parameter("Cascading_state_multi_select", "CA")
61+
.parameter("Cascading_state_multi_select", "OR", "WA")
62+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
63+
.parameter("Country_multi_select", "USA")
64+
.run();
65+
66+
InputStream entity = result.getEntity();
67+
/** Then **/
68+
Assert.assertNotNull(entity);
69+
70+
}
71+
@Test
72+
public void should_return_proper_entity_if_pass_string_output_format() {
73+
74+
/** When **/
75+
OperationResult<InputStream> result = session
76+
.reportingService()
77+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
78+
.prepareForRun("PDF", 1)
79+
.parameter("Cascading_state_multi_select", "CA")
80+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
81+
.parameter("Country_multi_select", "USA")
82+
.run();
83+
84+
InputStream entity = result.getEntity();
85+
/** Then **/
86+
Assert.assertNotNull(entity);
87+
88+
}
89+
90+
@Test
91+
public void should_return_proper_entity_if_passed_wrong_number_of_pages() {
92+
93+
/** When **/
94+
OperationResult<InputStream> result = session
95+
.reportingService()
96+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
97+
.prepareForRun("PDF", 0,-1,1)
98+
.parameter("Cascading_state_multi_select", "CA")
99+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
100+
.parameter("Country_multi_select", "USA")
101+
.run();
102+
103+
InputStream entity = result.getEntity();
104+
/** Then **/
105+
Assert.assertNotNull(entity);
106+
107+
}
108+
109+
@Test
110+
public void should_return_proper_entity_if_passed_all_wrong_number_of_pages() {
111+
112+
/** When **/
113+
OperationResult<InputStream> result = session
114+
.reportingService()
115+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
116+
.prepareForRun("PDF", 0,-1)
117+
.parameter("Cascading_state_multi_select", "CA")
118+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
119+
.parameter("Country_multi_select", "USA")
120+
.run();
121+
122+
InputStream entity = result.getEntity();
123+
/** Then **/
124+
Assert.assertNotNull(entity);
125+
126+
}
127+
128+
@Test
129+
public void should_return_proper_entity_without_numbers_of_pages() {
130+
131+
/** When **/
132+
OperationResult<InputStream> result = session
133+
.reportingService()
134+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
135+
.prepareForRun(ReportOutputFormat.PDF)
136+
.parameter("Cascading_state_multi_select", "CA")
137+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
138+
.parameter("Country_multi_select", "USA")
139+
.run();
140+
141+
InputStream entity = result.getEntity();
142+
/** Then **/
143+
Assert.assertNotNull(entity);
144+
145+
}
146+
147+
@Test
148+
public void should_return_proper_entity_with_page_range() {
149+
150+
/** When **/
151+
OperationResult<InputStream> result = session
152+
.reportingService()
153+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
154+
.prepareForRun(ReportOutputFormat.PDF)
155+
.parameter("Cascading_state_multi_select", "CA")
156+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
157+
.parameter("Country_multi_select", "USA")
158+
.run();
159+
160+
InputStream entity = result.getEntity();
161+
/** Then **/
162+
Assert.assertNotNull(entity);
163+
164+
}
165+
166+
@Test
167+
public void should_return_proper_entity_in_async_mode() {
168+
169+
/** When **/
170+
ReportExecutionRequest request = new ReportExecutionRequest();
171+
request.setReportUnitUri("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic");
172+
request
173+
.setAsync(true)
174+
.setOutputFormat(ReportOutputFormat.HTML);
175+
176+
OperationResult<ReportExecutionDescriptor> operationResult = session
177+
.reportingService()
178+
.newReportExecutionRequest(request);
179+
180+
ReportExecutionDescriptor reportExecutionDescriptor = operationResult.getEntity();
181+
/** Then **/
182+
Assert.assertNotNull(reportExecutionDescriptor);
183+
}
184+
185+
@Test
186+
public void should_return_proper_entity_in_async_mode_if_format_is_string() {
187+
188+
/** When **/
189+
ReportExecutionRequest request = new ReportExecutionRequest();
190+
request.setReportUnitUri("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic");
191+
request
192+
.setAsync(true)
193+
.setOutputFormat("html");
194+
195+
OperationResult<ReportExecutionDescriptor> operationResult = session
196+
.reportingService()
197+
.newReportExecutionRequest(request);
198+
199+
ReportExecutionDescriptor reportExecutionDescriptor = operationResult.getEntity();
200+
/** Then **/
201+
Assert.assertNotNull(reportExecutionDescriptor);
202+
}
203+
48204
@AfterMethod
49205
public void after() {
50206
client = null;

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/ReportsAdapter.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,43 @@ public class ReportsAdapter extends AbstractAdapter {
3535

3636
private final String reportUnitUri;
3737

38-
public ReportsAdapter(SessionStorage sessionStorage, String reportUnitUri){
38+
public ReportsAdapter(SessionStorage sessionStorage, String reportUnitUri) {
3939
super(sessionStorage);
4040
this.reportUnitUri = reportUnitUri;
4141
}
4242

43-
public ReorderingReportParametersAdapter reportParameters(){
43+
public ReorderingReportParametersAdapter reportParameters() {
4444
return new ReorderingReportParametersAdapter(sessionStorage, reportUnitUri);
4545
}
4646

47-
public ReportParametersAdapter reportParameters(String mandatoryId, String... otherIds){
47+
public ReportParametersAdapter reportParameters(String mandatoryId, String... otherIds) {
4848
List<String> ids = new ArrayList<String>(Arrays.asList(otherIds));
4949
ids.add(0, mandatoryId);
5050
return new ReportParametersAdapter(sessionStorage, reportUnitUri, ReportParametersUtils.toPathSegment(ids));
5151
}
5252

53-
public RunReportAdapter prepareForRun(ReportOutputFormat format, Integer... pages){
53+
public RunReportAdapter prepareForRun(ReportOutputFormat format, Integer... pages) {
54+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase(), pages);
55+
}
56+
57+
public RunReportAdapter prepareForRun(ReportOutputFormat format, PageRange range) {
58+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase(), range);
59+
}
60+
61+
public RunReportAdapter prepareForRun(ReportOutputFormat format) {
62+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase());
63+
}
64+
public RunReportAdapter prepareForRun(String format, Integer... pages) {
5465
return new RunReportAdapter(sessionStorage, reportUnitUri, format, pages);
5566
}
5667

57-
public RunReportAdapter prepareForRun(ReportOutputFormat format, PageRange range){
68+
public RunReportAdapter prepareForRun(String format, PageRange range) {
5869
return new RunReportAdapter(sessionStorage, reportUnitUri, format, range);
5970
}
6071

72+
public RunReportAdapter prepareForRun(String format) {
73+
return new RunReportAdapter(sessionStorage, reportUnitUri, format);
74+
}
75+
76+
6177
}

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/RunReportAdapter.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,53 @@
2828
import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage;
2929
import com.jaspersoft.jasperserver.jaxrs.client.core.ThreadPoolUtil;
3030
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult;
31-
32-
import javax.ws.rs.core.MultivaluedHashMap;
33-
import javax.ws.rs.core.MultivaluedMap;
3431
import java.io.InputStream;
3532
import java.util.Arrays;
33+
import java.util.Iterator;
34+
import java.util.LinkedList;
3635
import java.util.List;
3736
import java.util.regex.Matcher;
3837
import java.util.regex.Pattern;
38+
import javax.ws.rs.core.MultivaluedHashMap;
39+
import javax.ws.rs.core.MultivaluedMap;
3940

4041
import static java.util.regex.Pattern.compile;
4142

42-
/**
43-
* @author
44-
* @author Tetiana Iefimenko
45-
* */
4643
public class RunReportAdapter extends AbstractAdapter {
4744

4845
private final MultivaluedMap<String, String> params;
4946
private final String reportUnitUri;
50-
private final ReportOutputFormat format;
51-
private String[] pages;
47+
private final String format;
48+
private String[] pages = new String[0];
5249

53-
private RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format) {
50+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format) {
5451
super(sessionStorage);
5552
this.params = new MultivaluedHashMap<String, String>();
5653
this.reportUnitUri = reportUnitUri;
57-
this.format = format;
54+
this.format = format.toLowerCase();
5855
}
5956

60-
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format,
57+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format,
6158
Integer[] pages) {
6259
this(sessionStorage, reportUnitUri, format);
63-
this.pages = toStringArray(pages);
60+
if (pages != null && !(pages.length == 1 && pages[0] == 0)) {
61+
this.pages = toStringArray(validArray(pages));
62+
}
63+
}
64+
65+
private Integer[] validArray (Integer[] pages) {
66+
List<Integer> list = new LinkedList<Integer>(Arrays.asList(pages));
67+
Iterator<Integer> iterator = list.iterator();
68+
while (iterator.hasNext()) {
69+
Integer next = iterator.next();
70+
if (next <= 0) {
71+
iterator.remove();
72+
}
73+
}
74+
return list.toArray(new Integer[list.size()]);
6475
}
6576

66-
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format,
77+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format,
6778
PageRange range) {
6879
this(sessionStorage, reportUnitUri, format);
6980
this.pages = new String[]{range.getRange()};
@@ -106,12 +117,12 @@ private JerseyRequest<InputStream> prepareRunRequest() {
106117
JerseyRequest<InputStream> request = JerseyRequest.buildRequest(
107118
sessionStorage,
108119
InputStream.class,
109-
new String[]{"/reports", reportUnitUri + "." + format.toString().toLowerCase()},
120+
new String[]{"/reports", reportUnitUri + "." + format},
110121
new RunReportErrorHandler());
111122

112123
request.addParams(params);
113124

114-
if (pages != null && pages.length > 0) {
125+
if (pages.length > 0) {
115126
if (pages.length == 1) {
116127
Pattern pattern = compile("^(\\d+)-(\\d+)$");
117128
Matcher matcher = pattern.matcher(pages[0]);

0 commit comments

Comments
 (0)