2424import org .apache .poi .ss .usermodel .CellStyle ;
2525import org .apache .poi .ss .usermodel .CellType ;
2626import org .apache .poi .ss .usermodel .CreationHelper ;
27+ import org .apache .poi .ss .usermodel .DataValidation ;
28+ import org .apache .poi .ss .usermodel .DataValidationConstraint ;
29+ import org .apache .poi .ss .usermodel .DataValidationHelper ;
2730import org .apache .poi .ss .usermodel .Font ;
2831import org .apache .poi .ss .usermodel .HorizontalAlignment ;
2932import org .apache .poi .ss .usermodel .IndexedColors ;
3033import org .apache .poi .ss .usermodel .Row ;
3134import org .apache .poi .ss .usermodel .Sheet ;
3235import org .apache .poi .ss .usermodel .Workbook ;
36+ import org .apache .poi .ss .util .CellAddress ;
37+ import org .apache .poi .ss .util .CellRangeAddressList ;
38+ import org .apache .poi .ss .util .CellReference ;
39+ import org .apache .poi .xssf .usermodel .XSSFDataValidationHelper ;
40+ import org .apache .poi .xssf .usermodel .XSSFSheet ;
3341import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
42+ import org .openlowcode .server .data .ChoiceValue ;
3443import org .openlowcode .server .data .DataObject ;
3544import org .openlowcode .server .data .DataObjectDefinition ;
45+ import org .openlowcode .server .data .FieldChoiceDefinition ;
3646import org .openlowcode .server .data .NodeTree ;
3747import org .openlowcode .tools .file .StringParser ;
3848import org .openlowcode .tools .messages .SFile ;
@@ -75,7 +85,9 @@ public SFile extractToExcel(E[] objectarray, String[] specificaliaslist) {
7585 try {
7686 Workbook workbook = new XSSFWorkbook ();
7787 Sheet sheet = workbook .createSheet ("Export Data" );
78- loadWorkbook (sheet , objectarray , specificaliaslist );
88+ Sheet referencessheet = workbook .createSheet ("Reference Values" );
89+ loadWorkbook (sheet ,referencessheet , objectarray , specificaliaslist );
90+ workbook .setActiveSheet (0 ); // setting active sheet to export data
7991 ByteArrayOutputStream documentinmemory = new ByteArrayOutputStream ();
8092 workbook .write (documentinmemory );
8193 workbook .close ();
@@ -266,11 +278,12 @@ public SFile extractToExcel(E[] objectarray) {
266278 * loads into specified sheet the array of objects
267279 *
268280 * @param sheet active sheet
281+ * @param referencessheet sheet to store reference values
269282 * @param objectarray array of objects
270283 * @param specificaliaslist the alias to put as column headers (also gives the
271284 * order of fields)
272285 */
273- private void loadWorkbook (Sheet sheet , E [] objectarray , String [] specificaliaslist ) {
286+ private void loadWorkbook (Sheet sheet ,Sheet referencessheet , E [] objectarray , String [] specificaliaslist ) {
274287 String [] aliaslisttoconsider = specificaliaslist ;
275288 // if zero element, put it to null
276289 if (aliaslisttoconsider != null )
@@ -306,6 +319,8 @@ private void loadWorkbook(Sheet sheet, E[] objectarray, String[] specificaliasli
306319 int [] columnmaxchar = new int [aliaslisttoconsider .length ];
307320 for (int i = 0 ; i < columnmaxchar .length ; i ++)
308321 columnmaxchar [i ] = 0 ;
322+ ArrayList <String []> restrictionsperalias = new ArrayList <String []>();
323+ int maxrestrictionvalues =0 ;
309324 for (int i = 0 ; i < aliaslisttoconsider .length ; i ++) {
310325 Cell cell = headerrow .createCell (i );
311326 cell .setCellStyle (headerstyle );
@@ -316,6 +331,15 @@ private void loadWorkbook(Sheet sheet, E[] objectarray, String[] specificaliasli
316331 String [] headlinesplit = StringParser .splitwithdoubleescape (loaderdef , '&' );
317332 FlatFileLoaderColumn <E > column = definition .getFlatFileLoaderColumn (transientproperties , headlinesplit ,
318333 null );
334+ restrictionsperalias .add (null );
335+ if (column !=null ) {
336+ String [] valuesrestriction = column .getValueRestriction ();
337+ if (valuesrestriction !=null ) {
338+ restrictionsperalias .set (i ,valuesrestriction );
339+ int valuesnr = valuesrestriction .length ;
340+ if (valuesnr >maxrestrictionvalues ) maxrestrictionvalues =valuesnr ;
341+ }
342+ }
319343 columns .add (column );
320344 if (column != null )
321345 if (column .isComplexExtractor ()) {
@@ -326,12 +350,26 @@ private void loadWorkbook(Sheet sheet, E[] objectarray, String[] specificaliasli
326350 logger .info ("Complex extractor defined as column " + column );
327351 }
328352 }
353+
354+
355+ // write restrictions in referencesheet
356+ for (int i =0 ;i <maxrestrictionvalues ;i ++) {
357+ Row currentrow = referencessheet .createRow (i );
358+ for (int j =0 ;j <restrictionsperalias .size ();j ++) {
359+ String [] restrictionsvaluesperalias = restrictionsperalias .get (j );
360+ if (restrictionsvaluesperalias !=null ) if (i <restrictionsvaluesperalias .length ) {
361+ Cell cell = currentrow .createCell (j );
362+ cell .setCellValue (restrictionsvaluesperalias [i ]);
363+ }
364+ }
365+ }
329366 // by default, set height for two lines
330367 headerrow .setHeightInPoints (headerrow .getHeightInPoints () * 2 );
331368 // parse objects
332369 int rowindex = 1 ;
333370 CellStyle normalstyle = createBorderedStyle (sheet .getWorkbook ());
334371 logger .info ("parsing objects in array, nr = " + objectarray .length );
372+
335373 for (int i = 0 ; i < objectarray .length ; i ++) {
336374 E currentobject = objectarray [i ];
337375 String [] context = new String [] { null };
@@ -360,6 +398,15 @@ private void loadWorkbook(Sheet sheet, E[] objectarray, String[] specificaliasli
360398 }
361399
362400 }
401+
402+ // Put restrictions on cells if exists
403+ for (int i =0 ;i <aliaslisttoconsider .length ;i ++) {
404+ String [] restrictions = restrictionsperalias .get (i );
405+ if (restrictions !=null ) {
406+ setRestrictionsOnCell (sheet ,referencessheet ,i ,restrictions .length ,rowindex -2 );
407+ }
408+ }
409+
363410 // at the end, size columns
364411 for (int i = 0 ; i < columnmaxchar .length ; i ++) {
365412 int width = (int ) (columnmaxchar [i ] * 1.14388 * 256 );
@@ -443,5 +490,26 @@ public static CellStyle createDateStyle(Workbook wb, String simpledateformat) {
443490 style .setDataFormat (createHelper .createDataFormat ().getFormat (simpledateformat ));
444491 return style ;
445492 }
446-
493+
494+ /**
495+ * create restrictions on the data cells
496+ *
497+ * @param mainsheet sheet with data
498+ * @param restrictionsheet sheet with restriction values
499+ * @param column index of column (starting with zero)
500+ * @param nbofchoices number of choices (starting with zero)
501+ * @param nbofrows number of rows (starting with zero)
502+ */
503+ public static void setRestrictionsOnCell (Sheet mainsheet ,Sheet restrictionsheet ,int column ,int nbofchoices ,int nbofrows ) {
504+ DataValidationHelper validationHelper = new XSSFDataValidationHelper ((XSSFSheet )mainsheet );
505+ String columnletter = CellReference .convertNumToColString (column );
506+ String formula = "'" +restrictionsheet .getSheetName ()+ "'!$" +columnletter +"$" +1 +":$" +columnletter +"$" +nbofchoices ;
507+ DataValidationConstraint constraint = validationHelper .createFormulaListConstraint (formula );
508+ CellRangeAddressList addressList = new CellRangeAddressList (1 ,nbofrows +1 ,column ,column );
509+
510+ DataValidation dataValidation = validationHelper .createValidation (constraint , addressList );
511+ dataValidation .setErrorStyle (DataValidation .ErrorStyle .STOP );
512+ dataValidation .setSuppressDropDownArrow (true );
513+ mainsheet .addValidationData (dataValidation );
514+ }
447515}
0 commit comments