summaryrefslogtreecommitdiff
path: root/reportbuilder/java/com/sun/star/report/pentaho/output/text/MasterPageFactory.java
blob: af9dc9d2519b007a759825e006a0fa03a44ebe46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package com.sun.star.report.pentaho.output.text;

import com.sun.star.report.pentaho.OfficeNamespaces;
import com.sun.star.report.pentaho.model.OfficeMasterPage;
import com.sun.star.report.pentaho.model.OfficeMasterStyles;
import com.sun.star.report.pentaho.model.OfficeStyles;
import com.sun.star.report.pentaho.model.PageLayout;
import com.sun.star.report.pentaho.model.RawText;

import java.util.HashMap;
import java.util.Map;

import org.jfree.layouting.input.style.values.CSSNumericValue;
import org.jfree.report.ReportProcessingException;
import org.jfree.report.structure.Element;
import org.jfree.report.structure.Section;
import org.jfree.report.util.AttributeNameGenerator;


/**
 * Todo: Document me!
 *
 * @author Thomas Morgner
 * @since 14.03.2007
 */
public class MasterPageFactory
{

    private static class MasterPageFactoryKey
    {

        private final String template;
        private final String pageHeader;
        private final String pageFooter;

        public MasterPageFactoryKey(final String template,
                final String pageHeader,
                final String pageFooter)
        {
            this.template = template;
            this.pageHeader = pageHeader;
            this.pageFooter = pageFooter;
        }

        public boolean equals(final Object o)
        {
            if (this != o)
            {
                if (o == null || getClass() != o.getClass())
                {
                    return false;
                }

                final MasterPageFactoryKey that = (MasterPageFactoryKey) o;

                if (pageFooter != null ? !pageFooter.equals(
                        that.pageFooter) : that.pageFooter != null)
                {
                    return false;
                }
                if (pageHeader != null ? !pageHeader.equals(
                        that.pageHeader) : that.pageHeader != null)
                {
                    return false;
                }
                if (template != null ? !template.equals(
                        that.template) : that.template != null)
                {
                    return false;
                }
            }

            return true;
        }

        public int hashCode()
        {
            int result = (template != null ? template.hashCode() : 0);
            result = 31 * result + (pageHeader != null ? pageHeader.hashCode() : 0);
            result = 31 * result + (pageFooter != null ? pageFooter.hashCode() : 0);
            return result;
        }

        public String getTemplate()
        {
            return template;
        }

        public String getPageHeader()
        {
            return pageHeader;
        }

        public String getPageFooter()
        {
            return pageFooter;
        }
    }

    private static class PageLayoutKey
    {

        private final String templateName;
        private final CSSNumericValue headerHeight;
        private final CSSNumericValue footerHeight;

        public PageLayoutKey(final String templateName,
                final CSSNumericValue headerHeight,
                final CSSNumericValue footerHeight)
        {
            this.templateName = templateName;
            this.headerHeight = headerHeight;
            this.footerHeight = footerHeight;
        }

        public String getTemplateName()
        {
            return templateName;
        }

        public CSSNumericValue getHeaderHeight()
        {
            return headerHeight;
        }

        public CSSNumericValue getFooterHeight()
        {
            return footerHeight;
        }

        public boolean equals(final Object o)
        {
            if (this == o)
            {
                return true;
            }
            if (o == null || getClass() != o.getClass())
            {
                return false;
            }

            final PageLayoutKey key = (PageLayoutKey) o;

            if (footerHeight != null ? !footerHeight.equals(
                    key.footerHeight) : key.footerHeight != null)
            {
                return false;
            }
            if (headerHeight != null ? !headerHeight.equals(
                    key.headerHeight) : key.headerHeight != null)
            {
                return false;
            }
            return !(templateName != null ? !templateName.equals(
                    key.templateName) : key.templateName != null);

        }

