This is a new content application service built on the content repository services. It provides a counter with methods for creating, listing, incrementing, decrementing, and resetting integer counter instances and their values. The counter can be used to persist how many times a particular event has occurred. For example, to generate a unique reference number for a document, the counter can generate an integer for use as the basis of the reference number.
The service uses a three-tier approach. Java provides the underlying methods in Tier 1; the JavaScript API is used for Tier 2 to provide higher level methods allowing the counter to be called by using repository actions and is also exposed by using Tier 3 as the new RESTful counter API.
Building Tier 1 – Java Service Layer
public interface CounterService // Low level services for creating and updating counters. { public enum CounterOperation { INC, DEC }; // Create a counter and set the initial value. // Returns the counter id. String createCounter(int initialValue); // Get the value of a counter. int getCounterValue(counterId); // Update the counter value by a given step. The counter operation // is provided. int updateCounterValue(String counterId, CounterOperation operation, int step); // Delete the counter. int deleteCounter(); }
Building Tier 2 – Javascript API
// Create counter. Initial value and default step are optional parameters. // Returns the created counter object function createCounter(initialValue=1, defaultStep=1); // Get counter. // Returns the counter object function getCounter(counterId); Counter { var id; var defaultStep; var value; function increment(value=0); function decrement(value=0); function delete(); }
Building Tier 3 – RESTful API
-
Call the counter collection resource using an HTTP GET method against the
/alfresco/counters URI.
For example:
Method: GET /alfresco/counters Description: Returns a list of counters Example output: { "counters": { "counter0": { "id" : "counter0", "value" : 12, "url" : "/counters/counter0" }, "counter1": { "id" : "counter1", "value" : 3, "url" : "/counters/counter1" } } }
This provides a platform and language independent interface to get a list of counters. The response is formatted using JSON and the calling application can parse the JSON response to get the list of counters as appropriate.
-
Call the counter collection resource using an HTTP POST method to create a new
counter instance.
Method: POST /alfresco/counters Description: Creates a new counter Example input: { "id" : "counter2", "initialValue" : 0, "defaultStep" : 1 } Example output: { "id" : "counter2", "value" : 0, "url" : "/counters/counter2" }
The body of the post method uses a JSON formatted payload that includes the ID for the new counter, the initialValue, and also the default value (defaultStep) used to increment or decrement the counter.
-
Call the URI using an HTTP GET to return the current value as a JSON response for the
counter-ID identified at the end of the URI request.
Method: GET /alfresco/counters/{counter-id} Description: Gets the current value for a counter Example output: { "id" : "counter2", "value" : 0, "url" : "/counters/counter2" }
The counter resource reads and updates the values for a particular counter. As with the counter collection, the behavior is based on the HTTP request method used to call the resource.
-
Call the counter resource with POST to increment or decrement the counter according
to the step value passed in by using the request BODY. If no BODY is provided, the default
step value is used.
Method - POST /alfresco/counters/{counter-id} Description: Increments or decrements a counter’s value according to the passed in step value. Example input: { "step" : 1 } Example output where existing value is 11: { "id" : "counter0", "value" : 12, "url" : "/counters/counter0" }
This returns a JSON response including the new counter value.
-
Call the counter with an HTTP DELETE method to delete the counter.
Method - DELETE /alfresco/counters/{counter-id} Description: Deletes the counter
Note: In this case, there is no response in the HTTP body. A 204 status code would be returned in the HTTP header to indicate the delete had been successful.