final String structureName = Utils.getCamelNameFirstCapital(tableName);
final String className = structureName + "ObjectModel";
final PrintStream out = new PrintStream( Utils.getCamelNameFirstCapital(dirPath + className + ".java"), "UTF-8" );
Logger.getLogger(ObjectModelGeneration.class.getName()).info("Working on: " + tableName);
final ClassModel classModel = new ClassModel(Visibility.PUBLIC, className, false);
classModel.setPackageName(this.packageName);
// Let's look for the foreign keys and let's add the properties
final Map<String, String> fks = addExternalClasses(tableInfo, classModel);
// There can be more than one field for just one external table.
// Let's group by the Class name, saving just the first column name.
final HashSet<String> usedClasses = new HashSet<String>();
final Set<Entry<String,String>> fksEntries = fks.entrySet();
for (Entry<String, String> fksEntry : fksEntries) {
final String examinedClass = Utils.getCamelNameFirstCapital( fksEntry.getValue() ) + "ObjectModel";
if (!usedClasses.contains(examinedClass)) {
usedClasses.add(examinedClass);
classModel.addProperty(new PropertyModel(Visibility.PRIVATE, examinedClass, fksEntry.getKey()));
}
}
// Let's add the properties for the columns
for (PropertyModel prop : tableInfo.getProperties()) {
if (!fks.containsKey(prop.getName()))
classModel.addProperty(prop);
}
final List<PropertyModel> primaryKeys = new LinkedList<PropertyModel>();
final List<PropertyModel> presentProps = classModel.getProperties();
for(PropertyModel primaryKey : tableInfo.getPrimaryKey()) {
final String key = primaryKey.getName();
for (PropertyModel presentProp : presentProps) {
if (presentProp.getName().equals(key)) {
primaryKeys.add(new PropertyModel(Visibility.PRIVATE, presentProp.getPropertyClass(), key));
}
}
}
classModel.setConstructorGenerator(new PrimaryKeyConstructor(className, primaryKeys));
// Getters
classModel.createGetters();
// Setters
classModel.createSetters();
classModel.write(out);
out.flush();
out.close();
}
}