Search in sources :

Example 1 with JEnumConstant

use of com.helger.jcodemodel.JEnumConstant in project peppol-commons by phax.

the class MainCreatePredefinedEnumsFromXML_v8x method _handleParticipantIdentifierSchemes.

private static void _handleParticipantIdentifierSchemes(final Document aParticipantSheet) {
    final PCLParticipantIdentifierSchemesType aList = new GenericJAXBMarshaller<>(PCLParticipantIdentifierSchemesType.class, new QName("dummy")).read(aParticipantSheet);
    // Create Java source
    try {
        final JDefinedClass jEnum = CM._package(RESULT_PACKAGE_PREFIX + "pidscheme")._enum("EPredefinedParticipantIdentifierScheme")._implements(IParticipantIdentifierScheme.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 PCLParticipantIdentifierSchemeType aRow : aList.getParticipantIdentifierScheme()) {
            final String sSchemeID = aRow.getSchemeid();
            final String sISO6523 = aRow.getIso6523();
            final String sCountryCode = aRow.getCountry();
            final String sSchemeName = aRow.getSchemeName();
            final String sIssuingAgency = aRow.getIssuingAgency();
            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();
            final String sStructure = aRow.getStructure();
            final String sDisplay = aRow.getDisplay();
            final String sExamples = aRow.getExamples();
            final String sUsage = aRow.getUsage();
            final String sEnumConstName = RegExHelper.getAsIdentifier(sSchemeID);
            final JEnumConstant jEnumConst = jEnum.enumConstant(sEnumConstName);
            jEnumConst.arg(JExpr.lit(sSchemeID));
            jEnumConst.arg(JExpr.lit(sISO6523));
            jEnumConst.arg(JExpr.lit(sCountryCode));
            jEnumConst.arg(JExpr.lit(sSchemeName));
            jEnumConst.arg(sIssuingAgency == null ? JExpr._null() : JExpr.lit(sIssuingAgency));
            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("Prefix <code>" + sISO6523 + "</code>, scheme ID <code>" + sSchemeID + "</code><br>");
            if (StringHelper.hasText(sStructure))
                jEnumConst.javadoc().add("\nStructure of the code: " + CodeGenerationHelper.maskHtml(sStructure) + "<br>");
            if (StringHelper.hasText(sDisplay))
                jEnumConst.javadoc().add("\nDisplay requirements: " + CodeGenerationHelper.maskHtml(sDisplay) + "<br>");
            if (StringHelper.hasText(sExamples))
                jEnumConst.javadoc().add("\nExample value: " + CodeGenerationHelper.maskHtml(sExamples) + "<br>");
            if (StringHelper.hasText(sUsage))
                jEnumConst.javadoc().add("\nUsage information: " + CodeGenerationHelper.maskHtml(sUsage) + "<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!");
            }
        }
        // fields
        final JFieldVar fSchemeID = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sSchemeID");
        final JFieldVar fISO6523 = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sISO6523");
        final JFieldVar fCountryCode = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sCountryCode");
        final JFieldVar fSchemeName = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sSchemeName");
        final JFieldVar fIssuingAgency = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sIssuingAgency");
        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 jSchemeID = jCtor.param(JMod.FINAL, String.class, "sSchemeID");
        jSchemeID.annotate(Nonnull.class);
        jSchemeID.annotate(Nonempty.class);
        final JVar jISO6523 = jCtor.param(JMod.FINAL, String.class, "sISO6523");
        jISO6523.annotate(Nonnull.class);
        jISO6523.annotate(Nonempty.class);
        final JVar jCountryCode = jCtor.param(JMod.FINAL, String.class, "sCountryCode");
        jCountryCode.annotate(Nonnull.class);
        jCountryCode.annotate(Nonempty.class);
        final JVar jSchemeName = jCtor.param(JMod.FINAL, String.class, "sSchemeName");
        jSchemeName.annotate(Nonnull.class);
        jSchemeName.annotate(Nonempty.class);
        final JVar jIssuingAgency = jCtor.param(JMod.FINAL, String.class, "sIssuingAgency");
        jIssuingAgency.annotate(Nullable.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(fSchemeID, jSchemeID).assign(fISO6523, jISO6523).assign(fCountryCode, jCountryCode).assign(fSchemeName, jSchemeName).assign(fIssuingAgency, jIssuingAgency).assign(fInitialRelease, jInitialRelease).assign(fState, jState).assign(fDeprecationRelease, jDeprecationRelease).assign(fRemovalDate, jRemovalDate);
        // public String getSchemeID ()
        JMethod m = jEnum.method(JMod.PUBLIC, String.class, "getSchemeID");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fSchemeID);
        // public String getISO6523Code ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getISO6523Code");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fISO6523);
        // public String getCountryCode ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getCountryCode");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fCountryCode);
        // public String getSchemeName ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getSchemeName");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fSchemeName);
        // public String getSchemeAgency ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getSchemeAgency");
        m.annotate(Nullable.class);
        m.body()._return(fIssuingAgency);
        // 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 Exception ex) {
        LOGGER.warn("Failed to create source", ex);
    }
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) QName(javax.xml.namespace.QName) LocalDate(java.time.LocalDate) JCodeModelException(com.helger.jcodemodel.JCodeModelException) EPeppolCodeListItemState(com.helger.peppolid.peppol.EPeppolCodeListItemState) JEnumConstant(com.helger.jcodemodel.JEnumConstant) Version(com.helger.commons.version.Version) JFieldVar(com.helger.jcodemodel.JFieldVar) PCLParticipantIdentifierSchemeType(com.helger.xsds.peppol.codelists2.PCLParticipantIdentifierSchemeType) JMethod(com.helger.jcodemodel.JMethod) PCLParticipantIdentifierSchemesType(com.helger.xsds.peppol.codelists2.PCLParticipantIdentifierSchemesType) JVar(com.helger.jcodemodel.JVar)

