Skip to content

Commit 225fede

Browse files
author
Open Lowcode SAS
committed
Close #44
1 parent fd3509b commit 225fede

File tree

3 files changed

+100
-31
lines changed

3 files changed

+100
-31
lines changed

src/org/openlowcode/server/data/ChoiceDataObjectFieldFlatFileLoaderColumn.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,19 @@ protected boolean putContentInCell(E currentobject, Cell cell, String context) {
143143
+ " would be of type ChoiceDataObjectField but in reality, it is " + field.getClass().toString());
144144
ChoiceDataObjectField<F, E> choicefield = (ChoiceDataObjectField<F, E>) field;
145145
cell.setCellValue((choicefield.getValue() == null ? "" : choicefield.getValue().getDisplayValue()));
146-
// ChoiceDataObjectFieldFlatFileLoaderColumn.setRestrictionsOnCell(cell,fieldchoicedefinition);
147146
return false;
148147
}
149148

150-
/**
151-
* sets restriction on cell so that only display values in the list of value, or
152-
* empty string, can be entered. Warning: in current version, does not work in
153-
* excel (works with Libre Office though)
154-
*
155-
* @param cell cell
156-
* @param choicedefinition choice definition
157-
*/
158-
public static <E extends FieldChoiceDefinition<E>> void setRestrictionsOnCell(Cell cell, E choicedefinition) {
159-
ArrayList<String> restrictions = new ArrayList<String>();
160-
restrictions.add("");
161-
ChoiceValue<E>[] choices = choicedefinition.getChoiceValue();
162-
for (int i = 0; i < choices.length; i++)
163-
restrictions.add(choices[i].getDisplayValue());
164-
XSSFSheet sheet = (XSSFSheet) cell.getSheet();
165-
DataValidationHelper validationHelper = new XSSFDataValidationHelper(sheet);
166-
DataValidationConstraint constraint = validationHelper
167-
.createExplicitListConstraint(restrictions.toArray(new String[0]));
168-
CellRangeAddressList addressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(),
169-
cell.getColumnIndex(), cell.getColumnIndex());
170-
DataValidation dataValidation = validationHelper.createValidation(constraint, addressList);
171-
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
172-
dataValidation.setSuppressDropDownArrow(true);
173-
sheet.addValidationData(dataValidation);
174-
}
149+
@Override
150+
public String[] getValueRestriction() {
151+
ArrayList<String> allowedvalues = new ArrayList<String>();
152+
allowedvalues.add("");
153+
ChoiceValue<?>[] choices = fieldchoicedefinition.getChoiceValue();
154+
for (int i = 0; i < choices.length; i++)
155+
allowedvalues.add(choices[i].getDisplayValue());
156+
return allowedvalues.toArray(new String[0]);
157+
}
158+
159+
175160

176161
}

src/org/openlowcode/server/data/loader/FlatFileExtractor.java

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,25 @@
2424
import org.apache.poi.ss.usermodel.CellStyle;
2525
import org.apache.poi.ss.usermodel.CellType;
2626
import 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;
2730
import org.apache.poi.ss.usermodel.Font;
2831
import org.apache.poi.ss.usermodel.HorizontalAlignment;
2932
import org.apache.poi.ss.usermodel.IndexedColors;
3033
import org.apache.poi.ss.usermodel.Row;
3134
import org.apache.poi.ss.usermodel.Sheet;
3235
import 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;
3341
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
42+
import org.openlowcode.server.data.ChoiceValue;
3443
import org.openlowcode.server.data.DataObject;
3544
import org.openlowcode.server.data.DataObjectDefinition;
45+
import org.openlowcode.server.data.FieldChoiceDefinition;
3646
import org.openlowcode.server.data.NodeTree;
3747
import org.openlowcode.tools.file.StringParser;
3848
import 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
}

src/org/openlowcode/server/data/loader/FlatFileLoaderColumn.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public boolean secondpass() {
7373
* @param linepreparatorextracriterias extra criterias for the line prepataion
7474
* @return
7575
*/
76-
public LinePreparation<E> LinePreparation(Object maincolumnvalue,
76+
public LinePreparation<E> LinePreparation(
77+
Object maincolumnvalue,
7778
ArrayList<LinePreparationExtra<E>> linepreparatorextracriterias) {
7879
throw new RuntimeException("no line preparator with two attribute");
7980
}
@@ -152,7 +153,9 @@ public LinePreparationExtra<E> generateLinePreparatorExtra(Object data) {
152153
*/
153154
public static interface LinePreparationExtra<E extends DataObject<E>> {
154155
/**
155-
* generates a query condition to query relevant data to the object being processed
156+
* generates a query condition to query relevant data to the object being
157+
* processed
158+
*
156159
* @param definition object definition
157160
* @param alias alias of the query
158161
* @return
@@ -162,6 +165,7 @@ public static interface LinePreparationExtra<E extends DataObject<E>> {
162165

163166
/**
164167
* performs the load
168+
*
165169
* @param object the object on which to load data
166170
* @param value an object, either Date, String or Double
167171
* @param postupdateprocessingstore a store to perform post processing after the
@@ -227,4 +231,16 @@ public String[] initComplexExtractorForObject(E currentobject) {
227231
throw new RuntimeException("Not implemented for non-complex extractor");
228232
}
229233

234+
/**
235+
* allows to define a restriction (list of potential values) for a cell when
236+
* exporting data in a spreadsheet. The column should return null if no
237+
* restriction applies to cell.<br>
238+
* Should be overridden only by loaders that need to restrict values
239+
*
240+
* @return the list of potential values, or null if no restriction.
241+
*/
242+
public String[] getValueRestriction() {
243+
return null;
244+
}
245+
230246
}

0 commit comments

Comments
 (0)