Search in sources :

Example 1 with PCLProcessesType

use of com.helger.xsds.peppol.codelists2.PCLProcessesType in project peppol-commons by phax.

the class MainCreatePredefinedEnumsFromXML_v8x method _handleProcessIdentifiers.

private static void _handleProcessIdentifiers(final Document aProcessSheet) {
    final PCLProcessesType aList = new GenericJAXBMarshaller<>(PCLProcessesType.class, new QName("dummy")).read(aProcessSheet);
    final ICommonsSet<String> aAllShortcutNames = new CommonsHashSet<>();
    // Create Java source
    try {
        final JDefinedClass jEnum = CM._package(RESULT_PACKAGE_PREFIX + "process")._enum("EPredefinedProcessIdentifier")._implements(IPeppolPredefinedProcessIdentifier.class);
        jEnum.annotate(CodingStyleguideUnaware.class);
        jEnum.javadoc().add(DO_NOT_EDIT);
        // Add metadata
        jEnum.field(JMod.PUBLIC_STATIC_FINAL, CM.ref(String.class), "CODE_LIST_VERSION", JExpr.lit(aList.getVersion()));
        jEnum.field(JMod.PUBLIC_STATIC_FINAL, CM.INT, "CODE_LIST_ENTRY_COUNT", JExpr.lit(aList.getEntryCount().intValue()));
        // enum constants
        for (final PCLProcessType aRow : aList.getProcess()) {
            final String sScheme = aRow.getScheme();
            final String sValue = aRow.getValue();
            final EPeppolCodeListItemState eState = _getState(aRow.getState());
            final boolean bDeprecated = !eState.isActive();
            // Prepend the scheme, if it is non-default
            final String sIDPrefix = PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME.equals(sScheme) ? "" : sScheme + "-";
            final String sEnumConstName = RegExHelper.getAsIdentifier(sIDPrefix + sValue);
            final JEnumConstant jEnumConst = jEnum.enumConstant(sEnumConstName);
            jEnumConst.arg(JExpr.lit(sScheme));
            jEnumConst.arg(JExpr.lit(sValue));
            jEnumConst.arg(CM.ref(EPeppolCodeListItemState.class).enumConstantRef(eState));
            jEnumConst.javadoc().add("ID: <code>" + sScheme + "::" + sValue + "</code><br>");
            if (bDeprecated) {
                jEnumConst.annotate(Deprecated.class);
                jEnumConst.javadoc().addDeprecated().add("This item should not be used to issue new identifiers!");
            }
            // Also create a shortcut for more readable names
            final String sShortcutName = CodeGenerationHelper.createShortcutProcess(sScheme, sValue);
            if (sShortcutName != null) {
                // Make unique name
                int nNext = 2;
                String sRealShortcutName = sShortcutName;
                while (!aAllShortcutNames.add(sRealShortcutName)) {
                    sRealShortcutName = sShortcutName + nNext;
                    nNext++;
                }
                final JFieldVar aShortcut = jEnum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, jEnum, sRealShortcutName, jEnumConst);
                aShortcut.javadoc().add("Same as {@link #" + sEnumConstName + "}");
                if (bDeprecated) {
                    aShortcut.annotate(Deprecated.class);
                    aShortcut.javadoc().addDeprecated().add("This item should not be used to issue new identifiers!");
                }
                jEnumConst.javadoc().add("\nSame as {@link #" + sRealShortcutName + "}");
            }
        }
        {
            // Deprecated names
            final JFieldVar aShortcut = jEnum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, jEnum, "BIS5A_V3", jEnum.fields().get("BIS3_BILLING"));
            aShortcut.annotate(Deprecated.class);
            aShortcut.javadoc().addDeprecated().add("Use BIS3_BILLING instead!");
        }
        // fields
        final JFieldVar fScheme = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sScheme");
        final JFieldVar fValue = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sValue");
        final JFieldVar fState = jEnum.field(JMod.PRIVATE | JMod.FINAL, EPeppolCodeListItemState.class, "m_eState");
        // Constructor
        final JMethod jCtor = jEnum.constructor(0);
        final JVar jScheme = jCtor.param(JMod.FINAL, String.class, "sScheme");
        jScheme.annotate(Nonnull.class);
        jScheme.annotate(Nonempty.class);
        final JVar jValue = jCtor.param(JMod.FINAL, String.class, "sValue");
        jValue.annotate(Nonnull.class);
        jValue.annotate(Nonempty.class);
        final JVar jState = jCtor.param(JMod.FINAL, EPeppolCodeListItemState.class, "eState");
        jState.annotate(Nonnull.class);
        jCtor.body().assign(fScheme, jScheme).assign(fValue, jValue).assign(fState, jState);
        // public String getScheme ()
        JMethod m = jEnum.method(JMod.PUBLIC, String.class, "getScheme");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fScheme);
        // public String getValue ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getValue");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fValue);
        // public EPeppolCodeListItemState getState ()
        m = jEnum.method(JMod.PUBLIC, EPeppolCodeListItemState.class, "getState");
        m.annotate(Nonnull.class);
        m.body()._return(fState);
        // public PeppolProcessIdentifier getAsProcessIdentifier ()
        m = jEnum.method(JMod.PUBLIC, PeppolProcessIdentifier.class, "getAsProcessIdentifier");
        m.annotate(Nonnull.class);
        m.body()._return(JExpr._new(CM.ref(PeppolProcessIdentifier.class)).arg(JExpr._this()));
        // @Nullable public static EPredefinedProcessIdentifier
        // getFromProcessIdentifierOrNull(@Nullable final IProcessIdentifier
        // aProcessID)
        m = jEnum.method(JMod.PUBLIC | JMod.STATIC, jEnum, "getFromProcessIdentifierOrNull");
        {
            m.annotate(Nullable.class);
            final JVar jParam = m.param(JMod.FINAL, IProcessIdentifier.class, "aProcessID");
            jParam.annotate(Nullable.class);
            final JBlock jIf = m.body()._if(jParam.neNull())._then();
            final JForEach jForEach = jIf.forEach(jEnum, "e", jEnum.staticInvoke("values"));
            jForEach.body()._if(jForEach.var().invoke("hasScheme").arg(jParam.invoke("getScheme")).cand(jForEach.var().invoke("hasValue").arg(jParam.invoke("getValue"))))._then()._return(jForEach.var());
            m.body()._return(JExpr._null());
        }
    } catch (final JCodeModelException ex) {
        LOGGER.warn("Failed to create source", ex);
    }
}
Also used : PCLProcessType(com.helger.xsds.peppol.codelists2.PCLProcessType) JDefinedClass(com.helger.jcodemodel.JDefinedClass) QName(javax.xml.namespace.QName) PeppolProcessIdentifier(com.helger.peppolid.peppol.process.PeppolProcessIdentifier) IProcessIdentifier(com.helger.peppolid.IProcessIdentifier) EPeppolCodeListItemState(com.helger.peppolid.peppol.EPeppolCodeListItemState) JEnumConstant(com.helger.jcodemodel.JEnumConstant) JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JForEach(com.helger.jcodemodel.JForEach) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) Nullable(javax.annotation.Nullable) JCodeModelException(com.helger.jcodemodel.JCodeModelException) PCLProcessesType(com.helger.xsds.peppol.codelists2.PCLProcessesType) JVar(com.helger.jcodemodel.JVar)

Aggregations

CommonsHashSet (com.helger.commons.collection.impl.CommonsHashSet)1 JBlock (com.helger.jcodemodel.JBlock)1 JCodeModelException (com.helger.jcodemodel.JCodeModelException)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JEnumConstant (com.helger.jcodemodel.JEnumConstant)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 JForEach (com.helger.jcodemodel.JForEach)1 JMethod (com.helger.jcodemodel.JMethod)1 JVar (com.helger.jcodemodel.JVar)1 IProcessIdentifier (com.helger.peppolid.IProcessIdentifier)1 EPeppolCodeListItemState (com.helger.peppolid.peppol.EPeppolCodeListItemState)1 PeppolProcessIdentifier (com.helger.peppolid.peppol.process.PeppolProcessIdentifier)1 PCLProcessType (com.helger.xsds.peppol.codelists2.PCLProcessType)1 PCLProcessesType (com.helger.xsds.peppol.codelists2.PCLProcessesType)1 Nullable (javax.annotation.Nullable)1 QName (javax.xml.namespace.QName)1