        public int hashCode()
        {
            int result;
            result = (templateName != null ? templateName.hashCode() : 0);
            result = 31 * result + (headerHeight != null ? headerHeight.hashCode() : 0);
            result = 31 * result + (footerHeight != null ? footerHeight.hashCode() : 0);
            return result;
        }
    }
    // todo: Patch the page-layout ...
    private static final String DEFAULT_PAGE_NAME = "Default";
    private final OfficeMasterStyles predefinedStyles;
    private final AttributeNameGenerator masterPageNameGenerator;
    private final Map<MasterPageFactoryKey,OfficeMasterPage> masterPages;
    private final AttributeNameGenerator pageLayoutNameGenerator;
    private final Map<PageLayoutKey,String> pageLayouts;

    public MasterPageFactory(final OfficeMasterStyles predefinedStyles)
    {
        this.predefinedStyles = predefinedStyles;
        this.masterPages = new HashMap<MasterPageFactoryKey,OfficeMasterPage>();
        this.masterPageNameGenerator = new AttributeNameGenerator();
        this.pageLayouts = new HashMap<PageLayoutKey,String>();
        this.pageLayoutNameGenerator = new AttributeNameGenerator();
    }

    public OfficeMasterPage getMasterPage(final String template,
            final String pageHeader,
            final String pageFooter)
    {
        final MasterPageFactoryKey key =
                new MasterPageFactoryKey(template, pageHeader, pageFooter);
        return masterPages.get(key);
    }

    public boolean containsMasterPage(final String template,
            final String pageHeader,
            final String pageFooter)
    {
        final MasterPageFactoryKey key =
                new MasterPageFactoryKey(template, pageHeader, pageFooter);
        return masterPages.containsKey(key);
    }

    public OfficeMasterPage createMasterPage(final String template,
            final String pageHeader,
            final String pageFooter)
    {
        final MasterPageFactoryKey key =
                new MasterPageFactoryKey(template, pageHeader, pageFooter);
        final OfficeMasterPage cached = masterPages.get(key);
        if (cached != null)
        {
            return cached;
        }

        final String targetName = (masterPages.isEmpty()) ? "Standard" : template;

        OfficeMasterPage predef = predefinedStyles.getMasterPage(template);
        if (predef == null)
        {
            // This is a 'magic' name ..
            // todo: It could be that this should be called 'Standard' instead
            predef = predefinedStyles.getMasterPage(MasterPageFactory.DEFAULT_PAGE_NAME);
        }

        if (predef != null)
        {
            try
            {
                // derive
                final OfficeMasterPage derived = (OfficeMasterPage) predef.clone();
                return setupMasterPage(derived, targetName, pageHeader, pageFooter,
                        key);
            }
            catch (CloneNotSupportedException cne)
            {
                throw new IllegalStateException("Implementation error: Unable to derive page", cne);
            }
        }

        final OfficeMasterPage masterPage = new OfficeMasterPage();
        masterPage.setNamespace(OfficeNamespaces.STYLE_NS);
        masterPage.setType("master-page");
        return setupMasterPage(masterPage, targetName, pageHeader, pageFooter, key);
    }

    private OfficeMasterPage setupMasterPage(final OfficeMasterPage derived,
            final String targetName,
            final String pageHeader,
            final String pageFooter,
            final MasterPageFactoryKey key)
    {
        derived.setStyleName(masterPageNameGenerator.generateName(targetName));
        masterPages.put(key, derived);

        if (pageHeader != null)
        {
            final Section header = new Section();
            header.setNamespace(OfficeNamespaces.STYLE_NS);
            header.setType("header");
            header.addNode(new RawText(pageHeader));
            derived.addNode(header);
        }

        if (pageFooter != null)
        {
            final Section footer = new Section();
            footer.setNamespace(OfficeNamespaces.STYLE_NS);
            footer.setType("footer");
            footer.addNode(new RawText(pageFooter));
            derived.addNode(footer);
        }

        return derived;
    }

