Search in sources :

Example 1 with PCLTransportProfileType

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

the class MainCreatePredefinedEnumsFromXML_v8x method _handleTransportProfileIdentifiers.

private static void _handleTransportProfileIdentifiers(final Document aTPSheet) {
    final PCLTransportProfilesType aList = new GenericJAXBMarshaller<>(PCLTransportProfilesType.class, new QName("dummy")).read(aTPSheet);
    // Create Java source
    try {
        final JDefinedClass jEnum = CM._package(RESULT_PACKAGE_PREFIX + "transportprofile")._enum("EPredefinedTransportProfileIdentifier");
        jEnum._implements(CM.ref(IPredefinedTransportProfileIdentifier.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
        final ICommonsSet<String> aAllShortcutNames = new CommonsHashSet<>();
        for (final PCLTransportProfileType aRow : aList.getTransportProfile()) {
            final String sProtocol = aRow.getProtocol();
            final String sProfileVersion = aRow.getProfileVersion();
            final String sProfileID = aRow.getProfileId();
            final String sInitialRelease = aRow.getInitialRelease();
            final EPeppolCodeListItemState eState = _getState(aRow.getState());
            final boolean bDeprecated = !eState.isActive();
            final String sDeprecationRelease = aRow.getDeprecationRelease();
            final LocalDate aRemovalDate = aRow.getRemovalDateLocal();
            // Prepend the scheme, if it is non-default
            final String sEnumConstName = RegExHelper.getAsIdentifier(sProfileID);
            final JEnumConstant jEnumConst = jEnum.enumConstant(sEnumConstName);
            jEnumConst.arg(JExpr.lit(sProtocol));
            jEnumConst.arg(JExpr.lit(sProfileVersion));
            jEnumConst.arg(JExpr.lit(sProfileID));
            jEnumConst.arg(CM.ref(Version.class).staticInvoke("parse").arg(sInitialRelease));
            jEnumConst.arg(CM.ref(EPeppolCodeListItemState.class).enumConstantRef(eState));
            jEnumConst.arg(bDeprecated ? CM.ref(Version.class).staticInvoke("parse").arg(sDeprecationRelease) : JExpr._null());
            jEnumConst.arg(_asExpr(aRemovalDate));
            jEnumConst.javadoc().add("ID: <code>" + sProfileID + "</code><br>");
            jEnumConst.javadoc().addTag(JDocComment.TAG_SINCE).add("code list " + sInitialRelease);
            if (bDeprecated) {
                jEnumConst.annotate(Deprecated.class);
                jEnumConst.javadoc().addDeprecated().add("since " + sDeprecationRelease + " - this item should not be used to issue new identifiers!");
            }
            // Emit shortcut name for better readability
            final String sShortcutName = CodeGenerationHelper.createShortcutTransportProtocolName(sProtocol + "_" + sProfileVersion);
            if (sShortcutName != null) {
                final String sRealShortcutName = sShortcutName;
                if (!aAllShortcutNames.add(sRealShortcutName))
                    throw new IllegalStateException("The Transport Profile shortcut '" + sRealShortcutName + "' is already used - please review the algorithm!");
                final JFieldVar aShortcut = jEnum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, jEnum, sRealShortcutName, jEnumConst);
                aShortcut.javadoc().add("Same as {@link #" + sEnumConstName + "}");
                jEnumConst.javadoc().add("\nSame as {@link #" + sRealShortcutName + "}");
                if (bDeprecated) {
                    aShortcut.annotate(Deprecated.class);
                    aShortcut.javadoc().addDeprecated().add("since " + sDeprecationRelease + " - this item should not be used to issue new identifiers!");
                }
            }
        }
        // fields
        final JFieldVar fProtocol = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sProtocol");
        final JFieldVar fProfileVersion = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sProfileVersion");
        final JFieldVar fProfileID = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sProfileID");
        final JFieldVar fInitialRelease = jEnum.field(JMod.PRIVATE | JMod.FINAL, Version.class, "m_aInitialRelease");
        final JFieldVar fState = jEnum.field(JMod.PRIVATE | JMod.FINAL, EPeppolCodeListItemState.class, "m_eState");
        final JFieldVar fDeprecationRelease = jEnum.field(JMod.PRIVATE | JMod.FINAL, Version.class, "m_aDeprecationRelease");
        final JFieldVar fRemovalDate = jEnum.field(JMod.PRIVATE | JMod.FINAL, LocalDate.class, "m_aRemovalDate");
        // Constructor
        final JMethod jCtor = jEnum.constructor(0);
        final JVar jProtocol = jCtor.param(JMod.FINAL, String.class, "sProtocol");
        jProtocol.annotate(Nonnull.class);
        jProtocol.annotate(Nonempty.class);
        final JVar jProfileVersion = jCtor.param(JMod.FINAL, String.class, "sProfileVersion");
        jProfileVersion.annotate(Nonnull.class);
        jProfileVersion.annotate(Nonempty.class);
        final JVar jProfileID = jCtor.param(JMod.FINAL, String.class, "sProfileID");
        jProfileID.annotate(Nonnull.class);
        jProfileID.annotate(Nonempty.class);
        final JVar jInitialRelease = jCtor.param(JMod.FINAL, Version.class, "aInitialRelease");
        jInitialRelease.annotate(Nonnull.class);
        final JVar jState = jCtor.param(JMod.FINAL, EPeppolCodeListItemState.class, "eState");
        jState.annotate(Nonnull.class);
        final JVar jDeprecationRelease = jCtor.param(JMod.FINAL, Version.class, "aDeprecationRelease");
        jDeprecationRelease.annotate(Nullable.class);
        final JVar jRemovalDate = jCtor.param(JMod.FINAL, LocalDate.class, "aRemovalDate");
        jRemovalDate.annotate(Nullable.class);
        jCtor.body().assign(fProtocol, jProtocol).assign(fProfileVersion, jProfileVersion).assign(fProfileID, jProfileID).assign(fInitialRelease, jInitialRelease).assign(fState, jState).assign(fDeprecationRelease, jDeprecationRelease).assign(fRemovalDate, jRemovalDate);
        // public String getProtocol()
        JMethod m = jEnum.method(JMod.PUBLIC, String.class, "getProtocol");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fProtocol);
        // public String getProfileVersion ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getProfileVersion");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fProfileVersion);
        // public String getProfileID ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getProfileID");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fProfileID);
        // public Version getInitialRelease ()
        m = jEnum.method(JMod.PUBLIC, Version.class, "getInitialRelease");
        m.annotate(Nonnull.class);
        m.body()._return(fInitialRelease);
        // public EPeppolCodeListItemState getState ()
        m = jEnum.method(JMod.PUBLIC, EPeppolCodeListItemState.class, "getState");
        m.annotate(Nonnull.class);
        m.body()._return(fState);
        // public Version getDeprecationRelease ()
        m = jEnum.method(JMod.PUBLIC, Version.class, "getDeprecationRelease");
        m.annotate(Nullable.class);
        m.body()._return(fDeprecationRelease);
        // public LocalDate getRemovalDate ()
        m = jEnum.method(JMod.PUBLIC, LocalDate.class, "getRemovalDate");
        m.annotate(Nullable.class);
        m.body()._return(fRemovalDate);
    } catch (final JCodeModelException ex) {
        LOGGER.warn("Failed to create source", ex);
    }
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) QName(javax.xml.namespace.QName) LocalDate(java.time.LocalDate) PCLTransportProfilesType(com.helger.xsds.peppol.codelists2.PCLTransportProfilesType) EPeppolCodeListItemState(com.helger.peppolid.peppol.EPeppolCodeListItemState) JEnumConstant(com.helger.jcodemodel.JEnumConstant) Version(com.helger.commons.version.Version) JFieldVar(com.helger.jcodemodel.JFieldVar) IPredefinedTransportProfileIdentifier(com.helger.peppolid.peppol.transportprofile.IPredefinedTransportProfileIdentifier) PCLTransportProfileType(com.helger.xsds.peppol.codelists2.PCLTransportProfileType) JMethod(com.helger.jcodemodel.JMethod) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) JCodeModelException(com.helger.jcodemodel.JCodeModelException) JVar(com.helger.jcodemodel.JVar)

Aggregations

CommonsHashSet (com.helger.commons.collection.impl.CommonsHashSet)1 Version (com.helger.commons.version.Version)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 JMethod (com.helger.jcodemodel.JMethod)1 JVar (com.helger.jcodemodel.JVar)1 EPeppolCodeListItemState (com.helger.peppolid.peppol.EPeppolCodeListItemState)1 IPredefinedTransportProfileIdentifier (com.helger.peppolid.peppol.transportprofile.IPredefinedTransportProfileIdentifier)1 PCLTransportProfileType (com.helger.xsds.peppol.codelists2.PCLTransportProfileType)1 PCLTransportProfilesType (com.helger.xsds.peppol.codelists2.PCLTransportProfilesType)1 LocalDate (java.time.LocalDate)1 QName (javax.xml.namespace.QName)1