Example 2 with JEnumConstant

use of com.helger.jcodemodel.JEnumConstant in project peppol-commons by phax.

the class MainCreatePredefinedEnumsFromXML_v8x method _handleDocumentTypes.

private static void _handleDocumentTypes(final Document aDocumentSheet) {
    final PCLDocumentTypesType aList = new GenericJAXBMarshaller<>(PCLDocumentTypesType.class, new QName("dummy")).read(aDocumentSheet);
    // Create Java source
    try {
        final JDefinedClass jEnum = CM._package(RESULT_PACKAGE_PREFIX + "doctype")._enum("EPredefinedDocumentTypeIdentifier")._implements(IPeppolPredefinedDocumentTypeIdentifier.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()));
        final ICommonsSet<String> aAllShortcutNames = new CommonsHashSet<>();
        // Add all enum constants
        for (final PCLDocumentTypeType aRow : aList.getDocumentType()) {
            final String sProfileCode = aRow.getName();
            final String sScheme = aRow.getScheme();
            final String sValue = aRow.getValue();
            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();
            // Split ID in it's pieces
            final IPeppolDocumentTypeIdentifierParts aDocIDParts = PeppolDocumentTypeIdentifierParts.extractFromString(sValue);
            final String sEnumConstName = RegExHelper.getAsIdentifier(sValue);
            if (jEnum.getEnumConstantOfName(sEnumConstName) != null) {
                // Avoid weird side effects
                throw new IllegalStateException("The enum constant '" + sEnumConstName + "' is already in use");
            }
            final JEnumConstant jEnumConst = jEnum.enumConstant(sEnumConstName);
            if (bDeprecated) {
                jEnumConst.annotate(Deprecated.class);
                jEnumConst.javadoc().addDeprecated().add("since " + sDeprecationRelease + " - this item should not be used to issue new identifiers!");
            }
            jEnumConst.arg(JExpr.lit(sScheme));
            {
                final JInvocation aNew = JExpr._new(CM.ref(PeppolDocumentTypeIdentifierParts.class)).arg(aDocIDParts.getRootNS()).arg(aDocIDParts.getLocalName()).arg(aDocIDParts.getCustomizationID()).arg(aDocIDParts.getVersion());
                jEnumConst.arg(aNew);
            }
            jEnumConst.arg(JExpr.lit(sProfileCode));
            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.arg(JExpr.lit(aRow.isIssuedByOpenpeppol()));
            jEnumConst.arg(JExpr.lit(StringParser.parseInt(aRow.getBisVersion(), -1)));
            jEnumConst.arg(aRow.getDomainCommunity() == null ? JExpr._null() : JExpr.lit(aRow.getDomainCommunity()));
            {
                final JInvocation aNew = JExpr._new(CM.ref(CommonsArrayList.class).narrowEmpty());
                for (final PCLProcessIDType aProcID : aRow.getProcessId()) aNew.arg(CIdentifier.getURIEncoded(aProcID.getScheme(), aProcID.getValue()));
                jEnumConst.arg(aNew);
            }
            jEnumConst.javadoc().add("<code>" + sValue + "</code><br>");
            jEnumConst.javadoc().addTag(JDocComment.TAG_SINCE).add("code list " + sInitialRelease);
            // Also create a shortcut for more readable names
            final String sShortcutName = CodeGenerationHelper.createShortcutDocumentTypeIDName(aDocIDParts);
            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("since " + sDeprecationRelease + " - this item should not be used to issue new identifiers!");
                }
                jEnumConst.javadoc().add("\nSame as {@link #" + sRealShortcutName + "}");
            }
        }
        // fields
        final JFieldVar fScheme = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sScheme");
        final JFieldVar fParts = jEnum.field(JMod.PRIVATE | JMod.FINAL, IPeppolDocumentTypeIdentifierParts.class, "m_aParts");
        final JFieldVar fID = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sID");
        final JFieldVar fProfileCode = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sProfileCode");
        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");
        final JFieldVar fIssuedByOpenPeppol = jEnum.field(JMod.PRIVATE | JMod.FINAL, boolean.class, "m_bIssuedByOpenPeppol");
        final JFieldVar fBISVersion = jEnum.field(JMod.PRIVATE | JMod.FINAL, int.class, "m_nBISVersion");
        final JFieldVar fDomainCommunity = jEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "m_sDomainCommunity");
        final JFieldVar fProcessIDs = jEnum.field(JMod.PRIVATE | JMod.FINAL, CM.ref(ICommonsList.class).narrow(IProcessIdentifier.class), "m_aProcessIDs");
        // 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 jParts = jCtor.param(JMod.FINAL, IPeppolDocumentTypeIdentifierParts.class, "aParts");
        jParts.annotate(Nonnull.class);
        final JVar jProfileCode = jCtor.param(JMod.FINAL, String.class, "sProfileCode");
        jProfileCode.annotate(Nonnull.class);
        jProfileCode.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);
        final JVar jIssuedByOpenPeppol = jCtor.param(JMod.FINAL, boolean.class, "bIssuedByOpenPeppol");
        final JVar jBISVersion = jCtor.param(JMod.FINAL, int.class, "nBISVersion");
        final JVar jDomainCommunity = jCtor.param(JMod.FINAL, String.class, "sDomainCommunity");
        jDomainCommunity.annotate(Nullable.class);
        final JVar jProcessIDs = jCtor.param(JMod.FINAL, CM.ref(ICommonsList.class).narrow(String.class), "aProcessIDs");
        jCtor.body().assign(fScheme, jScheme).assign(fParts, jParts).assign(fProfileCode, jProfileCode).assign(fID, fParts.invoke("getAsDocumentTypeIdentifierValue")).assign(fInitialRelease, jInitialRelease).assign(fState, jState).assign(fDeprecationRelease, jDeprecationRelease).assign(fRemovalDate, jRemovalDate).assign(fIssuedByOpenPeppol, jIssuedByOpenPeppol).assign(fBISVersion, jBISVersion).assign(fDomainCommunity, jDomainCommunity).assign(fProcessIDs, CM.ref(CommonsArrayList.class).narrowEmpty()._new().arg(jProcessIDs).arg(new JLambdaMethodRef(CM.ref(PeppolIdentifierFactory.class).staticRef("INSTANCE"), "parseProcessIdentifier")));
        // 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(fID);
        // public String getRootNS ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getRootNS");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fParts.invoke(m.name()));
        // public String getLocalName ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getLocalName");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fParts.invoke(m.name()));
        // public String getSubTypeIdentifier ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getSubTypeIdentifier");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fParts.invoke(m.name()));
        if (false) {
            // public String getTransactionID ()
            m = jEnum.method(JMod.PUBLIC, String.class, "getTransactionID");
            m.annotate(Nonnull.class);
            m.annotate(Nonempty.class);
            m.body()._return(fParts.invoke(m.name()));
            // public List<String> getExtensionIDs ()
            m = jEnum.method(JMod.PUBLIC, CM.ref(ICommonsList.class).narrow(String.class), "getExtensionIDs");
            m.annotate(Nonnull.class);
            m.annotate(ReturnsMutableCopy.class);
            m.body()._return(fParts.invoke(m.name()));
        }
        // public String getCustomizationID ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getCustomizationID");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fParts.invoke(m.name()));
        // public String getVersion ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getVersion");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fParts.invoke(m.name()));
        // public String getCommonName ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getCommonName");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fProfileCode);
        // public String getAsDocumentTypeIdentifierValue ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getAsDocumentTypeIdentifierValue");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.body()._return(fID);
        // public PeppolDocumentTypeIdentifier getAsDocumentTypeIdentifier ()
        m = jEnum.method(JMod.PUBLIC, PeppolDocumentTypeIdentifier.class, "getAsDocumentTypeIdentifier");
        m.annotate(Nonnull.class);
        m.body()._return(JExpr._new(CM.ref(PeppolDocumentTypeIdentifier.class)).arg(JExpr._this()));
        // public IPeppolDocumentTypeIdentifierParts getParts
        m = jEnum.method(JMod.PUBLIC, IPeppolDocumentTypeIdentifierParts.class, "getParts");
        m.annotate(Nonnull.class);
        m.body()._return(fParts);
        // 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);
        // public boolean isIssuedByOpenPeppol ()
        m = jEnum.method(JMod.PUBLIC, boolean.class, "isIssuedByOpenPeppol");
        m.body()._return(fIssuedByOpenPeppol);
        // public int getBISVersion ()
        m = jEnum.method(JMod.PUBLIC, int.class, "getBISVersion");
        m.annotate(CheckForSigned.class);
        m.body()._return(fBISVersion);
        // since 7.3 this method is nullable
        // public String getDomainCommunity ()
        m = jEnum.method(JMod.PUBLIC, String.class, "getDomainCommunity");
        m.annotate(Nullable.class);
        m.body()._return(fDomainCommunity);
        // public ICommonsList<IProcessIdentifier> getAllProcessIDs()
        m = jEnum.method(JMod.PUBLIC, CM.ref(ICommonsList.class).narrow(IProcessIdentifier.class), "getAllProcessIDs");
        m.annotate(Nonnull.class);
        m.annotate(Nonempty.class);
        m.annotate(ReturnsMutableCopy.class);
        m.body()._return(fProcessIDs.invoke("getClone"));
        // @Nullable
        // public static EPredefinedDocumentTypeIdentifier
        // getFromDocumentTypeIdentifierOrNull(@Nullable final
        // IDocumentTypeIdentifier aDocTypeID)
        m = jEnum.method(JMod.PUBLIC | JMod.STATIC, jEnum, "getFromDocumentTypeIdentifierOrNull");
        {
            m.annotate(Nullable.class);
            final JVar jValue = m.param(JMod.FINAL, IDocumentTypeIdentifier.class, "aDocTypeID");
            jValue.annotate(Nullable.class);
            final JBlock jIf = m.body()._if(jValue.neNull())._then();
            final JForEach jForEach = jIf.forEach(jEnum, "e", jEnum.staticInvoke("values"));
            jForEach.body()._if(jForEach.var().invoke("hasScheme").arg(jValue.invoke("getScheme")).cand(jForEach.var().invoke("hasValue").arg(jValue.invoke("getValue"))))._then()._return(jForEach.var());
            m.body()._return(JExpr._null());
        }
    } catch (final JCodeModelException ex) {
        LOGGER.error("Failed to create source", ex);
    }
}
Also used : ICommonsList(com.helger.commons.collection.impl.ICommonsList) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) JLambdaMethodRef(com.helger.jcodemodel.JLambdaMethodRef) LocalDate(java.time.LocalDate) IProcessIdentifier(com.helger.peppolid.IProcessIdentifier) IPeppolDocumentTypeIdentifierParts(com.helger.peppolid.peppol.doctype.IPeppolDocumentTypeIdentifierParts) PCLDocumentTypesType(com.helger.xsds.peppol.codelists2.PCLDocumentTypesType) JEnumConstant(com.helger.jcodemodel.JEnumConstant) Version(com.helger.commons.version.Version) JForEach(com.helger.jcodemodel.JForEach) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) JCodeModelException(com.helger.jcodemodel.JCodeModelException) JVar(com.helger.jcodemodel.JVar) JDefinedClass(com.helger.jcodemodel.JDefinedClass) QName(javax.xml.namespace.QName) PCLProcessIDType(com.helger.xsds.peppol.codelists2.PCLProcessIDType) JInvocation(com.helger.jcodemodel.JInvocation) EPeppolCodeListItemState(com.helger.peppolid.peppol.EPeppolCodeListItemState) PCLDocumentTypeType(com.helger.xsds.peppol.codelists2.PCLDocumentTypeType) JFieldVar(com.helger.jcodemodel.JFieldVar) PeppolDocumentTypeIdentifier(com.helger.peppolid.peppol.doctype.PeppolDocumentTypeIdentifier) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 3 with JEnumConstant