    public String createPageStyle(final OfficeStyles commonStyles,
            final CSSNumericValue headerHeight,
            final CSSNumericValue footerHeight)
    {
        final PageLayoutKey key =
                new PageLayoutKey(null, headerHeight, footerHeight);
        final PageLayout derived = new PageLayout();
        final String name = pageLayoutNameGenerator.generateName("autogenerated");
        derived.setStyleName(name);
        commonStyles.addPageStyle(derived);

        if (headerHeight != null)
        {
            final Section headerStyle = new Section();
            headerStyle.setNamespace(OfficeNamespaces.STYLE_NS);
            headerStyle.setType("header-style");
            derived.addNode(headerStyle);
            MasterPageFactory.applyHeaderFooterHeight(headerStyle, headerHeight);
        }

        if (footerHeight != null)
        {
            final Section footerStyle = new Section();
            footerStyle.setNamespace(OfficeNamespaces.STYLE_NS);
            footerStyle.setType("footer-style");
            derived.addNode(footerStyle);
            MasterPageFactory.applyHeaderFooterHeight(footerStyle, footerHeight);
        }
        pageLayouts.put(key, name);
        return name;
    }

    public String derivePageStyle(final String pageStyleTemplate,
            final OfficeStyles predefined,
            final OfficeStyles commonStyles,
            final CSSNumericValue headerHeight,
            final CSSNumericValue footerHeight)
            throws ReportProcessingException
    {
        if (pageStyleTemplate == null)
        {
            throw new NullPointerException("A style-name must be given");
        }

        final PageLayoutKey key =
                new PageLayoutKey(pageStyleTemplate, headerHeight, footerHeight);
        final String pageLayoutName = pageLayouts.get(key);
        if (pageLayoutName != null)
        {
            // there's already a suitable version included.
            return pageLayoutName;
        }

        final PageLayout original = predefined.getPageStyle(pageStyleTemplate);
        if (original == null)
        {
            throw new ReportProcessingException("Invalid page-layout '" + pageStyleTemplate + "', will not continue.");
        }

        try
        {
            final PageLayout derived = (PageLayout) original.clone();
            final String name = pageLayoutNameGenerator.generateName(
                    pageStyleTemplate);
            derived.setStyleName(name);
            commonStyles.addPageStyle(derived);

            if (headerHeight != null)
            {
                Section headerStyle = derived.getHeaderStyle();
                if (headerStyle == null)
                {
                    headerStyle = new Section();
                    headerStyle.setNamespace(OfficeNamespaces.STYLE_NS);
                    headerStyle.setType("header-style");
                    derived.addNode(headerStyle);
                }
                MasterPageFactory.applyHeaderFooterHeight(headerStyle, headerHeight);
            }

            if (footerHeight != null)
            {
                Section footerStyle = derived.getFooterStyle();
                if (footerStyle == null)
                {
                    footerStyle = new Section();
                    footerStyle.setNamespace(OfficeNamespaces.STYLE_NS);
                    footerStyle.setType("footer-style");
                    derived.addNode(footerStyle);
                }

                MasterPageFactory.applyHeaderFooterHeight(footerStyle, footerHeight);
            }
            pageLayouts.put(key, name);
            return name;
        }
        catch (CloneNotSupportedException e)
        {
            throw new IllegalStateException("Clone failed.", e);
        }
    }

    private static void applyHeaderFooterHeight(final Section headerFooterStyle,
            final CSSNumericValue style)
    {
        Element headerFooterProps = headerFooterStyle.findFirstChild(OfficeNamespaces.STYLE_NS, "header-footer-properties");
        if (headerFooterProps == null)
        {
            headerFooterProps = new Section();
            headerFooterProps.setNamespace(OfficeNamespaces.STYLE_NS);
            headerFooterProps.setType("header-footer-properties");
            headerFooterStyle.addNode(headerFooterProps);
        }
        headerFooterProps.setAttribute(OfficeNamespaces.SVG_NS, "height", style.getValue() + style.getType().getType());
    }
}