com.ksc.mission.base.codegen.Model Maven / Gradle / Ivy
package com.ksc.mission.base.codegen;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringJoiner;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ksc.mission.base.fileutils.FileUtil;
import com.ksc.mission.base.interfaces.IOwned;
import com.ksc.mission.base.interfaces.Initializable;
import com.ksc.mission.base.relatedobjects.IOwnedService;
import com.ksc.mission.base.relatedobjects.OwnedList;
import com.ksc.mission.base.relatedobjects.OwnedObject;
import com.ksc.mission.base.relatedobjects.OwningContainer;
public class Model extends OwnedObject implements Initializable {
private static final long serialVersionUID = 1L;
final protected OwnedList modelsViews = new OwnedList(ModelView.class, this);
final protected List propertyLists = new ArrayList();
final protected List properties = new ArrayList();
final protected List templateTypes = new ArrayList();
protected String type;
@JsonIgnore
protected CodeGenerator getApplication() {
return (CodeGenerator)getOwner();
}
@Override
public Model initializeFromStrings(Iterator iterator) {
initializeFromFile(getApplication().resourcePath + iterator.next());
return this;
}
public Model initializeFromFile(String fileName) {
new FileUtil("").doWithLinesIterator(fileName, (iterator) -> {
String[] line = iterator.next();
setType(line[0]);
setId(line[0]);
iterator.next(); // Skip blank line
readModelsViews(iterator);
readPropertyLists(iterator);
});
return this;
}
@JsonIgnore
public List getGenerations() {
List list = new ArrayList();
for(ModelView mv : modelsViews)
for(TemplateType templateType : mv.templateTypes)
list.add(new Generation(mv, templateType));
return list;
}
public void generateAll() {
for(Generation generation : getGenerations())
generation.merge();
}
@JsonIgnore
public List getProperties() {
if(properties != null && properties.isEmpty()) {
for(PropertyList propList : propertyLists)
properties.addAll(propList.properties);
}
return properties;
}
@JsonIgnore
protected PropertyList getPrimaryPropertyList() {
return propertyLists.get(0);
}
@JsonIgnore
public List getPrimaryProperties() {
return getPrimaryPropertyList().properties;
}
@JsonIgnore
public String getTableName() {
return getPrimaryPropertyList().tableName;
}
@JsonIgnore
public String getSchema() {
return getPrimaryPropertyList().schema;
}
@JsonIgnore
public String getCustomHql() {
return getPrimaryPropertyList().customHql;
}
@JsonIgnore
public List getKeys() {
List list = new ArrayList();
for(Property prop : getPrimaryProperties()) {
if("Y".equalsIgnoreCase((String)prop.get("PK")))
list.add(prop.getName());
}
return list;
}
@JsonIgnore
public List getKeyColumns() {
List list = new ArrayList();
for(Property prop : getPrimaryProperties()) {
if("Y".equalsIgnoreCase((String)prop.get("PK")))
list.add(prop.get("Column"));
}
return list;
}
@JsonIgnore
public String getKeysAsJSON() {
StringJoiner stringJoiner = new StringJoiner(",","[","]");
for(String key : getKeys())
stringJoiner.add(key);
return stringJoiner.toString();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private void readModelsViews(Iterator iterator) {
iterator.next(); // Skip ModelsViews header
modelsViews.initializeFromLinesUpTo(iterator, "Primary Properties");
}
private void readPropertyLists(Iterator iterator) {
while(iterator.hasNext()) {
PropertyList propList = new PropertyList(this).initializeFrom(iterator);
propertyLists.add(propList);
}
}
}