use of com.helger.jcodemodel.JEnumConstant in project JCELechat by guiguilechat.

the class ClassBridge method getStringEnum.

protected AbstractJType getStringEnum(String name, List<String> enums) {
    JDefinedClass ret = null;
    try {
        ret = structurePackage._enum(JMod.PUBLIC, name);
        JFieldVar toStringf = ret.field(JMod.PUBLIC | JMod.FINAL, cm.ref(String.class), "toString");
        JMethod constr = ret.constructor(0);
        JVar toStringp = constr.param(cm.ref(String.class), "toString");
        constr.body().assign(JExpr.refthis(toStringf), toStringp);
        JMethod toStringm = ret.method(JMod.PUBLIC, cm.ref(String.class), "toString");
        toStringm.body()._return(toStringf);
        toStringm.annotate(Override.class);
        for (String s : enums) {
            JEnumConstant enumcst = ret.enumConstant(sanitizeEnumName(s)).arg(JExpr.lit(s));
            enumcst.annotate(JsonProperty.class).param("value", s);
        }
        // logger.info("created enum " + name + " with values " + enums);
        return ret;
    } catch (JClassAlreadyExistsException e) {
        // logger.info("can't recreate enum " + name + " with values " + enums);
        for (JDefinedClass cl : structurePackage.classes()) {
            if (cl.name().equals(name)) {
                return cl;
            }
        }
        return null;
    }
}
Also used : JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JEnumConstant(com.helger.jcodemodel.JEnumConstant) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 4 with JEnumConstant

use of com.helger.jcodemodel.JEnumConstant 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)

Example 5 with JEnumConstant

use of com.helger.jcodemodel.JEnumConstant 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

JDefinedClass (com.helger.jcodemodel.JDefinedClass)6 JEnumConstant (com.helger.jcodemodel.JEnumConstant)6 JFieldVar (com.helger.jcodemodel.JFieldVar)6 JMethod (com.helger.jcodemodel.JMethod)6 JVar (com.helger.jcodemodel.JVar)6 JCodeModelException (com.helger.jcodemodel.JCodeModelException)4 EPeppolCodeListItemState (com.helger.peppolid.peppol.EPeppolCodeListItemState)4 QName (javax.xml.namespace.QName)4 CommonsHashSet (com.helger.commons.collection.impl.CommonsHashSet)3 Version (com.helger.commons.version.Version)3 LocalDate (java.time.LocalDate)3 JBlock (com.helger.jcodemodel.JBlock)2 JForEach (com.helger.jcodemodel.JForEach)2 IProcessIdentifier (com.helger.peppolid.IProcessIdentifier)2 Nullable (javax.annotation.Nullable)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 IHasID (com.helger.commons.id.IHasID)1 Column (com.helger.genericode.v10.Column)1