org.databene.edifatto.template.DataModelBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edifatto Show documentation
Show all versions of edifatto Show documentation
'Edifatto' is an open source software library for parsing,
generating and verifying EDIFACT and X12 documents written by Volker Bergmann.
/*
* Copyright (C) 2013-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.databene.edifatto.template;
import org.databene.commons.Assert;
import org.databene.edifatto.gui.ParameterizationHelper;
import org.databene.edifatto.model.Component;
import org.databene.edifatto.model.EdiGroup;
import org.databene.edifatto.model.EdiItem;
import org.databene.edifatto.model.EdiParent;
import org.databene.edifatto.model.Interchange;
import org.databene.edifatto.model.Segment;
/**
* Creates a {@link DataModel} based on an {@link Interchange} with parameterization tags.
* Created: 28.07.2014 15:31:51
* @since 2.0.0
* @author Volker Bergmann
*/
public class DataModelBuilder {
private String javaPackage;
private String parentClass;
public DataModelBuilder(String javaPackage, String parentClass) {
this.javaPackage = javaPackage;
this.parentClass = parentClass;
}
public DataModel buildModel(Interchange interchange) {
DataModel dataModel = new DataModel();
analyzeGroup(interchange, true, dataModel);
return dataModel;
}
private void analyzeGroup(EdiGroup> group, boolean root, DataModel dataModel) {
String beanClassName = Assert.notNull(ParameterizationHelper.getBeanClassName(group), "beanClassName");
ClassInfo classInfo = new ClassInfo(beanClassName, group);
classInfo.setJavaPackage(javaPackage);
classInfo.setParentClass(parentClass);
dataModel.addClassInfo(classInfo);
int singularSegmentCount = countSingularSegments(group);
if (root) {
singularSegmentCount -= 2;
dataModel.setRootClassName(beanClassName);
}
classInfo.setBaseSegmentCount(singularSegmentCount);
extractSimpleProperties(group, classInfo, dataModel);
extractCollectionProperties(group, classInfo, dataModel);
}
private int countSingularSegments(EdiGroup> group) {
int count = 0;
for (int i = 0; i < group.getChildCount(); i++) {
EdiItem child = group.getChild(i);
if (child instanceof EdiGroup && !ParameterizationHelper.isParameterized(child))
count += countSingularSegments((EdiGroup>) child);
else if (child instanceof Segment)
count++;
}
return count;
}
private void extractSimpleProperties(EdiParent extends EdiItem> group, ClassInfo classInfo, DataModel dataModel) {
for (int i = 0; i < group.getChildCount(); i++) {
EdiItem child = group.getChild(i);
String propertyName = ParameterizationHelper.getPropertyName(child);
if (child instanceof EdiParent && !ParameterizationHelper.isParameterized(child))
extractSimpleProperties((EdiParent>) child, classInfo, dataModel);
if (child instanceof Component && propertyName != null)
classInfo.addPropertyInfo(propertyName, "String", false, child);
}
}
private void extractCollectionProperties(EdiParent extends EdiItem> group, ClassInfo classInfo, DataModel dataModel) {
for (int i = 0; i < group.getChildCount(); i++) {
EdiItem child = group.getChild(i);
String propertyName = ParameterizationHelper.getPropertyName(child);
if (child instanceof EdiGroup) {
if (propertyName != null) {
String type = ParameterizationHelper.getBeanClassName(child);
classInfo.addPropertyInfo(propertyName, type, true, child);
analyzeGroup((EdiGroup>) child, false, dataModel);
} else {
extractCollectionProperties((EdiGroup>) child, classInfo, dataModel);
}
}
}
}
}