|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.othree.apx; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.JsonNode; |
| 9 | +import com.google.gson.annotations.SerializedName; |
| 10 | +import com.j256.ormlite.field.DatabaseField; |
| 11 | +import com.sun.codemodel.JDefinedClass; |
| 12 | +import com.sun.codemodel.JFieldVar; |
| 13 | +import com.sun.codemodel.JMethod; |
| 14 | +import com.sun.codemodel.JMod; |
| 15 | +import org.jsonschema2pojo.Schema; |
| 16 | +import org.jsonschema2pojo.rules.Rule; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * @author root |
| 21 | + */ |
| 22 | +public class APXCustomRule implements Rule<JDefinedClass, JDefinedClass> { |
| 23 | + |
| 24 | + APXCustomRuleFactory fx; |
| 25 | + |
| 26 | + APXCustomRule(APXCustomRuleFactory aThis) { |
| 27 | + this.fx = aThis; |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public JDefinedClass apply(String string, JsonNode jn, JDefinedClass t, Schema schema) { |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + JFieldVar idField = t.field(JMod.PRIVATE, int.class, "id"); |
| 37 | + JMethod getter = t.method(JMod.PUBLIC, idField.type(), "getId"); |
| 38 | + getter.body()._return(idField); |
| 39 | + |
| 40 | + idField.javadoc().add("This is compulsory for both the database and to access models"); |
| 41 | + |
| 42 | + |
| 43 | + JMethod setter = t.method(JMod.PUBLIC, void.class, "setId"); |
| 44 | + setter.param(idField.type(), idField.name()) |
| 45 | + .assign(idField); |
| 46 | + |
| 47 | + idField.annotate(DatabaseField.class) |
| 48 | + .param("generatedId", true) |
| 49 | + .param("columnName", idField.name()); |
| 50 | + |
| 51 | + idField.annotate(SerializedName.class) |
| 52 | + .param("value", "id"); |
| 53 | + |
| 54 | + return t; |
| 55 | + } |
| 56 | +} |
0 commit comments