25
25
import net .sf .json .JSON ;
26
26
import net .sf .json .JSONArray ;
27
27
import net .sf .json .JSONObject ;
28
- import org .apache .commons .lang .BooleanUtils ;
29
28
import org .apache .commons .lang .math .NumberUtils ;
30
29
31
30
import java .lang .reflect .Constructor ;
32
31
import java .util .ArrayList ;
33
- import java .util .HashMap ;
34
32
import java .util .List ;
35
- import java .util .Map ;
36
33
37
34
/**
38
35
* A base class for Agile resources.
39
36
*
37
+ * @author pldupont
40
38
* @see "https://docs.atlassian.com/jira-software/REST/cloud/"
41
39
*/
42
40
public abstract class AgileResource {
@@ -51,15 +49,16 @@ public abstract class AgileResource {
51
49
private long id = 0 ;
52
50
private String name ;
53
51
private String self ;
54
- private Map < String , Object > attributes = new HashMap < String , Object > ();
52
+ private JSONObject attributes = new JSONObject ();
55
53
56
54
/**
57
55
* Creates a new Agile resource.
58
56
*
59
57
* @param restclient REST client instance
60
58
* @param json JSON payload
59
+ * @throws JiraException when the retrieval fails
61
60
*/
62
- public AgileResource (RestClient restclient , JSONObject json ) {
61
+ public AgileResource (RestClient restclient , JSONObject json ) throws JiraException {
63
62
this .restclient = restclient ;
64
63
if (json != null ) {
65
64
deserialize (json );
@@ -73,8 +72,9 @@ public AgileResource(RestClient restclient, JSONObject json) {
73
72
* @param r a JSONObject instance
74
73
* @param restclient REST client instance
75
74
* @return a Resource instance or null if r isn't a JSONObject instance
75
+ * @throws JiraException when the retrieval fails
76
76
*/
77
- private static <T extends AgileResource > T getResource (
77
+ protected static <T extends AgileResource > T getResource (
78
78
Class <T > type , Object r , RestClient restclient ) throws JiraException {
79
79
80
80
if (!(r instanceof JSONObject )) {
@@ -101,26 +101,29 @@ private static <T extends AgileResource> T getResource(
101
101
* @param type Resource data type
102
102
* @param ra a JSONArray instance
103
103
* @param restclient REST client instance
104
+ * @param listName The name of the list of items from the JSON result.
104
105
* @return a list of Resources found in ra
106
+ * @throws JiraException when the retrieval fails
105
107
*/
106
- private static <T extends AgileResource > List <T > getResourceArray (
107
- Class <T > type , Object ra , RestClient restclient ) throws JiraException {
108
+ protected static <T extends AgileResource > List <T > getResourceArray (
109
+ Class <T > type , Object ra , RestClient restclient , String listName ) throws JiraException {
108
110
if (!(ra instanceof JSONObject )) {
109
111
throw new JiraException ("JSON payload is malformed" );
110
112
}
111
113
112
114
JSONObject jo = (JSONObject ) ra ;
113
115
114
- if (!jo .containsKey ("values" ) || !(jo .get ("values" ) instanceof JSONArray )) {
116
+ if (!jo .containsKey (listName ) || !(jo .get (listName ) instanceof JSONArray )) {
115
117
throw new JiraException (type .getSimpleName () + " result is malformed" );
116
118
}
117
119
118
120
List <T > results = new ArrayList <T >();
119
121
120
- for (Object v : (JSONArray ) jo .get ("values" )) {
122
+ for (Object v : (JSONArray ) jo .get (listName )) {
121
123
T item = getResource (type , v , restclient );
122
- if (item != null )
124
+ if (item != null ) {
123
125
results .add (item );
126
+ }
124
127
}
125
128
126
129
return results ;
@@ -130,10 +133,28 @@ private static <T extends AgileResource> List<T> getResourceArray(
130
133
* Retrieves all boards visible to the session user.
131
134
*
132
135
* @param restclient REST client instance
136
+ * @param type The type of the object to deserialize.
137
+ * @param url The URL to call.
138
+ * @return a list of boards
139
+ * @throws JiraException when the retrieval fails
140
+ */
141
+ static <T extends AgileResource > List <T > list (
142
+ RestClient restclient , Class <T > type , String url ) throws JiraException {
143
+ return list (restclient , type , url , "values" );
144
+ }
145
+
146
+ /**
147
+ * Retrieves all boards visible to the session user.
148
+ *
149
+ * @param restclient REST client instance
150
+ * @param type The type of the object to deserialize.
151
+ * @param url The URL to call.
152
+ * @param listName The name of the list of items in the JSON response.
133
153
* @return a list of boards
134
154
* @throws JiraException when the retrieval fails
135
155
*/
136
- static <T extends AgileResource > List <T > list (RestClient restclient , Class <T > type , String url ) throws JiraException {
156
+ static <T extends AgileResource > List <T > list (
157
+ RestClient restclient , Class <T > type , String url , String listName ) throws JiraException {
137
158
138
159
JSON result ;
139
160
try {
@@ -145,7 +166,8 @@ static <T extends AgileResource> List<T> list(RestClient restclient, Class<T> ty
145
166
return getResourceArray (
146
167
type ,
147
168
result ,
148
- restclient
169
+ restclient ,
170
+ listName
149
171
);
150
172
}
151
173
@@ -172,6 +194,44 @@ static <T extends AgileResource> T get(RestClient restclient, Class<T> type, Str
172
194
);
173
195
}
174
196
197
+ /**
198
+ * Extract from a sub list the Resource array, if present.
199
+ *
200
+ * @param type Resource data type
201
+ * @param subJson a JSONObject instance
202
+ * @param resourceName The name of the list of items from the JSON result.
203
+ * @param <T> The type of Agile resource to return.
204
+ * @return The list of resources if present.
205
+ * @throws JiraException when the retrieval fails
206
+ */
207
+ <T extends AgileResource > List <T > getSubResourceArray (
208
+ Class <T > type , JSONObject subJson , String resourceName ) throws JiraException {
209
+ List <T > result = null ;
210
+ if (subJson .containsKey (resourceName )) {
211
+ result = getResourceArray (type , subJson .get (resourceName ), getRestclient (), resourceName + "s" );
212
+ }
213
+ return result ;
214
+ }
215
+
216
+ /**
217
+ * Extract from a sub list the Resource, if present.
218
+ *
219
+ * @param type Resource data type
220
+ * @param subJson a JSONObject instance
221
+ * @param resourceName The name of the item from the JSON result.
222
+ * @param <T> The type of Agile resource to return.
223
+ * @return The resource if present.
224
+ * @throws JiraException when the retrieval fails
225
+ */
226
+ <T extends AgileResource > T getSubResource (
227
+ Class <T > type , JSONObject subJson , String resourceName ) throws JiraException {
228
+ T result = null ;
229
+ if (subJson .containsKey (resourceName )) {
230
+ result = getResource (type , subJson .get (resourceName ), getRestclient ());
231
+ }
232
+ return result ;
233
+ }
234
+
175
235
/**
176
236
* @return Internal JIRA ID.
177
237
*/
@@ -186,6 +246,13 @@ public String getName() {
186
246
return name ;
187
247
}
188
248
249
+ /**
250
+ * @param name Setter for the resource name. In some case, the name is called something else.
251
+ */
252
+ void setName (String name ) {
253
+ this .name = name ;
254
+ }
255
+
189
256
/**
190
257
* @return The resource URL.
191
258
*/
@@ -206,47 +273,36 @@ protected RestClient getRestclient() {
206
273
* @param name The name of the attribute to retrieve.
207
274
* @return The value of the attribute.
208
275
*/
209
- String getAttribute (String name ) {
276
+ public Object getAttribute (String name ) {
210
277
return (String ) attributes .get (name );
211
278
}
212
279
213
- /**
214
- * Retrieve the specified attribute as a generic object.
215
- *
216
- * @param name The name of the attribute to retrieve.
217
- * @return The value of the attribute.
218
- */
219
- int getAttributeAsInt (String name ) {
220
- return NumberUtils .toInt (getAttribute (name ), 0 );
221
- }
222
-
223
- /**
224
- * Retrieve the specified attribute as a generic object.
225
- *
226
- * @param name The name of the attribute to retrieve.
227
- * @return The value of the attribute.
228
- */
229
- boolean getAttributeAsBoolean (String name ) {
230
- return BooleanUtils .toBoolean (getAttribute (name ));
231
- }
232
-
233
280
/**
234
281
* Deserialize the json to extract standard attributes and keep a reference of
235
282
* other attributes.
236
283
*
237
284
* @param json The JSON object to read.
238
285
*/
239
- protected void deserialize (JSONObject json ) {
286
+ void deserialize (JSONObject json ) throws JiraException {
240
287
241
288
id = getLong (json .get ("id" ));
242
289
name = Field .getString (json .get ("name" ));
243
290
self = Field .getString (json .get ("self" ));
291
+ addAttributes (json );
292
+ }
293
+
294
+ /**
295
+ * Allow to add more attributes.
296
+ *
297
+ * @param json The json object to extract attributes from.
298
+ */
299
+ void addAttributes (JSONObject json ) {
244
300
attributes .putAll (json );
245
301
}
246
302
247
303
long getLong (Object o ) {
248
304
if (o instanceof Integer || o instanceof Long ) {
249
- return Field .getInteger (o );
305
+ return Field .getLong (o );
250
306
} else if (o instanceof String && NumberUtils .isDigits ((String ) o )) {
251
307
return NumberUtils .toLong ((String ) o , 0L );
252
308
} else {
0 commit comments