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,13 +48,14 @@ 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
60
public static final int ADDITIONAL_REQ_TABLE_INDEX = 5 ;
59
61
public static final int DEV_DETAIL_TABLE_INDEX = 2 ;
@@ -136,7 +138,7 @@ private void fillDisclosureDocument(
136
138
fillReleaseBulletList (document , projectLicenseInfoResults );
137
139
fillReleaseDetailList (document , projectLicenseInfoResults , includeObligations );
138
140
fillLicenseList (document , projectLicenseInfoResults );
139
- }
141
+ }
140
142
141
143
private void fillReportDocument (
142
144
XWPFDocument document ,
@@ -147,6 +149,7 @@ private void fillReportDocument(
147
149
Collection <ObligationParsingResult > obligationResults ,
148
150
User user ) throws XmlException , TException {
149
151
152
+ String businessUnit = project .getBusinessUnit ();
150
153
String projectName = project .getName ();
151
154
String projectVersion = project .getVersion ();
152
155
String obligationsText = project .getObligationsText ();
@@ -157,11 +160,13 @@ private void fillReportDocument(
157
160
String deliveryChannelsText = project .getDeliveryChannels ();
158
161
String remarksAdditionalRequirementsText = project .getRemarksAdditionalRequirements ();
159
162
String projectDescription = project .getDescription ();
160
-
163
+ // extract licenses that appear at least ADDITIONAL_REQ_THRESHOLD times
164
+ Set <String > mostLicenses = extractMostCommonLicenses (obligationResults , ADDITIONAL_REQ_THRESHOLD );
161
165
162
166
fillOwnerGroup (document , project );
163
167
fillAttendeesTable (document , project );
164
168
169
+ replaceText (document , "$bunit" , businessUnit );
165
170
replaceText (document , "$license-info-header" , licenseInfoHeaderText );
166
171
replaceText (document , "$project-name" , projectName );
167
172
replaceText (document , "$project-version" , projectVersion );
@@ -180,8 +185,6 @@ private void fillReportDocument(
180
185
fillCommonRulesTable (document , project );
181
186
fillAdditionalRequirementsTable (document , obligationResults );
182
187
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
188
writeComponentSubsections (document , projectLicenseInfoResults , obligationResults );
186
189
}
187
190
@@ -194,9 +197,9 @@ private void fillOwnerGroup(XWPFDocument document, Project project) throws XmlEx
194
197
}
195
198
196
199
private void fillAttendeesTable (XWPFDocument document , Project project ) throws XmlException , TException {
197
- XWPFTable table = document .getTables ().get (0 );
200
+ XWPFTable table = document .getTables ().get (OVERVIEW_TABLE_INDEX );
198
201
199
- int currentRow = 6 ;
202
+ int currentRow = 7 ;
200
203
201
204
UserService .Iface userClient = new ThriftClients ().makeUserClient ();
202
205
@@ -220,7 +223,12 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
220
223
continue ;
221
224
}
222
225
223
- User user = userClient .getByEmail (email );
226
+ User user = null ;
227
+ try {
228
+ user = userClient .getByEmail (email );
229
+ } catch (TException te ) {
230
+
231
+ }
224
232
225
233
XWPFTableRow row = table .insertNewTableRow (currentRow ++);
226
234
String name = email ;
@@ -229,7 +237,7 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
229
237
}
230
238
String department = "N.A." ;
231
239
if (user != null ) {
232
- name = user .getDepartment ();
240
+ department = user .getDepartment ();
233
241
}
234
242
235
243
row .addNewTableCell ().setText (name );
@@ -241,7 +249,7 @@ private void fillAttendeesTable(XWPFDocument document, Project project) throws X
241
249
}
242
250
243
251
private void fillSpecialOSSRisksTable (XWPFDocument document , Project project , Collection <ObligationParsingResult > obligationResults ) throws XmlException , TException {
244
- XWPFTable table = document .getTables ().get (1 );
252
+ XWPFTable table = document .getTables ().get (SPECIAL_OSS_RISKS_TABLE_INDEX );
245
253
final int [] currentRow = new int []{0 };
246
254
247
255
obligationResults .stream ()
@@ -260,7 +268,7 @@ private void fillSpecialOSSRisksTable(XWPFDocument document, Project project, Co
260
268
}
261
269
262
270
private void fillOverview3rdPartyComponentTable (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults ) throws XmlException {
263
- XWPFTable table = document .getTables ().get (3 );
271
+ XWPFTable table = document .getTables ().get (THIRD_PARTY_COMPONENT_OVERVIEW_TABLE_INDEX );
264
272
265
273
int currentRow = 1 ;
266
274
for (LicenseInfoParsingResult result : projectLicenseInfoResults ) {
@@ -292,17 +300,31 @@ private static Optional<ObligationParsingResult> obligationsForRelease(Release r
292
300
return obligationResults .stream ().filter (opr -> opr .getRelease () == release ).findFirst ();
293
301
}
294
302
295
- private void writeComponentSubsections (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults , Collection <ObligationParsingResult > obligationResults ) throws XmlException {
303
+ private void writeComponentSubsections (XWPFDocument document , Collection <LicenseInfoParsingResult > projectLicenseInfoResults , Collection <ObligationParsingResult > obligationResults ) throws SW360Exception , XmlException {
304
+ XmlCursor cursor = document .getTables ().get (ADDITIONAL_REQ_TABLE_INDEX ).getCTTbl ().newCursor ();
305
+ cursor .toEndToken ();
296
306
297
307
for (LicenseInfoParsingResult result : projectLicenseInfoResults ) {
308
+ while (cursor .currentTokenType () != XmlCursor .TokenType .START && cursor .hasNextToken ()) {
309
+ cursor .toNextToken ();
310
+ }
311
+
312
+ if (cursor .currentTokenType () != XmlCursor .TokenType .START ) {
313
+ throw new SW360Exception ("Corrupt template; unable find start token" );
314
+ }
298
315
299
- XWPFParagraph title = document .createParagraph ( );
316
+ XWPFParagraph title = document .insertNewParagraph ( cursor );
300
317
title .setStyle (STYLE_HEADING_3 );
301
318
title .setNumID (new BigInteger ("2" ));
302
319
XWPFRun titleRun = title .createRun ();
303
320
titleRun .setText (result .getVendor () + " " + result .getName ());
304
321
305
- XWPFParagraph description = document .createParagraph ();
322
+ if (cursor .hasNextToken ()) {
323
+ cursor .toNextToken ();
324
+ } else {
325
+ throw new SW360Exception ("Corrupt template; unable to proceed to next token" );
326
+ }
327
+ XWPFParagraph description = document .insertNewParagraph (cursor );
306
328
XWPFRun descriptionRun = description .createRun ();
307
329
308
330
LicenseInfo licenseInfo = result .getLicenseInfo ();
@@ -331,7 +353,7 @@ private void writeComponentSubsections(XWPFDocument document, Collection<License
331
353
332
354
int currentRow = 0 ;
333
355
Collection <Obligation > obligations = obligationsResult .getObligations ();
334
- XWPFTable table = document .createTable ( );
356
+ XWPFTable table = document .insertNewTbl ( cursor );
335
357
for (Obligation o : obligations ) {
336
358
XWPFTableRow row = table .insertNewTableRow (currentRow ++);
337
359
String licensesString = String .join (" " , o .getLicenseIDs ());
@@ -359,13 +381,13 @@ private void fillDevelopmentDetailsTable(XWPFDocument document, Project project,
359
381
360
382
row .addNewTableCell ().setText (component .getName ());
361
383
362
- String operatingSystems = component .getOperatingSystemsSize () == 0 ? "Unknown operating systems " : String .join (" " , component .getOperatingSystems ());
384
+ String operatingSystems = component .getOperatingSystemsSize () == 0 ? "N/A " : String .join (" " , component .getOperatingSystems ());
363
385
row .addNewTableCell ().setText (operatingSystems );
364
386
365
- String langs = component .getLanguagesSize () == 0 ? "Unknown languages " : String .join (" " , component .getLanguages ());
387
+ String langs = component .getLanguagesSize () == 0 ? "N/A " : String .join (" " , component .getLanguages ());
366
388
row .addNewTableCell ().setText (langs );
367
389
368
- String platforms = component .getSoftwarePlatformsSize () == 0 ? "Unknown platforms " : String .join (" " , component .getSoftwarePlatforms ());
390
+ String platforms = component .getSoftwarePlatformsSize () == 0 ? "N/A " : String .join (" " , component .getSoftwarePlatforms ());
369
391
row .addNewTableCell ().setText (platforms );
370
392
}
371
393
}
@@ -379,21 +401,19 @@ protected static Set<String> extractMostCommonLicenses(Collection<ObligationPars
379
401
.entrySet ().stream ()
380
402
.filter (entry -> entry .getValue ().longValue () >= threshold )
381
403
.map (entry -> entry .getKey ())
404
+ .map (license -> license .replace ("\n " , "" ).replace ("\r " , "" ))
382
405
.collect (Collectors .toSet ());
383
406
}
384
407
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
-
408
+ private void fillAdditionalRequirementsTable (XWPFDocument document , Collection <ObligationParsingResult > obligationResults , Set <String > mostLicenses ) throws XmlException {
389
409
XWPFTable table = document .getTables ().get (ADDITIONAL_REQ_TABLE_INDEX );
390
410
final int [] currentRow = new int []{0 };
391
411
392
412
obligationResults .stream ()
393
413
.filter (opr -> opr .getStatus () == ObligationInfoRequestStatus .SUCCESS )
394
414
.flatMap (opr -> opr .getObligations ().stream ())
395
415
.filter (o -> o .getLicenseIDs ().stream ()
396
- .anyMatch (lid -> mostLicenses .parallelStream ().anyMatch (mlid -> mlid .equals (lid ))))
416
+ .anyMatch (lid -> mostLicenses .parallelStream ().anyMatch (mlid -> mlid .equals (lid . replace ( " \n " , "" ). replace ( " \r " , "" ) ))))
397
417
.forEach (o -> {
398
418
currentRow [0 ] = currentRow [0 ] + 1 ;
399
419
XWPFTableRow row = table .insertNewTableRow (currentRow [0 ]);
0 commit comments