Search in sources :

Example 6 with PeppolProcessIdentifier

use of com.helger.peppolid.peppol.process.PeppolProcessIdentifier 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

PeppolProcessIdentifier (com.helger.peppolid.peppol.process.PeppolProcessIdentifier)6 PeppolDocumentTypeIdentifier (com.helger.peppolid.peppol.doctype.PeppolDocumentTypeIdentifier)5 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)5 EndpointType (com.helger.xsds.peppol.smp1.EndpointType)5 ProcessListType (com.helger.xsds.peppol.smp1.ProcessListType)5 ProcessType (com.helger.xsds.peppol.smp1.ProcessType)5 ServiceEndpointList (com.helger.xsds.peppol.smp1.ServiceEndpointList)5 ServiceInformationType (com.helger.xsds.peppol.smp1.ServiceInformationType)5 StopWatch (com.helger.commons.timing.StopWatch)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 MockHttpServletRequest (com.helger.servlet.mock.MockHttpServletRequest)3 WebScoped (com.helger.web.scope.mgr.WebScoped)3 ServiceMetadataType (com.helger.xsds.peppol.smp1.ServiceMetadataType)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PeppolParticipantIdentifier (com.helger.peppolid.peppol.participant.PeppolParticipantIdentifier)2 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)2 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)2 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)2 MockSMPClient (com.helger.phoss.smp.mock.MockSMPClient)2