|  | @@ -1,6 +1,7 @@
 | 
		
	
		
			
			| 1 | 1 |  package com.rthoni.intellij.codefromds.business;
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			| 3 | 3 |  import com.intellij.database.model.DasForeignKey;
 | 
		
	
		
			
			|  | 4 | +import com.intellij.database.model.DataType;
 | 
		
	
		
			
			| 4 | 5 |  import com.intellij.database.model.MultiRef;
 | 
		
	
		
			
			| 5 | 6 |  import com.intellij.database.psi.DbDataSource;
 | 
		
	
		
			
			| 6 | 7 |  import com.intellij.database.util.DasUtil;
 | 
		
	
	
		
			
			|  | @@ -8,10 +9,12 @@ import com.intellij.openapi.project.Project;
 | 
		
	
		
			
			| 8 | 9 |  import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
 | 
		
	
		
			
			| 9 | 10 |  import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
 | 
		
	
		
			
			| 10 | 11 |  import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
 | 
		
	
		
			
			|  | 12 | +import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
 | 
		
	
		
			
			| 11 | 13 |  import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
 | 
		
	
		
			
			| 12 | 14 |  import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
 | 
		
	
		
			
			| 13 | 15 |  import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
 | 
		
	
		
			
			| 14 | 16 |  import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
 | 
		
	
		
			
			|  | 17 | +import org.json.JSONArray;
 | 
		
	
		
			
			| 15 | 18 |  import org.json.JSONObject;
 | 
		
	
		
			
			| 16 | 19 |  import org.jtwig.JtwigModel;
 | 
		
	
		
			
			| 17 | 20 |  import org.jtwig.JtwigTemplate;
 | 
		
	
	
		
			
			|  | @@ -44,7 +47,7 @@ public abstract class Generator {
 | 
		
	
		
			
			| 44 | 47 |  
 | 
		
	
		
			
			| 45 | 48 |      public static GenerateOptions loadOptions(String configPath) throws Exception
 | 
		
	
		
			
			| 46 | 49 |      {
 | 
		
	
		
			
			| 47 |  | -        String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
 | 
		
	
		
			
			|  | 50 | +        String data = Helper.readFile(configPath);
 | 
		
	
		
			
			| 48 | 51 |  
 | 
		
	
		
			
			| 49 | 52 |          JSONObject obj = new JSONObject(data);
 | 
		
	
		
			
			| 50 | 53 |          String src = obj.getJSONObject("selection").getString("source");
 | 
		
	
	
		
			
			|  | @@ -81,32 +84,53 @@ public abstract class Generator {
 | 
		
	
		
			
			| 81 | 84 |          return dbo;
 | 
		
	
		
			
			| 82 | 85 |      }
 | 
		
	
		
			
			| 83 | 86 |  
 | 
		
	
		
			
			| 84 |  | -    public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
 | 
		
	
		
			
			|  | 87 | +    public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection, TypesCastOptions options)
 | 
		
	
		
			
			| 85 | 88 |      {
 | 
		
	
		
			
			| 86 | 89 |          ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
 | 
		
	
		
			
			| 87 | 90 |          dbo.setName(columnSelection.getColumn().getName());
 | 
		
	
		
			
			| 88 | 91 |          dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
 | 
		
	
		
			
			| 89 | 92 |          dbo.setSelected(columnSelection.isSelected());
 | 
		
	
		
			
			|  | 93 | +
 | 
		
	
		
			
			|  | 94 | +        DataType type = columnSelection.getColumn().getDataType();
 | 
		
	
		
			
			|  | 95 | +        boolean isArray = type.typeName.endsWith("[]");
 | 
		
	
		
			
			|  | 96 | +        String sqlTypeName = isArray ? type.typeName.substring(0, type.typeName.length() - 2) : type.typeName;
 | 
		
	
		
			
			|  | 97 | +        String typeName = type.typeName;
 | 
		
	
		
			
			|  | 98 | +        HashMap<String, HashMap<String, String>> types = options.getTypes();
 | 
		
	
		
			
			|  | 99 | +
 | 
		
	
		
			
			|  | 100 | +        if (types.containsKey(sqlTypeName)) {
 | 
		
	
		
			
			|  | 101 | +            HashMap<String, String> subtype = types.get(sqlTypeName);
 | 
		
	
		
			
			|  | 102 | +            if (subtype.containsKey(type.vagueArg)) {
 | 
		
	
		
			
			|  | 103 | +                typeName = subtype.get(type.vagueArg);
 | 
		
	
		
			
			|  | 104 | +            }
 | 
		
	
		
			
			|  | 105 | +            else if (subtype.containsKey("*")) {
 | 
		
	
		
			
			|  | 106 | +                typeName = subtype.get("*");
 | 
		
	
		
			
			|  | 107 | +            }
 | 
		
	
		
			
			|  | 108 | +        }
 | 
		
	
		
			
			|  | 109 | +        if (isArray) {
 | 
		
	
		
			
			|  | 110 | +            typeName = options.getArrayTemplate().replace("%t", typeName);
 | 
		
	
		
			
			|  | 111 | +        }
 | 
		
	
		
			
			|  | 112 | +        dbo.setType(typeName);
 | 
		
	
		
			
			|  | 113 | +
 | 
		
	
		
			
			| 90 | 114 |          return dbo;
 | 
		
	
		
			
			| 91 | 115 |      }
 | 
		
	
		
			
			| 92 | 116 |  
 | 
		
	
		
			
			| 93 |  | -    public static TableDataSourceDbo convertTable(TableSelection tableSelection)
 | 
		
	
		
			
			|  | 117 | +    public static TableDataSourceDbo convertTable(TableSelection tableSelection, TypesCastOptions types)
 | 
		
	
		
			
			| 94 | 118 |      {
 | 
		
	
		
			
			| 95 | 119 |          TableDataSourceDbo dbo = new TableDataSourceDbo();
 | 
		
	
		
			
			| 96 | 120 |          dbo.setName(tableSelection.getTable().getName());
 | 
		
	
		
			
			| 97 |  | -        dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
 | 
		
	
		
			
			|  | 121 | +        dbo.setColumns(tableSelection.getColumns().stream().map(c -> convertColumn(c, types)).collect(Collectors.toList()));
 | 
		
	
		
			
			| 98 | 122 |          dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
 | 
		
	
		
			
			| 99 | 123 |          dbo.setHasAny(!tableSelection.hasNone());
 | 
		
	
		
			
			| 100 | 124 |          dbo.setTargetForeignKeys(new Vector<>());
 | 
		
	
		
			
			| 101 | 125 |          return dbo;
 | 
		
	
		
			
			| 102 | 126 |      }
 | 
		
	
		
			
			| 103 | 127 |  
 | 
		
	
		
			
			| 104 |  | -    public static DataSourceDbo convertOptions(GenerateOptions options)
 | 
		
	
		
			
			|  | 128 | +    public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
 | 
		
	
		
			
			| 105 | 129 |      {
 | 
		
	
		
			
			| 106 | 130 |          DataSourceDbo dbo = new DataSourceDbo();
 | 
		
	
		
			
			| 107 | 131 |  
 | 
		
	
		
			
			| 108 | 132 |          dbo.setName(options.getSelection().getSource().getName());
 | 
		
	
		
			
			| 109 |  | -        dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
 | 
		
	
		
			
			|  | 133 | +        dbo.setTables(options.getSelection().getTables().stream().map(t -> convertTable(t, types)).collect(Collectors.toList()));
 | 
		
	
		
			
			| 110 | 134 |  
 | 
		
	
		
			
			| 111 | 135 |          List<TableDataSourceDbo> tables = dbo.getTables();
 | 
		
	
		
			
			| 112 | 136 |          List<TableSelection> tableSelections = options.getSelection().getTables();
 | 
		
	
	
		
			
			|  | @@ -119,6 +143,36 @@ public abstract class Generator {
 | 
		
	
		
			
			| 119 | 143 |          return dbo;
 | 
		
	
		
			
			| 120 | 144 |      }
 | 
		
	
		
			
			| 121 | 145 |  
 | 
		
	
		
			
			|  | 146 | +    public static TypesCastOptions loadTypesCast(String file) throws IOException
 | 
		
	
		
			
			|  | 147 | +    {
 | 
		
	
		
			
			|  | 148 | +        TypesCastOptions dbo = new TypesCastOptions();
 | 
		
	
		
			
			|  | 149 | +        String data = Helper.readFile(file);
 | 
		
	
		
			
			|  | 150 | +
 | 
		
	
		
			
			|  | 151 | +        JSONObject obj = new JSONObject(data);
 | 
		
	
		
			
			|  | 152 | +        JSONObject objTypes = obj.getJSONObject("types");
 | 
		
	
		
			
			|  | 153 | +        HashMap<String, HashMap<String, String>> map = new HashMap<>();
 | 
		
	
		
			
			|  | 154 | +
 | 
		
	
		
			
			|  | 155 | +        for (String key : objTypes.keySet()) {
 | 
		
	
		
			
			|  | 156 | +            HashMap<String, String> typeMap = new HashMap<>();
 | 
		
	
		
			
			|  | 157 | +            String type = objTypes.optString(key);
 | 
		
	
		
			
			|  | 158 | +            JSONObject typeObject = objTypes.optJSONObject(key);
 | 
		
	
		
			
			|  | 159 | +            if (type != null) {
 | 
		
	
		
			
			|  | 160 | +                typeMap.put("*", type);
 | 
		
	
		
			
			|  | 161 | +            }
 | 
		
	
		
			
			|  | 162 | +            else if (typeObject != null) {
 | 
		
	
		
			
			|  | 163 | +                for (String subtype : typeObject.keySet()) {
 | 
		
	
		
			
			|  | 164 | +                    typeMap.put(subtype, typeObject.getString(subtype));
 | 
		
	
		
			
			|  | 165 | +                }
 | 
		
	
		
			
			|  | 166 | +            }
 | 
		
	
		
			
			|  | 167 | +            map.put(key, typeMap);
 | 
		
	
		
			
			|  | 168 | +        }
 | 
		
	
		
			
			|  | 169 | +        dbo.setTypes(map);
 | 
		
	
		
			
			|  | 170 | +
 | 
		
	
		
			
			|  | 171 | +        dbo.setArrayTemplate(obj.getString("arrayTemplate"));
 | 
		
	
		
			
			|  | 172 | +
 | 
		
	
		
			
			|  | 173 | +        return dbo;
 | 
		
	
		
			
			|  | 174 | +    }
 | 
		
	
		
			
			|  | 175 | +
 | 
		
	
		
			
			| 122 | 176 |      public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
 | 
		
	
		
			
			| 123 | 177 |      {
 | 
		
	
		
			
			| 124 | 178 |          String data = Helper.readFile(templatePath);
 | 
		
	
	
		
			
			|  | @@ -138,8 +192,11 @@ public abstract class Generator {
 | 
		
	
		
			
			| 138 | 192 |          String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
 | 
		
	
		
			
			| 139 | 193 |          String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
 | 
		
	
		
			
			| 140 | 194 |          String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
 | 
		
	
		
			
			|  | 195 | +        String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
 | 
		
	
		
			
			|  | 196 | +
 | 
		
	
		
			
			|  | 197 | +        TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
 | 
		
	
		
			
			| 141 | 198 |  
 | 
		
	
		
			
			| 142 |  | -        DataSourceDbo dbo = convertOptions(options);
 | 
		
	
		
			
			|  | 199 | +        DataSourceDbo dbo = convertOptions(options, types);
 | 
		
	
		
			
			| 143 | 200 |  
 | 
		
	
		
			
			| 144 | 201 |          generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
 | 
		
	
		
			
			| 145 | 202 |                  "Database." + options.getFilesExtension(), dbo, null);
 |