Search in sources :

Example 1 with SimpleCodeList

use of com.helger.genericode.v10.SimpleCodeList in project peppol-commons by phax.

the class MainCreateEnumsFromUBLGenericode method _createGenericode10.

private static void _createGenericode10(final File aFile, final CodeListDocument aCodeList10) throws JCodeModelException {
    final SimpleCodeList aSimpleCodeList = aCodeList10.getSimpleCodeList();
    if (aSimpleCodeList == null) {
        LOGGER.info("  does not contain a SimpleCodeList!");
        return;
    }
    final Column aColCode = Genericode10Helper.getColumnOfID(aCodeList10.getColumnSet(), COLID_CODE);
    if (aColCode == null) {
        LOGGER.info("  No '" + COLID_CODE + "' column found");
        return;
    }
    if (!Genericode10Helper.isKeyColumn(aCodeList10.getColumnSet(), COLID_CODE)) {
        LOGGER.info("  Column '" + COLID_CODE + "' is not a key");
        return;
    }
    final Column aColName = Genericode10Helper.getColumnOfID(aCodeList10.getColumnSet(), COLID_NAME);
    if (aColName == null) {
        LOGGER.info("  No '" + COLID_NAME + "' column found");
        return;
    }
    final JDefinedClass jEnum = CM._package("com.helger.peppol.codelist")._enum("E" + RegExHelper.getAsIdentifier(aCodeList10.getIdentification().getShortName().getValue()))._implements(CM.ref(IHasID.class).narrow(String.class))._implements(IHasDisplayName.class);
    jEnum.javadoc().add("This file is generated from Genericode file " + aFile.getName() + ". Do NOT edit!");
    for (final Row aRow : aCodeList10.getSimpleCodeList().getRow()) {
        final String sCode = Genericode10Helper.getRowValue(aRow, COLID_CODE);
        final String sName = Genericode10Helper.getRowValue(aRow, COLID_NAME);
        String sIdentifier = RegExHelper.getAsIdentifier(sName.toUpperCase(Locale.US));
        sIdentifier = StringHelper.replaceAllRepeatedly(sIdentifier, "__", "_");
        final JEnumConstant jEnumConst = jEnum.enumConstant(sIdentifier);
        jEnumConst.arg(JExpr.lit(sCode));
        jEnumConst.arg(JExpr.lit(sName));
    }
    // fields
    final JFieldVar fID = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sID");
    final JFieldVar fDisplayName = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sDisplayName");
    // Constructor
    final JMethod jCtor = jEnum.constructor(JMod.PRIVATE);
    JVar jID = jCtor.param(JMod.FINAL, String.class, "sID");
    jID.annotate(Nonnull.class);
    jID.annotate(Nonempty.class);
    final JVar jDisplayName = jCtor.param(JMod.FINAL, String.class, "sDisplayName");
    jDisplayName.annotate(Nonnull.class);
    jCtor.body().assign(fID, jID).assign(fDisplayName, jDisplayName);
    // public String getID ()
    JMethod m = jEnum.method(JMod.PUBLIC, String.class, "getID");
    m.annotate(Nonnull.class);
    m.annotate(Nonempty.class);
    m.body()._return(fID);
    // public String getDisplayName ()
    m = jEnum.method(JMod.PUBLIC, String.class, "getDisplayName");
    m.annotate(Nonnull.class);
    m.body()._return(fDisplayName);
    // public static E... getFromIDOrNull (@Nullable String sID)
    m = jEnum.method(JMod.PUBLIC | JMod.STATIC, jEnum, "getFromIDOrNull");
    m.annotate(Nullable.class);
    jID = m.param(JMod.FINAL, String.class, "sID");
    jID.annotate(Nullable.class);
    m.body()._return(CM.ref(EnumHelper.class).staticInvoke("getFromIDOrNull").arg(JExpr.dotClass(jEnum)).arg(jID));
    // public static String getDisplayNameFromIDOrNull (@Nullable String sID)
    m = jEnum.method(JMod.PUBLIC | JMod.STATIC, String.class, "getDisplayNameFromIDOrNull");
    m.annotate(Nullable.class);
    jID = m.param(JMod.FINAL, String.class, "sID");
    jID.annotate(Nullable.class);
    final JVar jValue = m.body().decl(JMod.FINAL, jEnum, "eValue", jEnum.staticInvoke("getFromIDOrNull").arg(jID));
    m.body()._return(JOp.cond(JOp.eq(jValue, JExpr._null()), JExpr._null(), jValue.invoke("getDisplayName")));
}
Also used : SimpleCodeList(com.helger.genericode.v10.SimpleCodeList) JEnumConstant(com.helger.jcodemodel.JEnumConstant) Column(com.helger.genericode.v10.Column) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) IHasID(com.helger.commons.id.IHasID) Row(com.helger.genericode.v10.Row) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Aggregations

IHasID (com.helger.commons.id.IHasID)1 Column (com.helger.genericode.v10.Column)1 Row (com.helger.genericode.v10.Row)1 SimpleCodeList (com.helger.genericode.v10.SimpleCodeList)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JEnumConstant (com.helger.jcodemodel.JEnumConstant)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 JMethod (com.helger.jcodemodel.JMethod)1 JVar (com.helger.jcodemodel.JVar)1