blob: 1088e4093ad077e5add90d6e04e2b3d358f86283 [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.xtext.table.generator;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.function.Consumer;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
import org.eclipse.osbp.dsl.semantic.common.types.LPackage;
import org.eclipse.osbp.dsl.semantic.dto.LDto;
import org.eclipse.osbp.infogrid.model.gridsource.CxGridNestedField;
import org.eclipse.osbp.infogrid.model.gridsource.CxGridProperty;
import org.eclipse.osbp.infogrid.model.gridsource.style.CxGridPropPriceStyle;
import org.eclipse.osbp.infogrid.model.gridsource.style.CxGridPropQuantityStyle;
import org.eclipse.osbp.infogrid.model.gridsource.style.CxGridPropStyle;
import org.eclipse.osbp.infogrid.model.gridsource.util.Util;
import org.eclipse.osbp.xtext.table.Table;
import org.eclipse.osbp.xtext.table.TableDtoDatasource;
import org.eclipse.osbp.xtext.table.TableGrid;
import org.eclipse.osbp.xtext.table.TableModel;
import org.eclipse.osbp.xtext.table.TableOption;
import org.eclipse.osbp.xtext.table.TablePackage;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.xbase.lib.Exceptions;
/**
* Generates code from your model files on save.
*
* see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
*/
@SuppressWarnings("all")
public class TableGridSourceGenerator implements IGenerator {
public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
this.toBinary(resource, fsa);
}
public void toBinary(final Resource input, final IFileSystemAccess fsa) {
EList<EObject> _contents = input.getContents();
EObject _get = _contents.get(0);
final TableModel lModel = ((TableModel) _get);
EList<TablePackage> _packages = lModel.getPackages();
final Consumer<TablePackage> _function = new Consumer<TablePackage>() {
public void accept(final TablePackage it) {
final String packageName = it.getName();
EList<Table> _tables = it.getTables();
final Consumer<Table> _function = new Consumer<Table>() {
public void accept(final Table it) {
final String tableName = it.getName();
TableOption _tabletype = it.getTabletype();
if ((_tabletype instanceof TableGrid)) {
TableOption _tabletype_1 = it.getTabletype();
TableGridSourceGenerator.this.generateXMI(((TableGrid) _tabletype_1), packageName, tableName, fsa);
}
}
};
_tables.forEach(_function);
}
};
_packages.forEach(_function);
}
public void generateXMI(final TableGrid grid, final String packageName, final String tableName, final IFileSystemAccess fsa) {
try {
final XMLResource outputRes = new XMLResourceImpl();
final TableDtoDatasource ds = grid.getSource();
ds.setId((((packageName + ".") + tableName) + "Grid"));
StringConcatenation _builder = new StringConcatenation();
LDto _dtoSource = ds.getDtoSource();
EObject _eContainer = _dtoSource.eContainer();
String _name = ((LPackage) _eContainer).getName();
_builder.append(_name, "");
_builder.append(".");
LDto _dtoSource_1 = ds.getDtoSource();
String _name_1 = _dtoSource_1.getName();
_builder.append(_name_1, "");
ds.setRootTypeFQN(_builder.toString());
EList<CxGridProperty> _properties = ds.getProperties();
for (final CxGridProperty prop : _properties) {
{
String _calcDotPath = Util.calcDotPath(prop);
prop.setDotPath(_calcDotPath);
CxGridPropStyle _style = prop.getStyle();
this.prepare(_style);
}
}
final TableDtoDatasource copy = EcoreUtil.<TableDtoDatasource>copy(ds);
EList<EObject> _contents = outputRes.getContents();
_contents.add(copy);
final Writer writer = new StringWriter();
outputRes.save(writer, null);
String _id = ds.getId();
String _plus = (_id + ".gridsource_xmi");
String _string = writer.toString();
fsa.generateFile(_plus, "xmi", _string);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
protected void _prepare(final CxGridPropStyle style) {
}
protected void _prepare(final CxGridPropPriceStyle style) {
CxGridNestedField _valuePropertyPath = style.getValuePropertyPath();
String _calcDotPath = Util.calcDotPath(_valuePropertyPath);
style.setValuePropertyDotPath(_calcDotPath);
CxGridNestedField _currencyPropertyPath = style.getCurrencyPropertyPath();
String _calcDotPath_1 = Util.calcDotPath(_currencyPropertyPath);
style.setCurrencyPropertyDotPath(_calcDotPath_1);
}
protected void _prepare(final CxGridPropQuantityStyle style) {
CxGridNestedField _valuePropertyPath = style.getValuePropertyPath();
String _calcDotPath = Util.calcDotPath(_valuePropertyPath);
style.setValuePropertyDotPath(_calcDotPath);
CxGridNestedField _uomPropertyPath = style.getUomPropertyPath();
String _calcDotPath_1 = Util.calcDotPath(_uomPropertyPath);
style.setUomPropertyDotPath(_calcDotPath_1);
}
public void prepare(final CxGridPropStyle style) {
if (style instanceof CxGridPropPriceStyle) {
_prepare((CxGridPropPriceStyle)style);
return;
} else if (style instanceof CxGridPropQuantityStyle) {
_prepare((CxGridPropQuantityStyle)style);
return;
} else if (style != null) {
_prepare(style);
return;
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(style).toString());
}
}
}