github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/status/status.go (about)

     1  // Copyright 2017 European Digital Reading Lab. All rights reserved.
     2  // Licensed to the Readium Foundation under one or more contributor license agreements.
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     5  
     6  package status
     7  
     8  import (
     9  	"strconv"
    10  	"strings"
    11  )
    12  
    13  // List of status values as strings
    14  const (
    15  	STATUS_READY     = "ready"
    16  	STATUS_ACTIVE    = "active"
    17  	STATUS_REVOKED   = "revoked"
    18  	STATUS_RETURNED  = "returned"
    19  	STATUS_CANCELLED = "cancelled"
    20  	STATUS_EXPIRED   = "expired"
    21  	EVENT_RENEWED    = "renewed"
    22  )
    23  
    24  // List of status values as int
    25  const (
    26  	STATUS_READY_INT     = 0
    27  	STATUS_ACTIVE_INT    = 1
    28  	STATUS_REVOKED_INT   = 2
    29  	STATUS_RETURNED_INT  = 3
    30  	STATUS_CANCELLED_INT = 4
    31  	STATUS_EXPIRED_INT   = 5
    32  	EVENT_RENEWED_INT    = 6
    33  )
    34  
    35  // StatusValues defines status values logged in license status documents
    36  var StatusValues = map[int]string{
    37  	STATUS_READY_INT:     STATUS_READY,
    38  	STATUS_ACTIVE_INT:    STATUS_ACTIVE,
    39  	STATUS_REVOKED_INT:   STATUS_REVOKED,
    40  	STATUS_RETURNED_INT:  STATUS_RETURNED,
    41  	STATUS_CANCELLED_INT: STATUS_CANCELLED,
    42  	STATUS_EXPIRED_INT:   STATUS_EXPIRED,
    43  }
    44  
    45  // EventTypes defines additional event types.
    46  // It reuses all status values and adds one for renewed licenses.
    47  var EventTypes = map[int]string{
    48  	STATUS_ACTIVE_INT:    "register",
    49  	STATUS_REVOKED_INT:   "revoke",
    50  	STATUS_RETURNED_INT:  "return",
    51  	STATUS_CANCELLED_INT: "cancel",
    52  	STATUS_EXPIRED_INT:   "expire",
    53  	EVENT_RENEWED_INT:    "renew",
    54  }
    55  
    56  // GetStatus translates status number to status string
    57  func GetStatus(statusDB int64, status *string) {
    58  	resultStr := reverse(strconv.FormatInt(statusDB, 2))
    59  
    60  	if count := strings.Count(resultStr, "1"); count == 1 {
    61  		index := strings.Index(resultStr, "1")
    62  
    63  		if len(StatusValues) >= index+1 {
    64  			*status = StatusValues[index]
    65  		}
    66  	}
    67  }
    68  
    69  // SetStatus translates status string to status number
    70  func SetStatus(status string) (int64, error) {
    71  	reg := make([]string, len(StatusValues))
    72  
    73  	for key := range StatusValues {
    74  		if StatusValues[key] == status {
    75  			reg[key] = "1"
    76  		} else {
    77  			reg[key] = "0"
    78  		}
    79  	}
    80  
    81  	resultStr := reverse(strings.Join(reg[:], ""))
    82  
    83  	statusDB, err := strconv.ParseInt(resultStr, 2, 64)
    84  	return statusDB, err
    85  }
    86  
    87  func reverse(s string) string {
    88  	r := []rune(s)
    89  	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
    90  		r[i], r[j] = r[j], r[i]
    91  	}
    92  	return string(r)
    93  }