org.openl.rules.ui.tablewizard.SimpleRulesCreationWizard Maven / Gradle / Ivy
package org.openl.rules.ui.tablewizard;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
import javax.faces.validator.ValidatorException;
import javax.validation.GroupSequence;
import javax.validation.constraints.NotBlank;
import org.richfaces.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openl.rules.lang.xls.XlsSheetSourceCodeModule;
import org.openl.rules.table.properties.def.TablePropertyDefinition;
import org.openl.rules.table.properties.def.TablePropertyDefinitionUtils;
import org.openl.rules.table.properties.inherit.InheritanceLevel;
import org.openl.rules.table.xls.XlsSheetGridModel;
import org.openl.rules.table.xls.builder.CreateTableException;
import org.openl.rules.table.xls.builder.DataTableBuilder;
import org.openl.rules.table.xls.builder.SimpleRulesTableBuilder;
import org.openl.rules.table.xls.builder.TableBuilder;
import org.openl.rules.ui.tablewizard.util.CellStyleManager;
import org.openl.rules.ui.tablewizard.util.JSONHolder;
import org.openl.rules.ui.validation.StringPresentedGroup;
import org.openl.rules.ui.validation.StringValidGroup;
import org.openl.rules.ui.validation.TableNameConstraint;
import org.openl.util.StringUtils;
import org.openl.util.TableNameChecker;
@GroupSequence({SimpleRulesCreationWizard.class, StringPresentedGroup.class, StringValidGroup.class})
public class SimpleRulesCreationWizard extends TableCreationWizard {
private static final String TABLE_TYPE = "xls.dt";
private static final String RESTORE_TABLE_FUNCTION = "tableModel.restoreTableFromJSONString";
private final Logger log = LoggerFactory.getLogger(SimpleRulesCreationWizard.class);
@NotBlank(message = "Cannot be empty", groups = StringPresentedGroup.class)
@TableNameConstraint(groups = StringValidGroup.class)
private String tableName;
private List domainTypes;
private String jsonTable;
private JSONHolder table;
private String restoreTable;
private List typesList;
private String returnValueType;
private List parameters = new ArrayList<>();
@Override
protected void onCancel() {
reset();
}
@Override
protected void onStart() {
reset();
domainTypes = new ArrayList<>(WizardUtils.predefinedTypes());
domainTypes.add("");
domainTypes.addAll(WizardUtils.declaredDatatypes());
domainTypes.add("");
domainTypes.addAll(WizardUtils.declaredAliases());
domainTypes.add("");
domainTypes.addAll(WizardUtils.importedClasses());
this.typesList = domainTypes.stream()
.filter(x -> !x.isEmpty())
.map(DomainTypeHolder::new)
.collect(Collectors.toList());
initWorkbooks();
}
public int getColumnSize() {
int size = 0;
size += this.parameters.size();
size++;
return size;
}
public List getPropertyList() {
List propertyNames = new ArrayList<>();
TablePropertyDefinition[] propDefinitions = TablePropertyDefinitionUtils
.getDefaultDefinitionsForTable(TABLE_TYPE, InheritanceLevel.TABLE, true);
SelectItem selectItem = new SelectItem("");
selectItem.setLabel("");
propertyNames.add(selectItem);
Map> propGroups = TablePropertyDefinitionUtils
.groupProperties(propDefinitions);
for (Map.Entry> entry : propGroups.entrySet()) {
List items = new ArrayList<>();
for (TablePropertyDefinition propDefinition : entry.getValue()) {
String propName = propDefinition.getName();
if (propDefinition.getDeprecation() == null) {
items.add(new SelectItem(propName, propDefinition.getDisplayName()));
}
}
if (!items.isEmpty()) {
SelectItemGroup itemGroup = new SelectItemGroup(entry.getKey());
itemGroup.setSelectItems(items.toArray(new SelectItem[0]));
propertyNames.add(itemGroup);
}
}
return propertyNames;
}
public DomainTypeHolder getReturnValue() {
return getTypedParameterByName(this.returnValueType);
}
private DomainTypeHolder getTypedParameterByName(String name) {
DomainTypeHolder dth = null;
for (DomainTypeHolder type : typesList) {
if (type.name.equals(name)) {
dth = type.clone();
break;
}
}
if (dth == null) {
dth = new DomainTypeHolder(name, "STRING");
}
return dth;
}
public List getTypedParameters() {
List typedParameters = new ArrayList<>();
for (TypeNamePair tnp : this.parameters) {
DomainTypeHolder gth = getTypedParameterByName(tnp.getType());
gth.setTypeName(tnp.getName());
gth.setIterable(tnp.isIterable());
typedParameters.add(gth);
}
return typedParameters;
}
@Override
protected void onFinish() throws Exception {
XlsSheetSourceCodeModule sheetSourceModule = getDestinationSheet();
String newTableUri = buildTable(sheetSourceModule);
setNewTableURI(newTableUri);
getModifiedWorkbooks().add(sheetSourceModule.getWorkbookSource());
super.onFinish();
}
protected String buildTable(XlsSheetSourceCodeModule sourceCodeModule) throws CreateTableException {
XlsSheetGridModel gridModel = new XlsSheetGridModel(sourceCodeModule);
SimpleRulesTableBuilder builder = new SimpleRulesTableBuilder(gridModel);
CellStyleManager styleManager = new CellStyleManager(gridModel, table);
Map properties = buildProperties();
properties.putAll(table.getProperties());
int width = DataTableBuilder.MIN_WIDTH;
if (!properties.isEmpty()) {
width = TableBuilder.PROPERTIES_MIN_WIDTH;
}
List>> rows = table.getDataRows(styleManager);
width = Math.max(table.getFieldsCount(), width);
int height = TableBuilder.HEADER_HEIGHT + properties.size() + rows.size();
builder.beginTable(width, height);
builder.writeHeader(table.getHeaderStr(), styleManager.getHeaderStyle());
builder.writeProperties(properties, styleManager.getPropertyStyles());
for (List