15
15
import org .apache .log4j .Logger ;
16
16
import org .apache .poi .xwpf .usermodel .*;
17
17
import org .apache .thrift .TException ;
18
+ import org .apache .xmlbeans .XmlCursor ;
18
19
import org .apache .xmlbeans .XmlException ;
19
20
import org .eclipse .sw360 .datahandler .common .CommonUtils ;
20
21
import org .eclipse .sw360 .datahandler .common .SW360Utils ;
@@ -47,17 +48,23 @@ public class DocxGenerator extends OutputGenerator<byte[]> {
47
48
private static final Logger LOGGER = Logger .getLogger (DocxGenerator .class );
48
49
private static final String UNKNOWN_LICENSE_NAME = "Unknown license name" ;
49
50
private static final String UNKNOWN_FILE_NAME = "Unknown file name" ;
51
+ private static final String UNKNOWN_LICENSE = "Unknown" ;
50
52
private static final String TODO_DEFAULT_TEXT = "todo not determined so far." ;
51
53
52
54
private static final String DOCX_TEMPLATE_FILE = "/templateFrontpageContent.docx" ;
53
55
private static final String DOCX_TEMPLATE_REPORT_FILE = "/templateReport.docx" ;
54
56
private static final String DOCX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ;
55
57
private static final String DOCX_OUTPUT_TYPE = "docx" ;
56
- public static final String UNKNOWN_LICENSE = "Unknown" ;
58
+
57
59
private static final long ADDITIONAL_REQ_THRESHOLD = 3 ;
58
- public static final int ADDITIONAL_REQ_TABLE_INDEX = 5 ;
60
+
61
+ public static final int OVERVIEW_TABLE_INDEX = 0 ;
62
+ public static final int SPECIAL_OSS_RISKS_TABLE_INDEX = 1 ;
59
63
public static final int DEV_DETAIL_TABLE_INDEX = 2 ;
60
- private static final int COMMON_RULES_TABLE_INDEX = 4 ;
64
+ public static final int THIRD_PARTY_COMPONENT_OVERVIEW_TABLE_INDEX = 3 ;
65
+ public static final int ADDITIONAL_REQ_TABLE_INDEX = 4 ;
66
+
67
+
61
68
62
69
public DocxGenerator (OutputFormatVariant outputFormatVariant , String description ) {
63
70
super (DOCX_OUTPUT_TYPE , description , true , DOCX_MIME_TYPE , outputFormatVariant );
@@ -136,7 +143,7 @@ private void fillDisclosureDocument(
136
143
fillReleaseBulletList (document , projectLicenseInfoResults );
137
144
fillReleaseDetailList (document , projectLicenseInfoResults , includeObligations );
138
145
fillLicenseList (document , projectLicenseInfoResults );
139
- }
146
+ }
140
147
141
148
private void fillReportDocument (
142
149
XWPFDocument document ,
@@ -147,6 +154,7 @@ private void fillReportDocument(
147
154
Collection <ObligationParsingResult > obligationResults ,
148
155
User user ) throws XmlException , TException {
149
156
157
+ String businessUnit = project .getBusinessUnit ();
150
158
String projectName = project .getName ();
151
159
String projectVersion = project .getVersion ();
152
160
String obligationsText = project .getObligationsText ();
@@ -157,11 +165,13 @@ private void fillReportDocument(
157
165
String deliveryChannelsText = project .getDeliveryChannels ();
158
166
String remarksAdditionalRequirementsText = project .getRemarksAdditionalRequirements ();
159
167
String projectDescription = project .getDescription ();
160
-
168
+ // extract licenses that appear at least ADDITIONAL_REQ_THRESHOLD times
169
+ Set <String > mostLicenses = extractMostCommonLicenses (obligationResults , ADDITIONAL_REQ_THRESHOLD );
161
170
162
171
fillOwnerGroup (document , project );
163
172
fillAttendeesTable (document , project );
164
173
174
+ replaceText (document , "$bunit" , businessUnit );
165
175
replaceText (document , "$license-info-header" , licenseInfoHeaderText );
166
176
replaceText (document , "$project-name" , projectName );
167
177
replaceText (document , "$project-version" , projectVersion );
@@ -177,11 +187,14 @@ private void fillReportDocument(
177
187
fillSpecialOSSRisksTable (document , project , obligationResults );
178
188
fillDevelopmentDetailsTable (document , project , user );
179
189
fillOverview3rdPartyComponentTable (document , projectLicenseInfoResults );
190
+ <<<<<<< HEAD
180
191
fillCommonRulesTable (document , project );
181
192
fillAdditionalRequirementsTable (document , obligationResults );
193
+ =======
194
+ replaceText (document , "$list_comma_sep_licenses_above_threshold" , String .join (", " , mostLicenses ));
195
+ fillAdditionalRequirementsTable (document , obligationResults , mostLicenses );
196
+ >>>>>>> 3 c674cb ... Corrections to the license report
182
197
183
- // because of the impossible API component subsections must be the last thing in the docx file
184
- // the rest of the sections must be generated after this
185
198
writeComponentSubsections (document , projectLicenseInfoResults , obligationResults );
186
199
}
187
200
@@ -194,9 +207,9 @@ private void fillOwnerGroup(XWPFDocument document, Project project) throws XmlEx
194
207
}
195
208
196
209
private void fillAttendeesTable (XWPFDocument document , Project project ) throws XmlException , TException {
197
- XWPFTable table = document .getTables ().get (0 );
210
+ XWPFTable table = document .getTables ().get (OVERVIEW_TABLE_INDEX );
198
211
199
- int currentRow = 6 ;
212
+ int currentRow = 7 ;
200
213
201
214
UserService .Iface userClient = new ThriftClients ().makeUserClient ();
202
215
@@ -220,7 +233,12 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
220
233
continue ;
221
234
}
222
235
223
- User user = userClient .getByEmail (email );
236
+ User user = null ;
237
+ try {
238
+ user = userClient .getByEmail (email );
239
+ } catch (TException te ) {
240
+
241
+ }
224
242
225
243
XWPFTableRow row = table .insertNewTableRow (currentRow ++);
226
244
String name = email ;
@@ -229,7 +247,7 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
229
247
}
230
248
String department = "N.A." ;
231
249
if (user != null ) {
232
- name = user .getDepartment ();
250
+ department = user .getDepartment ();
233
251
}
234
252
235
253
row .addNewTableCell ().setText (name );
@@ -241,7 +259,7 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
241
259
}
242
260
243
261
private void fillSpecialOSSRisksTable (XWPFDocument document , Project project , Collection <ObligationParsingResult > obligationResults ) throws XmlException , TException {
244
- XWPFTable table = document .getTables ().get (1 );
262
+ XWPFTable table = document .getTables ().get (SPECIAL_OSS_RISKS_TABLE_INDEX );
245
263
final int [] currentRow = new int []{0 };
246
264
247
265
obligationResults .stream ()
@@ -260,7 +278,7 @@ private void fillSpecialOSSRisksTable(XWPFDocument document, Project project, Co
260
278
}
261
279
262
280
private void fillOverview3rdPartyComponentTable (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults ) throws XmlException {
263
- XWPFTable table = document .getTables ().get (3 );
281
+ XWPFTable table = document .getTables ().get (THIRD_PARTY_COMPONENT_OVERVIEW_TABLE_INDEX );
264
282
265
283
int currentRow = 1 ;
266
284
for (LicenseInfoParsingResult result : projectLicenseInfoResults ) {
@@ -292,17 +310,31 @@ private static Optional<ObligationParsingResult> obligationsForRelease(Release r
292
310
return obligationResults .stream ().filter (opr -> opr .getRelease () == release ).findFirst ();
293
311
}
294
312
295
- private void writeComponentSubsections (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults , Collection <ObligationParsingResult > obligationResults ) throws XmlException {
313
+ private void writeComponentSubsections (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults , Collection <ObligationParsingResult > obligationResults ) throws SW360Exception , XmlException {
314
+ XmlCursor cursor = document .getTables ().get (ADDITIONAL_REQ_TABLE_INDEX ).getCTTbl ().newCursor ();
315
+ cursor .toEndToken ();
296
316
297
317
for (LicenseInfoParsingResult result : projectLicenseInfoResults ) {
318
+ while (cursor .currentTokenType () != XmlCursor .TokenType .START && cursor .hasNextToken ()) {
319
+ cursor .toNextToken ();
320
+ }
321
+
322
+ if (cursor .currentTokenType () != XmlCursor .TokenType .START ) {
323
+ throw new SW360Exception ("Corrupt template; unable find start token" );
324
+ }
298
325
299
- XWPFParagraph title = document .createParagraph ( );
326
+ XWPFParagraph title = document .insertNewParagraph ( cursor );
300
327
title .setStyle (STYLE_HEADING_3 );
301
328
title .setNumID (new BigInteger ("2" ));
302
329
XWPFRun titleRun = title .createRun ();
303
330
titleRun .setText (result .getVendor () + " " + result .getName ());
304
331
305
- XWPFParagraph description = document .createParagraph ();
332
+ if (cursor .hasNextToken ()) {
333
+ cursor .toNextToken ();
334
+ } else {
335
+ throw new SW360Exception ("Corrupt template; unable to proceed to next token" );
336
+ }
337
+ XWPFParagraph description = document .insertNewParagraph (cursor );
306
338
XWPFRun descriptionRun = description .createRun ();
307
339
308
340
LicenseInfo licenseInfo = result .getLicenseInfo ();
@@ -331,7 +363,7 @@ private void writeComponentSubsections(XWPFDocument document, Collection<License
331
363
332
364
int currentRow = 0 ;
333
365
Collection <Obligation > obligations = obligationsResult .getObligations ();
334
- XWPFTable table = document .createTable ( );
366
+ XWPFTable table = document .insertNewTbl ( cursor );
335
367
for (Obligation o : obligations ) {
336
368
XWPFTableRow row = table .insertNewTableRow (currentRow ++);
337
369
String licensesString = String .join (" " , o .getLicenseIDs ());
@@ -359,13 +391,13 @@ private void fillDevelopmentDetailsTable(XWPFDocument document, Project project,
359
391
360
392
row .addNewTableCell ().setText (component .getName ());
361
393
362
- String operatingSystems = component .getOperatingSystemsSize () == 0 ? "Unknown operating systems " : String .join (" " , component .getOperatingSystems ());
394
+ String operatingSystems = component .getOperatingSystemsSize () == 0 ? "N/A " : String .join (" " , component .getOperatingSystems ());
363
395
row .addNewTableCell ().setText (operatingSystems );
364
396
365
- String langs = component .getLanguagesSize () == 0 ? "Unknown languages " : String .join (" " , component .getLanguages ());
397
+ String langs = component .getLanguagesSize () == 0 ? "N/A " : String .join (" " , component .getLanguages ());
366
398
row .addNewTableCell ().setText (langs );
367
399
368
- String platforms = component .getSoftwarePlatformsSize () == 0 ? "Unknown platforms " : String .join (" " , component .getSoftwarePlatforms ());
400
+ String platforms = component .getSoftwarePlatformsSize () == 0 ? "N/A " : String .join (" " , component .getSoftwarePlatforms ());
369
401
row .addNewTableCell ().setText (platforms );
370
402
}
371
403
}
@@ -379,21 +411,19 @@ protected static Set<String> extractMostCommonLicenses(Collection<ObligationPars
379
411
.entrySet ().stream ()
380
412
.filter (entry -> entry .getValue ().longValue () >= threshold )
381
413
.map (entry -> entry .getKey ())
414
+ .map (license -> license .replace ("\n " , "" ).replace ("\r " , "" ))
382
415
.collect (Collectors .toSet ());
383
416
}
384
417
385
- private void fillAdditionalRequirementsTable (XWPFDocument document , Collection <ObligationParsingResult > obligationResults ) throws XmlException {
386
- // extract licenses that appear at least ADDITIONAL_REQ_THRESHOLD times
387
- Set <String > mostLicenses = extractMostCommonLicenses (obligationResults , ADDITIONAL_REQ_THRESHOLD );
388
-
418
+ private void fillAdditionalRequirementsTable (XWPFDocument document , Collection <ObligationParsingResult > obligationResults , Set <String > mostLicenses ) throws XmlException {
389
419
XWPFTable table = document .getTables ().get (ADDITIONAL_REQ_TABLE_INDEX );
390
420
final int [] currentRow = new int []{0 };
391
421
392
422
obligationResults .stream ()
393
423
.filter (opr -> opr .getStatus () == ObligationInfoRequestStatus .SUCCESS )
394
424
.flatMap (opr -> opr .getObligations ().stream ())
395
425
.filter (o -> o .getLicenseIDs ().stream ()
396
- .anyMatch (lid -> mostLicenses .parallelStream ().anyMatch (mlid -> mlid .equals (lid ))))
426
+ .anyMatch (lid -> mostLicenses .parallelStream ().anyMatch (mlid -> mlid .equals (lid . replace ( " \n " , "" ). replace ( " \r " , "" ) ))))
397
427
.forEach (o -> {
398
428
currentRow [0 ] = currentRow [0 ] + 1 ;
399
429
XWPFTableRow row = table .insertNewTableRow (currentRow [0 ]);
0 commit comments