1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
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: import ;
54:
55:
65: public abstract class PaginatingReportProcessor extends AbstractReportProcessor
66: {
67: private PageableOutputProcessor outputProcessor;
68: private PageStateList stateList;
69: private IntList physicalMapping;
70: private IntList logicalMapping;
71:
72: protected PaginatingReportProcessor(final PageableOutputProcessor outputProcessor)
73: {
74: this.outputProcessor = outputProcessor;
75: }
76:
77: public PageableOutputProcessor getOutputProcessor()
78: {
79: return outputProcessor;
80: }
81:
82: protected LibLayoutReportTarget createTarget(final ReportJob job)
83: {
84: if (outputProcessor == null)
85: {
86: throw new IllegalStateException("OutputProcessor is invalid.");
87: }
88: final LayoutProcess layoutProcess =
89: new ChainingLayoutProcess(new DefaultLayoutProcess(outputProcessor));
90: final ResourceManager resourceManager = job.getReportStructureRoot().getResourceManager();
91: final ResourceKey resourceKey = job.getReportStructureRoot().getBaseResource();
92:
93: return new LibLayoutReportTarget
94: (job, resourceKey, resourceManager, layoutProcess);
95: }
96:
97:
98:
99:
100:
101:
102:
103:
104:
105: protected void prepareReportProcessing(final ReportJob job)
106: throws ReportDataFactoryException, DataSourceException, ReportProcessingException
107: {
108: if (job == null)
109: {
110: throw new NullPointerException();
111: }
112:
113: final long start = System.currentTimeMillis();
114:
115: processReportRun(job, createTarget(job));
116: if (outputProcessor.isGlobalStateComputed() == false)
117: {
118: throw new ReportProcessingException
119: ("Pagination has not yet been finished.");
120: }
121:
122:
123: processPaginationRun(job, createTarget(job));
124: if (outputProcessor.isPaginationFinished() == false)
125: {
126: throw new ReportProcessingException
127: ("Pagination has not yet been finished.");
128: }
129:
130: if (outputProcessor.isContentGeneratable() == false)
131: {
132: throw new ReportProcessingException
133: ("Illegal State.");
134: }
135: final long end = System.currentTimeMillis();
136:
137: }
138:
139: protected PageStateList processPaginationRun(final ReportJob job,
140: final LibLayoutReportTarget target)
141: throws ReportDataFactoryException,
142: DataSourceException, ReportProcessingException
143: {
144: if (job == null)
145: {
146: throw new NullPointerException();
147: }
148: stateList = new PageStateList(this);
149: physicalMapping = new IntList(40);
150: logicalMapping = new IntList(20);
151:
152: final ReportContext context = createReportContext(job, target);
153: final LayoutControllerFactory layoutFactory =
154: context.getLayoutControllerFactory();
155:
156:
157:
158: final FlowController flowController = createFlowControler(context, job);
159:
160: LayoutController layoutController =
161: layoutFactory.create(flowController, job.getReportStructureRoot(), null);
162:
163: try
164: {
165: stateList.add(new PageState(target.saveState(), layoutController,
166: outputProcessor.getPageCursor()));
167: int logPageCount = outputProcessor.getLogicalPageCount();
168: int physPageCount = outputProcessor.getPhysicalPageCount();
169:
170: while (layoutController.isAdvanceable())
171: {
172: layoutController = layoutController.advance(target);
173: target.commit();
174:
175: while (layoutController.isAdvanceable() == false &&
176: layoutController.getParent() != null)
177: {
178: final LayoutController parent = layoutController.getParent();
179: layoutController = parent.join(layoutController.getFlowController());
180: }
181:
182:
183: if (target.isPagebreakEncountered())
184: {
185:
186:
187:
188: final int newLogPageCount = outputProcessor.getLogicalPageCount();
189: final int newPhysPageCount = outputProcessor.getPhysicalPageCount();
190:
191: final int result = stateList.size() - 1;
192: for (; physPageCount < newPhysPageCount; physPageCount++)
193: {
194: physicalMapping.add(result);
195: }
196:
197: for (; logPageCount < newLogPageCount; logPageCount++)
198: {
199: logicalMapping.add(result);
200: }
201:
202: logPageCount = newLogPageCount;
203: physPageCount = newPhysPageCount;
204:
205: final ReportTargetState targetState = target.saveState();
206: final PageState state =
207: new PageState (targetState, layoutController,
208: outputProcessor.getPageCursor());
209: stateList.add(state);
210:
211:
212:
213: if (PaginatingReportProcessor.ASSERTATION)
214: {
215: final ReportTarget reportTarget =
216: targetState.restore(outputProcessor);
217: }
218:
219: target.resetPagebreakFlag();
220: }
221: }
222:
223:
224: final int newLogPageCount = outputProcessor.getLogicalPageCount();
225: final int newPhysPageCount = outputProcessor.getPhysicalPageCount();
226:
227: final int result = stateList.size() - 1;
228: for (; physPageCount < newPhysPageCount; physPageCount++)
229: {
230: physicalMapping.add(result);
231: }
232:
233: for (; logPageCount < newLogPageCount; logPageCount++)
234: {
235: logicalMapping.add(result);
236: }
237: }
238: catch (final StateException e)
239: {
240: throw new ReportProcessingException("Argh, Unable to save the state!");
241: }
242:
243: Log.debug("After pagination we have " + stateList.size() + " states");
244: return stateList;
245: }
246:
247: public boolean isPaginated()
248: {
249: return outputProcessor.isPaginationFinished();
250: }
251:
252: protected PageState getLogicalPageState (final int page)
253: {
254: return stateList.get(logicalMapping.get(page));
255: }
256:
257: protected PageState getPhysicalPageState (final int page)
258: {
259: return stateList.get(physicalMapping.get(page));
260: }
261:
262: public PageState processPage(final PageState previousState)
263: throws StateException, ReportProcessingException,
264: ReportDataFactoryException, DataSourceException
265: {
266: final ReportTargetState targetState = previousState.getTargetState();
267: final LibLayoutReportTarget target =
268: (LibLayoutReportTarget) targetState.restore(outputProcessor);
269: outputProcessor.setPageCursor(previousState.getPageCursor());
270:
271: LayoutController position = previousState.getLayoutController();
272:
273:
274:
275:
276: while (position.isAdvanceable())
277: {
278: position = position.advance(target);
279: target.commit();
280:
281:
282:
283: while (position.isAdvanceable() == false &&
284: position.getParent() != null)
285: {
286: final LayoutController parent = position.getParent();
287: position = parent.join(position.getFlowController());
288: }
289:
290:
291: if (target.isPagebreakEncountered())
292: {
293:
294:
295: final PageState state = new PageState
296: (target.saveState(), position, outputProcessor.getPageCursor());
297: target.resetPagebreakFlag();
298: return state;
299: }
300:
301: }
302:
303:
304: return null;
305: }
306:
307: private static final boolean ASSERTATION = true;
308: }