1:
31:
32: package ;
33:
34: import ;
35: import ;
36: import ;
37:
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
59: public class PrintReportProcessor extends PaginatingReportProcessor
60: implements Pageable
61: {
62: private ReportJob job;
63: private Throwable error;
64:
65: public PrintReportProcessor(final ReportJob job)
66: {
67: super(new GraphicsOutputProcessor(job.getConfiguration()));
68: this.job = job;
69:
70: synchronized(job)
71: {
72: final ReportDataFactory dataFactory = job.getDataFactory();
73: if (dataFactory != null)
74: {
75: dataFactory.open();
76: }
77: }
78: }
79:
80: protected GraphicsOutputProcessor getGraphicsProcessor()
81: {
82: return (GraphicsOutputProcessor) getOutputProcessor();
83: }
84:
85: public boolean isError()
86: {
87: return error != null;
88: }
89:
90: protected ReportJob getJob()
91: {
92: return job;
93: }
94:
95: public void close()
96: {
97: getJob().close();
98: }
99:
100: protected PageDrawable processPage(final int page)
101: throws ReportDataFactoryException,
102: DataSourceException, ReportProcessingException, StateException
103: {
104: final ReportJob job = getJob();
105: synchronized (job)
106: {
107:
108: final PageState state = getPhysicalPageState(page);
109:
110: final ReportTargetState targetState = state.getTargetState();
111: final GraphicsOutputProcessor outputProcessor = getGraphicsProcessor();
112: outputProcessor.setPageCursor(state.getPageCursor());
113: final QueryPhysicalPageInterceptor interceptor =
114: new QueryPhysicalPageInterceptor(outputProcessor.getPhysicalPage(page));
115: outputProcessor.setInterceptor(interceptor);
116:
117: final LibLayoutReportTarget target =
118: (LibLayoutReportTarget) targetState.restore(outputProcessor);
119:
120: LayoutController position = state.getLayoutController();
121:
122:
123:
124: while (position.isAdvanceable())
125: {
126: position = position.advance(target);
127: target.commit();
128:
129: while (position.isAdvanceable() == false &&
130: position.getParent() != null)
131: {
132: final LayoutController parent = position.getParent();
133: position = parent.join(position.getFlowController());
134: }
135:
136: if (interceptor.isMoreContentNeeded() == false)
137: {
138: outputProcessor.setInterceptor(null);
139: return interceptor.getDrawable();
140: }
141:
142: }
143:
144: outputProcessor.setInterceptor(null);
145: return interceptor.getDrawable();
146: }
147: }
148:
149:
157: public synchronized int getNumberOfPages()
158: {
159: if (isError())
160: {
161: return 0;
162: }
163:
164: if (isPaginated() == false)
165: {
166: try
167: {
168: prepareReportProcessing(getJob());
169: }
170: catch (Exception e)
171: {
172: Log.debug("PrintReportProcessor: ", e);
173: error = e;
174: return 0;
175: }
176: }
177: Log.debug ("After pagination, we have " +
178: getGraphicsProcessor().getPhysicalPageCount() + " physical pages.");
179: return getGraphicsProcessor().getPhysicalPageCount();
180: }
181:
182: public boolean paginate()
183: {
184: if (isError())
185: {
186: return false;
187: }
188:
189: if (isPaginated() == false)
190: {
191: try
192: {
193: prepareReportProcessing(getJob());
194: return true;
195: }
196: catch (Exception e)
197: {
198: error = e;
199: return false;
200: }
201: }
202: return true;
203: }
204:
205:
215: public synchronized PageFormat getPageFormat(final int pageIndex)
216: throws IndexOutOfBoundsException
217: {
218: if (isError())
219: {
220: return null;
221: }
222:
223: if (isPaginated() == false)
224: {
225: try
226: {
227: prepareReportProcessing(getJob());
228: }
229: catch (Exception e)
230: {
231: error = e;
232: return null;
233: }
234: }
235:
236: try
237: {
238: final PageDrawable pageDrawable = processPage(pageIndex);
239: return pageDrawable.getPageFormat();
240: }
241: catch (Exception e)
242: {
243: throw new IllegalStateException("Unable to return a valid pageformat.");
244: }
245: }
246:
247:
257: public synchronized Printable getPrintable(final int pageIndex)
258: throws IndexOutOfBoundsException
259: {
260: if (isError())
261: {
262: return null;
263: }
264:
265: if (isPaginated() == false)
266: {
267: try
268: {
269: prepareReportProcessing(getJob());
270: }
271: catch (Exception e)
272: {
273: error = e;
274: return null;
275: }
276: }
277:
278: try
279: {
280: final PageDrawable pageDrawable = processPage(pageIndex);
281: return new DrawablePrintable(pageDrawable);
282: }
283: catch (Exception e)
284: {
285: throw new IllegalStateException("Unable to return a valid pageformat.");
286: }
287: }
288:
289: public PageDrawable getPageDrawable(final int pageIndex)
290: {
291: if (isError())
292: {
293: return null;
294: }
295:
296: if (isPaginated() == false)
297: {
298: try
299: {
300: prepareReportProcessing(getJob());
301: }
302: catch (Exception e)
303: {
304: error = e;
305: return null;
306: }
307: }
308:
309: try
310: {
311: return processPage(pageIndex);
312: }
313: catch (Exception e)
314: {
315: error = e;
316: Log.debug("Failed to process the page", e);
317: throw new IllegalStateException("Unable to return a valid pageformat.");
318: }
319: }
320:
321:
329: public final void processReport(final ReportJob job)
330: {
331: throw new UnsupportedOperationException("Printing is a passive process.");
332: }
333: }