View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core.reference;
20  
21  import com.fasterxml.jackson.core.JsonGenerator;
22  import com.fasterxml.jackson.databind.ObjectWriter;
23  import com.fasterxml.jackson.dataformat.csv.CsvMapper;
24  import com.fasterxml.jackson.dataformat.csv.CsvSchema;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import org.apache.pdfbox.pdmodel.PDDocument;
28  import org.apache.pdfbox.pdmodel.PDPage;
29  import org.apache.pdfbox.pdmodel.PDPageContentStream;
30  import org.apache.pdfbox.pdmodel.font.PDType1Font;
31  import org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName;
32  import org.apache.syncope.common.lib.report.ReportConf;
33  import org.apache.syncope.core.provisioning.api.job.report.ReportConfClass;
34  import org.apache.syncope.core.provisioning.java.job.report.AbstractReportJobDelegate;
35  import org.quartz.JobExecutionContext;
36  import org.quartz.JobExecutionException;
37  import org.springframework.http.MediaType;
38  
39  @ReportConfClass(SampleReportConf.class)
40  public class SampleReportJobDelegate extends AbstractReportJobDelegate {
41  
42      private SampleReportConf sampleReportConf;
43  
44      private void generateSamplePdfContent(final OutputStream os) throws IOException {
45          try (PDDocument doc = new PDDocument()) {
46              PDPage myPage = new PDPage();
47              doc.addPage(myPage);
48  
49              try (PDPageContentStream cont = new PDPageContentStream(doc, myPage)) {
50                  cont.beginText();
51  
52                  cont.setFont(new PDType1Font(FontName.HELVETICA_BOLD), 12);
53                  cont.setLeading(14.5f);
54  
55                  cont.newLineAtOffset(25, 700);
56                  String line1 = "World War II (often abbreviated to WWII or WW2), "
57                          + "also known as the Second World War,";
58                  cont.showText(line1);
59  
60                  cont.newLine();
61  
62                  String line2 = "was a global war that lasted from 1939 to 1945, "
63                          + "although related conflicts began earlier.";
64                  cont.showText(line2);
65                  cont.newLine();
66  
67                  String line3 = "It involved the vast majority of the world's "
68                          + "countries—including all of the great powers—";
69                  cont.showText(line3);
70                  cont.newLine();
71  
72                  String line4 = "eventually forming two opposing military "
73                          + "alliances: the Allies and the Axis.";
74                  cont.showText(line4);
75                  cont.newLine();
76  
77                  cont.endText();
78              }
79  
80              doc.save(os);
81          }
82      }
83  
84      private void generateSampleCsvContent(final OutputStream os) throws IOException {
85          CsvMapper mapper = new CsvMapper();
86          mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
87  
88          CsvSchema schema = CsvSchema.builder().
89                  setUseHeader(true).
90                  addColumn("stringValue").
91                  addColumn("intValue").
92                  addColumn("longValue").
93                  addColumn("floatValue").
94                  addColumn("doubleValue").
95                  addColumn("booleanValue").
96                  addColumn("listValues").
97                  build();
98  
99          ObjectWriter writer = mapper.writerFor(SampleReportConf.class).with(schema);
100 
101         writer.writeValues(os).write(sampleReportConf);
102     }
103 
104     @Override
105     public void setConf(final ReportConf conf) {
106         if (conf instanceof SampleReportConf) {
107             sampleReportConf = (SampleReportConf) conf;
108         } else {
109             throw new IllegalArgumentException("Expected " + SampleReportConf.class.getName() + ", got " + conf);
110         }
111     }
112 
113     @Override
114     protected String doExecute(
115             final boolean dryRun,
116             final OutputStream os,
117             final String executor,
118             final JobExecutionContext context) throws JobExecutionException {
119 
120         if (!dryRun) {
121             try {
122                 switch (report.getMimeType()) {
123                     case MediaType.APPLICATION_PDF_VALUE:
124                         generateSamplePdfContent(os);
125                         break;
126 
127                     case "text/csv":
128                         generateSampleCsvContent(os);
129                         break;
130 
131                     default:
132                 }
133             } catch (IOException e) {
134                 throw new JobExecutionException(e);
135             }
136         }
137 
138         return (dryRun
139                 ? "DRY "
140                 : "") + "RUNNING";
141     }
142 }