Class OdooUtils

java.lang.Object
io.openems.backend.metadata.odoo.odoo.OdooUtils

public class OdooUtils extends Object
  • Method Details

    • sendJsonrpcRequest

      public static OdooUtils.SuccessResponseAndHeaders sendJsonrpcRequest(String url, com.google.gson.JsonObject request) throws OpenemsError.OpenemsNamedException
      Sends a JSON-RPC Request to an Odoo server - without Cookie header.
      Parameters:
      url - the URL
      request - the JSON-RPC Request as JsonObject
      Returns:
      the JsonObject response and HTTP connection headers on success
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • sendJsonrpcRequest

      public static OdooUtils.SuccessResponseAndHeaders sendJsonrpcRequest(String url, String cookie, com.google.gson.JsonObject request) throws OpenemsError.OpenemsNamedException
      Sends a JSON-RPC Request to an Odoo server - without Cookie header.
      Parameters:
      url - the URL
      cookie - the Cookie
      request - the JSON-RPC Request as JsonObject
      Returns:
      the JsonObject response and HTTP connection headers on success
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • sendJsonrpcRequest

      public static OdooUtils.SuccessResponseAndHeaders sendJsonrpcRequest(String url, String cookie, com.google.gson.JsonObject request, int timeout) throws OpenemsError.OpenemsNamedException
      Sends a JSON-RPC Request to an Odoo server.
      Parameters:
      url - the URL
      cookie - a Cookie string
      request - the JSON-RPC Request as JsonObject
      timeout - readtimeout in milliseconds
      Returns:
      the JsonObject response and HTTP connection headers on success
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • sendAdminJsonrpcRequest

      protected static OdooUtils.SuccessResponseAndHeaders sendAdminJsonrpcRequest(Credentials credentials, String url, com.google.gson.JsonObject request, int timeout) throws OpenemsError.OpenemsNamedException
      Throws:
      OpenemsError.OpenemsNamedException
    • sendAdminJsonrpcRequest

      protected static OdooUtils.SuccessResponseAndHeaders sendAdminJsonrpcRequest(Credentials credentials, String url, com.google.gson.JsonObject request) throws OpenemsError.OpenemsNamedException
      Sends a request with admin privileges.
      Parameters:
      credentials - the Odoo credentials
      url - to send the request
      request - to send
      Returns:
      SuccessResponseAndHeaders response
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • sendAdminJsonrpcRequestAsync

      protected static Future<OdooUtils.SuccessResponseAndHeaders> sendAdminJsonrpcRequestAsync(Credentials credentials, String url, com.google.gson.JsonObject request) throws OpenemsError.OpenemsNamedException
      Sends a request with admin privileges in async.
      Parameters:
      credentials - the Odoo credentials
      url - to send the request
      request - to send
      Returns:
      SuccessResponseAndHeaders response as Future
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • login

      protected static String login(Credentials credentials, String username, String password) throws OpenemsError.OpenemsNamedException
      Authenticates a user using Username and Password.
      Parameters:
      credentials - used to get Odoo url
      username - the Username
      password - the Password
      Returns:
      the session_id
      Throws:
      OpenemsError.OpenemsNamedException - on login error
    • getAsStringArray

      protected static String[] getAsStringArray(Field... fields)
    • getAsObjectArray

      protected static Object[] getAsObjectArray(Domain... domains)
    • search

      protected static Integer[] search(Credentials credentials, String model, Domain... domains) throws OpenemsException
      Executes a search on Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - Odoo model to query (e.g. 'res.partner')
      domains - Odoo domain filters
      Returns:
      Odoo object ids
      Throws:
      OpenemsException - on error
    • readOne

      protected static Map<String,Object> readOne(Credentials credentials, String model, int id, Field... fields) throws OpenemsException
      Reads a record from Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - Odoo model to query (e.g. 'res.partner')
      id - id of model to read
      fields - fields that should be read
      Returns:
      the record as a Map
      Throws:
      OpenemsException - on error
    • readMany

      protected static Map<String,Object>[] readMany(Credentials credentials, String model, Integer[] ids, Field... fields) throws OpenemsException
      Reads multiple records from Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - Odoo model to query (e.g. 'res.partner')
      ids - ids of model to read
      fields - fields that should be read
      Returns:
      the records as a Map array
      Throws:
      OpenemsException - on error
    • searchRead

      protected static Map<String,Object>[] searchRead(Credentials credentials, String model, Field[] fields, Domain... domains) throws OpenemsException
      Search-Reads multiple records from Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - Odoo model to query (e.g. 'res.partner')
      fields - fields that should be read
      domains - filter domains
      Returns:
      the records as a Map array
      Throws:
      OpenemsException - on error
    • getObjectReference

      protected static int getObjectReference(Credentials credentials, String module, String name) throws OpenemsException
      Executes a get object reference from Odoo.
      Parameters:
      credentials - the Odoo credentials
      module - the Odoo module
      name - the external identifier
      Returns:
      internal id of external identifier
      Throws:
      OpenemsException - on error
    • create

      protected static int create(Credentials credentials, String model, FieldValue<?>... fieldValues) throws OpenemsException
      Create a record in Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - the Oddo model
      fieldValues - fields and values that should be written
      Returns:
      Odoo id of created record
      Throws:
      OpenemsException - on error
    • create

      protected static int create(Credentials credentials, String model, Map<String,Object> fieldValues) throws OpenemsException
      Create a record in Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - the Oddo model
      fieldValues - fields and values that should be written
      Returns:
      Odoo id of created record
      Throws:
      OpenemsException - on error
    • write

      public static void write(Credentials credentials, String model, Integer[] ids, FieldValue<?>... fieldValues) throws OpenemsException
      Update a record in Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - the Odoo model
      ids - ids of model to update
      fieldValues - fields and values that should be written
      Throws:
      OpenemsException - on error
    • write

      protected static void write(Credentials credentials, String model, Integer[] ids, Map<String,Object> fieldValues) throws OpenemsException
      Update a record in Odoo.
      Parameters:
      credentials - the Odoo credentials
      model - the Odoo model
      ids - ids of model to update
      fieldValues - fields and values that should be written
      Throws:
      OpenemsException - on error
    • getAsOptional

      protected static <T> Optional<T> getAsOptional(Object object, Class<T> type)
      Return the Object type-safe as a Optional of type T; or otherwise as an empty Optional.
      Type Parameters:
      T - expected type
      Parameters:
      object - the value as object
      type - the expected type of object
      Returns:
      the value as Optional String
    • getOdooReferenceId

      protected static Optional<Integer> getOdooReferenceId(Object object)
      Return the odoo reference id as a Integer, otherwise empty Optional.
      Parameters:
      object - the odoo reference to extract
      Returns:
      the odoo reference id or empty Optional
    • getOdooReport

      protected static byte[] getOdooReport(Credentials credentials, String report, int id) throws OpenemsError.OpenemsNamedException
      Returns a Odoo report as a byte array. Search for the given template id in combination with the concrete report id.
      Parameters:
      credentials - the Odoo credentialss
      report - the Odoo template id
      id - the Odoo report id
      Returns:
      the Odoo report as a byte array
      Throws:
      OpenemsError.OpenemsNamedException - on error
    • getAs

      public static <T> T getAs(Field field, Map<String,?> values, Class<T> type)
      Return Field value in values and cast it to type.
      Type Parameters:
      T - expected type of value
      Parameters:
      field - to search for
      values - map with values to search in
      type - to cast into
      Returns:
      value found in map casted to type or null on error
    • getAsOrElse

      public static <T> T getAsOrElse(Field field, Map<String,?> values, Class<T> type, T alternate)
      Return Field value in values and cast it to type.
      Type Parameters:
      T - expected type of value
      Parameters:
      field - to search for
      values - map with values to search in
      type - to cast into
      alternate - value to return
      Returns:
      value found in map casted to type or alternate on error
    • getAsEnum

      public static <T extends Enum<T>> T getAsEnum(Field field, Map<String,?> values, Class<T> enumType, T alternate)
      Return Field value in values and cast it to value of type 'enumType'.
      Type Parameters:
      T - expected type of value
      Parameters:
      field - to search for
      values - map with values to search in
      enumType - to cast into
      alternate - value to return
      Returns:
      value found in map casted to type or alternate on error