blob: ca6538b5c8e3808088a380a90b6094d0e4a13627 [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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* 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 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.style.CxGridPropPriceStyle
import org.eclipse.osbp.infogrid.model.gridsource.style.CxGridPropQuantityStyle
import org.eclipse.osbp.infogrid.model.gridsource.style.CxGridPropStyle
import org.eclipse.osbp.xtext.table.TableGrid
import org.eclipse.osbp.xtext.table.TableModel
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import static extension org.eclipse.osbp.infogrid.model.gridsource.util.Util.*
/**
* Generates code from your model files on save.
*
* see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
*/
class TableGridSourceGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
resource.toBinary(fsa)
}
def toBinary(Resource input, IFileSystemAccess fsa) {
val TableModel lModel = input.contents.get(0) as TableModel
lModel.packages.forEach [
val packageName = it.name
it.tables.forEach [
val tableName = it.name
if (it.tabletype instanceof TableGrid) {
generateXMI(it.tabletype as TableGrid, packageName, tableName, fsa);
}
]
]
}
def generateXMI(TableGrid grid, String packageName, String tableName, IFileSystemAccess fsa) {
val XMLResource outputRes = new XMLResourceImpl
val ds = grid.source
ds.id = packageName + "." + tableName + "Grid"
// set required inputs using the dto defined in the grid table
ds.rootTypeFQN = '''«(ds.dtoSource.eContainer as LPackage).name».«ds.dtoSource.name»'''
// set required properties
for (prop : ds.properties) {
prop.dotPath = prop.calcDotPath
// prepare dot path in styles
prop.style.prepare
}
val copy = EcoreUtil.copy(ds)
outputRes.contents += copy
// create a writer
val Writer writer = new StringWriter
outputRes.save(writer, null)
fsa.generateFile(ds.id + ".gridsource_xmi", "xmi", writer.toString)
}
def dispatch void prepare(CxGridPropStyle style) {
}
def dispatch void prepare(CxGridPropPriceStyle style) {
style.valuePropertyDotPath = style.valuePropertyPath.calcDotPath
style.currencyPropertyDotPath = style.currencyPropertyPath.calcDotPath
}
def dispatch void prepare(CxGridPropQuantityStyle style) {
style.valuePropertyDotPath = style.valuePropertyPath.calcDotPath
style.uomPropertyDotPath = style.uomPropertyPath.calcDotPath
}
}