All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.davidmoten.microsoft.dynamics.Dynamics Maven / Gradle / Ivy

The newest version!
package com.github.davidmoten.microsoft.dynamics;

import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.function.Supplier;

import com.github.davidmoten.guavamini.Preconditions;
import com.github.davidmoten.microsoft.client.builder.Creator;
import com.github.davidmoten.microsoft.client.builder.MicrosoftClientBuilder;
import com.github.davidmoten.microsoft.client.builder.MicrosoftClientBuilder.UsernamePassword;
import com.github.davidmoten.odata.client.ClientException;
import com.github.davidmoten.odata.client.Context;
import com.github.davidmoten.odata.client.HasContext;

import com.github.davidmoten.odata.client.PathStyle;
import microsoft.dynamics.crm.schema.SchemaInfo;

public final class Dynamics {

    private Dynamics() {
        // prevent instantiation
    }

    public static  Builder service(Class serviceClass) {
        return new Builder(serviceClass);
    }

    public static final class Builder {

        private final Class serviceCls;
        private Optional baseUrl = Optional.empty();
        private PathStyle pathStyle = PathStyle.IDENTIFIERS_IN_ROUND_BRACKETS;

        Builder(Class serviceClass) {
            Preconditions.checkNotNull(serviceClass);
            this.serviceCls = serviceClass;
        }

        public Builder pathStyle(PathStyle pathStyle) {
            this.pathStyle = pathStyle;
            return this;
        }

        /**
         * Expected URL is like https://SOLUTION.crm4.dynamics.com.
         * @param baseUrl
         * @return builder
         */
        public Builder3 baseUrl(String baseUrl) {
            Preconditions.checkNotNull(baseUrl);
            this.baseUrl = Optional.of(baseUrl);
            return new Builder3(this);
        }

    }

    public static final class Builder3 {

        private final Builder b;

        public Builder3(Builder b) {
            this.b = b;
        }

        public MicrosoftClientBuilder.BuilderWithBasicAuthentication basicAuthentication(
                Supplier usernamePassword) {
            return createBuilder().basicAuthentication(usernamePassword);
        }

        public MicrosoftClientBuilder.BuilderWithBasicAuthentication basicAuthentication(
                String username, String password) {
            return basicAuthentication(() -> UsernamePassword.create(username, password));
        }

        public com.github.davidmoten.microsoft.client.builder.MicrosoftClientBuilder.Builder tenantName(String tenantName) {
            return createBuilder().tenantName(tenantName);
        }

        private MicrosoftClientBuilder createBuilder() {
            Creator creator = context -> {
                try {
                    return b.serviceCls.getConstructor(Context.class).newInstance(context);
                } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                    throw new ClientException(e);
                }
            };
            return MicrosoftClientBuilder //
                    .baseUrl(b.baseUrl.get()) //
                    .creator(creator) //
                    .addSchema(SchemaInfo.INSTANCE) //
                    .pathStyle(b.pathStyle) //
                    .build();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy