View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.rest;
23  
24  import org.json.simple.JSONArray;
25  import org.json.simple.JSONObject;
26  
27  /**
28   * This class is used to store the results of a bulk operation on an endpoint and to easily return those results.
29   */
30  public class BulkOperationResult {
31    public static final String OK_KEY = "ok";
32    public static final String ACCEPTED_KEY = "accepted";
33    public static final String BAD_REQUEST_KEY = "badRequest";
34    public static final String UNAUTHORIZED_KEY = "unauthorized";
35    public static final String NOT_FOUND_KEY = "notFound";
36    public static final String ERROR_KEY = "error";
37  
38    private JSONArray ok = new JSONArray();
39    private JSONArray accepted = new JSONArray();
40    private JSONArray badRequest = new JSONArray();
41    private JSONArray unauthorized = new JSONArray();
42    private JSONArray notFound = new JSONArray();
43    private JSONArray serverError = new JSONArray();
44  
45    @SuppressWarnings("unchecked")
46    public void addOk(String id) {
47      ok.add(id);
48    }
49  
50    @SuppressWarnings("unchecked")
51    public void addAccepted(String id) {
52      accepted.add(id);
53    }
54  
55    @SuppressWarnings("unchecked")
56    public void addBadRequest(String id) {
57      badRequest.add(id);
58    }
59  
60    @SuppressWarnings("unchecked")
61    public void addNotFound(String id) {
62      notFound.add(id);
63    }
64  
65    @SuppressWarnings("unchecked")
66    public void addServerError(String id) {
67      serverError.add(id);
68    }
69  
70    public void addOk(Long id) {
71      addOk(Long.toString(id));
72    }
73  
74    public void addBadRequest(Long id) {
75      addBadRequest(Long.toString(id));
76    }
77  
78    public void addUnauthorized(String id) {
79      unauthorized.add(id);
80    }
81  
82    public void addNotFound(Long id) {
83      addNotFound(Long.toString(id));
84    }
85  
86    public void addServerError(Long id) {
87      addServerError(Long.toString(id));
88    }
89  
90    public JSONArray getOks() {
91      return ok;
92    }
93  
94    public JSONArray getAccepted() {
95      return accepted;
96    }
97  
98    public JSONArray getBadRequests() {
99      return badRequest;
100   }
101 
102   public JSONArray getUnauthorized() {
103     return unauthorized;
104   }
105 
106   public JSONArray getNotFound() {
107     return notFound;
108   }
109 
110   public JSONArray getServerError() {
111     return serverError;
112   }
113 
114   @SuppressWarnings("unchecked")
115   public String toJson() {
116     JSONObject bulkOperationResult = new JSONObject();
117     bulkOperationResult.put(OK_KEY, ok);
118     bulkOperationResult.put(ACCEPTED_KEY, accepted);
119     bulkOperationResult.put(BAD_REQUEST_KEY, badRequest);
120     bulkOperationResult.put(NOT_FOUND_KEY, notFound);
121     bulkOperationResult.put(UNAUTHORIZED_KEY, unauthorized);
122     bulkOperationResult.put(ERROR_KEY, serverError);
123     return bulkOperationResult.toJSONString();
124   }
125 
126 }