blob: e8b51212cd4363097b0f2948b24d93856206b377 [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.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 {
@Override
public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
this.toBinary(resource, fsa);
}
public void toBinary(final Resource input, final IFileSystemAccess fsa) {
EObject _get = input.getContents().get(0);
final TableModel lModel = ((TableModel) _get);
final Consumer<TablePackage> _function = (TablePackage it) -> {
final String packageName = it.getName();
final Consumer<Table> _function_1 = (Table it_1) -> {
final String tableName = it_1.getName();
TableOption _tabletype = it_1.getTabletype();
if ((_tabletype instanceof TableGrid)) {
TableOption _tabletype_1 = it_1.getTabletype();
this.generateXMI(((TableGrid) _tabletype_1), packageName, tableName, fsa);
}
};
it.getTables().forEach(_function_1);
};
lModel.getPackages().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();
EObject _eContainer = ds.getDtoSource().eContainer();
String _name = ((LPackage) _eContainer).getName();
_builder.append(_name);
_builder.append(".");
String _name_1 = ds.getDtoSource().getName();
_builder.append(_name_1);
ds.setRootTypeFQN(_builder.toString());
EList<CxGridProperty> _properties = ds.getProperties();
for (final CxGridProperty prop : _properties) {
{
prop.setDotPath(Util.calcDotPath(prop));
this.prepare(prop.getStyle());
}
}
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");
fsa.generateFile(_plus, "xmi", writer.toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
protected void _prepare(final CxGridPropStyle style) {
}
protected void _prepare(final CxGridPropPriceStyle style) {
style.setValuePropertyDotPath(Util.calcDotPath(style.getValuePropertyPath()));
style.setCurrencyPropertyDotPath(Util.calcDotPath(style.getCurrencyPropertyPath()));
}
protected void _prepare(final CxGridPropQuantityStyle style) {
style.setValuePropertyDotPath(Util.calcDotPath(style.getValuePropertyPath()));
style.setUomPropertyDotPath(Util.calcDotPath(style.getUomPropertyPath()));
}
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());
}
}
}