Class CpoBaseAdapter<D>

java.lang.Object
org.synchronoss.cpo.core.cache.CpoAdapterCache
org.synchronoss.cpo.core.CpoBaseAdapter<D>
Type Parameters:
D - The type of the Datasource
All Implemented Interfaces:
Serializable, CpoAdapter
Direct Known Subclasses:
CassandraCpoAdapter, JdbcCpoAdapter

public abstract class CpoBaseAdapter<D> extends CpoAdapterCache implements CpoAdapter
The CpoBaseAdapter has common functionality needed by all Adapter implementations
See Also:
  • Constructor Details

    • CpoBaseAdapter

      public CpoBaseAdapter(String dataSourceName, int fetchSize, int batchSize)
      Constructs the adapter with the given data source name and default fetch/batch sizes.
      Parameters:
      dataSourceName - the name of the datasource this adapter is bound to
      fetchSize - the default fetch size to use when reading from the datasource
      batchSize - the default batch size to use when writing to the datasource
  • Method Details

    • getDataSourceName

      public String getDataSourceName()
      Description copied from interface: CpoAdapter
      Get the name of the datasource
      Specified by:
      getDataSourceName in interface CpoAdapter
      Returns:
      The name of the datasource
    • getFetchSize

      public int getFetchSize()
      Description copied from interface: CpoAdapter
      Get the fetch size for the datasource
      Specified by:
      getFetchSize in interface CpoAdapter
      Returns:
      The fetchsize
    • setFetchSize

      public void setFetchSize(int fetchSize)
      Description copied from interface: CpoAdapter
      set the fetch size for the datasource
      Specified by:
      setFetchSize in interface CpoAdapter
      Parameters:
      fetchSize - The fetchsize to set for retrieving data
    • getBatchSize

      public int getBatchSize()
      Description copied from interface: CpoAdapter
      Get the batch size for updating the datasource
      Specified by:
      getBatchSize in interface CpoAdapter
      Returns:
      The batchsize
    • setBatchSize

      public void setBatchSize(int batchSize)
      Description copied from interface: CpoAdapter
      set the batch size for updating the datasource
      Specified by:
      setBatchSize in interface CpoAdapter
      Parameters:
      batchSize - The batchsize for updating the datasource
    • insertBean

      public <T> long insertBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Creates the bean in the datasource using the query's CREATE function group. The assumption is that the bean does not exist in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("SomeName");
       cpo.insertBean(CpoQuery.group("createBean"), bean);
       
      Specified by:
      insertBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans created in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • insertBeans

      public <T> long insertBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Creates the beans in the datasource using the query's CREATE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are created.
      
       List<SomeBean> beans = List.of(bean1, bean2);
       long created = cpo.insertBeans(CpoQuery.group("createBean"), beans);
       
      Specified by:
      insertBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to create
      Returns:
      The number of beans created in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • deleteBean

      public <T> long deleteBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Removes the bean from the datasource using the query's DELETE function group. The assumption is that the bean exists in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       cpo.deleteBean(CpoQuery.group("deleteBean"), bean);
       
      Specified by:
      deleteBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans deleted from the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • deleteBeans

      public <T> long deleteBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Removes the beans from the datasource using the query's DELETE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are deleted.
      
       long deleted = cpo.deleteBeans(CpoQuery.group("deleteBean"), beans);
       
      Specified by:
      deleteBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to delete
      Returns:
      The number of beans deleted from the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • updateBean

      public <T> long updateBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Updates the bean in the datasource using the query's UPDATE function group. The assumption is that the bean exists in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("NewName");
       cpo.updateBean(CpoQuery.group("updateBean"), bean);
       
      Specified by:
      updateBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans updated in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • updateBeans

      public <T> long updateBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Updates the beans in the datasource using the query's UPDATE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are updated.
      
       long updated = cpo.updateBeans(CpoQuery.group("updateBean"), beans);
       
      Specified by:
      updateBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to update
      Returns:
      The number of beans updated in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • upsertBean

      public <T> long upsertBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Inserts or updates the bean using the query's UPSERT function group: the EXIST function decides whether the CREATE or UPDATE function is executed.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("SomeName");
       cpo.upsertBean(CpoQuery.group("upsertBean"), bean); // inserts or updates as needed
       
      Specified by:
      upsertBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans upserted in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource, or if the EXIST function matches more than one record
    • upsertBeans

      public <T> long upsertBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Inserts or updates the beans using the query's UPSERT function group: for each bean the EXIST function decides whether the CREATE or UPDATE function is executed.
      
       long upserted = cpo.upsertBeans(CpoQuery.group("upsertBean"), beans);
       
      Specified by:
      upsertBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to upsert
      Returns:
      The number of beans upserted in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource, or if the EXIST function matches more than one record
    • executeBean

      public <T, C> T executeBean(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Executes the EXECUTE function group identified by the query — typically a stored procedure — using the criteria bean to populate the IN arguments and the result bean type for the OUT arguments.
      
       SomeResult result =
           cpo.executeBean(CpoQuery.group("calculateTotals"), criteria, new SomeResult());
       
      Specified by:
      executeBean in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group to execute
      criteria - A bean defined within the metadata of the datasource used to populate the IN arguments
      result - A bean defined within the metadata of the datasource that defines the bean type populated with the OUT arguments
      Returns:
      A result bean populated with the OUT arguments
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • retrieveBean

      public <T> T retrieveBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves a single bean from the datasource using the query's RETRIEVE function group, with the given bean supplying the search criteria and receiving the result. If the function returns more than one row, an exception is thrown.
      
       SomeBean criteria = new SomeBean();
       criteria.setId(1);
       SomeBean bean = cpo.retrieveBean(CpoQuery.group("retrieveBean"), criteria);
       
      Specified by:
      retrieveBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource whose attributes supply the search criteria
      Returns:
      A populated bean of the same type as the bean passed in, or null if no beans match
      Throws:
      CpoException - Thrown if there are errors accessing the datasource or more than one row is returned
    • retrieveBean

      public <T, C> T retrieveBean(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves the first bean produced by the query's RETRIEVE function group, using separate criteria and result beans.
      
       SomeResult result =
           cpo.retrieveBean(CpoQuery.group("retrieveResult"), criteria, new SomeResult());
       
      Specified by:
      retrieveBean in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group and clauses to apply
      criteria - A bean defined within the metadata of the datasource that supplies the retrieval parameters
      result - A bean defined within the metadata of the datasource that specifies the returned bean type
      Returns:
      A bean of the same type as the result parameter, or null if no beans match
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • retrieveBeans

      public <T, C> Stream<T> retrieveBeans(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves beans from the datasource using the query's LIST function group, with separate criteria and result beans.
      
       CpoWhere where = cpo.newWhere(Logical.NONE, "dept", Comparison.EQ, "sales");
       CpoOrderBy orderBy = cpo.newOrderBy("name", true);
       try (Stream<SomeResult> beans =
           cpo.retrieveBeans(
               CpoQuery.group("listResults").where(where).orderBy(orderBy),
               criteria,
               new SomeResult())) {
         beans.forEach(...);
       }
       
      Specified by:
      retrieveBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group and clauses to apply
      criteria - A bean defined within the metadata of the datasource that supplies the retrieval parameters
      result - A bean defined within the metadata of the datasource that specifies the returned bean type
      Returns:
      A stream of beans that meet the criteria; empty if none match. The stream is backed by open datastore resources (statement, result set, and connection) that are released only when the stream is closed; terminal operations do not close it. Always close the returned stream, preferably with try-with-resources.
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • newOrderBy

      public CpoOrderBy newOrderBy(String attribute, boolean ascending) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy for the attribute and direction.
      
       CpoOrderBy byNameAscending = cpo.newOrderBy("name", true);
       
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String marker, String attribute, boolean ascending) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy bound to a marker within the expression.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      marker - The marker in the expression that this order-by replaces
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String attribute, boolean ascending, String function) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy applying a datasource function to the attribute.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      function - A datasource function to apply to the attribute
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String marker, String attribute, boolean ascending, String function) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy bound to a marker, applying a datasource function to the attribute.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      marker - The marker in the expression that this order-by replaces
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      function - A datasource function to apply to the attribute
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newWhere

      public CpoWhere newWhere() throws CpoException
      Description copied from interface: CpoAdapter
      Creates an empty CpoWhere.
      
       CpoWhere where = cpo.newWhere();
       
      Specified by:
      newWhere in interface CpoAdapter
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • newWhere

      public <T> CpoWhere newWhere(Logical logical, String attr, Comparison comp, T value) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoWhere comparing the attribute to the value.
      
       CpoWhere where = cpo.newWhere(Logical.NONE, "id", Comparison.EQ, 42);
       
      Specified by:
      newWhere in interface CpoAdapter
      Type Parameters:
      T - The type of the value
      Parameters:
      logical - How this where combines with the preceding where (AND, OR, NONE)
      attr - The metadata attribute name to constrain
      comp - The comparison operator
      value - The value to compare against
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • newWhere

      public <T> CpoWhere newWhere(Logical logical, String attr, Comparison comp, T value, boolean not) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoWhere comparing the attribute to the value, optionally negated.
      Specified by:
      newWhere in interface CpoAdapter
      Type Parameters:
      T - The type of the value
      Parameters:
      logical - How this where combines with the preceding where (AND, OR, NONE)
      attr - The metadata attribute name to constrain
      comp - The comparison operator
      value - The value to compare against
      not - true to negate the comparison
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • existsBean

      public abstract <T> long existsBean(CpoQuery query, T bean) throws CpoException
      The CpoAdapter will check to see if this object exists in the datasource.
      Example:
       
       class SomeObject so = new SomeObject();
       long count = 0;
       class CpoAdapter cpo = null;
      
        try {
          cpo = new CpoAdapter(new JdbcDataSourceInfo(driver, url, user, password,1,1,false));
        } catch (CpoException ce) {
          // Handle the error
          cpo = null;
        }
      
        if (cpo!=null) {
          so.setId(1);
          so.setName("SomeName");
          try{
            CpoWhere where = cpo.newCpoWhere(Logical.NONE, id, Comparison.EQ);
            count = cpo.existsObject("SomeExistCheck",so, where);
            if (count>0) {
              // object exists
            } else {
              // object does not exist
            }
          } catch (CpoException ce) {
            // Handle the error
          }
        }
       
       
      Specified by:
      existsBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - This is an object that has been defined within the metadata of the datasource. If the class is not defined an exception will be thrown. This object will be searched for inside the datasource.
      Returns:
      The number of objects that exist in the datasource that match the specified object
      Throws:
      CpoException - Thrown if there are errors accessing the datasource