edu.ucsb.nceas.metacat.dataone.quota

## Class QuotaService

- java.lang.Object
- - edu.ucsb.nceas.metacat.dataone.quota.QuotaService

- Direct Known Subclasses:
    PortalQuotaService

      public abstract class QuotaService
      extends java.lang.Object

  A class represents an abstract quota service. Its child classes, such as PortalQuotqService and StorageQuotaService should implement the abstract method - enforce.
  Author:
    tao

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static BookKeeperClient` | `client`
    `static int` | `DEFAULT_QUOTA_ID`
    `protected static java.util.concurrent.ExecutorService` | `executor`
    `static java.lang.String` | `nodeId`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `QuotaService()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected int` | `checkQuota(boolean checkEnoughSpace, java.lang.String quotaSubject, java.lang.String requestor, java.lang.String quotaType, double quantity, java.lang.String instanceId)` Check if the quota has enough space for this request.
    `protected void` | `deleteUsage(int quotaId, java.lang.String instanceId, double quantity)` Delete a usage associated with the given quota id in the remote book keeper server.
    `abstract void` | `enforce(java.lang.String quotaSubject, org.dataone.service.types.v1.Subject requestor, java.lang.String instanceId, org.dataone.service.types.v2.SystemMetadata sysmeta, java.lang.String method)` The method will be implemented by the child classes to enforce the quota service
    `protected boolean` | `isLastUnarchivedInChain(java.lang.String pid, java.lang.String sid)` Checking if the given pid is last one in this series chain hasn't been archived
    `protected boolean` | `isLastUndeletedInChain(java.lang.String pid, java.lang.String sid)` Checking if the given pid is last one in this series chain hasn't been deleted
    `protected void` | `updateUsage(int quotaId, java.lang.String instanceId, double quantity)` Update a usage with the archived status associated with the given quota id in the remote book keeper server.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### nodeId

          public static java.lang.String nodeId

    - #### DEFAULT_QUOTA_ID

          public static int DEFAULT_QUOTA_ID

    - #### executor

          protected static java.util.concurrent.ExecutorService executor

    - #### client

          protected static BookKeeperClient client

  - ### Constructor Detail

    - #### QuotaService

          public QuotaService()

  - ### Method Detail

    - #### enforce

          public abstract void enforce(java.lang.String quotaSubject,
                                       org.dataone.service.types.v1.Subject requestor,
                                       java.lang.String instanceId,
                                       org.dataone.service.types.v2.SystemMetadata sysmeta,
                                       java.lang.String method)
                                throws org.dataone.service.exceptions.ServiceFailure,
                                       org.dataone.service.exceptions.InvalidRequest,
                                       org.dataone.service.exceptions.InsufficientResources,
                                       org.dataone.service.exceptions.NotImplemented,
                                       org.dataone.service.exceptions.NotFound,
                                       java.io.UnsupportedEncodingException

      The method will be implemented by the child classes to enforce the quota service
      Parameters:
        `quotaSubject` - the subject of the quota which will be used
        `requestor` - the subject of the user who requests the usage
        `instanceId` - the id of the object will be applied the quota
        `sysmeta` - the system metadata of the object which will use the quota
        `method` - the method name which will call the createUsage method (create or update)
      Throws:
        `org.dataone.service.exceptions.ServiceFailure`
        `org.dataone.service.exceptions.InvalidRequest`
        `org.dataone.service.exceptions.InsufficientResources`
        `org.dataone.service.exceptions.NotImplemented`
        `org.dataone.service.exceptions.NotFound`
        `java.io.UnsupportedEncodingException`

    - #### isLastUnarchivedInChain

          protected boolean isLastUnarchivedInChain(java.lang.String pid,
                                                    java.lang.String sid)
                                             throws org.dataone.service.exceptions.InvalidRequest,
                                                    org.dataone.service.exceptions.ServiceFailure

      Checking if the given pid is last one in this series chain hasn't been archived
      Parameters:
        `pid` - the pid of the object will be checked
        `sid` - the id of the series chain will be checked
      Returns:
        true if the pid is the last one hasn't been archived; otherwise fals.
      Throws:
        `java.sql.SQLException`
        `org.dataone.service.exceptions.InvalidRequest`
        `org.dataone.service.exceptions.ServiceFailure`

    - #### isLastUndeletedInChain

          protected boolean isLastUndeletedInChain(java.lang.String pid,
                                                   java.lang.String sid)
                                            throws org.dataone.service.exceptions.InvalidRequest,
                                                   org.dataone.service.exceptions.ServiceFailure

      Checking if the given pid is last one in this series chain hasn't been deleted
      Parameters:
        `pid` - the pid of the object will be checked
        `sid` - the id of the series chain will be checked
      Returns:
        true if the pid is the last one hasn't been deleted; otherwise fals.
      Throws:
        `java.sql.SQLException`
        `org.dataone.service.exceptions.InvalidRequest`
        `org.dataone.service.exceptions.ServiceFailure`

    - #### checkQuota

          protected int checkQuota(boolean checkEnoughSpace,
                                   java.lang.String quotaSubject,
                                   java.lang.String requestor,
                                   java.lang.String quotaType,
                                   double quantity,
                                   java.lang.String instanceId)
                            throws org.dataone.service.exceptions.InvalidRequest,
                                   org.dataone.service.exceptions.ServiceFailure,
                                   org.dataone.service.exceptions.InsufficientResources,
                                   org.dataone.service.exceptions.NotFound,
                                   java.io.UnsupportedEncodingException

      Check if the quota has enough space for this request. If there is not enough space, an exception will be thrown
      Parameters:
        `checkEnoughSpace` - indicator if we need to check if the found quota has enough space for this usage
        `quotaSubject` - the subject of the quota which will be used
        `requestor` - the subject of the user who requests the usage
        `quotaType` - the type of quota
        `quantity` - the amount of the usage for the request
      Returns:
        the quota id which will be used. -1 will be returned if the quota service is disabled
      Throws:
        `org.dataone.service.exceptions.InvalidRequest`
        `java.io.IOException`
        `org.dataone.service.exceptions.ServiceFailure`
        `org.dataone.service.exceptions.NotFound`
        `org.apache.http.client.ClientProtocolException`
        `org.dataone.service.exceptions.InsufficientResources`
        `java.io.UnsupportedEncodingException`

    - #### updateUsage

          protected void updateUsage(int quotaId,
                                     java.lang.String instanceId,
                                     double quantity)

      Update a usage with the archived status associated with the given quota id in the remote book keeper server. Locally we will add a new record with the archived status in the table. It will be run by another thread. Metacat executes it without checking if the service is enabled. So the caller should check it.
      Parameters:
        `quotaId` - the quota id which the usage will be associated with
        `instanceId` - the id of the usage instance (pid for the storage type, and sid for the portal type)
        `quantity` - the amount of the usage

    - #### deleteUsage

          protected void deleteUsage(int quotaId,
                                     java.lang.String instanceId,
                                     double quantity)

      Delete a usage associated with the given quota id in the remote book keeper server. However, locally we will add a new record with the deleted status in the table. It will be run by another thread. Metacat executes it without checking if the service is enabled. So the caller should check it.
      Parameters:
        `quotaId` - the quota id which the usage will be associated with
        `instanceId` - the id of the usage instance (pid for the storage type, and sid for the portal type)
        `quantity` - the amount of the usage

Copyright © 2023 Regents of the University of California. All Rights Reserved.